Merge pull request #2648 from BrzVlad/fix-cprop-vregs
[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                                 mono_runtime_class_init (vtable);
5331                 } else if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5332                         if (cfg->run_cctors && method->klass->has_cctor) {
5333                                 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
5334                                 if (!method->klass->runtime_info)
5335                                         /* No vtable created yet */
5336                                         return FALSE;
5337                                 vtable = mono_class_vtable (cfg->domain, method->klass);
5338                                 if (!vtable)
5339                                         return FALSE;
5340                                 /* This makes so that inline cannot trigger */
5341                                 /* .cctors: too many apps depend on them */
5342                                 /* running with a specific order... */
5343                                 if (! vtable->initialized)
5344                                         return FALSE;
5345                                 mono_runtime_class_init (vtable);
5346                         }
5347                 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
5348                         if (!method->klass->runtime_info)
5349                                 /* No vtable created yet */
5350                                 return FALSE;
5351                         vtable = mono_class_vtable (cfg->domain, method->klass);
5352                         if (!vtable)
5353                                 return FALSE;
5354                         if (!vtable->initialized)
5355                                 return FALSE;
5356                 }
5357         } else {
5358                 /* 
5359                  * If we're compiling for shared code
5360                  * the cctor will need to be run at aot method load time, for example,
5361                  * or at the end of the compilation of the inlining method.
5362                  */
5363                 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
5364                         return FALSE;
5365         }
5366
5367 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5368         if (mono_arch_is_soft_float ()) {
5369                 /* FIXME: */
5370                 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
5371                         return FALSE;
5372                 for (i = 0; i < sig->param_count; ++i)
5373                         if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
5374                                 return FALSE;
5375         }
5376 #endif
5377
5378         if (g_list_find (cfg->dont_inline, method))
5379                 return FALSE;
5380
5381         return TRUE;
5382 }
5383
5384 static gboolean
5385 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
5386 {
5387         if (!cfg->compile_aot) {
5388                 g_assert (vtable);
5389                 if (vtable->initialized)
5390                         return FALSE;
5391         }
5392
5393         if (klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5394                 if (cfg->method == method)
5395                         return FALSE;
5396         }
5397
5398         if (!mono_class_needs_cctor_run (klass, method))
5399                 return FALSE;
5400
5401         if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
5402                 /* The initialization is already done before the method is called */
5403                 return FALSE;
5404
5405         return TRUE;
5406 }
5407
5408 static MonoInst*
5409 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
5410 {
5411         MonoInst *ins;
5412         guint32 size;
5413         int mult_reg, add_reg, array_reg, index_reg, index2_reg;
5414         int context_used;
5415
5416         if (mini_is_gsharedvt_variable_klass (klass)) {
5417                 size = -1;
5418         } else {
5419                 mono_class_init (klass);
5420                 size = mono_class_array_element_size (klass);
5421         }
5422
5423         mult_reg = alloc_preg (cfg);
5424         array_reg = arr->dreg;
5425         index_reg = index->dreg;
5426
5427 #if SIZEOF_REGISTER == 8
5428         /* The array reg is 64 bits but the index reg is only 32 */
5429         if (COMPILE_LLVM (cfg)) {
5430                 /* Not needed */
5431                 index2_reg = index_reg;
5432         } else {
5433                 index2_reg = alloc_preg (cfg);
5434                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
5435         }
5436 #else
5437         if (index->type == STACK_I8) {
5438                 index2_reg = alloc_preg (cfg);
5439                 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
5440         } else {
5441                 index2_reg = index_reg;
5442         }
5443 #endif
5444
5445         if (bcheck)
5446                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
5447
5448 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5449         if (size == 1 || size == 2 || size == 4 || size == 8) {
5450                 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
5451
5452                 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
5453                 ins->klass = mono_class_get_element_class (klass);
5454                 ins->type = STACK_MP;
5455
5456                 return ins;
5457         }
5458 #endif          
5459
5460         add_reg = alloc_ireg_mp (cfg);
5461
5462         if (size == -1) {
5463                 MonoInst *rgctx_ins;
5464
5465                 /* gsharedvt */
5466                 g_assert (cfg->gshared);
5467                 context_used = mini_class_check_context_used (cfg, klass);
5468                 g_assert (context_used);
5469                 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
5470                 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
5471         } else {
5472                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
5473         }
5474         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
5475         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5476         ins->klass = mono_class_get_element_class (klass);
5477         ins->type = STACK_MP;
5478         MONO_ADD_INS (cfg->cbb, ins);
5479
5480         return ins;
5481 }
5482
5483 static MonoInst*
5484 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
5485 {
5486         int bounds_reg = alloc_preg (cfg);
5487         int add_reg = alloc_ireg_mp (cfg);
5488         int mult_reg = alloc_preg (cfg);
5489         int mult2_reg = alloc_preg (cfg);
5490         int low1_reg = alloc_preg (cfg);
5491         int low2_reg = alloc_preg (cfg);
5492         int high1_reg = alloc_preg (cfg);
5493         int high2_reg = alloc_preg (cfg);
5494         int realidx1_reg = alloc_preg (cfg);
5495         int realidx2_reg = alloc_preg (cfg);
5496         int sum_reg = alloc_preg (cfg);
5497         int index1, index2, tmpreg;
5498         MonoInst *ins;
5499         guint32 size;
5500
5501         mono_class_init (klass);
5502         size = mono_class_array_element_size (klass);
5503
5504         index1 = index_ins1->dreg;
5505         index2 = index_ins2->dreg;
5506
5507 #if SIZEOF_REGISTER == 8
5508         /* The array reg is 64 bits but the index reg is only 32 */
5509         if (COMPILE_LLVM (cfg)) {
5510                 /* Not needed */
5511         } else {
5512                 tmpreg = alloc_preg (cfg);
5513                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
5514                 index1 = tmpreg;
5515                 tmpreg = alloc_preg (cfg);
5516                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
5517                 index2 = tmpreg;
5518         }
5519 #else
5520         // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
5521         tmpreg = -1;
5522 #endif
5523
5524         /* range checking */
5525         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, 
5526                                        arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5527
5528         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg, 
5529                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5530         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
5531         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg, 
5532                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5533         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
5534         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5535
5536         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg, 
5537                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5538         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
5539         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg, 
5540                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5541         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
5542         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5543
5544         MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
5545         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
5546         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
5547         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
5548         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5549
5550         ins->type = STACK_MP;
5551         ins->klass = klass;
5552         MONO_ADD_INS (cfg->cbb, ins);
5553
5554         return ins;
5555 }
5556
5557 static MonoInst*
5558 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
5559 {
5560         int rank;
5561         MonoInst *addr;
5562         MonoMethod *addr_method;
5563         int element_size;
5564         MonoClass *eclass = cmethod->klass->element_class;
5565
5566         rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
5567
5568         if (rank == 1)
5569                 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
5570
5571         /* emit_ldelema_2 depends on OP_LMUL */
5572         if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
5573                 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
5574         }
5575
5576         if (mini_is_gsharedvt_variable_klass (eclass))
5577                 element_size = 0;
5578         else
5579                 element_size = mono_class_array_element_size (eclass);
5580         addr_method = mono_marshal_get_array_address (rank, element_size);
5581         addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
5582
5583         return addr;
5584 }
5585
5586 static MonoBreakPolicy
5587 always_insert_breakpoint (MonoMethod *method)
5588 {
5589         return MONO_BREAK_POLICY_ALWAYS;
5590 }
5591
5592 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
5593
5594 /**
5595  * mono_set_break_policy:
5596  * policy_callback: the new callback function
5597  *
5598  * Allow embedders to decide wherther to actually obey breakpoint instructions
5599  * (both break IL instructions and Debugger.Break () method calls), for example
5600  * to not allow an app to be aborted by a perfectly valid IL opcode when executing
5601  * untrusted or semi-trusted code.
5602  *
5603  * @policy_callback will be called every time a break point instruction needs to
5604  * be inserted with the method argument being the method that calls Debugger.Break()
5605  * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
5606  * if it wants the breakpoint to not be effective in the given method.
5607  * #MONO_BREAK_POLICY_ALWAYS is the default.
5608  */
5609 void
5610 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
5611 {
5612         if (policy_callback)
5613                 break_policy_func = policy_callback;
5614         else
5615                 break_policy_func = always_insert_breakpoint;
5616 }
5617
5618 static gboolean
5619 should_insert_brekpoint (MonoMethod *method) {
5620         switch (break_policy_func (method)) {
5621         case MONO_BREAK_POLICY_ALWAYS:
5622                 return TRUE;
5623         case MONO_BREAK_POLICY_NEVER:
5624                 return FALSE;
5625         case MONO_BREAK_POLICY_ON_DBG:
5626                 g_warning ("mdb no longer supported");
5627                 return FALSE;
5628         default:
5629                 g_warning ("Incorrect value returned from break policy callback");
5630                 return FALSE;
5631         }
5632 }
5633
5634 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
5635 static MonoInst*
5636 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5637 {
5638         MonoInst *addr, *store, *load;
5639         MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
5640
5641         /* the bounds check is already done by the callers */
5642         addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5643         if (is_set) {
5644                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
5645                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
5646                 if (mini_type_is_reference (fsig->params [2]))
5647                         emit_write_barrier (cfg, addr, load);
5648         } else {
5649                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
5650                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
5651         }
5652         return store;
5653 }
5654
5655
5656 static gboolean
5657 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5658 {
5659         return mini_type_is_reference (&klass->byval_arg);
5660 }
5661
5662 static MonoInst*
5663 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
5664 {
5665         if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
5666                 !(sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL)) {
5667                 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
5668                 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
5669                 MonoInst *iargs [3];
5670
5671                 if (!helper->slot)
5672                         mono_class_setup_vtable (obj_array);
5673                 g_assert (helper->slot);
5674
5675                 if (sp [0]->type != STACK_OBJ)
5676                         return NULL;
5677                 if (sp [2]->type != STACK_OBJ)
5678                         return NULL;
5679
5680                 iargs [2] = sp [2];
5681                 iargs [1] = sp [1];
5682                 iargs [0] = sp [0];
5683
5684                 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
5685         } else {
5686                 MonoInst *ins;
5687
5688                 if (mini_is_gsharedvt_variable_klass (klass)) {
5689                         MonoInst *addr;
5690
5691                         // FIXME-VT: OP_ICONST optimization
5692                         addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
5693                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5694                         ins->opcode = OP_STOREV_MEMBASE;
5695                 } else if (sp [1]->opcode == OP_ICONST) {
5696                         int array_reg = sp [0]->dreg;
5697                         int index_reg = sp [1]->dreg;
5698                         int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5699
5700                         if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
5701                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
5702
5703                         if (safety_checks)
5704                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5705                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5706                 } else {
5707                         MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5708                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5709                         if (generic_class_is_reference_type (cfg, klass))
5710                                 emit_write_barrier (cfg, addr, sp [2]);
5711                 }
5712                 return ins;
5713         }
5714 }
5715
5716 static MonoInst*
5717 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5718 {
5719         MonoClass *eklass;
5720         
5721         if (is_set)
5722                 eklass = mono_class_from_mono_type (fsig->params [2]);
5723         else
5724                 eklass = mono_class_from_mono_type (fsig->ret);
5725
5726         if (is_set) {
5727                 return emit_array_store (cfg, eklass, args, FALSE);
5728         } else {
5729                 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5730                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5731                 return ins;
5732         }
5733 }
5734
5735 static gboolean
5736 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5737 {
5738         uint32_t align;
5739         int param_size, return_size;
5740
5741         param_klass = mono_class_from_mono_type (mini_get_underlying_type (&param_klass->byval_arg));
5742         return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
5743
5744         if (cfg->verbose_level > 3)
5745                 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
5746
5747         //Don't allow mixing reference types with value types
5748         if (param_klass->valuetype != return_klass->valuetype) {
5749                 if (cfg->verbose_level > 3)
5750                         printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5751                 return FALSE;
5752         }
5753
5754         if (!param_klass->valuetype) {
5755                 if (cfg->verbose_level > 3)
5756                         printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5757                 return TRUE;
5758         }
5759
5760         //That are blitable
5761         if (param_klass->has_references || return_klass->has_references)
5762                 return FALSE;
5763
5764         /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5765         if ((MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5766                 (!MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5767                         if (cfg->verbose_level > 3)
5768                                 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5769                 return FALSE;
5770         }
5771
5772         if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5773                 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5774                 if (cfg->verbose_level > 3)
5775                         printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5776                 return FALSE;
5777         }
5778
5779         param_size = mono_class_value_size (param_klass, &align);
5780         return_size = mono_class_value_size (return_klass, &align);
5781
5782         //We can do it if sizes match
5783         if (param_size == return_size) {
5784                 if (cfg->verbose_level > 3)
5785                         printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5786                 return TRUE;
5787         }
5788
5789         //No simple way to handle struct if sizes don't match
5790         if (MONO_TYPE_ISSTRUCT (&param_klass->byval_arg)) {
5791                 if (cfg->verbose_level > 3)
5792                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5793                 return FALSE;
5794         }
5795
5796         /*
5797          * Same reg size category.
5798          * A quick note on why we don't require widening here.
5799          * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5800          *
5801          * Since the source value comes from a function argument, the JIT will already have
5802          * the value in a VREG and performed any widening needed before (say, when loading from a field).
5803          */
5804         if (param_size <= 4 && return_size <= 4) {
5805                 if (cfg->verbose_level > 3)
5806                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5807                 return TRUE;
5808         }
5809
5810         return FALSE;
5811 }
5812
5813 static MonoInst*
5814 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5815 {
5816         MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5817         MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5818
5819         if (mini_is_gsharedvt_variable_type (fsig->ret))
5820                 return NULL;
5821
5822         //Valuetypes that are semantically equivalent or numbers than can be widened to
5823         if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5824                 return args [0];
5825
5826         //Arrays of valuetypes that are semantically equivalent
5827         if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5828                 return args [0];
5829
5830         return NULL;
5831 }
5832
5833 static MonoInst*
5834 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5835 {
5836 #ifdef MONO_ARCH_SIMD_INTRINSICS
5837         MonoInst *ins = NULL;
5838
5839         if (cfg->opt & MONO_OPT_SIMD) {
5840                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5841                 if (ins)
5842                         return ins;
5843         }
5844 #endif
5845
5846         return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5847 }
5848
5849 static MonoInst*
5850 emit_memory_barrier (MonoCompile *cfg, int kind)
5851 {
5852         MonoInst *ins = NULL;
5853         MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5854         MONO_ADD_INS (cfg->cbb, ins);
5855         ins->backend.memory_barrier_kind = kind;
5856
5857         return ins;
5858 }
5859
5860 static MonoInst*
5861 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5862 {
5863         MonoInst *ins = NULL;
5864         int opcode = 0;
5865
5866         /* The LLVM backend supports these intrinsics */
5867         if (cmethod->klass == mono_defaults.math_class) {
5868                 if (strcmp (cmethod->name, "Sin") == 0) {
5869                         opcode = OP_SIN;
5870                 } else if (strcmp (cmethod->name, "Cos") == 0) {
5871                         opcode = OP_COS;
5872                 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5873                         opcode = OP_SQRT;
5874                 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5875                         opcode = OP_ABS;
5876                 }
5877
5878                 if (opcode && fsig->param_count == 1) {
5879                         MONO_INST_NEW (cfg, ins, opcode);
5880                         ins->type = STACK_R8;
5881                         ins->dreg = mono_alloc_freg (cfg);
5882                         ins->sreg1 = args [0]->dreg;
5883                         MONO_ADD_INS (cfg->cbb, ins);
5884                 }
5885
5886                 opcode = 0;
5887                 if (cfg->opt & MONO_OPT_CMOV) {
5888                         if (strcmp (cmethod->name, "Min") == 0) {
5889                                 if (fsig->params [0]->type == MONO_TYPE_I4)
5890                                         opcode = OP_IMIN;
5891                                 if (fsig->params [0]->type == MONO_TYPE_U4)
5892                                         opcode = OP_IMIN_UN;
5893                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
5894                                         opcode = OP_LMIN;
5895                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
5896                                         opcode = OP_LMIN_UN;
5897                         } else if (strcmp (cmethod->name, "Max") == 0) {
5898                                 if (fsig->params [0]->type == MONO_TYPE_I4)
5899                                         opcode = OP_IMAX;
5900                                 if (fsig->params [0]->type == MONO_TYPE_U4)
5901                                         opcode = OP_IMAX_UN;
5902                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
5903                                         opcode = OP_LMAX;
5904                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
5905                                         opcode = OP_LMAX_UN;
5906                         }
5907                 }
5908
5909                 if (opcode && fsig->param_count == 2) {
5910                         MONO_INST_NEW (cfg, ins, opcode);
5911                         ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5912                         ins->dreg = mono_alloc_ireg (cfg);
5913                         ins->sreg1 = args [0]->dreg;
5914                         ins->sreg2 = args [1]->dreg;
5915                         MONO_ADD_INS (cfg->cbb, ins);
5916                 }
5917         }
5918
5919         return ins;
5920 }
5921
5922 static MonoInst*
5923 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5924 {
5925         if (cmethod->klass == mono_defaults.array_class) {
5926                 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5927                         return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5928                 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5929                         return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5930                 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5931                         return emit_array_unsafe_mov (cfg, fsig, args);
5932         }
5933
5934         return NULL;
5935 }
5936
5937 static MonoInst*
5938 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5939 {
5940         MonoInst *ins = NULL;
5941
5942          MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
5943
5944         if (cmethod->klass == mono_defaults.string_class) {
5945                 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5946                         int dreg = alloc_ireg (cfg);
5947                         int index_reg = alloc_preg (cfg);
5948                         int add_reg = alloc_preg (cfg);
5949
5950 #if SIZEOF_REGISTER == 8
5951                         if (COMPILE_LLVM (cfg)) {
5952                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
5953                         } else {
5954                                 /* The array reg is 64 bits but the index reg is only 32 */
5955                                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5956                         }
5957 #else
5958                         index_reg = args [1]->dreg;
5959 #endif  
5960                         MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5961
5962 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5963                         EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5964                         add_reg = ins->dreg;
5965                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
5966                                                                    add_reg, 0);
5967 #else
5968                         int mult_reg = alloc_preg (cfg);
5969                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5970                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5971                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
5972                                                                    add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5973 #endif
5974                         type_from_op (cfg, ins, NULL, NULL);
5975                         return ins;
5976                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5977                         int dreg = alloc_ireg (cfg);
5978                         /* Decompose later to allow more optimizations */
5979                         EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
5980                         ins->type = STACK_I4;
5981                         ins->flags |= MONO_INST_FAULT;
5982                         cfg->cbb->has_array_access = TRUE;
5983                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
5984
5985                         return ins;
5986                 } else 
5987                         return NULL;
5988         } else if (cmethod->klass == mono_defaults.object_class) {
5989                 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
5990                         int dreg = alloc_ireg_ref (cfg);
5991                         int vt_reg = alloc_preg (cfg);
5992                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5993                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
5994                         type_from_op (cfg, ins, NULL, NULL);
5995
5996                         return ins;
5997                 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
5998                         int dreg = alloc_ireg (cfg);
5999                         int t1 = alloc_ireg (cfg);
6000         
6001                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
6002                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
6003                         ins->type = STACK_I4;
6004
6005                         return ins;
6006                 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
6007                         MONO_INST_NEW (cfg, ins, OP_NOP);
6008                         MONO_ADD_INS (cfg->cbb, ins);
6009                         return ins;
6010                 } else
6011                         return NULL;
6012         } else if (cmethod->klass == mono_defaults.array_class) {
6013                 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6014                         return emit_array_generic_access (cfg, fsig, args, FALSE);
6015                 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6016                         return emit_array_generic_access (cfg, fsig, args, TRUE);
6017
6018 #ifndef MONO_BIG_ARRAYS
6019                 /*
6020                  * This is an inline version of GetLength/GetLowerBound(0) used frequently in
6021                  * Array methods.
6022                  */
6023                 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
6024                          (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
6025                          args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
6026                         int dreg = alloc_ireg (cfg);
6027                         int bounds_reg = alloc_ireg_mp (cfg);
6028                         MonoBasicBlock *end_bb, *szarray_bb;
6029                         gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
6030
6031                         NEW_BBLOCK (cfg, end_bb);
6032                         NEW_BBLOCK (cfg, szarray_bb);
6033
6034                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
6035                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
6036                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
6037                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
6038                         /* Non-szarray case */
6039                         if (get_length)
6040                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6041                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
6042                         else
6043                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6044                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
6045                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6046                         MONO_START_BB (cfg, szarray_bb);
6047                         /* Szarray case */
6048                         if (get_length)
6049                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6050                                                                            args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6051                         else
6052                                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6053                         MONO_START_BB (cfg, end_bb);
6054
6055                         EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
6056                         ins->type = STACK_I4;
6057                         
6058                         return ins;
6059                 }
6060 #endif
6061
6062                 if (cmethod->name [0] != 'g')
6063                         return NULL;
6064
6065                 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
6066                         int dreg = alloc_ireg (cfg);
6067                         int vtable_reg = alloc_preg (cfg);
6068                         MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg, 
6069                                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6070                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
6071                                                                    vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
6072                         type_from_op (cfg, ins, NULL, NULL);
6073
6074                         return ins;
6075                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
6076                         int dreg = alloc_ireg (cfg);
6077
6078                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg, 
6079                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6080                         type_from_op (cfg, ins, NULL, NULL);
6081
6082                         return ins;
6083                 } else
6084                         return NULL;
6085         } else if (cmethod->klass == runtime_helpers_class) {
6086                 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
6087                         EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
6088                         return ins;
6089                 } else
6090                         return NULL;
6091         } else if (cmethod->klass == mono_defaults.monitor_class) {
6092                 gboolean is_enter = FALSE;
6093                 gboolean is_v4 = FALSE;
6094
6095                 if (!strcmp (cmethod->name, "enter_with_atomic_var") && mono_method_signature (cmethod)->param_count == 2) {
6096                         is_enter = TRUE;
6097                         is_v4 = TRUE;
6098                 }
6099                 if (!strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1)
6100                         is_enter = TRUE;
6101
6102                 if (is_enter) {
6103                         /*
6104                          * To make async stack traces work, icalls which can block should have a wrapper.
6105                          * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
6106                          */
6107                         MonoBasicBlock *end_bb;
6108
6109                         NEW_BBLOCK (cfg, end_bb);
6110
6111                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
6112                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
6113                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
6114                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4 : (gpointer)mono_monitor_enter, args);
6115                         MONO_START_BB (cfg, end_bb);
6116                         return ins;
6117                 }
6118         } else if (cmethod->klass == mono_defaults.thread_class) {
6119                 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
6120                         MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
6121                         MONO_ADD_INS (cfg->cbb, ins);
6122                         return ins;
6123                 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
6124                         return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6125                 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
6126                         guint32 opcode = 0;
6127                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6128
6129                         if (fsig->params [0]->type == MONO_TYPE_I1)
6130                                 opcode = OP_LOADI1_MEMBASE;
6131                         else if (fsig->params [0]->type == MONO_TYPE_U1)
6132                                 opcode = OP_LOADU1_MEMBASE;
6133                         else if (fsig->params [0]->type == MONO_TYPE_I2)
6134                                 opcode = OP_LOADI2_MEMBASE;
6135                         else if (fsig->params [0]->type == MONO_TYPE_U2)
6136                                 opcode = OP_LOADU2_MEMBASE;
6137                         else if (fsig->params [0]->type == MONO_TYPE_I4)
6138                                 opcode = OP_LOADI4_MEMBASE;
6139                         else if (fsig->params [0]->type == MONO_TYPE_U4)
6140                                 opcode = OP_LOADU4_MEMBASE;
6141                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6142                                 opcode = OP_LOADI8_MEMBASE;
6143                         else if (fsig->params [0]->type == MONO_TYPE_R4)
6144                                 opcode = OP_LOADR4_MEMBASE;
6145                         else if (fsig->params [0]->type == MONO_TYPE_R8)
6146                                 opcode = OP_LOADR8_MEMBASE;
6147                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6148                                 opcode = OP_LOAD_MEMBASE;
6149
6150                         if (opcode) {
6151                                 MONO_INST_NEW (cfg, ins, opcode);
6152                                 ins->inst_basereg = args [0]->dreg;
6153                                 ins->inst_offset = 0;
6154                                 MONO_ADD_INS (cfg->cbb, ins);
6155
6156                                 switch (fsig->params [0]->type) {
6157                                 case MONO_TYPE_I1:
6158                                 case MONO_TYPE_U1:
6159                                 case MONO_TYPE_I2:
6160                                 case MONO_TYPE_U2:
6161                                 case MONO_TYPE_I4:
6162                                 case MONO_TYPE_U4:
6163                                         ins->dreg = mono_alloc_ireg (cfg);
6164                                         ins->type = STACK_I4;
6165                                         break;
6166                                 case MONO_TYPE_I8:
6167                                 case MONO_TYPE_U8:
6168                                         ins->dreg = mono_alloc_lreg (cfg);
6169                                         ins->type = STACK_I8;
6170                                         break;
6171                                 case MONO_TYPE_I:
6172                                 case MONO_TYPE_U:
6173                                         ins->dreg = mono_alloc_ireg (cfg);
6174 #if SIZEOF_REGISTER == 8
6175                                         ins->type = STACK_I8;
6176 #else
6177                                         ins->type = STACK_I4;
6178 #endif
6179                                         break;
6180                                 case MONO_TYPE_R4:
6181                                 case MONO_TYPE_R8:
6182                                         ins->dreg = mono_alloc_freg (cfg);
6183                                         ins->type = STACK_R8;
6184                                         break;
6185                                 default:
6186                                         g_assert (mini_type_is_reference (fsig->params [0]));
6187                                         ins->dreg = mono_alloc_ireg_ref (cfg);
6188                                         ins->type = STACK_OBJ;
6189                                         break;
6190                                 }
6191
6192                                 if (opcode == OP_LOADI8_MEMBASE)
6193                                         ins = mono_decompose_opcode (cfg, ins);
6194
6195                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6196
6197                                 return ins;
6198                         }
6199                 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
6200                         guint32 opcode = 0;
6201                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6202
6203                         if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
6204                                 opcode = OP_STOREI1_MEMBASE_REG;
6205                         else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
6206                                 opcode = OP_STOREI2_MEMBASE_REG;
6207                         else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
6208                                 opcode = OP_STOREI4_MEMBASE_REG;
6209                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6210                                 opcode = OP_STOREI8_MEMBASE_REG;
6211                         else if (fsig->params [0]->type == MONO_TYPE_R4)
6212                                 opcode = OP_STORER4_MEMBASE_REG;
6213                         else if (fsig->params [0]->type == MONO_TYPE_R8)
6214                                 opcode = OP_STORER8_MEMBASE_REG;
6215                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6216                                 opcode = OP_STORE_MEMBASE_REG;
6217
6218                         if (opcode) {
6219                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6220
6221                                 MONO_INST_NEW (cfg, ins, opcode);
6222                                 ins->sreg1 = args [1]->dreg;
6223                                 ins->inst_destbasereg = args [0]->dreg;
6224                                 ins->inst_offset = 0;
6225                                 MONO_ADD_INS (cfg->cbb, ins);
6226
6227                                 if (opcode == OP_STOREI8_MEMBASE_REG)
6228                                         ins = mono_decompose_opcode (cfg, ins);
6229
6230                                 return ins;
6231                         }
6232                 }
6233         } else if (cmethod->klass->image == mono_defaults.corlib &&
6234                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6235                            (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
6236                 ins = NULL;
6237
6238 #if SIZEOF_REGISTER == 8
6239                 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
6240                         if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
6241                                 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
6242                                 ins->dreg = mono_alloc_preg (cfg);
6243                                 ins->sreg1 = args [0]->dreg;
6244                                 ins->type = STACK_I8;
6245                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
6246                                 MONO_ADD_INS (cfg->cbb, ins);
6247                         } else {
6248                                 MonoInst *load_ins;
6249
6250                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6251
6252                                 /* 64 bit reads are already atomic */
6253                                 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
6254                                 load_ins->dreg = mono_alloc_preg (cfg);
6255                                 load_ins->inst_basereg = args [0]->dreg;
6256                                 load_ins->inst_offset = 0;
6257                                 load_ins->type = STACK_I8;
6258                                 MONO_ADD_INS (cfg->cbb, load_ins);
6259
6260                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6261
6262                                 ins = load_ins;
6263                         }
6264                 }
6265 #endif
6266
6267                 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
6268                         MonoInst *ins_iconst;
6269                         guint32 opcode = 0;
6270
6271                         if (fsig->params [0]->type == MONO_TYPE_I4) {
6272                                 opcode = OP_ATOMIC_ADD_I4;
6273                                 cfg->has_atomic_add_i4 = TRUE;
6274                         }
6275 #if SIZEOF_REGISTER == 8
6276                         else if (fsig->params [0]->type == MONO_TYPE_I8)
6277                                 opcode = OP_ATOMIC_ADD_I8;
6278 #endif
6279                         if (opcode) {
6280                                 if (!mono_arch_opcode_supported (opcode))
6281                                         return NULL;
6282                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6283                                 ins_iconst->inst_c0 = 1;
6284                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
6285                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
6286
6287                                 MONO_INST_NEW (cfg, ins, opcode);
6288                                 ins->dreg = mono_alloc_ireg (cfg);
6289                                 ins->inst_basereg = args [0]->dreg;
6290                                 ins->inst_offset = 0;
6291                                 ins->sreg2 = ins_iconst->dreg;
6292                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6293                                 MONO_ADD_INS (cfg->cbb, ins);
6294                         }
6295                 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
6296                         MonoInst *ins_iconst;
6297                         guint32 opcode = 0;
6298
6299                         if (fsig->params [0]->type == MONO_TYPE_I4) {
6300                                 opcode = OP_ATOMIC_ADD_I4;
6301                                 cfg->has_atomic_add_i4 = TRUE;
6302                         }
6303 #if SIZEOF_REGISTER == 8
6304                         else if (fsig->params [0]->type == MONO_TYPE_I8)
6305                                 opcode = OP_ATOMIC_ADD_I8;
6306 #endif
6307                         if (opcode) {
6308                                 if (!mono_arch_opcode_supported (opcode))
6309                                         return NULL;
6310                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6311                                 ins_iconst->inst_c0 = -1;
6312                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
6313                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
6314
6315                                 MONO_INST_NEW (cfg, ins, opcode);
6316                                 ins->dreg = mono_alloc_ireg (cfg);
6317                                 ins->inst_basereg = args [0]->dreg;
6318                                 ins->inst_offset = 0;
6319                                 ins->sreg2 = ins_iconst->dreg;
6320                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6321                                 MONO_ADD_INS (cfg->cbb, ins);
6322                         }
6323                 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
6324                         guint32 opcode = 0;
6325
6326                         if (fsig->params [0]->type == MONO_TYPE_I4) {
6327                                 opcode = OP_ATOMIC_ADD_I4;
6328                                 cfg->has_atomic_add_i4 = TRUE;
6329                         }
6330 #if SIZEOF_REGISTER == 8
6331                         else if (fsig->params [0]->type == MONO_TYPE_I8)
6332                                 opcode = OP_ATOMIC_ADD_I8;
6333 #endif
6334                         if (opcode) {
6335                                 if (!mono_arch_opcode_supported (opcode))
6336                                         return NULL;
6337                                 MONO_INST_NEW (cfg, ins, opcode);
6338                                 ins->dreg = mono_alloc_ireg (cfg);
6339                                 ins->inst_basereg = args [0]->dreg;
6340                                 ins->inst_offset = 0;
6341                                 ins->sreg2 = args [1]->dreg;
6342                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6343                                 MONO_ADD_INS (cfg->cbb, ins);
6344                         }
6345                 }
6346                 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
6347                         MonoInst *f2i = NULL, *i2f;
6348                         guint32 opcode, f2i_opcode, i2f_opcode;
6349                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6350                         gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6351
6352                         if (fsig->params [0]->type == MONO_TYPE_I4 ||
6353                             fsig->params [0]->type == MONO_TYPE_R4) {
6354                                 opcode = OP_ATOMIC_EXCHANGE_I4;
6355                                 f2i_opcode = OP_MOVE_F_TO_I4;
6356                                 i2f_opcode = OP_MOVE_I4_TO_F;
6357                                 cfg->has_atomic_exchange_i4 = TRUE;
6358                         }
6359 #if SIZEOF_REGISTER == 8
6360                         else if (is_ref ||
6361                                  fsig->params [0]->type == MONO_TYPE_I8 ||
6362                                  fsig->params [0]->type == MONO_TYPE_R8 ||
6363                                  fsig->params [0]->type == MONO_TYPE_I) {
6364                                 opcode = OP_ATOMIC_EXCHANGE_I8;
6365                                 f2i_opcode = OP_MOVE_F_TO_I8;
6366                                 i2f_opcode = OP_MOVE_I8_TO_F;
6367                         }
6368 #else
6369                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
6370                                 opcode = OP_ATOMIC_EXCHANGE_I4;
6371                                 cfg->has_atomic_exchange_i4 = TRUE;
6372                         }
6373 #endif
6374                         else
6375                                 return NULL;
6376
6377                         if (!mono_arch_opcode_supported (opcode))
6378                                 return NULL;
6379
6380                         if (is_float) {
6381                                 /* TODO: Decompose these opcodes instead of bailing here. */
6382                                 if (COMPILE_SOFT_FLOAT (cfg))
6383                                         return NULL;
6384
6385                                 MONO_INST_NEW (cfg, f2i, f2i_opcode);
6386                                 f2i->dreg = mono_alloc_ireg (cfg);
6387                                 f2i->sreg1 = args [1]->dreg;
6388                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
6389                                         f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6390                                 MONO_ADD_INS (cfg->cbb, f2i);
6391                         }
6392
6393                         MONO_INST_NEW (cfg, ins, opcode);
6394                         ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
6395                         ins->inst_basereg = args [0]->dreg;
6396                         ins->inst_offset = 0;
6397                         ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
6398                         MONO_ADD_INS (cfg->cbb, ins);
6399
6400                         switch (fsig->params [0]->type) {
6401                         case MONO_TYPE_I4:
6402                                 ins->type = STACK_I4;
6403                                 break;
6404                         case MONO_TYPE_I8:
6405                                 ins->type = STACK_I8;
6406                                 break;
6407                         case MONO_TYPE_I:
6408 #if SIZEOF_REGISTER == 8
6409                                 ins->type = STACK_I8;
6410 #else
6411                                 ins->type = STACK_I4;
6412 #endif
6413                                 break;
6414                         case MONO_TYPE_R4:
6415                         case MONO_TYPE_R8:
6416                                 ins->type = STACK_R8;
6417                                 break;
6418                         default:
6419                                 g_assert (mini_type_is_reference (fsig->params [0]));
6420                                 ins->type = STACK_OBJ;
6421                                 break;
6422                         }
6423
6424                         if (is_float) {
6425                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6426                                 i2f->dreg = mono_alloc_freg (cfg);
6427                                 i2f->sreg1 = ins->dreg;
6428                                 i2f->type = STACK_R8;
6429                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
6430                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6431                                 MONO_ADD_INS (cfg->cbb, i2f);
6432
6433                                 ins = i2f;
6434                         }
6435
6436                         if (cfg->gen_write_barriers && is_ref)
6437                                 emit_write_barrier (cfg, args [0], args [1]);
6438                 }
6439                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
6440                         MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
6441                         guint32 opcode, f2i_opcode, i2f_opcode;
6442                         gboolean is_ref = mini_type_is_reference (fsig->params [1]);
6443                         gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
6444
6445                         if (fsig->params [1]->type == MONO_TYPE_I4 ||
6446                             fsig->params [1]->type == MONO_TYPE_R4) {
6447                                 opcode = OP_ATOMIC_CAS_I4;
6448                                 f2i_opcode = OP_MOVE_F_TO_I4;
6449                                 i2f_opcode = OP_MOVE_I4_TO_F;
6450                                 cfg->has_atomic_cas_i4 = TRUE;
6451                         }
6452 #if SIZEOF_REGISTER == 8
6453                         else if (is_ref ||
6454                                  fsig->params [1]->type == MONO_TYPE_I8 ||
6455                                  fsig->params [1]->type == MONO_TYPE_R8 ||
6456                                  fsig->params [1]->type == MONO_TYPE_I) {
6457                                 opcode = OP_ATOMIC_CAS_I8;
6458                                 f2i_opcode = OP_MOVE_F_TO_I8;
6459                                 i2f_opcode = OP_MOVE_I8_TO_F;
6460                         }
6461 #else
6462                         else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
6463                                 opcode = OP_ATOMIC_CAS_I4;
6464                                 cfg->has_atomic_cas_i4 = TRUE;
6465                         }
6466 #endif
6467                         else
6468                                 return NULL;
6469
6470                         if (!mono_arch_opcode_supported (opcode))
6471                                 return NULL;
6472
6473                         if (is_float) {
6474                                 /* TODO: Decompose these opcodes instead of bailing here. */
6475                                 if (COMPILE_SOFT_FLOAT (cfg))
6476                                         return NULL;
6477
6478                                 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
6479                                 f2i_new->dreg = mono_alloc_ireg (cfg);
6480                                 f2i_new->sreg1 = args [1]->dreg;
6481                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
6482                                         f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6483                                 MONO_ADD_INS (cfg->cbb, f2i_new);
6484
6485                                 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
6486                                 f2i_cmp->dreg = mono_alloc_ireg (cfg);
6487                                 f2i_cmp->sreg1 = args [2]->dreg;
6488                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
6489                                         f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6490                                 MONO_ADD_INS (cfg->cbb, f2i_cmp);
6491                         }
6492
6493                         MONO_INST_NEW (cfg, ins, opcode);
6494                         ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
6495                         ins->sreg1 = args [0]->dreg;
6496                         ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
6497                         ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
6498                         MONO_ADD_INS (cfg->cbb, ins);
6499
6500                         switch (fsig->params [1]->type) {
6501                         case MONO_TYPE_I4:
6502                                 ins->type = STACK_I4;
6503                                 break;
6504                         case MONO_TYPE_I8:
6505                                 ins->type = STACK_I8;
6506                                 break;
6507                         case MONO_TYPE_I:
6508 #if SIZEOF_REGISTER == 8
6509                                 ins->type = STACK_I8;
6510 #else
6511                                 ins->type = STACK_I4;
6512 #endif
6513                                 break;
6514                         case MONO_TYPE_R4:
6515                                 ins->type = cfg->r4_stack_type;
6516                                 break;
6517                         case MONO_TYPE_R8:
6518                                 ins->type = STACK_R8;
6519                                 break;
6520                         default:
6521                                 g_assert (mini_type_is_reference (fsig->params [1]));
6522                                 ins->type = STACK_OBJ;
6523                                 break;
6524                         }
6525
6526                         if (is_float) {
6527                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6528                                 i2f->dreg = mono_alloc_freg (cfg);
6529                                 i2f->sreg1 = ins->dreg;
6530                                 i2f->type = STACK_R8;
6531                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
6532                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6533                                 MONO_ADD_INS (cfg->cbb, i2f);
6534
6535                                 ins = i2f;
6536                         }
6537
6538                         if (cfg->gen_write_barriers && is_ref)
6539                                 emit_write_barrier (cfg, args [0], args [1]);
6540                 }
6541                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
6542                          fsig->params [1]->type == MONO_TYPE_I4) {
6543                         MonoInst *cmp, *ceq;
6544
6545                         if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
6546                                 return NULL;
6547
6548                         /* int32 r = CAS (location, value, comparand); */
6549                         MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
6550                         ins->dreg = alloc_ireg (cfg);
6551                         ins->sreg1 = args [0]->dreg;
6552                         ins->sreg2 = args [1]->dreg;
6553                         ins->sreg3 = args [2]->dreg;
6554                         ins->type = STACK_I4;
6555                         MONO_ADD_INS (cfg->cbb, ins);
6556
6557                         /* bool result = r == comparand; */
6558                         MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
6559                         cmp->sreg1 = ins->dreg;
6560                         cmp->sreg2 = args [2]->dreg;
6561                         cmp->type = STACK_I4;
6562                         MONO_ADD_INS (cfg->cbb, cmp);
6563
6564                         MONO_INST_NEW (cfg, ceq, OP_ICEQ);
6565                         ceq->dreg = alloc_ireg (cfg);
6566                         ceq->type = STACK_I4;
6567                         MONO_ADD_INS (cfg->cbb, ceq);
6568
6569                         /* *success = result; */
6570                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
6571
6572                         cfg->has_atomic_cas_i4 = TRUE;
6573                 }
6574                 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
6575                         ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6576
6577                 if (ins)
6578                         return ins;
6579         } else if (cmethod->klass->image == mono_defaults.corlib &&
6580                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6581                            (strcmp (cmethod->klass->name, "Volatile") == 0)) {
6582                 ins = NULL;
6583
6584                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
6585                         guint32 opcode = 0;
6586                         MonoType *t = fsig->params [0];
6587                         gboolean is_ref;
6588                         gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
6589
6590                         g_assert (t->byref);
6591                         /* t is a byref type, so the reference check is more complicated */
6592                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6593                         if (t->type == MONO_TYPE_I1)
6594                                 opcode = OP_ATOMIC_LOAD_I1;
6595                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6596                                 opcode = OP_ATOMIC_LOAD_U1;
6597                         else if (t->type == MONO_TYPE_I2)
6598                                 opcode = OP_ATOMIC_LOAD_I2;
6599                         else if (t->type == MONO_TYPE_U2)
6600                                 opcode = OP_ATOMIC_LOAD_U2;
6601                         else if (t->type == MONO_TYPE_I4)
6602                                 opcode = OP_ATOMIC_LOAD_I4;
6603                         else if (t->type == MONO_TYPE_U4)
6604                                 opcode = OP_ATOMIC_LOAD_U4;
6605                         else if (t->type == MONO_TYPE_R4)
6606                                 opcode = OP_ATOMIC_LOAD_R4;
6607                         else if (t->type == MONO_TYPE_R8)
6608                                 opcode = OP_ATOMIC_LOAD_R8;
6609 #if SIZEOF_REGISTER == 8
6610                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6611                                 opcode = OP_ATOMIC_LOAD_I8;
6612                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6613                                 opcode = OP_ATOMIC_LOAD_U8;
6614 #else
6615                         else if (t->type == MONO_TYPE_I)
6616                                 opcode = OP_ATOMIC_LOAD_I4;
6617                         else if (is_ref || t->type == MONO_TYPE_U)
6618                                 opcode = OP_ATOMIC_LOAD_U4;
6619 #endif
6620
6621                         if (opcode) {
6622                                 if (!mono_arch_opcode_supported (opcode))
6623                                         return NULL;
6624
6625                                 MONO_INST_NEW (cfg, ins, opcode);
6626                                 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
6627                                 ins->sreg1 = args [0]->dreg;
6628                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
6629                                 MONO_ADD_INS (cfg->cbb, ins);
6630
6631                                 switch (t->type) {
6632                                 case MONO_TYPE_BOOLEAN:
6633                                 case MONO_TYPE_I1:
6634                                 case MONO_TYPE_U1:
6635                                 case MONO_TYPE_I2:
6636                                 case MONO_TYPE_U2:
6637                                 case MONO_TYPE_I4:
6638                                 case MONO_TYPE_U4:
6639                                         ins->type = STACK_I4;
6640                                         break;
6641                                 case MONO_TYPE_I8:
6642                                 case MONO_TYPE_U8:
6643                                         ins->type = STACK_I8;
6644                                         break;
6645                                 case MONO_TYPE_I:
6646                                 case MONO_TYPE_U:
6647 #if SIZEOF_REGISTER == 8
6648                                         ins->type = STACK_I8;
6649 #else
6650                                         ins->type = STACK_I4;
6651 #endif
6652                                         break;
6653                                 case MONO_TYPE_R4:
6654                                         ins->type = cfg->r4_stack_type;
6655                                         break;
6656                                 case MONO_TYPE_R8:
6657                                         ins->type = STACK_R8;
6658                                         break;
6659                                 default:
6660                                         g_assert (is_ref);
6661                                         ins->type = STACK_OBJ;
6662                                         break;
6663                                 }
6664                         }
6665                 }
6666
6667                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6668                         guint32 opcode = 0;
6669                         MonoType *t = fsig->params [0];
6670                         gboolean is_ref;
6671
6672                         g_assert (t->byref);
6673                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6674                         if (t->type == MONO_TYPE_I1)
6675                                 opcode = OP_ATOMIC_STORE_I1;
6676                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6677                                 opcode = OP_ATOMIC_STORE_U1;
6678                         else if (t->type == MONO_TYPE_I2)
6679                                 opcode = OP_ATOMIC_STORE_I2;
6680                         else if (t->type == MONO_TYPE_U2)
6681                                 opcode = OP_ATOMIC_STORE_U2;
6682                         else if (t->type == MONO_TYPE_I4)
6683                                 opcode = OP_ATOMIC_STORE_I4;
6684                         else if (t->type == MONO_TYPE_U4)
6685                                 opcode = OP_ATOMIC_STORE_U4;
6686                         else if (t->type == MONO_TYPE_R4)
6687                                 opcode = OP_ATOMIC_STORE_R4;
6688                         else if (t->type == MONO_TYPE_R8)
6689                                 opcode = OP_ATOMIC_STORE_R8;
6690 #if SIZEOF_REGISTER == 8
6691                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6692                                 opcode = OP_ATOMIC_STORE_I8;
6693                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6694                                 opcode = OP_ATOMIC_STORE_U8;
6695 #else
6696                         else if (t->type == MONO_TYPE_I)
6697                                 opcode = OP_ATOMIC_STORE_I4;
6698                         else if (is_ref || t->type == MONO_TYPE_U)
6699                                 opcode = OP_ATOMIC_STORE_U4;
6700 #endif
6701
6702                         if (opcode) {
6703                                 if (!mono_arch_opcode_supported (opcode))
6704                                         return NULL;
6705
6706                                 MONO_INST_NEW (cfg, ins, opcode);
6707                                 ins->dreg = args [0]->dreg;
6708                                 ins->sreg1 = args [1]->dreg;
6709                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6710                                 MONO_ADD_INS (cfg->cbb, ins);
6711
6712                                 if (cfg->gen_write_barriers && is_ref)
6713                                         emit_write_barrier (cfg, args [0], args [1]);
6714                         }
6715                 }
6716
6717                 if (ins)
6718                         return ins;
6719         } else if (cmethod->klass->image == mono_defaults.corlib &&
6720                            (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6721                            (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6722                 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6723                         if (should_insert_brekpoint (cfg->method)) {
6724                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6725                         } else {
6726                                 MONO_INST_NEW (cfg, ins, OP_NOP);
6727                                 MONO_ADD_INS (cfg->cbb, ins);
6728                         }
6729                         return ins;
6730                 }
6731         } else if (cmethod->klass->image == mono_defaults.corlib &&
6732                    (strcmp (cmethod->klass->name_space, "System") == 0) &&
6733                    (strcmp (cmethod->klass->name, "Environment") == 0)) {
6734                 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6735 #ifdef TARGET_WIN32
6736                         EMIT_NEW_ICONST (cfg, ins, 1);
6737 #else
6738                         EMIT_NEW_ICONST (cfg, ins, 0);
6739 #endif
6740                 }
6741         } else if (cmethod->klass->image == mono_defaults.corlib &&
6742                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6743                            (strcmp (cmethod->klass->name, "Assembly") == 0)) {
6744                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
6745                         /* No stack walks are currently available, so implement this as an intrinsic */
6746                         MonoInst *assembly_ins;
6747
6748                         EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6749                         ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6750                         return ins;
6751                 }
6752         } else if (cmethod->klass->image == mono_defaults.corlib &&
6753                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6754                            (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6755                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6756                         /* No stack walks are currently available, so implement this as an intrinsic */
6757                         MonoInst *method_ins;
6758                         MonoMethod *declaring = cfg->method;
6759
6760                         /* This returns the declaring generic method */
6761                         if (declaring->is_inflated)
6762                                 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6763                         EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6764                         ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6765                         cfg->no_inline = TRUE;
6766                         if (cfg->method != cfg->current_method)
6767                                 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6768                         return ins;
6769                 }
6770         } else if (cmethod->klass == mono_defaults.math_class) {
6771                 /* 
6772                  * There is general branchless code for Min/Max, but it does not work for 
6773                  * all inputs:
6774                  * http://everything2.com/?node_id=1051618
6775                  */
6776         } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6777                     !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6778                                 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6779                                 !strcmp (cmethod->klass->name, "Selector")) ||
6780                            (!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") &&
6781                                 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6782                                 !strcmp (cmethod->klass->name, "Selector"))
6783                            ) {
6784                 if (cfg->backend->have_objc_get_selector &&
6785                         !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6786                     (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6787                     cfg->compile_aot && !cfg->llvm_only) {
6788                         MonoInst *pi;
6789                         MonoJumpInfoToken *ji;
6790                         MonoString *s;
6791
6792                         // FIXME: llvmonly
6793
6794                         cfg->exception_message = g_strdup ("GetHandle");
6795                         cfg->disable_llvm = TRUE;
6796
6797                         if (args [0]->opcode == OP_GOT_ENTRY) {
6798                                 pi = (MonoInst *)args [0]->inst_p1;
6799                                 g_assert (pi->opcode == OP_PATCH_INFO);
6800                                 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6801                                 ji = (MonoJumpInfoToken *)pi->inst_p0;
6802                         } else {
6803                                 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6804                                 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6805                         }
6806
6807                         NULLIFY_INS (args [0]);
6808
6809                         // FIXME: Ugly
6810                         s = mono_ldstr (cfg->domain, ji->image, mono_metadata_token_index (ji->token));
6811                         MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6812                         ins->dreg = mono_alloc_ireg (cfg);
6813                         // FIXME: Leaks
6814                         ins->inst_p0 = mono_string_to_utf8 (s);
6815                         MONO_ADD_INS (cfg->cbb, ins);
6816                         return ins;
6817                 }
6818         }
6819
6820 #ifdef MONO_ARCH_SIMD_INTRINSICS
6821         if (cfg->opt & MONO_OPT_SIMD) {
6822                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6823                 if (ins)
6824                         return ins;
6825         }
6826 #endif
6827
6828         ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6829         if (ins)
6830                 return ins;
6831
6832         if (COMPILE_LLVM (cfg)) {
6833                 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6834                 if (ins)
6835                         return ins;
6836         }
6837
6838         return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6839 }
6840
6841 /*
6842  * This entry point could be used later for arbitrary method
6843  * redirection.
6844  */
6845 inline static MonoInst*
6846 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,  
6847                                         MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6848 {
6849         if (method->klass == mono_defaults.string_class) {
6850                 /* managed string allocation support */
6851                 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6852                         MonoInst *iargs [2];
6853                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6854                         MonoMethod *managed_alloc = NULL;
6855
6856                         g_assert (vtable); /*Should not fail since it System.String*/
6857 #ifndef MONO_CROSS_COMPILE
6858                         managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6859 #endif
6860                         if (!managed_alloc)
6861                                 return NULL;
6862                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6863                         iargs [1] = args [0];
6864                         return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6865                 }
6866         }
6867         return NULL;
6868 }
6869
6870 static void
6871 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6872 {
6873         MonoInst *store, *temp;
6874         int i;
6875
6876         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6877                 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6878
6879                 /*
6880                  * FIXME: We should use *args++ = sp [0], but that would mean the arg
6881                  * would be different than the MonoInst's used to represent arguments, and
6882                  * the ldelema implementation can't deal with that.
6883                  * Solution: When ldelema is used on an inline argument, create a var for 
6884                  * it, emit ldelema on that var, and emit the saving code below in
6885                  * inline_method () if needed.
6886                  */
6887                 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6888                 cfg->args [i] = temp;
6889                 /* This uses cfg->args [i] which is set by the preceeding line */
6890                 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6891                 store->cil_code = sp [0]->cil_code;
6892                 sp++;
6893         }
6894 }
6895
6896 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6897 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6898
6899 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6900 static gboolean
6901 check_inline_called_method_name_limit (MonoMethod *called_method)
6902 {
6903         int strncmp_result;
6904         static const char *limit = NULL;
6905         
6906         if (limit == NULL) {
6907                 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6908
6909                 if (limit_string != NULL)
6910                         limit = limit_string;
6911                 else
6912                         limit = "";
6913         }
6914
6915         if (limit [0] != '\0') {
6916                 char *called_method_name = mono_method_full_name (called_method, TRUE);
6917
6918                 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6919                 g_free (called_method_name);
6920         
6921                 //return (strncmp_result <= 0);
6922                 return (strncmp_result == 0);
6923         } else {
6924                 return TRUE;
6925         }
6926 }
6927 #endif
6928
6929 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6930 static gboolean
6931 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6932 {
6933         int strncmp_result;
6934         static const char *limit = NULL;
6935         
6936         if (limit == NULL) {
6937                 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6938                 if (limit_string != NULL) {
6939                         limit = limit_string;
6940                 } else {
6941                         limit = "";
6942                 }
6943         }
6944
6945         if (limit [0] != '\0') {
6946                 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6947
6948                 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6949                 g_free (caller_method_name);
6950         
6951                 //return (strncmp_result <= 0);
6952                 return (strncmp_result == 0);
6953         } else {
6954                 return TRUE;
6955         }
6956 }
6957 #endif
6958
6959 static void
6960 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6961 {
6962         static double r8_0 = 0.0;
6963         static float r4_0 = 0.0;
6964         MonoInst *ins;
6965         int t;
6966
6967         rtype = mini_get_underlying_type (rtype);
6968         t = rtype->type;
6969
6970         if (rtype->byref) {
6971                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6972         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6973                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6974         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6975                 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6976         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6977                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6978                 ins->type = STACK_R4;
6979                 ins->inst_p0 = (void*)&r4_0;
6980                 ins->dreg = dreg;
6981                 MONO_ADD_INS (cfg->cbb, ins);
6982         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6983                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6984                 ins->type = STACK_R8;
6985                 ins->inst_p0 = (void*)&r8_0;
6986                 ins->dreg = dreg;
6987                 MONO_ADD_INS (cfg->cbb, ins);
6988         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6989                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6990                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6991         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6992                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6993         } else {
6994                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6995         }
6996 }
6997
6998 static void
6999 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
7000 {
7001         int t;
7002
7003         rtype = mini_get_underlying_type (rtype);
7004         t = rtype->type;
7005
7006         if (rtype->byref) {
7007                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
7008         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7009                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
7010         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7011                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
7012         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
7013                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
7014         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7015                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
7016         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7017                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
7018                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7019         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
7020                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7021         } else {
7022                 emit_init_rvar (cfg, dreg, rtype);
7023         }
7024 }
7025
7026 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
7027 static void
7028 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
7029 {
7030         MonoInst *var = cfg->locals [local];
7031         if (COMPILE_SOFT_FLOAT (cfg)) {
7032                 MonoInst *store;
7033                 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
7034                 emit_init_rvar (cfg, reg, type);
7035                 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
7036         } else {
7037                 if (init)
7038                         emit_init_rvar (cfg, var->dreg, type);
7039                 else
7040                         emit_dummy_init_rvar (cfg, var->dreg, type);
7041         }
7042 }
7043
7044 /*
7045  * inline_method:
7046  *
7047  *   Return the cost of inlining CMETHOD.
7048  */
7049 static int
7050 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
7051                            guchar *ip, guint real_offset, gboolean inline_always)
7052 {
7053         MonoInst *ins, *rvar = NULL;
7054         MonoMethodHeader *cheader;
7055         MonoBasicBlock *ebblock, *sbblock;
7056         int i, costs;
7057         MonoMethod *prev_inlined_method;
7058         MonoInst **prev_locals, **prev_args;
7059         MonoType **prev_arg_types;
7060         guint prev_real_offset;
7061         GHashTable *prev_cbb_hash;
7062         MonoBasicBlock **prev_cil_offset_to_bb;
7063         MonoBasicBlock *prev_cbb;
7064         unsigned char* prev_cil_start;
7065         guint32 prev_cil_offset_to_bb_len;
7066         MonoMethod *prev_current_method;
7067         MonoGenericContext *prev_generic_context;
7068         gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
7069
7070         g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
7071
7072 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
7073         if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
7074                 return 0;
7075 #endif
7076 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
7077         if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
7078                 return 0;
7079 #endif
7080
7081         if (!fsig)
7082                 fsig = mono_method_signature (cmethod);
7083
7084         if (cfg->verbose_level > 2)
7085                 printf ("INLINE START %p %s -> %s\n", cmethod,  mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7086
7087         if (!cmethod->inline_info) {
7088                 cfg->stat_inlineable_methods++;
7089                 cmethod->inline_info = 1;
7090         }
7091
7092         /* allocate local variables */
7093         cheader = mono_method_get_header (cmethod);
7094
7095         if (cheader == NULL || mono_loader_get_last_error ()) {
7096                 if (cheader)
7097                         mono_metadata_free_mh (cheader);
7098                 if (inline_always && mono_loader_get_last_error ()) {
7099                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7100                         mono_error_set_from_loader_error (&cfg->error);
7101                 }
7102
7103                 mono_loader_clear_error ();
7104                 return 0;
7105         }
7106
7107         /*Must verify before creating locals as it can cause the JIT to assert.*/
7108         if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
7109                 mono_metadata_free_mh (cheader);
7110                 return 0;
7111         }
7112
7113         /* allocate space to store the return value */
7114         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
7115                 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
7116         }
7117
7118         prev_locals = cfg->locals;
7119         cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
7120         for (i = 0; i < cheader->num_locals; ++i)
7121                 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
7122
7123         /* allocate start and end blocks */
7124         /* This is needed so if the inline is aborted, we can clean up */
7125         NEW_BBLOCK (cfg, sbblock);
7126         sbblock->real_offset = real_offset;
7127
7128         NEW_BBLOCK (cfg, ebblock);
7129         ebblock->block_num = cfg->num_bblocks++;
7130         ebblock->real_offset = real_offset;
7131
7132         prev_args = cfg->args;
7133         prev_arg_types = cfg->arg_types;
7134         prev_inlined_method = cfg->inlined_method;
7135         cfg->inlined_method = cmethod;
7136         cfg->ret_var_set = FALSE;
7137         cfg->inline_depth ++;
7138         prev_real_offset = cfg->real_offset;
7139         prev_cbb_hash = cfg->cbb_hash;
7140         prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
7141         prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
7142         prev_cil_start = cfg->cil_start;
7143         prev_cbb = cfg->cbb;
7144         prev_current_method = cfg->current_method;
7145         prev_generic_context = cfg->generic_context;
7146         prev_ret_var_set = cfg->ret_var_set;
7147         prev_disable_inline = cfg->disable_inline;
7148
7149         if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
7150                 virtual_ = TRUE;
7151
7152         costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
7153
7154         ret_var_set = cfg->ret_var_set;
7155
7156         cfg->inlined_method = prev_inlined_method;
7157         cfg->real_offset = prev_real_offset;
7158         cfg->cbb_hash = prev_cbb_hash;
7159         cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
7160         cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
7161         cfg->cil_start = prev_cil_start;
7162         cfg->locals = prev_locals;
7163         cfg->args = prev_args;
7164         cfg->arg_types = prev_arg_types;
7165         cfg->current_method = prev_current_method;
7166         cfg->generic_context = prev_generic_context;
7167         cfg->ret_var_set = prev_ret_var_set;
7168         cfg->disable_inline = prev_disable_inline;
7169         cfg->inline_depth --;
7170
7171         if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
7172                 if (cfg->verbose_level > 2)
7173                         printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7174                 
7175                 cfg->stat_inlined_methods++;
7176
7177                 /* always add some code to avoid block split failures */
7178                 MONO_INST_NEW (cfg, ins, OP_NOP);
7179                 MONO_ADD_INS (prev_cbb, ins);
7180
7181                 prev_cbb->next_bb = sbblock;
7182                 link_bblock (cfg, prev_cbb, sbblock);
7183
7184                 /* 
7185                  * Get rid of the begin and end bblocks if possible to aid local
7186                  * optimizations.
7187                  */
7188                 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
7189
7190                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
7191                         mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
7192
7193                 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
7194                         MonoBasicBlock *prev = ebblock->in_bb [0];
7195
7196                         if (prev->next_bb == ebblock) {
7197                                 mono_merge_basic_blocks (cfg, prev, ebblock);
7198                                 cfg->cbb = prev;
7199                                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
7200                                         mono_merge_basic_blocks (cfg, prev_cbb, prev);
7201                                         cfg->cbb = prev_cbb;
7202                                 }
7203                         } else {
7204                                 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
7205                                 cfg->cbb = ebblock;
7206                         }
7207                 } else {
7208                         /* 
7209                          * Its possible that the rvar is set in some prev bblock, but not in others.
7210                          * (#1835).
7211                          */
7212                         if (rvar) {
7213                                 MonoBasicBlock *bb;
7214
7215                                 for (i = 0; i < ebblock->in_count; ++i) {
7216                                         bb = ebblock->in_bb [i];
7217
7218                                         if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
7219                                                 cfg->cbb = bb;
7220
7221                                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7222                                         }
7223                                 }
7224                         }
7225
7226                         cfg->cbb = ebblock;
7227                 }
7228
7229                 if (rvar) {
7230                         /*
7231                          * If the inlined method contains only a throw, then the ret var is not 
7232                          * set, so set it to a dummy value.
7233                          */
7234                         if (!ret_var_set)
7235                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7236
7237                         EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
7238                         *sp++ = ins;
7239                 }
7240                 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7241                 return costs + 1;
7242         } else {
7243                 if (cfg->verbose_level > 2)
7244                         printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
7245                 cfg->exception_type = MONO_EXCEPTION_NONE;
7246                 mono_loader_clear_error ();
7247
7248                 /* This gets rid of the newly added bblocks */
7249                 cfg->cbb = prev_cbb;
7250         }
7251         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7252         return 0;
7253 }
7254
7255 /*
7256  * Some of these comments may well be out-of-date.
7257  * Design decisions: we do a single pass over the IL code (and we do bblock 
7258  * splitting/merging in the few cases when it's required: a back jump to an IL
7259  * address that was not already seen as bblock starting point).
7260  * Code is validated as we go (full verification is still better left to metadata/verify.c).
7261  * Complex operations are decomposed in simpler ones right away. We need to let the 
7262  * arch-specific code peek and poke inside this process somehow (except when the 
7263  * optimizations can take advantage of the full semantic info of coarse opcodes).
7264  * All the opcodes of the form opcode.s are 'normalized' to opcode.
7265  * MonoInst->opcode initially is the IL opcode or some simplification of that 
7266  * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific 
7267  * opcode with value bigger than OP_LAST.
7268  * At this point the IR can be handed over to an interpreter, a dumb code generator
7269  * or to the optimizing code generator that will translate it to SSA form.
7270  *
7271  * Profiling directed optimizations.
7272  * We may compile by default with few or no optimizations and instrument the code
7273  * or the user may indicate what methods to optimize the most either in a config file
7274  * or through repeated runs where the compiler applies offline the optimizations to 
7275  * each method and then decides if it was worth it.
7276  */
7277
7278 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
7279 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
7280 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
7281 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
7282 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
7283 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
7284 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
7285 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) TYPE_LOAD_ERROR ((klass))
7286
7287 /* offset from br.s -> br like opcodes */
7288 #define BIG_BRANCH_OFFSET 13
7289
7290 static gboolean
7291 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
7292 {
7293         MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
7294
7295         return b == NULL || b == bb;
7296 }
7297
7298 static int
7299 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
7300 {
7301         unsigned char *ip = start;
7302         unsigned char *target;
7303         int i;
7304         guint cli_addr;
7305         MonoBasicBlock *bblock;
7306         const MonoOpcode *opcode;
7307
7308         while (ip < end) {
7309                 cli_addr = ip - start;
7310                 i = mono_opcode_value ((const guint8 **)&ip, end);
7311                 if (i < 0)
7312                         UNVERIFIED;
7313                 opcode = &mono_opcodes [i];
7314                 switch (opcode->argument) {
7315                 case MonoInlineNone:
7316                         ip++; 
7317                         break;
7318                 case MonoInlineString:
7319                 case MonoInlineType:
7320                 case MonoInlineField:
7321                 case MonoInlineMethod:
7322                 case MonoInlineTok:
7323                 case MonoInlineSig:
7324                 case MonoShortInlineR:
7325                 case MonoInlineI:
7326                         ip += 5;
7327                         break;
7328                 case MonoInlineVar:
7329                         ip += 3;
7330                         break;
7331                 case MonoShortInlineVar:
7332                 case MonoShortInlineI:
7333                         ip += 2;
7334                         break;
7335                 case MonoShortInlineBrTarget:
7336                         target = start + cli_addr + 2 + (signed char)ip [1];
7337                         GET_BBLOCK (cfg, bblock, target);
7338                         ip += 2;
7339                         if (ip < end)
7340                                 GET_BBLOCK (cfg, bblock, ip);
7341                         break;
7342                 case MonoInlineBrTarget:
7343                         target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
7344                         GET_BBLOCK (cfg, bblock, target);
7345                         ip += 5;
7346                         if (ip < end)
7347                                 GET_BBLOCK (cfg, bblock, ip);
7348                         break;
7349                 case MonoInlineSwitch: {
7350                         guint32 n = read32 (ip + 1);
7351                         guint32 j;
7352                         ip += 5;
7353                         cli_addr += 5 + 4 * n;
7354                         target = start + cli_addr;
7355                         GET_BBLOCK (cfg, bblock, target);
7356                         
7357                         for (j = 0; j < n; ++j) {
7358                                 target = start + cli_addr + (gint32)read32 (ip);
7359                                 GET_BBLOCK (cfg, bblock, target);
7360                                 ip += 4;
7361                         }
7362                         break;
7363                 }
7364                 case MonoInlineR:
7365                 case MonoInlineI8:
7366                         ip += 9;
7367                         break;
7368                 default:
7369                         g_assert_not_reached ();
7370                 }
7371
7372                 if (i == CEE_THROW) {
7373                         unsigned char *bb_start = ip - 1;
7374                         
7375                         /* Find the start of the bblock containing the throw */
7376                         bblock = NULL;
7377                         while ((bb_start >= start) && !bblock) {
7378                                 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
7379                                 bb_start --;
7380                         }
7381                         if (bblock)
7382                                 bblock->out_of_line = 1;
7383                 }
7384         }
7385         return 0;
7386 unverified:
7387 exception_exit:
7388         *pos = ip;
7389         return 1;
7390 }
7391
7392 static inline MonoMethod *
7393 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
7394 {
7395         MonoMethod *method;
7396
7397         mono_error_init (error);
7398
7399         if (m->wrapper_type != MONO_WRAPPER_NONE) {
7400                 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
7401                 if (context) {
7402                         method = mono_class_inflate_generic_method_checked (method, context, error);
7403                 }
7404         } else {
7405                 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
7406         }
7407
7408         return method;
7409 }
7410
7411 static inline MonoMethod *
7412 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7413 {
7414         MonoError error;
7415         MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
7416
7417         if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
7418                 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
7419                 method = NULL;
7420         }
7421
7422         if (!method && !cfg)
7423                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7424
7425         return method;
7426 }
7427
7428 static inline MonoClass*
7429 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
7430 {
7431         MonoError error;
7432         MonoClass *klass;
7433
7434         if (method->wrapper_type != MONO_WRAPPER_NONE) {
7435                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7436                 if (context)
7437                         klass = mono_class_inflate_generic_class (klass, context);
7438         } else {
7439                 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
7440                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7441         }
7442         if (klass)
7443                 mono_class_init (klass);
7444         return klass;
7445 }
7446
7447 static inline MonoMethodSignature*
7448 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context)
7449 {
7450         MonoMethodSignature *fsig;
7451
7452         if (method->wrapper_type != MONO_WRAPPER_NONE) {
7453                 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
7454         } else {
7455                 fsig = mono_metadata_parse_signature (method->klass->image, token);
7456         }
7457         if (context) {
7458                 MonoError error;
7459                 fsig = mono_inflate_generic_signature(fsig, context, &error);
7460                 // FIXME:
7461                 g_assert(mono_error_ok(&error));
7462         }
7463         return fsig;
7464 }
7465
7466 static MonoMethod*
7467 throw_exception (void)
7468 {
7469         static MonoMethod *method = NULL;
7470
7471         if (!method) {
7472                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7473                 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
7474         }
7475         g_assert (method);
7476         return method;
7477 }
7478
7479 static void
7480 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
7481 {
7482         MonoMethod *thrower = throw_exception ();
7483         MonoInst *args [1];
7484
7485         EMIT_NEW_PCONST (cfg, args [0], ex);
7486         mono_emit_method_call (cfg, thrower, args, NULL);
7487 }
7488
7489 /*
7490  * Return the original method is a wrapper is specified. We can only access 
7491  * the custom attributes from the original method.
7492  */
7493 static MonoMethod*
7494 get_original_method (MonoMethod *method)
7495 {
7496         if (method->wrapper_type == MONO_WRAPPER_NONE)
7497                 return method;
7498
7499         /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
7500         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
7501                 return NULL;
7502
7503         /* in other cases we need to find the original method */
7504         return mono_marshal_method_from_wrapper (method);
7505 }
7506
7507 static void
7508 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
7509 {
7510         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7511         MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
7512         if (ex)
7513                 emit_throw_exception (cfg, ex);
7514 }
7515
7516 static void
7517 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
7518 {
7519         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7520         MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
7521         if (ex)
7522                 emit_throw_exception (cfg, ex);
7523 }
7524
7525 /*
7526  * Check that the IL instructions at ip are the array initialization
7527  * sequence and return the pointer to the data and the size.
7528  */
7529 static const char*
7530 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
7531 {
7532         /*
7533          * newarr[System.Int32]
7534          * dup
7535          * ldtoken field valuetype ...
7536          * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
7537          */
7538         if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
7539                 MonoError error;
7540                 guint32 token = read32 (ip + 7);
7541                 guint32 field_token = read32 (ip + 2);
7542                 guint32 field_index = field_token & 0xffffff;
7543                 guint32 rva;
7544                 const char *data_ptr;
7545                 int size = 0;
7546                 MonoMethod *cmethod;
7547                 MonoClass *dummy_class;
7548                 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
7549                 int dummy_align;
7550
7551                 if (!field) {
7552                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
7553                         return NULL;
7554                 }
7555
7556                 *out_field_token = field_token;
7557
7558                 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
7559                 if (!cmethod)
7560                         return NULL;
7561                 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
7562                         return NULL;
7563                 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
7564                 case MONO_TYPE_BOOLEAN:
7565                 case MONO_TYPE_I1:
7566                 case MONO_TYPE_U1:
7567                         size = 1; break;
7568                 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
7569 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
7570                 case MONO_TYPE_CHAR:
7571                 case MONO_TYPE_I2:
7572                 case MONO_TYPE_U2:
7573                         size = 2; break;
7574                 case MONO_TYPE_I4:
7575                 case MONO_TYPE_U4:
7576                 case MONO_TYPE_R4:
7577                         size = 4; break;
7578                 case MONO_TYPE_R8:
7579                 case MONO_TYPE_I8:
7580                 case MONO_TYPE_U8:
7581                         size = 8; break;
7582 #endif
7583                 default:
7584                         return NULL;
7585                 }
7586                 size *= len;
7587                 if (size > mono_type_size (field->type, &dummy_align))
7588                     return NULL;
7589                 *out_size = size;
7590                 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
7591                 if (!image_is_dynamic (method->klass->image)) {
7592                         field_index = read32 (ip + 2) & 0xffffff;
7593                         mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
7594                         data_ptr = mono_image_rva_map (method->klass->image, rva);
7595                         /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
7596                         /* for aot code we do the lookup on load */
7597                         if (aot && data_ptr)
7598                                 return (const char *)GUINT_TO_POINTER (rva);
7599                 } else {
7600                         /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */ 
7601                         g_assert (!aot);
7602                         data_ptr = mono_field_get_data (field);
7603                 }
7604                 return data_ptr;
7605         }
7606         return NULL;
7607 }
7608
7609 static void
7610 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
7611 {
7612         char *method_fname = mono_method_full_name (method, TRUE);
7613         char *method_code;
7614         MonoMethodHeader *header = mono_method_get_header (method);
7615
7616         if (header->code_size == 0)
7617                 method_code = g_strdup ("method body is empty.");
7618         else
7619                 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
7620         mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
7621         g_free (method_fname);
7622         g_free (method_code);
7623         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7624 }
7625
7626 static void
7627 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
7628 {
7629         MonoInst *ins;
7630         guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
7631         if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0]  &&
7632                         ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
7633                 /* Optimize reg-reg moves away */
7634                 /* 
7635                  * Can't optimize other opcodes, since sp[0] might point to
7636                  * the last ins of a decomposed opcode.
7637                  */
7638                 sp [0]->dreg = (cfg)->locals [n]->dreg;
7639         } else {
7640                 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
7641         }
7642 }
7643
7644 /*
7645  * ldloca inhibits many optimizations so try to get rid of it in common
7646  * cases.
7647  */
7648 static inline unsigned char *
7649 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7650 {
7651         int local, token;
7652         MonoClass *klass;
7653         MonoType *type;
7654
7655         if (size == 1) {
7656                 local = ip [1];
7657                 ip += 2;
7658         } else {
7659                 local = read16 (ip + 2);
7660                 ip += 4;
7661         }
7662         
7663         if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7664                 /* From the INITOBJ case */
7665                 token = read32 (ip + 2);
7666                 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7667                 CHECK_TYPELOAD (klass);
7668                 type = mini_get_underlying_type (&klass->byval_arg);
7669                 emit_init_local (cfg, local, type, TRUE);
7670                 return ip + 6;
7671         }
7672  exception_exit:
7673         return NULL;
7674 }
7675
7676 static MonoInst*
7677 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
7678 {
7679         MonoInst *icall_args [16];
7680         MonoInst *call_target, *ins, *vtable_ins;
7681         int arg_reg, this_reg, vtable_reg;
7682         gboolean is_iface = cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE;
7683         gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
7684         gboolean variant_iface = FALSE;
7685         guint32 slot;
7686         int offset;
7687
7688         /*
7689          * In llvm-only mode, vtables contain function descriptors instead of
7690          * method addresses/trampolines.
7691          */
7692         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
7693
7694         if (is_iface)
7695                 slot = mono_method_get_imt_slot (cmethod);
7696         else
7697                 slot = mono_method_get_vtable_index (cmethod);
7698
7699         this_reg = sp [0]->dreg;
7700
7701         if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
7702                 variant_iface = TRUE;
7703
7704         if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
7705                 /*
7706                  * The simplest case, a normal virtual call.
7707                  */
7708                 int slot_reg = alloc_preg (cfg);
7709                 int addr_reg = alloc_preg (cfg);
7710                 int arg_reg = alloc_preg (cfg);
7711                 MonoBasicBlock *non_null_bb;
7712
7713                 vtable_reg = alloc_preg (cfg);
7714                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7715                 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7716
7717                 /* Load the vtable slot, which contains a function descriptor. */
7718                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7719
7720                 NEW_BBLOCK (cfg, non_null_bb);
7721
7722                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7723                 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
7724                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
7725
7726                 /* Slow path */
7727                 // FIXME: Make the wrapper use the preserveall cconv
7728                 // FIXME: Use one icall per slot for small slot numbers ?
7729                 icall_args [0] = vtable_ins;
7730                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7731                 /* Make the icall return the vtable slot value to save some code space */
7732                 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7733                 ins->dreg = slot_reg;
7734                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7735
7736                 /* Fastpath */
7737                 MONO_START_BB (cfg, non_null_bb);
7738                 /* Load the address + arg from the vtable slot */
7739                 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7740                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7741
7742                 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7743         }
7744
7745         if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt) {
7746                 /*
7747                  * A simple interface call
7748                  *
7749                  * We make a call through an imt slot to obtain the function descriptor we need to call.
7750                  * The imt slot contains a function descriptor for a runtime function + arg.
7751                  */
7752                 int slot_reg = alloc_preg (cfg);
7753                 int addr_reg = alloc_preg (cfg);
7754                 int arg_reg = alloc_preg (cfg);
7755                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7756
7757                 vtable_reg = alloc_preg (cfg);
7758                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7759                 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7760
7761                 /*
7762                  * The slot is already initialized when the vtable is created so there is no need
7763                  * to check it here.
7764                  */
7765
7766                 /* Load the imt slot, which contains a function descriptor. */
7767                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7768
7769                 /* Load the address + arg of the imt thunk from the imt slot */
7770                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7771                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7772                 /*
7773                  * IMT thunks in llvm-only mode are C functions which take an info argument
7774                  * plus the imt method and return the ftndesc to call.
7775                  */
7776                 icall_args [0] = thunk_arg_ins;
7777                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7778                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7779                 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7780
7781                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7782         }
7783
7784         if ((fsig->generic_param_count || variant_iface) && !is_gsharedvt) {
7785                 /*
7786                  * This is similar to the interface case, the vtable slot points to an imt thunk which is
7787                  * dynamically extended as more instantiations are discovered.
7788                  * This handles generic virtual methods both on classes and interfaces.
7789                  */
7790                 int slot_reg = alloc_preg (cfg);
7791                 int addr_reg = alloc_preg (cfg);
7792                 int arg_reg = alloc_preg (cfg);
7793                 int ftndesc_reg = alloc_preg (cfg);
7794                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7795                 MonoBasicBlock *slowpath_bb, *end_bb;
7796
7797                 NEW_BBLOCK (cfg, slowpath_bb);
7798                 NEW_BBLOCK (cfg, end_bb);
7799
7800                 vtable_reg = alloc_preg (cfg);
7801                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7802                 if (is_iface)
7803                         offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7804                 else
7805                         offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7806
7807                 /* Load the slot, which contains a function descriptor. */
7808                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7809
7810                 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7811                 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7812                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7813                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7814
7815                 /* Fastpath */
7816                 /* Same as with iface calls */
7817                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7818                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7819                 icall_args [0] = thunk_arg_ins;
7820                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7821                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7822                 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7823                 ftndesc_ins->dreg = ftndesc_reg;
7824                 /*
7825                  * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7826                  * they don't know about yet. Fall back to the slowpath in that case.
7827                  */
7828                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7829                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7830
7831                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7832
7833                 /* Slowpath */
7834                 MONO_START_BB (cfg, slowpath_bb);
7835                 icall_args [0] = vtable_ins;
7836                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7837                 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7838                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7839                 if (is_iface)
7840                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7841                 else
7842                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7843                 ftndesc_ins->dreg = ftndesc_reg;
7844                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7845
7846                 /* Common case */
7847                 MONO_START_BB (cfg, end_bb);
7848                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7849         }
7850
7851         /*
7852          * Non-optimized cases
7853          */
7854         icall_args [0] = sp [0];
7855         EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7856
7857         icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7858                                                                                         cmethod, MONO_RGCTX_INFO_METHOD);
7859
7860         arg_reg = alloc_preg (cfg);
7861         MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7862         EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7863
7864         g_assert (is_gsharedvt);
7865         if (is_iface)
7866                 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7867         else
7868                 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7869
7870         /*
7871          * Pass the extra argument even if the callee doesn't receive it, most
7872          * calling conventions allow this.
7873          */
7874         return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7875 }
7876
7877 static gboolean
7878 is_exception_class (MonoClass *klass)
7879 {
7880         while (klass) {
7881                 if (klass == mono_defaults.exception_class)
7882                         return TRUE;
7883                 klass = klass->parent;
7884         }
7885         return FALSE;
7886 }
7887
7888 /*
7889  * is_jit_optimizer_disabled:
7890  *
7891  *   Determine whenever M's assembly has a DebuggableAttribute with the
7892  * IsJITOptimizerDisabled flag set.
7893  */
7894 static gboolean
7895 is_jit_optimizer_disabled (MonoMethod *m)
7896 {
7897         MonoAssembly *ass = m->klass->image->assembly;
7898         MonoCustomAttrInfo* attrs;
7899         MonoClass *klass;
7900         int i;
7901         gboolean val = FALSE;
7902
7903         g_assert (ass);
7904         if (ass->jit_optimizer_disabled_inited)
7905                 return ass->jit_optimizer_disabled;
7906
7907         klass = mono_class_try_get_debuggable_attribute_class ();
7908
7909         if (!klass) {
7910                 /* Linked away */
7911                 ass->jit_optimizer_disabled = FALSE;
7912                 mono_memory_barrier ();
7913                 ass->jit_optimizer_disabled_inited = TRUE;
7914                 return FALSE;
7915         }
7916
7917         attrs = mono_custom_attrs_from_assembly (ass);
7918         if (attrs) {
7919                 for (i = 0; i < attrs->num_attrs; ++i) {
7920                         MonoCustomAttrEntry *attr = &attrs->attrs [i];
7921                         const gchar *p;
7922                         MonoMethodSignature *sig;
7923
7924                         if (!attr->ctor || attr->ctor->klass != klass)
7925                                 continue;
7926                         /* Decode the attribute. See reflection.c */
7927                         p = (const char*)attr->data;
7928                         g_assert (read16 (p) == 0x0001);
7929                         p += 2;
7930
7931                         // FIXME: Support named parameters
7932                         sig = mono_method_signature (attr->ctor);
7933                         if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7934                                 continue;
7935                         /* Two boolean arguments */
7936                         p ++;
7937                         val = *p;
7938                 }
7939                 mono_custom_attrs_free (attrs);
7940         }
7941
7942         ass->jit_optimizer_disabled = val;
7943         mono_memory_barrier ();
7944         ass->jit_optimizer_disabled_inited = TRUE;
7945
7946         return val;
7947 }
7948
7949 static gboolean
7950 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7951 {
7952         gboolean supported_tail_call;
7953         int i;
7954
7955         supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7956
7957         for (i = 0; i < fsig->param_count; ++i) {
7958                 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7959                         /* These can point to the current method's stack */
7960                         supported_tail_call = FALSE;
7961         }
7962         if (fsig->hasthis && cmethod->klass->valuetype)
7963                 /* this might point to the current method's stack */
7964                 supported_tail_call = FALSE;
7965         if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7966                 supported_tail_call = FALSE;
7967         if (cfg->method->save_lmf)
7968                 supported_tail_call = FALSE;
7969         if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7970                 supported_tail_call = FALSE;
7971         if (call_opcode != CEE_CALL)
7972                 supported_tail_call = FALSE;
7973
7974         /* Debugging support */
7975 #if 0
7976         if (supported_tail_call) {
7977                 if (!mono_debug_count ())
7978                         supported_tail_call = FALSE;
7979         }
7980 #endif
7981
7982         return supported_tail_call;
7983 }
7984
7985 /*
7986  * handle_ctor_call:
7987  *
7988  *   Handle calls made to ctors from NEWOBJ opcodes.
7989  */
7990 static void
7991 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7992                                   MonoInst **sp, guint8 *ip, int *inline_costs)
7993 {
7994         MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7995
7996         if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7997                                         mono_method_is_generic_sharable (cmethod, TRUE)) {
7998                 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7999                         mono_class_vtable (cfg->domain, cmethod->klass);
8000                         CHECK_TYPELOAD (cmethod->klass);
8001
8002                         vtable_arg = emit_get_rgctx_method (cfg, context_used,
8003                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8004                 } else {
8005                         if (context_used) {
8006                                 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
8007                                                                                                    cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8008                         } else {
8009                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8010
8011                                 CHECK_TYPELOAD (cmethod->klass);
8012                                 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8013                         }
8014                 }
8015         }
8016
8017         /* Avoid virtual calls to ctors if possible */
8018         if (mono_class_is_marshalbyref (cmethod->klass))
8019                 callvirt_this_arg = sp [0];
8020
8021         if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
8022                 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
8023                 CHECK_CFG_EXCEPTION;
8024         } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
8025                            mono_method_check_inlining (cfg, cmethod) &&
8026                            !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
8027                 int costs;
8028
8029                 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
8030                         cfg->real_offset += 5;
8031
8032                         *inline_costs += costs - 5;
8033                 } else {
8034                         INLINE_FAILURE ("inline failure");
8035                         // FIXME-VT: Clean this up
8036                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8037                                 GSHAREDVT_FAILURE(*ip);
8038                         mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
8039                 }
8040         } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8041                 MonoInst *addr;
8042
8043                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
8044
8045                 if (cfg->llvm_only) {
8046                         // FIXME: Avoid initializing vtable_arg
8047                         emit_llvmonly_calli (cfg, fsig, sp, addr);
8048                 } else {
8049                         mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
8050                 }
8051         } else if (context_used &&
8052                            ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
8053                                  !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
8054                 MonoInst *cmethod_addr;
8055
8056                 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
8057
8058                 if (cfg->llvm_only) {
8059                         MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
8060                                                                                                         MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8061                         emit_llvmonly_calli (cfg, fsig, sp, addr);
8062                 } else {
8063                         cmethod_addr = emit_get_rgctx_method (cfg, context_used,
8064                                                                                                   cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8065
8066                         mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
8067                 }
8068         } else {
8069                 INLINE_FAILURE ("ctor call");
8070                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
8071                                                                                   callvirt_this_arg, NULL, vtable_arg);
8072         }
8073  exception_exit:
8074         return;
8075 }
8076
8077 static void
8078 emit_setret (MonoCompile *cfg, MonoInst *val)
8079 {
8080         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
8081         MonoInst *ins;
8082
8083         if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
8084                 MonoInst *ret_addr;
8085
8086                 if (!cfg->vret_addr) {
8087                         EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
8088                 } else {
8089                         EMIT_NEW_RETLOADA (cfg, ret_addr);
8090
8091                         EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
8092                         ins->klass = mono_class_from_mono_type (ret_type);
8093                 }
8094         } else {
8095 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
8096                 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
8097                         MonoInst *iargs [1];
8098                         MonoInst *conv;
8099
8100                         iargs [0] = val;
8101                         conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
8102                         mono_arch_emit_setret (cfg, cfg->method, conv);
8103                 } else {
8104                         mono_arch_emit_setret (cfg, cfg->method, val);
8105                 }
8106 #else
8107                 mono_arch_emit_setret (cfg, cfg->method, val);
8108 #endif
8109         }
8110 }
8111
8112 /*
8113  * mono_method_to_ir:
8114  *
8115  *   Translate the .net IL into linear IR.
8116  */
8117 int
8118 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
8119                    MonoInst *return_var, MonoInst **inline_args, 
8120                    guint inline_offset, gboolean is_virtual_call)
8121 {
8122         MonoError error;
8123         MonoInst *ins, **sp, **stack_start;
8124         MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
8125         MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
8126         MonoMethod *cmethod, *method_definition;
8127         MonoInst **arg_array;
8128         MonoMethodHeader *header;
8129         MonoImage *image;
8130         guint32 token, ins_flag;
8131         MonoClass *klass;
8132         MonoClass *constrained_class = NULL;
8133         unsigned char *ip, *end, *target, *err_pos;
8134         MonoMethodSignature *sig;
8135         MonoGenericContext *generic_context = NULL;
8136         MonoGenericContainer *generic_container = NULL;
8137         MonoType **param_types;
8138         int i, n, start_new_bblock, dreg;
8139         int num_calls = 0, inline_costs = 0;
8140         int breakpoint_id = 0;
8141         guint num_args;
8142         GSList *class_inits = NULL;
8143         gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
8144         int context_used;
8145         gboolean init_locals, seq_points, skip_dead_blocks;
8146         gboolean sym_seq_points = FALSE;
8147         MonoDebugMethodInfo *minfo;
8148         MonoBitSet *seq_point_locs = NULL;
8149         MonoBitSet *seq_point_set_locs = NULL;
8150
8151         cfg->disable_inline = is_jit_optimizer_disabled (method);
8152
8153         /* serialization and xdomain stuff may need access to private fields and methods */
8154         dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
8155         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
8156         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
8157         dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
8158         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
8159         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
8160
8161         /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
8162         dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
8163         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
8164         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
8165         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
8166
8167         image = method->klass->image;
8168         header = mono_method_get_header (method);
8169         if (!header) {
8170                 if (mono_loader_get_last_error ()) {
8171                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
8172                         mono_error_set_from_loader_error (&cfg->error);
8173                 } else {
8174                         mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name));
8175                 }
8176                 goto exception_exit;
8177         }
8178         generic_container = mono_method_get_generic_container (method);
8179         sig = mono_method_signature (method);
8180         num_args = sig->hasthis + sig->param_count;
8181         ip = (unsigned char*)header->code;
8182         cfg->cil_start = ip;
8183         end = ip + header->code_size;
8184         cfg->stat_cil_code_size += header->code_size;
8185
8186         seq_points = cfg->gen_seq_points && cfg->method == method;
8187
8188         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8189                 /* We could hit a seq point before attaching to the JIT (#8338) */
8190                 seq_points = FALSE;
8191         }
8192
8193         if (cfg->gen_sdb_seq_points && cfg->method == method) {
8194                 minfo = mono_debug_lookup_method (method);
8195                 if (minfo) {
8196                         MonoSymSeqPoint *sps;
8197                         int i, n_il_offsets;
8198
8199                         mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
8200                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8201                         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);
8202                         sym_seq_points = TRUE;
8203                         for (i = 0; i < n_il_offsets; ++i) {
8204                                 if (sps [i].il_offset < header->code_size)
8205                                         mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
8206                         }
8207                         g_free (sps);
8208                 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
8209                         /* Methods without line number info like auto-generated property accessors */
8210                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8211                         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);
8212                         sym_seq_points = TRUE;
8213                 }
8214         }
8215
8216         /* 
8217          * Methods without init_locals set could cause asserts in various passes
8218          * (#497220). To work around this, we emit dummy initialization opcodes
8219          * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
8220          * on some platforms.
8221          */
8222         if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
8223                 init_locals = header->init_locals;
8224         else
8225                 init_locals = TRUE;
8226
8227         method_definition = method;
8228         while (method_definition->is_inflated) {
8229                 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
8230                 method_definition = imethod->declaring;
8231         }
8232
8233         /* SkipVerification is not allowed if core-clr is enabled */
8234         if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
8235                 dont_verify = TRUE;
8236                 dont_verify_stloc = TRUE;
8237         }
8238
8239         if (sig->is_inflated)
8240                 generic_context = mono_method_get_context (method);
8241         else if (generic_container)
8242                 generic_context = &generic_container->context;
8243         cfg->generic_context = generic_context;
8244
8245         if (!cfg->gshared)
8246                 g_assert (!sig->has_type_parameters);
8247
8248         if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
8249                 g_assert (method->is_inflated);
8250                 g_assert (mono_method_get_context (method)->method_inst);
8251         }
8252         if (method->is_inflated && mono_method_get_context (method)->method_inst)
8253                 g_assert (sig->generic_param_count);
8254
8255         if (cfg->method == method) {
8256                 cfg->real_offset = 0;
8257         } else {
8258                 cfg->real_offset = inline_offset;
8259         }
8260
8261         cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
8262         cfg->cil_offset_to_bb_len = header->code_size;
8263
8264         cfg->current_method = method;
8265
8266         if (cfg->verbose_level > 2)
8267                 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
8268
8269         param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
8270         if (sig->hasthis)
8271                 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
8272         for (n = 0; n < sig->param_count; ++n)
8273                 param_types [n + sig->hasthis] = sig->params [n];
8274         cfg->arg_types = param_types;
8275
8276         cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
8277         if (cfg->method == method) {
8278
8279                 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
8280                         cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
8281
8282                 /* ENTRY BLOCK */
8283                 NEW_BBLOCK (cfg, start_bblock);
8284                 cfg->bb_entry = start_bblock;
8285                 start_bblock->cil_code = NULL;
8286                 start_bblock->cil_length = 0;
8287
8288                 /* EXIT BLOCK */
8289                 NEW_BBLOCK (cfg, end_bblock);
8290                 cfg->bb_exit = end_bblock;
8291                 end_bblock->cil_code = NULL;
8292                 end_bblock->cil_length = 0;
8293                 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
8294                 g_assert (cfg->num_bblocks == 2);
8295
8296                 arg_array = cfg->args;
8297
8298                 if (header->num_clauses) {
8299                         cfg->spvars = g_hash_table_new (NULL, NULL);
8300                         cfg->exvars = g_hash_table_new (NULL, NULL);
8301                 }
8302                 /* handle exception clauses */
8303                 for (i = 0; i < header->num_clauses; ++i) {
8304                         MonoBasicBlock *try_bb;
8305                         MonoExceptionClause *clause = &header->clauses [i];
8306                         GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
8307
8308                         try_bb->real_offset = clause->try_offset;
8309                         try_bb->try_start = TRUE;
8310                         try_bb->region = ((i + 1) << 8) | clause->flags;
8311                         GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
8312                         tblock->real_offset = clause->handler_offset;
8313                         tblock->flags |= BB_EXCEPTION_HANDLER;
8314
8315                         /*
8316                          * Linking the try block with the EH block hinders inlining as we won't be able to 
8317                          * merge the bblocks from inlining and produce an artificial hole for no good reason.
8318                          */
8319                         if (COMPILE_LLVM (cfg))
8320                                 link_bblock (cfg, try_bb, tblock);
8321
8322                         if (*(ip + clause->handler_offset) == CEE_POP)
8323                                 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
8324
8325                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
8326                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
8327                             clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
8328                                 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8329                                 MONO_ADD_INS (tblock, ins);
8330
8331                                 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
8332                                         /* finally clauses already have a seq point */
8333                                         /* seq points for filter clauses are emitted below */
8334                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8335                                         MONO_ADD_INS (tblock, ins);
8336                                 }
8337
8338                                 /* todo: is a fault block unsafe to optimize? */
8339                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
8340                                         tblock->flags |= BB_EXCEPTION_UNSAFE;
8341                         }
8342
8343                         /*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);
8344                           while (p < end) {
8345                           printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
8346                           }*/
8347                         /* catch and filter blocks get the exception object on the stack */
8348                         if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
8349                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8350
8351                                 /* mostly like handle_stack_args (), but just sets the input args */
8352                                 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
8353                                 tblock->in_scount = 1;
8354                                 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8355                                 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8356
8357                                 cfg->cbb = tblock;
8358
8359 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
8360                                 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
8361                                 if (!cfg->compile_llvm) {
8362                                         MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
8363                                         ins->dreg = tblock->in_stack [0]->dreg;
8364                                         MONO_ADD_INS (tblock, ins);
8365                                 }
8366 #else
8367                                 MonoInst *dummy_use;
8368
8369                                 /* 
8370                                  * Add a dummy use for the exvar so its liveness info will be
8371                                  * correct.
8372                                  */
8373                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
8374 #endif
8375
8376                                 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8377                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8378                                         MONO_ADD_INS (tblock, ins);
8379                                 }
8380                                 
8381                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8382                                         GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
8383                                         tblock->flags |= BB_EXCEPTION_HANDLER;
8384                                         tblock->real_offset = clause->data.filter_offset;
8385                                         tblock->in_scount = 1;
8386                                         tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8387                                         /* The filter block shares the exvar with the handler block */
8388                                         tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8389                                         MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8390                                         MONO_ADD_INS (tblock, ins);
8391                                 }
8392                         }
8393
8394                         if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
8395                                         clause->data.catch_class &&
8396                                         cfg->gshared &&
8397                                         mono_class_check_context_used (clause->data.catch_class)) {
8398                                 /*
8399                                  * In shared generic code with catch
8400                                  * clauses containing type variables
8401                                  * the exception handling code has to
8402                                  * be able to get to the rgctx.
8403                                  * Therefore we have to make sure that
8404                                  * the vtable/mrgctx argument (for
8405                                  * static or generic methods) or the
8406                                  * "this" argument (for non-static
8407                                  * methods) are live.
8408                                  */
8409                                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8410                                                 mini_method_get_context (method)->method_inst ||
8411                                                 method->klass->valuetype) {
8412                                         mono_get_vtable_var (cfg);
8413                                 } else {
8414                                         MonoInst *dummy_use;
8415
8416                                         EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
8417                                 }
8418                         }
8419                 }
8420         } else {
8421                 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
8422                 cfg->cbb = start_bblock;
8423                 cfg->args = arg_array;
8424                 mono_save_args (cfg, sig, inline_args);
8425         }
8426
8427         /* FIRST CODE BLOCK */
8428         NEW_BBLOCK (cfg, tblock);
8429         tblock->cil_code = ip;
8430         cfg->cbb = tblock;
8431         cfg->ip = ip;
8432
8433         ADD_BBLOCK (cfg, tblock);
8434
8435         if (cfg->method == method) {
8436                 breakpoint_id = mono_debugger_method_has_breakpoint (method);
8437                 if (breakpoint_id) {
8438                         MONO_INST_NEW (cfg, ins, OP_BREAK);
8439                         MONO_ADD_INS (cfg->cbb, ins);
8440                 }
8441         }
8442
8443         /* we use a separate basic block for the initialization code */
8444         NEW_BBLOCK (cfg, init_localsbb);
8445         cfg->bb_init = init_localsbb;
8446         init_localsbb->real_offset = cfg->real_offset;
8447         start_bblock->next_bb = init_localsbb;
8448         init_localsbb->next_bb = cfg->cbb;
8449         link_bblock (cfg, start_bblock, init_localsbb);
8450         link_bblock (cfg, init_localsbb, cfg->cbb);
8451                 
8452         cfg->cbb = init_localsbb;
8453
8454         if (cfg->gsharedvt && cfg->method == method) {
8455                 MonoGSharedVtMethodInfo *info;
8456                 MonoInst *var, *locals_var;
8457                 int dreg;
8458
8459                 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
8460                 info->method = cfg->method;
8461                 info->count_entries = 16;
8462                 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
8463                 cfg->gsharedvt_info = info;
8464
8465                 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8466                 /* prevent it from being register allocated */
8467                 //var->flags |= MONO_INST_VOLATILE;
8468                 cfg->gsharedvt_info_var = var;
8469
8470                 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
8471                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
8472
8473                 /* Allocate locals */
8474                 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8475                 /* prevent it from being register allocated */
8476                 //locals_var->flags |= MONO_INST_VOLATILE;
8477                 cfg->gsharedvt_locals_var = locals_var;
8478
8479                 dreg = alloc_ireg (cfg);
8480                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
8481
8482                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
8483                 ins->dreg = locals_var->dreg;
8484                 ins->sreg1 = dreg;
8485                 MONO_ADD_INS (cfg->cbb, ins);
8486                 cfg->gsharedvt_locals_var_ins = ins;
8487                 
8488                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
8489                 /*
8490                 if (init_locals)
8491                         ins->flags |= MONO_INST_INIT;
8492                 */
8493         }
8494
8495         if (mono_security_core_clr_enabled ()) {
8496                 /* check if this is native code, e.g. an icall or a p/invoke */
8497                 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
8498                         MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8499                         if (wrapped) {
8500                                 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
8501                                 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
8502
8503                                 /* if this ia a native call then it can only be JITted from platform code */
8504                                 if ((icall || pinvk) && method->klass && method->klass->image) {
8505                                         if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
8506                                                 MonoException *ex = icall ? mono_get_exception_security () : 
8507                                                         mono_get_exception_method_access ();
8508                                                 emit_throw_exception (cfg, ex);
8509                                         }
8510                                 }
8511                         }
8512                 }
8513         }
8514
8515         CHECK_CFG_EXCEPTION;
8516
8517         if (header->code_size == 0)
8518                 UNVERIFIED;
8519
8520         if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
8521                 ip = err_pos;
8522                 UNVERIFIED;
8523         }
8524
8525         if (cfg->method == method)
8526                 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
8527
8528         for (n = 0; n < header->num_locals; ++n) {
8529                 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
8530                         UNVERIFIED;
8531         }
8532         class_inits = NULL;
8533
8534         /* We force the vtable variable here for all shared methods
8535            for the possibility that they might show up in a stack
8536            trace where their exact instantiation is needed. */
8537         if (cfg->gshared && method == cfg->method) {
8538                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8539                                 mini_method_get_context (method)->method_inst ||
8540                                 method->klass->valuetype) {
8541                         mono_get_vtable_var (cfg);
8542                 } else {
8543                         /* FIXME: Is there a better way to do this?
8544                            We need the variable live for the duration
8545                            of the whole method. */
8546                         cfg->args [0]->flags |= MONO_INST_VOLATILE;
8547                 }
8548         }
8549
8550         /* add a check for this != NULL to inlined methods */
8551         if (is_virtual_call) {
8552                 MonoInst *arg_ins;
8553
8554                 NEW_ARGLOAD (cfg, arg_ins, 0);
8555                 MONO_ADD_INS (cfg->cbb, arg_ins);
8556                 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
8557         }
8558
8559         skip_dead_blocks = !dont_verify;
8560         if (skip_dead_blocks) {
8561                 original_bb = bb = mono_basic_block_split (method, &cfg->error);
8562                 CHECK_CFG_ERROR;
8563                 g_assert (bb);
8564         }
8565
8566         /* we use a spare stack slot in SWITCH and NEWOBJ and others */
8567         stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
8568
8569         ins_flag = 0;
8570         start_new_bblock = 0;
8571         while (ip < end) {
8572                 if (cfg->method == method)
8573                         cfg->real_offset = ip - header->code;
8574                 else
8575                         cfg->real_offset = inline_offset;
8576                 cfg->ip = ip;
8577
8578                 context_used = 0;
8579
8580                 if (start_new_bblock) {
8581                         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
8582                         if (start_new_bblock == 2) {
8583                                 g_assert (ip == tblock->cil_code);
8584                         } else {
8585                                 GET_BBLOCK (cfg, tblock, ip);
8586                         }
8587                         cfg->cbb->next_bb = tblock;
8588                         cfg->cbb = tblock;
8589                         start_new_bblock = 0;
8590                         for (i = 0; i < cfg->cbb->in_scount; ++i) {
8591                                 if (cfg->verbose_level > 3)
8592                                         printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8593                                 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8594                                 *sp++ = ins;
8595                         }
8596                         if (class_inits)
8597                                 g_slist_free (class_inits);
8598                         class_inits = NULL;
8599                 } else {
8600                         if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
8601                                 link_bblock (cfg, cfg->cbb, tblock);
8602                                 if (sp != stack_start) {
8603                                         handle_stack_args (cfg, stack_start, sp - stack_start);
8604                                         sp = stack_start;
8605                                         CHECK_UNVERIFIABLE (cfg);
8606                                 }
8607                                 cfg->cbb->next_bb = tblock;
8608                                 cfg->cbb = tblock;
8609                                 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8610                                         if (cfg->verbose_level > 3)
8611                                                 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8612                                         EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8613                                         *sp++ = ins;
8614                                 }
8615                                 g_slist_free (class_inits);
8616                                 class_inits = NULL;
8617                         }
8618                 }
8619
8620                 if (skip_dead_blocks) {
8621                         int ip_offset = ip - header->code;
8622
8623                         if (ip_offset == bb->end)
8624                                 bb = bb->next;
8625
8626                         if (bb->dead) {
8627                                 int op_size = mono_opcode_size (ip, end);
8628                                 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8629
8630                                 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8631
8632                                 if (ip_offset + op_size == bb->end) {
8633                                         MONO_INST_NEW (cfg, ins, OP_NOP);
8634                                         MONO_ADD_INS (cfg->cbb, ins);
8635                                         start_new_bblock = 1;
8636                                 }
8637
8638                                 ip += op_size;
8639                                 continue;
8640                         }
8641                 }
8642                 /*
8643                  * Sequence points are points where the debugger can place a breakpoint.
8644                  * Currently, we generate these automatically at points where the IL
8645                  * stack is empty.
8646                  */
8647                 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8648                         /*
8649                          * Make methods interruptable at the beginning, and at the targets of
8650                          * backward branches.
8651                          * Also, do this at the start of every bblock in methods with clauses too,
8652                          * to be able to handle instructions with inprecise control flow like
8653                          * throw/endfinally.
8654                          * Backward branches are handled at the end of method-to-ir ().
8655                          */
8656                         gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8657                         gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8658
8659                         /* Avoid sequence points on empty IL like .volatile */
8660                         // FIXME: Enable this
8661                         //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8662                         NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8663                         if ((sp != stack_start) && !sym_seq_point)
8664                                 ins->flags |= MONO_INST_NONEMPTY_STACK;
8665                         MONO_ADD_INS (cfg->cbb, ins);
8666
8667                         if (sym_seq_points)
8668                                 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8669                 }
8670
8671                 cfg->cbb->real_offset = cfg->real_offset;
8672
8673                 if ((cfg->method == method) && cfg->coverage_info) {
8674                         guint32 cil_offset = ip - header->code;
8675                         cfg->coverage_info->data [cil_offset].cil_code = ip;
8676
8677                         /* TODO: Use an increment here */
8678 #if defined(TARGET_X86)
8679                         MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8680                         ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8681                         ins->inst_imm = 1;
8682                         MONO_ADD_INS (cfg->cbb, ins);
8683 #else
8684                         EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8685                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8686 #endif
8687                 }
8688
8689                 if (cfg->verbose_level > 3)
8690                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8691
8692                 switch (*ip) {
8693                 case CEE_NOP:
8694                         if (seq_points && !sym_seq_points && sp != stack_start) {
8695                                 /*
8696                                  * The C# compiler uses these nops to notify the JIT that it should
8697                                  * insert seq points.
8698                                  */
8699                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8700                                 MONO_ADD_INS (cfg->cbb, ins);
8701                         }
8702                         if (cfg->keep_cil_nops)
8703                                 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8704                         else
8705                                 MONO_INST_NEW (cfg, ins, OP_NOP);
8706                         ip++;
8707                         MONO_ADD_INS (cfg->cbb, ins);
8708                         break;
8709                 case CEE_BREAK:
8710                         if (should_insert_brekpoint (cfg->method)) {
8711                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8712                         } else {
8713                                 MONO_INST_NEW (cfg, ins, OP_NOP);
8714                         }
8715                         ip++;
8716                         MONO_ADD_INS (cfg->cbb, ins);
8717                         break;
8718                 case CEE_LDARG_0:
8719                 case CEE_LDARG_1:
8720                 case CEE_LDARG_2:
8721                 case CEE_LDARG_3:
8722                         CHECK_STACK_OVF (1);
8723                         n = (*ip)-CEE_LDARG_0;
8724                         CHECK_ARG (n);
8725                         EMIT_NEW_ARGLOAD (cfg, ins, n);
8726                         ip++;
8727                         *sp++ = ins;
8728                         break;
8729                 case CEE_LDLOC_0:
8730                 case CEE_LDLOC_1:
8731                 case CEE_LDLOC_2:
8732                 case CEE_LDLOC_3:
8733                         CHECK_STACK_OVF (1);
8734                         n = (*ip)-CEE_LDLOC_0;
8735                         CHECK_LOCAL (n);
8736                         EMIT_NEW_LOCLOAD (cfg, ins, n);
8737                         ip++;
8738                         *sp++ = ins;
8739                         break;
8740                 case CEE_STLOC_0:
8741                 case CEE_STLOC_1:
8742                 case CEE_STLOC_2:
8743                 case CEE_STLOC_3: {
8744                         CHECK_STACK (1);
8745                         n = (*ip)-CEE_STLOC_0;
8746                         CHECK_LOCAL (n);
8747                         --sp;
8748                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8749                                 UNVERIFIED;
8750                         emit_stloc_ir (cfg, sp, header, n);
8751                         ++ip;
8752                         inline_costs += 1;
8753                         break;
8754                         }
8755                 case CEE_LDARG_S:
8756                         CHECK_OPSIZE (2);
8757                         CHECK_STACK_OVF (1);
8758                         n = ip [1];
8759                         CHECK_ARG (n);
8760                         EMIT_NEW_ARGLOAD (cfg, ins, n);
8761                         *sp++ = ins;
8762                         ip += 2;
8763                         break;
8764                 case CEE_LDARGA_S:
8765                         CHECK_OPSIZE (2);
8766                         CHECK_STACK_OVF (1);
8767                         n = ip [1];
8768                         CHECK_ARG (n);
8769                         NEW_ARGLOADA (cfg, ins, n);
8770                         MONO_ADD_INS (cfg->cbb, ins);
8771                         *sp++ = ins;
8772                         ip += 2;
8773                         break;
8774                 case CEE_STARG_S:
8775                         CHECK_OPSIZE (2);
8776                         CHECK_STACK (1);
8777                         --sp;
8778                         n = ip [1];
8779                         CHECK_ARG (n);
8780                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8781                                 UNVERIFIED;
8782                         EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8783                         ip += 2;
8784                         break;
8785                 case CEE_LDLOC_S:
8786                         CHECK_OPSIZE (2);
8787                         CHECK_STACK_OVF (1);
8788                         n = ip [1];
8789                         CHECK_LOCAL (n);
8790                         EMIT_NEW_LOCLOAD (cfg, ins, n);
8791                         *sp++ = ins;
8792                         ip += 2;
8793                         break;
8794                 case CEE_LDLOCA_S: {
8795                         unsigned char *tmp_ip;
8796                         CHECK_OPSIZE (2);
8797                         CHECK_STACK_OVF (1);
8798                         CHECK_LOCAL (ip [1]);
8799
8800                         if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8801                                 ip = tmp_ip;
8802                                 inline_costs += 1;
8803                                 break;
8804                         }
8805
8806                         EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8807                         *sp++ = ins;
8808                         ip += 2;
8809                         break;
8810                 }
8811                 case CEE_STLOC_S:
8812                         CHECK_OPSIZE (2);
8813                         CHECK_STACK (1);
8814                         --sp;
8815                         CHECK_LOCAL (ip [1]);
8816                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8817                                 UNVERIFIED;
8818                         emit_stloc_ir (cfg, sp, header, ip [1]);
8819                         ip += 2;
8820                         inline_costs += 1;
8821                         break;
8822                 case CEE_LDNULL:
8823                         CHECK_STACK_OVF (1);
8824                         EMIT_NEW_PCONST (cfg, ins, NULL);
8825                         ins->type = STACK_OBJ;
8826                         ++ip;
8827                         *sp++ = ins;
8828                         break;
8829                 case CEE_LDC_I4_M1:
8830                         CHECK_STACK_OVF (1);
8831                         EMIT_NEW_ICONST (cfg, ins, -1);
8832                         ++ip;
8833                         *sp++ = ins;
8834                         break;
8835                 case CEE_LDC_I4_0:
8836                 case CEE_LDC_I4_1:
8837                 case CEE_LDC_I4_2:
8838                 case CEE_LDC_I4_3:
8839                 case CEE_LDC_I4_4:
8840                 case CEE_LDC_I4_5:
8841                 case CEE_LDC_I4_6:
8842                 case CEE_LDC_I4_7:
8843                 case CEE_LDC_I4_8:
8844                         CHECK_STACK_OVF (1);
8845                         EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8846                         ++ip;
8847                         *sp++ = ins;
8848                         break;
8849                 case CEE_LDC_I4_S:
8850                         CHECK_OPSIZE (2);
8851                         CHECK_STACK_OVF (1);
8852                         ++ip;
8853                         EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8854                         ++ip;
8855                         *sp++ = ins;
8856                         break;
8857                 case CEE_LDC_I4:
8858                         CHECK_OPSIZE (5);
8859                         CHECK_STACK_OVF (1);
8860                         EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8861                         ip += 5;
8862                         *sp++ = ins;
8863                         break;
8864                 case CEE_LDC_I8:
8865                         CHECK_OPSIZE (9);
8866                         CHECK_STACK_OVF (1);
8867                         MONO_INST_NEW (cfg, ins, OP_I8CONST);
8868                         ins->type = STACK_I8;
8869                         ins->dreg = alloc_dreg (cfg, STACK_I8);
8870                         ++ip;
8871                         ins->inst_l = (gint64)read64 (ip);
8872                         MONO_ADD_INS (cfg->cbb, ins);
8873                         ip += 8;
8874                         *sp++ = ins;
8875                         break;
8876                 case CEE_LDC_R4: {
8877                         float *f;
8878                         gboolean use_aotconst = FALSE;
8879
8880 #ifdef TARGET_POWERPC
8881                         /* FIXME: Clean this up */
8882                         if (cfg->compile_aot)
8883                                 use_aotconst = TRUE;
8884 #endif
8885
8886                         /* FIXME: we should really allocate this only late in the compilation process */
8887                         f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8888                         CHECK_OPSIZE (5);
8889                         CHECK_STACK_OVF (1);
8890
8891                         if (use_aotconst) {
8892                                 MonoInst *cons;
8893                                 int dreg;
8894
8895                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8896
8897                                 dreg = alloc_freg (cfg);
8898                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8899                                 ins->type = cfg->r4_stack_type;
8900                         } else {
8901                                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8902                                 ins->type = cfg->r4_stack_type;
8903                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8904                                 ins->inst_p0 = f;
8905                                 MONO_ADD_INS (cfg->cbb, ins);
8906                         }
8907                         ++ip;
8908                         readr4 (ip, f);
8909                         ip += 4;
8910                         *sp++ = ins;                    
8911                         break;
8912                 }
8913                 case CEE_LDC_R8: {
8914                         double *d;
8915                         gboolean use_aotconst = FALSE;
8916
8917 #ifdef TARGET_POWERPC
8918                         /* FIXME: Clean this up */
8919                         if (cfg->compile_aot)
8920                                 use_aotconst = TRUE;
8921 #endif
8922
8923                         /* FIXME: we should really allocate this only late in the compilation process */
8924                         d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8925                         CHECK_OPSIZE (9);
8926                         CHECK_STACK_OVF (1);
8927
8928                         if (use_aotconst) {
8929                                 MonoInst *cons;
8930                                 int dreg;
8931
8932                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8933
8934                                 dreg = alloc_freg (cfg);
8935                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8936                                 ins->type = STACK_R8;
8937                         } else {
8938                                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8939                                 ins->type = STACK_R8;
8940                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8941                                 ins->inst_p0 = d;
8942                                 MONO_ADD_INS (cfg->cbb, ins);
8943                         }
8944                         ++ip;
8945                         readr8 (ip, d);
8946                         ip += 8;
8947                         *sp++ = ins;
8948                         break;
8949                 }
8950                 case CEE_DUP: {
8951                         MonoInst *temp, *store;
8952                         CHECK_STACK (1);
8953                         CHECK_STACK_OVF (1);
8954                         sp--;
8955                         ins = *sp;
8956
8957                         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8958                         EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8959
8960                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8961                         *sp++ = ins;
8962
8963                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8964                         *sp++ = ins;
8965
8966                         ++ip;
8967                         inline_costs += 2;
8968                         break;
8969                 }
8970                 case CEE_POP:
8971                         CHECK_STACK (1);
8972                         ip++;
8973                         --sp;
8974
8975 #ifdef TARGET_X86
8976                         if (sp [0]->type == STACK_R8)
8977                                 /* we need to pop the value from the x86 FP stack */
8978                                 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8979 #endif
8980                         break;
8981                 case CEE_JMP: {
8982                         MonoCallInst *call;
8983                         MonoMethodSignature *fsig;
8984                         int i, n;
8985
8986                         INLINE_FAILURE ("jmp");
8987                         GSHAREDVT_FAILURE (*ip);
8988
8989                         CHECK_OPSIZE (5);
8990                         if (stack_start != sp)
8991                                 UNVERIFIED;
8992                         token = read32 (ip + 1);
8993                         /* FIXME: check the signature matches */
8994                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8995                         CHECK_CFG_ERROR;
8996  
8997                         if (cfg->gshared && mono_method_check_context_used (cmethod))
8998                                 GENERIC_SHARING_FAILURE (CEE_JMP);
8999
9000                         emit_instrumentation_call (cfg, mono_profiler_method_leave);
9001
9002                         fsig = mono_method_signature (cmethod);
9003                         n = fsig->param_count + fsig->hasthis;
9004                         if (cfg->llvm_only) {
9005                                 MonoInst **args;
9006
9007                                 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9008                                 for (i = 0; i < n; ++i)
9009                                         EMIT_NEW_ARGLOAD (cfg, args [i], i);
9010                                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
9011                                 /*
9012                                  * The code in mono-basic-block.c treats the rest of the code as dead, but we
9013                                  * have to emit a normal return since llvm expects it.
9014                                  */
9015                                 if (cfg->ret)
9016                                         emit_setret (cfg, ins);
9017                                 MONO_INST_NEW (cfg, ins, OP_BR);
9018                                 ins->inst_target_bb = end_bblock;
9019                                 MONO_ADD_INS (cfg->cbb, ins);
9020                                 link_bblock (cfg, cfg->cbb, end_bblock);
9021                                 ip += 5;
9022                                 break;
9023                         } else if (cfg->backend->have_op_tail_call) {
9024                                 /* Handle tail calls similarly to calls */
9025                                 DISABLE_AOT (cfg);
9026
9027                                 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
9028                                 call->method = cmethod;
9029                                 call->tail_call = TRUE;
9030                                 call->signature = mono_method_signature (cmethod);
9031                                 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9032                                 call->inst.inst_p0 = cmethod;
9033                                 for (i = 0; i < n; ++i)
9034                                         EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
9035
9036                                 mono_arch_emit_call (cfg, call);
9037                                 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
9038                                 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
9039                         } else {
9040                                 for (i = 0; i < num_args; ++i)
9041                                         /* Prevent arguments from being optimized away */
9042                                         arg_array [i]->flags |= MONO_INST_VOLATILE;
9043
9044                                 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9045                                 ins = (MonoInst*)call;
9046                                 ins->inst_p0 = cmethod;
9047                                 MONO_ADD_INS (cfg->cbb, ins);
9048                         }
9049
9050                         ip += 5;
9051                         start_new_bblock = 1;
9052                         break;
9053                 }
9054                 case CEE_CALLI: {
9055                         MonoInst *addr;
9056                         MonoMethodSignature *fsig;
9057
9058                         CHECK_OPSIZE (5);
9059                         token = read32 (ip + 1);
9060
9061                         ins = NULL;
9062
9063                         //GSHAREDVT_FAILURE (*ip);
9064                         cmethod = NULL;
9065                         CHECK_STACK (1);
9066                         --sp;
9067                         addr = *sp;
9068                         fsig = mini_get_signature (method, token, generic_context);
9069
9070                         if (method->dynamic && fsig->pinvoke) {
9071                                 MonoInst *args [3];
9072
9073                                 /*
9074                                  * This is a call through a function pointer using a pinvoke
9075                                  * signature. Have to create a wrapper and call that instead.
9076                                  * FIXME: This is very slow, need to create a wrapper at JIT time
9077                                  * instead based on the signature.
9078                                  */
9079                                 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
9080                                 EMIT_NEW_PCONST (cfg, args [1], fsig);
9081                                 args [2] = addr;
9082                                 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
9083                         }
9084
9085                         n = fsig->param_count + fsig->hasthis;
9086
9087                         CHECK_STACK (n);
9088
9089                         //g_assert (!virtual_ || fsig->hasthis);
9090
9091                         sp -= n;
9092
9093                         inline_costs += 10 * num_calls++;
9094
9095                         /*
9096                          * Making generic calls out of gsharedvt methods.
9097                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9098                          * patching gshared method addresses into a gsharedvt method.
9099                          */
9100                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
9101                                 /*
9102                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
9103                                  */
9104                                 MonoInst *callee = addr;
9105
9106                                 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
9107                                         /* Not tested */
9108                                         GSHAREDVT_FAILURE (*ip);
9109
9110                                 if (cfg->llvm_only)
9111                                         // FIXME:
9112                                         GSHAREDVT_FAILURE (*ip);
9113
9114                                 addr = emit_get_rgctx_sig (cfg, context_used,
9115                                                                                           fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
9116                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
9117                                 goto calli_end;
9118                         }
9119
9120                         /* Prevent inlining of methods with indirect calls */
9121                         INLINE_FAILURE ("indirect call");
9122
9123                         if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
9124                                 MonoJumpInfoType info_type;
9125                                 gpointer info_data;
9126
9127                                 /*
9128                                  * Instead of emitting an indirect call, emit a direct call
9129                                  * with the contents of the aotconst as the patch info.
9130                                  */
9131                                 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
9132                                         info_type = (MonoJumpInfoType)addr->inst_c1;
9133                                         info_data = addr->inst_p0;
9134                                 } else {
9135                                         info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
9136                                         info_data = addr->inst_right->inst_left;
9137                                 }
9138
9139                                 if (info_type == MONO_PATCH_INFO_ICALL_ADDR || info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
9140                                         ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
9141                                         NULLIFY_INS (addr);
9142                                         goto calli_end;
9143                                 }
9144                         }
9145                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9146
9147                         calli_end:
9148
9149                         /* End of call, INS should contain the result of the call, if any */
9150
9151                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9152                                 g_assert (ins);
9153                                 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9154                         }
9155
9156                         CHECK_CFG_EXCEPTION;
9157
9158                         ip += 5;
9159                         ins_flag = 0;
9160                         constrained_class = NULL;
9161                         break;
9162                 }
9163                 case CEE_CALL:
9164                 case CEE_CALLVIRT: {
9165                         MonoInst *addr = NULL;
9166                         MonoMethodSignature *fsig = NULL;
9167                         int array_rank = 0;
9168                         int virtual_ = *ip == CEE_CALLVIRT;
9169                         gboolean pass_imt_from_rgctx = FALSE;
9170                         MonoInst *imt_arg = NULL;
9171                         MonoInst *keep_this_alive = NULL;
9172                         gboolean pass_vtable = FALSE;
9173                         gboolean pass_mrgctx = FALSE;
9174                         MonoInst *vtable_arg = NULL;
9175                         gboolean check_this = FALSE;
9176                         gboolean supported_tail_call = FALSE;
9177                         gboolean tail_call = FALSE;
9178                         gboolean need_seq_point = FALSE;
9179                         guint32 call_opcode = *ip;
9180                         gboolean emit_widen = TRUE;
9181                         gboolean push_res = TRUE;
9182                         gboolean skip_ret = FALSE;
9183                         gboolean delegate_invoke = FALSE;
9184                         gboolean direct_icall = FALSE;
9185                         gboolean constrained_partial_call = FALSE;
9186                         MonoMethod *cil_method;
9187
9188                         CHECK_OPSIZE (5);
9189                         token = read32 (ip + 1);
9190
9191                         ins = NULL;
9192
9193                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9194                         CHECK_CFG_ERROR;
9195
9196                         cil_method = cmethod;
9197                                 
9198                         if (constrained_class) {
9199                                 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9200                                         if (!mini_is_gsharedvt_klass (constrained_class)) {
9201                                                 g_assert (!cmethod->klass->valuetype);
9202                                                 if (!mini_type_is_reference (&constrained_class->byval_arg))
9203                                                         constrained_partial_call = TRUE;
9204                                         }
9205                                 }
9206
9207                                 if (method->wrapper_type != MONO_WRAPPER_NONE) {
9208                                         if (cfg->verbose_level > 2)
9209                                                 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9210                                         if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
9211                                                    constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
9212                                                   cfg->gshared)) {
9213                                                 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
9214                                                 CHECK_CFG_ERROR;
9215                                         }
9216                                 } else {
9217                                         if (cfg->verbose_level > 2)
9218                                                 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9219
9220                                         if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9221                                                 /* 
9222                                                  * This is needed since get_method_constrained can't find 
9223                                                  * the method in klass representing a type var.
9224                                                  * The type var is guaranteed to be a reference type in this
9225                                                  * case.
9226                                                  */
9227                                                 if (!mini_is_gsharedvt_klass (constrained_class))
9228                                                         g_assert (!cmethod->klass->valuetype);
9229                                         } else {
9230                                                 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
9231                                                 CHECK_CFG_ERROR;
9232                                         }
9233                                 }
9234                         }
9235                                         
9236                         if (!cmethod || mono_loader_get_last_error ()) {
9237                                 if (mono_loader_get_last_error ()) {
9238                                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
9239                                         mono_error_set_from_loader_error (&cfg->error);
9240                                         CHECK_CFG_ERROR;
9241                                 } else {
9242                                         LOAD_ERROR;
9243                                 }
9244                         }
9245                         if (!dont_verify && !cfg->skip_visibility) {
9246                                 MonoMethod *target_method = cil_method;
9247                                 if (method->is_inflated) {
9248                                         target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
9249                                         CHECK_CFG_ERROR;
9250                                 }
9251                                 if (!mono_method_can_access_method (method_definition, target_method) &&
9252                                         !mono_method_can_access_method (method, cil_method))
9253                                         METHOD_ACCESS_FAILURE (method, cil_method);
9254                         }
9255
9256                         if (mono_security_core_clr_enabled ())
9257                                 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
9258
9259                         if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
9260                                 /* MS.NET seems to silently convert this to a callvirt */
9261                                 virtual_ = 1;
9262
9263                         {
9264                                 /*
9265                                  * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
9266                                  * converts to a callvirt.
9267                                  *
9268                                  * tests/bug-515884.il is an example of this behavior
9269                                  */
9270                                 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
9271                                 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
9272                                 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
9273                                         virtual_ = 1;
9274                         }
9275
9276                         if (!cmethod->klass->inited)
9277                                 if (!mono_class_init (cmethod->klass))
9278                                         TYPE_LOAD_ERROR (cmethod->klass);
9279
9280                         fsig = mono_method_signature (cmethod);
9281                         if (!fsig)
9282                                 LOAD_ERROR;
9283                         if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
9284                                 mini_class_is_system_array (cmethod->klass)) {
9285                                 array_rank = cmethod->klass->rank;
9286                         } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
9287                                 direct_icall = TRUE;
9288                         } else if (fsig->pinvoke) {
9289                                 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9290                                 fsig = mono_method_signature (wrapper);
9291                         } else if (constrained_class) {
9292                         } else {
9293                                 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
9294                                 CHECK_CFG_ERROR;
9295                         }
9296
9297                         if (cfg->llvm_only && !cfg->method->wrapper_type)
9298                                 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
9299
9300                         /* See code below */
9301                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9302                                 MonoBasicBlock *tbb;
9303
9304                                 GET_BBLOCK (cfg, tbb, ip + 5);
9305                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9306                                         /*
9307                                          * We want to extend the try block to cover the call, but we can't do it if the
9308                                          * call is made directly since its followed by an exception check.
9309                                          */
9310                                         direct_icall = FALSE;
9311                                 }
9312                         }
9313
9314                         mono_save_token_info (cfg, image, token, cil_method);
9315
9316                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
9317                                 need_seq_point = TRUE;
9318
9319                         /* Don't support calls made using type arguments for now */
9320                         /*
9321                           if (cfg->gsharedvt) {
9322                           if (mini_is_gsharedvt_signature (fsig))
9323                           GSHAREDVT_FAILURE (*ip);
9324                           }
9325                         */
9326
9327                         if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
9328                                 g_assert_not_reached ();
9329
9330                         n = fsig->param_count + fsig->hasthis;
9331
9332                         if (!cfg->gshared && cmethod->klass->generic_container)
9333                                 UNVERIFIED;
9334
9335                         if (!cfg->gshared)
9336                                 g_assert (!mono_method_check_context_used (cmethod));
9337
9338                         CHECK_STACK (n);
9339
9340                         //g_assert (!virtual_ || fsig->hasthis);
9341
9342                         sp -= n;
9343
9344                         /*
9345                          * We have the `constrained.' prefix opcode.
9346                          */
9347                         if (constrained_class) {
9348                                 if (mini_is_gsharedvt_klass (constrained_class)) {
9349                                         if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
9350                                                 /* The 'Own method' case below */
9351                                         } else if (cmethod->klass->image != mono_defaults.corlib && !(cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !cmethod->klass->valuetype) {
9352                                                 /* 'The type parameter is instantiated as a reference type' case below. */
9353                                         } else {
9354                                                 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
9355                                                 CHECK_CFG_EXCEPTION;
9356                                                 g_assert (ins);
9357                                                 goto call_end;
9358                                         }
9359                                 }
9360
9361                                 if (constrained_partial_call) {
9362                                         gboolean need_box = TRUE;
9363
9364                                         /*
9365                                          * The receiver is a valuetype, but the exact type is not known at compile time. This means the
9366                                          * called method is not known at compile time either. The called method could end up being
9367                                          * one of the methods on the parent classes (object/valuetype/enum), in which case we need
9368                                          * to box the receiver.
9369                                          * A simple solution would be to box always and make a normal virtual call, but that would
9370                                          * be bad performance wise.
9371                                          */
9372                                         if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE && cmethod->klass->generic_class) {
9373                                                 /*
9374                                                  * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
9375                                                  */
9376                                                 need_box = FALSE;
9377                                         }
9378
9379                                         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)) {
9380                                                 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
9381                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9382                                                 ins->klass = constrained_class;
9383                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9384                                                 CHECK_CFG_EXCEPTION;
9385                                         } else if (need_box) {
9386                                                 MonoInst *box_type;
9387                                                 MonoBasicBlock *is_ref_bb, *end_bb;
9388                                                 MonoInst *nonbox_call;
9389
9390                                                 /*
9391                                                  * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
9392                                                  * if needed.
9393                                                  * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
9394                                                  * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
9395                                                  */
9396                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9397
9398                                                 NEW_BBLOCK (cfg, is_ref_bb);
9399                                                 NEW_BBLOCK (cfg, end_bb);
9400
9401                                                 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);
9402                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
9403                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
9404
9405                                                 /* Non-ref case */
9406                                                 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9407
9408                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9409
9410                                                 /* Ref case */
9411                                                 MONO_START_BB (cfg, is_ref_bb);
9412                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9413                                                 ins->klass = constrained_class;
9414                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9415                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9416
9417                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9418
9419                                                 MONO_START_BB (cfg, end_bb);
9420                                                 cfg->cbb = end_bb;
9421
9422                                                 nonbox_call->dreg = ins->dreg;
9423                                                 goto call_end;
9424                                         } else {
9425                                                 g_assert (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
9426                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9427                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9428                                                 goto call_end;
9429                                         }
9430                                 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
9431                                         /*
9432                                          * The type parameter is instantiated as a valuetype,
9433                                          * but that type doesn't override the method we're
9434                                          * calling, so we need to box `this'.
9435                                          */
9436                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9437                                         ins->klass = constrained_class;
9438                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9439                                         CHECK_CFG_EXCEPTION;
9440                                 } else if (!constrained_class->valuetype) {
9441                                         int dreg = alloc_ireg_ref (cfg);
9442
9443                                         /*
9444                                          * The type parameter is instantiated as a reference
9445                                          * type.  We have a managed pointer on the stack, so
9446                                          * we need to dereference it here.
9447                                          */
9448                                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
9449                                         ins->type = STACK_OBJ;
9450                                         sp [0] = ins;
9451                                 } else {
9452                                         if (cmethod->klass->valuetype) {
9453                                                 /* Own method */
9454                                         } else {
9455                                                 /* Interface method */
9456                                                 int ioffset, slot;
9457
9458                                                 mono_class_setup_vtable (constrained_class);
9459                                                 CHECK_TYPELOAD (constrained_class);
9460                                                 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
9461                                                 if (ioffset == -1)
9462                                                         TYPE_LOAD_ERROR (constrained_class);
9463                                                 slot = mono_method_get_vtable_slot (cmethod);
9464                                                 if (slot == -1)
9465                                                         TYPE_LOAD_ERROR (cmethod->klass);
9466                                                 cmethod = constrained_class->vtable [ioffset + slot];
9467
9468                                                 if (cmethod->klass == mono_defaults.enum_class) {
9469                                                         /* Enum implements some interfaces, so treat this as the first case */
9470                                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9471                                                         ins->klass = constrained_class;
9472                                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9473                                                         CHECK_CFG_EXCEPTION;
9474                                                 }
9475                                         }
9476                                         virtual_ = 0;
9477                                 }
9478                                 constrained_class = NULL;
9479                         }
9480
9481                         if (check_call_signature (cfg, fsig, sp))
9482                                 UNVERIFIED;
9483
9484                         if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
9485                                 delegate_invoke = TRUE;
9486
9487                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
9488                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9489                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
9490                                         emit_widen = FALSE;
9491                                 }
9492
9493                                 goto call_end;
9494                         }
9495
9496                         /* 
9497                          * If the callee is a shared method, then its static cctor
9498                          * might not get called after the call was patched.
9499                          */
9500                         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)) {
9501                                 emit_class_init (cfg, cmethod->klass);
9502                                 CHECK_TYPELOAD (cmethod->klass);
9503                         }
9504
9505                         check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
9506
9507                         if (cfg->gshared) {
9508                                 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
9509
9510                                 context_used = mini_method_check_context_used (cfg, cmethod);
9511
9512                                 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
9513                                         /* Generic method interface
9514                                            calls are resolved via a
9515                                            helper function and don't
9516                                            need an imt. */
9517                                         if (!cmethod_context || !cmethod_context->method_inst)
9518                                                 pass_imt_from_rgctx = TRUE;
9519                                 }
9520
9521                                 /*
9522                                  * If a shared method calls another
9523                                  * shared method then the caller must
9524                                  * have a generic sharing context
9525                                  * because the magic trampoline
9526                                  * requires it.  FIXME: We shouldn't
9527                                  * have to force the vtable/mrgctx
9528                                  * variable here.  Instead there
9529                                  * should be a flag in the cfg to
9530                                  * request a generic sharing context.
9531                                  */
9532                                 if (context_used &&
9533                                                 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
9534                                         mono_get_vtable_var (cfg);
9535                         }
9536
9537                         if (pass_vtable) {
9538                                 if (context_used) {
9539                                         vtable_arg = emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
9540                                 } else {
9541                                         MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9542
9543                                         CHECK_TYPELOAD (cmethod->klass);
9544                                         EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
9545                                 }
9546                         }
9547
9548                         if (pass_mrgctx) {
9549                                 g_assert (!vtable_arg);
9550
9551                                 if (!cfg->compile_aot) {
9552                                         /* 
9553                                          * emit_get_rgctx_method () calls mono_class_vtable () so check 
9554                                          * for type load errors before.
9555                                          */
9556                                         mono_class_setup_vtable (cmethod->klass);
9557                                         CHECK_TYPELOAD (cmethod->klass);
9558                                 }
9559
9560                                 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
9561
9562                                 /* !marshalbyref is needed to properly handle generic methods + remoting */
9563                                 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
9564                                          MONO_METHOD_IS_FINAL (cmethod)) &&
9565                                         !mono_class_is_marshalbyref (cmethod->klass)) {
9566                                         if (virtual_)
9567                                                 check_this = TRUE;
9568                                         virtual_ = 0;
9569                                 }
9570                         }
9571
9572                         if (pass_imt_from_rgctx) {
9573                                 g_assert (!pass_vtable);
9574
9575                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9576                                         cmethod, MONO_RGCTX_INFO_METHOD);
9577                         }
9578
9579                         if (check_this)
9580                                 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9581
9582                         /* Calling virtual generic methods */
9583                         if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
9584                             !(MONO_METHOD_IS_FINAL (cmethod) && 
9585                               cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
9586                             fsig->generic_param_count && 
9587                                 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
9588                                 !cfg->llvm_only) {
9589                                 MonoInst *this_temp, *this_arg_temp, *store;
9590                                 MonoInst *iargs [4];
9591
9592                                 g_assert (fsig->is_inflated);
9593
9594                                 /* Prevent inlining of methods that contain indirect calls */
9595                                 INLINE_FAILURE ("virtual generic call");
9596
9597                                 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
9598                                         GSHAREDVT_FAILURE (*ip);
9599
9600                                 if (cfg->backend->have_generalized_imt_thunk && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
9601                                         g_assert (!imt_arg);
9602                                         if (!context_used)
9603                                                 g_assert (cmethod->is_inflated);
9604                                         imt_arg = emit_get_rgctx_method (cfg, context_used,
9605                                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
9606                                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
9607                                 } else {
9608                                         this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
9609                                         NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
9610                                         MONO_ADD_INS (cfg->cbb, store);
9611
9612                                         /* FIXME: This should be a managed pointer */
9613                                         this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9614
9615                                         EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9616                                         iargs [1] = emit_get_rgctx_method (cfg, context_used,
9617                                                                                                            cmethod, MONO_RGCTX_INFO_METHOD);
9618                                         EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9619                                         addr = mono_emit_jit_icall (cfg,
9620                                                                                                 mono_helper_compile_generic_method, iargs);
9621
9622                                         EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9623
9624                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9625                                 }
9626
9627                                 goto call_end;
9628                         }
9629
9630                         /*
9631                          * Implement a workaround for the inherent races involved in locking:
9632                          * Monitor.Enter ()
9633                          * try {
9634                          * } finally {
9635                          *    Monitor.Exit ()
9636                          * }
9637                          * If a thread abort happens between the call to Monitor.Enter () and the start of the
9638                          * try block, the Exit () won't be executed, see:
9639                          * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9640                          * To work around this, we extend such try blocks to include the last x bytes
9641                          * of the Monitor.Enter () call.
9642                          */
9643                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9644                                 MonoBasicBlock *tbb;
9645
9646                                 GET_BBLOCK (cfg, tbb, ip + 5);
9647                                 /* 
9648                                  * Only extend try blocks with a finally, to avoid catching exceptions thrown
9649                                  * from Monitor.Enter like ArgumentNullException.
9650                                  */
9651                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9652                                         /* Mark this bblock as needing to be extended */
9653                                         tbb->extend_try_block = TRUE;
9654                                 }
9655                         }
9656
9657                         /* Conversion to a JIT intrinsic */
9658                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9659                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9660                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
9661                                         emit_widen = FALSE;
9662                                 }
9663                                 goto call_end;
9664                         }
9665
9666                         /* Inlining */
9667                         if ((cfg->opt & MONO_OPT_INLINE) &&
9668                                 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9669                             mono_method_check_inlining (cfg, cmethod)) {
9670                                 int costs;
9671                                 gboolean always = FALSE;
9672
9673                                 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9674                                         (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9675                                         /* Prevent inlining of methods that call wrappers */
9676                                         INLINE_FAILURE ("wrapper call");
9677                                         cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9678                                         always = TRUE;
9679                                 }
9680
9681                                 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9682                                 if (costs) {
9683                                         cfg->real_offset += 5;
9684
9685                                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9686                                                 /* *sp is already set by inline_method */
9687                                                 sp++;
9688                                                 push_res = FALSE;
9689                                         }
9690
9691                                         inline_costs += costs;
9692
9693                                         goto call_end;
9694                                 }
9695                         }
9696
9697                         /* Tail recursion elimination */
9698                         if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9699                                 gboolean has_vtargs = FALSE;
9700                                 int i;
9701
9702                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9703                                 INLINE_FAILURE ("tail call");
9704
9705                                 /* keep it simple */
9706                                 for (i =  fsig->param_count - 1; i >= 0; i--) {
9707                                         if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i])) 
9708                                                 has_vtargs = TRUE;
9709                                 }
9710
9711                                 if (!has_vtargs) {
9712                                         for (i = 0; i < n; ++i)
9713                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9714                                         MONO_INST_NEW (cfg, ins, OP_BR);
9715                                         MONO_ADD_INS (cfg->cbb, ins);
9716                                         tblock = start_bblock->out_bb [0];
9717                                         link_bblock (cfg, cfg->cbb, tblock);
9718                                         ins->inst_target_bb = tblock;
9719                                         start_new_bblock = 1;
9720
9721                                         /* skip the CEE_RET, too */
9722                                         if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9723                                                 skip_ret = TRUE;
9724                                         push_res = FALSE;
9725                                         goto call_end;
9726                                 }
9727                         }
9728
9729                         inline_costs += 10 * num_calls++;
9730
9731                         /*
9732                          * Making generic calls out of gsharedvt methods.
9733                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9734                          * patching gshared method addresses into a gsharedvt method.
9735                          */
9736                         if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || cmethod->klass->generic_class) &&
9737                                 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9738                                 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9739                                 MonoRgctxInfoType info_type;
9740
9741                                 if (virtual_) {
9742                                         //if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
9743                                                 //GSHAREDVT_FAILURE (*ip);
9744                                         // disable for possible remoting calls
9745                                         if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9746                                                 GSHAREDVT_FAILURE (*ip);
9747                                         if (fsig->generic_param_count) {
9748                                                 /* virtual generic call */
9749                                                 g_assert (!imt_arg);
9750                                                 /* Same as the virtual generic case above */
9751                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9752                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
9753                                                 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9754                                                 vtable_arg = NULL;
9755                                         } else if ((cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !imt_arg) {
9756                                                 /* This can happen when we call a fully instantiated iface method */
9757                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9758                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
9759                                                 vtable_arg = NULL;
9760                                         }
9761                                 }
9762
9763                                 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9764                                         keep_this_alive = sp [0];
9765
9766                                 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9767                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9768                                 else
9769                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9770                                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9771
9772                                 if (cfg->llvm_only) {
9773                                         // FIXME: Avoid initializing vtable_arg
9774                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9775                                 } else {
9776                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9777                                 }
9778                                 goto call_end;
9779                         }
9780
9781                         /* Generic sharing */
9782
9783                         /*
9784                          * Use this if the callee is gsharedvt sharable too, since
9785                          * at runtime we might find an instantiation so the call cannot
9786                          * be patched (the 'no_patch' code path in mini-trampolines.c).
9787                          */
9788                         if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9789                                 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9790                                  !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9791                                 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9792                                  !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9793                                 INLINE_FAILURE ("gshared");
9794
9795                                 g_assert (cfg->gshared && cmethod);
9796                                 g_assert (!addr);
9797
9798                                 /*
9799                                  * We are compiling a call to a
9800                                  * generic method from shared code,
9801                                  * which means that we have to look up
9802                                  * the method in the rgctx and do an
9803                                  * indirect call.
9804                                  */
9805                                 if (fsig->hasthis)
9806                                         MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9807
9808                                 if (cfg->llvm_only) {
9809                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9810                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9811                                         else
9812                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9813                                         // FIXME: Avoid initializing imt_arg/vtable_arg
9814                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9815                                 } else {
9816                                         addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9817                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9818                                 }
9819                                 goto call_end;
9820                         }
9821
9822                         /* Direct calls to icalls */
9823                         if (direct_icall) {
9824                                 MonoMethod *wrapper;
9825                                 int costs;
9826
9827                                 /* Inline the wrapper */
9828                                 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9829
9830                                 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9831                                 g_assert (costs > 0);
9832                                 cfg->real_offset += 5;
9833
9834                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9835                                         /* *sp is already set by inline_method */
9836                                         sp++;
9837                                         push_res = FALSE;
9838                                 }
9839
9840                                 inline_costs += costs;
9841
9842                                 goto call_end;
9843                         }
9844                                         
9845                         /* Array methods */
9846                         if (array_rank) {
9847                                 MonoInst *addr;
9848
9849                                 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */ 
9850                                         MonoInst *val = sp [fsig->param_count];
9851
9852                                         if (val->type == STACK_OBJ) {
9853                                                 MonoInst *iargs [2];
9854
9855                                                 iargs [0] = sp [0];
9856                                                 iargs [1] = val;
9857                                                 
9858                                                 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9859                                         }
9860                                         
9861                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9862                                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9863                                         if (cfg->gen_write_barriers && val->type == STACK_OBJ && !(val->opcode == OP_PCONST && val->inst_c0 == 0))
9864                                                 emit_write_barrier (cfg, addr, val);
9865                                         if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9866                                                 GSHAREDVT_FAILURE (*ip);
9867                                 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9868                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9869
9870                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9871                                 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9872                                         if (!cmethod->klass->element_class->valuetype && !readonly)
9873                                                 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9874                                         CHECK_TYPELOAD (cmethod->klass);
9875                                         
9876                                         readonly = FALSE;
9877                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9878                                         ins = addr;
9879                                 } else {
9880                                         g_assert_not_reached ();
9881                                 }
9882
9883                                 emit_widen = FALSE;
9884                                 goto call_end;
9885                         }
9886
9887                         ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9888                         if (ins)
9889                                 goto call_end;
9890
9891                         /* Tail prefix / tail call optimization */
9892
9893                         /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9894                         /* FIXME: runtime generic context pointer for jumps? */
9895                         /* FIXME: handle this for generic sharing eventually */
9896                         if ((ins_flag & MONO_INST_TAILCALL) &&
9897                                 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9898                                 supported_tail_call = TRUE;
9899
9900                         if (supported_tail_call) {
9901                                 MonoCallInst *call;
9902
9903                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9904                                 INLINE_FAILURE ("tail call");
9905
9906                                 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9907
9908                                 if (cfg->backend->have_op_tail_call) {
9909                                         /* Handle tail calls similarly to normal calls */
9910                                         tail_call = TRUE;
9911                                 } else {
9912                                         emit_instrumentation_call (cfg, mono_profiler_method_leave);
9913
9914                                         MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9915                                         call->tail_call = TRUE;
9916                                         call->method = cmethod;
9917                                         call->signature = mono_method_signature (cmethod);
9918
9919                                         /*
9920                                          * We implement tail calls by storing the actual arguments into the 
9921                                          * argument variables, then emitting a CEE_JMP.
9922                                          */
9923                                         for (i = 0; i < n; ++i) {
9924                                                 /* Prevent argument from being register allocated */
9925                                                 arg_array [i]->flags |= MONO_INST_VOLATILE;
9926                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9927                                         }
9928                                         ins = (MonoInst*)call;
9929                                         ins->inst_p0 = cmethod;
9930                                         ins->inst_p1 = arg_array [0];
9931                                         MONO_ADD_INS (cfg->cbb, ins);
9932                                         link_bblock (cfg, cfg->cbb, end_bblock);
9933                                         start_new_bblock = 1;
9934
9935                                         // FIXME: Eliminate unreachable epilogs
9936
9937                                         /*
9938                                          * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9939                                          * only reachable from this call.
9940                                          */
9941                                         GET_BBLOCK (cfg, tblock, ip + 5);
9942                                         if (tblock == cfg->cbb || tblock->in_count == 0)
9943                                                 skip_ret = TRUE;
9944                                         push_res = FALSE;
9945
9946                                         goto call_end;
9947                                 }
9948                         }
9949
9950                         /* 
9951                          * Synchronized wrappers.
9952                          * Its hard to determine where to replace a method with its synchronized
9953                          * wrapper without causing an infinite recursion. The current solution is
9954                          * to add the synchronized wrapper in the trampolines, and to
9955                          * change the called method to a dummy wrapper, and resolve that wrapper
9956                          * to the real method in mono_jit_compile_method ().
9957                          */
9958                         if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9959                                 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9960                                 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9961                                         cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9962                         }
9963
9964                         /*
9965                          * Virtual calls in llvm-only mode.
9966                          */
9967                         if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9968                                 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9969                                 goto call_end;
9970                         }
9971
9972                         /* Common call */
9973                         INLINE_FAILURE ("call");
9974                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9975                                                                                           imt_arg, vtable_arg);
9976
9977                         if (tail_call && !cfg->llvm_only) {
9978                                 link_bblock (cfg, cfg->cbb, end_bblock);
9979                                 start_new_bblock = 1;
9980
9981                                 // FIXME: Eliminate unreachable epilogs
9982
9983                                 /*
9984                                  * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9985                                  * only reachable from this call.
9986                                  */
9987                                 GET_BBLOCK (cfg, tblock, ip + 5);
9988                                 if (tblock == cfg->cbb || tblock->in_count == 0)
9989                                         skip_ret = TRUE;
9990                                 push_res = FALSE;
9991                         }
9992
9993                         call_end:
9994
9995                         /* End of call, INS should contain the result of the call, if any */
9996
9997                         if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9998                                 g_assert (ins);
9999                                 if (emit_widen)
10000                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
10001                                 else
10002                                         *sp++ = ins;
10003                         }
10004
10005                         if (keep_this_alive) {
10006                                 MonoInst *dummy_use;
10007
10008                                 /* See mono_emit_method_call_full () */
10009                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
10010                         }
10011
10012                         CHECK_CFG_EXCEPTION;
10013
10014                         ip += 5;
10015                         if (skip_ret) {
10016                                 g_assert (*ip == CEE_RET);
10017                                 ip += 1;
10018                         }
10019                         ins_flag = 0;
10020                         constrained_class = NULL;
10021                         if (need_seq_point)
10022                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10023                         break;
10024                 }
10025                 case CEE_RET:
10026                         if (cfg->method != method) {
10027                                 /* return from inlined method */
10028                                 /* 
10029                                  * If in_count == 0, that means the ret is unreachable due to
10030                                  * being preceeded by a throw. In that case, inline_method () will
10031                                  * handle setting the return value 
10032                                  * (test case: test_0_inline_throw ()).
10033                                  */
10034                                 if (return_var && cfg->cbb->in_count) {
10035                                         MonoType *ret_type = mono_method_signature (method)->ret;
10036
10037                                         MonoInst *store;
10038                                         CHECK_STACK (1);
10039                                         --sp;
10040
10041                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10042                                                 UNVERIFIED;
10043
10044                                         //g_assert (returnvar != -1);
10045                                         EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
10046                                         cfg->ret_var_set = TRUE;
10047                                 } 
10048                         } else {
10049                                 emit_instrumentation_call (cfg, mono_profiler_method_leave);
10050
10051                                 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
10052                                         emit_pop_lmf (cfg);
10053
10054                                 if (cfg->ret) {
10055                                         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
10056
10057                                         if (seq_points && !sym_seq_points) {
10058                                                 /* 
10059                                                  * Place a seq point here too even through the IL stack is not
10060                                                  * empty, so a step over on
10061                                                  * call <FOO>
10062                                                  * ret
10063                                                  * will work correctly.
10064                                                  */
10065                                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
10066                                                 MONO_ADD_INS (cfg->cbb, ins);
10067                                         }
10068
10069                                         g_assert (!return_var);
10070                                         CHECK_STACK (1);
10071                                         --sp;
10072
10073                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10074                                                 UNVERIFIED;
10075
10076                                         emit_setret (cfg, *sp);
10077                                 }
10078                         }
10079                         if (sp != stack_start)
10080                                 UNVERIFIED;
10081                         MONO_INST_NEW (cfg, ins, OP_BR);
10082                         ip++;
10083                         ins->inst_target_bb = end_bblock;
10084                         MONO_ADD_INS (cfg->cbb, ins);
10085                         link_bblock (cfg, cfg->cbb, end_bblock);
10086                         start_new_bblock = 1;
10087                         break;
10088                 case CEE_BR_S:
10089                         CHECK_OPSIZE (2);
10090                         MONO_INST_NEW (cfg, ins, OP_BR);
10091                         ip++;
10092                         target = ip + 1 + (signed char)(*ip);
10093                         ++ip;
10094                         GET_BBLOCK (cfg, tblock, target);
10095                         link_bblock (cfg, cfg->cbb, tblock);
10096                         ins->inst_target_bb = tblock;
10097                         if (sp != stack_start) {
10098                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10099                                 sp = stack_start;
10100                                 CHECK_UNVERIFIABLE (cfg);
10101                         }
10102                         MONO_ADD_INS (cfg->cbb, ins);
10103                         start_new_bblock = 1;
10104                         inline_costs += BRANCH_COST;
10105                         break;
10106                 case CEE_BEQ_S:
10107                 case CEE_BGE_S:
10108                 case CEE_BGT_S:
10109                 case CEE_BLE_S:
10110                 case CEE_BLT_S:
10111                 case CEE_BNE_UN_S:
10112                 case CEE_BGE_UN_S:
10113                 case CEE_BGT_UN_S:
10114                 case CEE_BLE_UN_S:
10115                 case CEE_BLT_UN_S:
10116                         CHECK_OPSIZE (2);
10117                         CHECK_STACK (2);
10118                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
10119                         ip++;
10120                         target = ip + 1 + *(signed char*)ip;
10121                         ip++;
10122
10123                         ADD_BINCOND (NULL);
10124
10125                         sp = stack_start;
10126                         inline_costs += BRANCH_COST;
10127                         break;
10128                 case CEE_BR:
10129                         CHECK_OPSIZE (5);
10130                         MONO_INST_NEW (cfg, ins, OP_BR);
10131                         ip++;
10132
10133                         target = ip + 4 + (gint32)read32(ip);
10134                         ip += 4;
10135                         GET_BBLOCK (cfg, tblock, target);
10136                         link_bblock (cfg, cfg->cbb, tblock);
10137                         ins->inst_target_bb = tblock;
10138                         if (sp != stack_start) {
10139                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10140                                 sp = stack_start;
10141                                 CHECK_UNVERIFIABLE (cfg);
10142                         }
10143
10144                         MONO_ADD_INS (cfg->cbb, ins);
10145
10146                         start_new_bblock = 1;
10147                         inline_costs += BRANCH_COST;
10148                         break;
10149                 case CEE_BRFALSE_S:
10150                 case CEE_BRTRUE_S:
10151                 case CEE_BRFALSE:
10152                 case CEE_BRTRUE: {
10153                         MonoInst *cmp;
10154                         gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
10155                         gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
10156                         guint32 opsize = is_short ? 1 : 4;
10157
10158                         CHECK_OPSIZE (opsize);
10159                         CHECK_STACK (1);
10160                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
10161                                 UNVERIFIED;
10162                         ip ++;
10163                         target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
10164                         ip += opsize;
10165
10166                         sp--;
10167
10168                         GET_BBLOCK (cfg, tblock, target);
10169                         link_bblock (cfg, cfg->cbb, tblock);
10170                         GET_BBLOCK (cfg, tblock, ip);
10171                         link_bblock (cfg, cfg->cbb, tblock);
10172
10173                         if (sp != stack_start) {
10174                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10175                                 CHECK_UNVERIFIABLE (cfg);
10176                         }
10177
10178                         MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
10179                         cmp->sreg1 = sp [0]->dreg;
10180                         type_from_op (cfg, cmp, sp [0], NULL);
10181                         CHECK_TYPE (cmp);
10182
10183 #if SIZEOF_REGISTER == 4
10184                         if (cmp->opcode == OP_LCOMPARE_IMM) {
10185                                 /* Convert it to OP_LCOMPARE */
10186                                 MONO_INST_NEW (cfg, ins, OP_I8CONST);
10187                                 ins->type = STACK_I8;
10188                                 ins->dreg = alloc_dreg (cfg, STACK_I8);
10189                                 ins->inst_l = 0;
10190                                 MONO_ADD_INS (cfg->cbb, ins);
10191                                 cmp->opcode = OP_LCOMPARE;
10192                                 cmp->sreg2 = ins->dreg;
10193                         }
10194 #endif
10195                         MONO_ADD_INS (cfg->cbb, cmp);
10196
10197                         MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
10198                         type_from_op (cfg, ins, sp [0], NULL);
10199                         MONO_ADD_INS (cfg->cbb, ins);
10200                         ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
10201                         GET_BBLOCK (cfg, tblock, target);
10202                         ins->inst_true_bb = tblock;
10203                         GET_BBLOCK (cfg, tblock, ip);
10204                         ins->inst_false_bb = tblock;
10205                         start_new_bblock = 2;
10206
10207                         sp = stack_start;
10208                         inline_costs += BRANCH_COST;
10209                         break;
10210                 }
10211                 case CEE_BEQ:
10212                 case CEE_BGE:
10213                 case CEE_BGT:
10214                 case CEE_BLE:
10215                 case CEE_BLT:
10216                 case CEE_BNE_UN:
10217                 case CEE_BGE_UN:
10218                 case CEE_BGT_UN:
10219                 case CEE_BLE_UN:
10220                 case CEE_BLT_UN:
10221                         CHECK_OPSIZE (5);
10222                         CHECK_STACK (2);
10223                         MONO_INST_NEW (cfg, ins, *ip);
10224                         ip++;
10225                         target = ip + 4 + (gint32)read32(ip);
10226                         ip += 4;
10227
10228                         ADD_BINCOND (NULL);
10229
10230                         sp = stack_start;
10231                         inline_costs += BRANCH_COST;
10232                         break;
10233                 case CEE_SWITCH: {
10234                         MonoInst *src1;
10235                         MonoBasicBlock **targets;
10236                         MonoBasicBlock *default_bblock;
10237                         MonoJumpInfoBBTable *table;
10238                         int offset_reg = alloc_preg (cfg);
10239                         int target_reg = alloc_preg (cfg);
10240                         int table_reg = alloc_preg (cfg);
10241                         int sum_reg = alloc_preg (cfg);
10242                         gboolean use_op_switch;
10243
10244                         CHECK_OPSIZE (5);
10245                         CHECK_STACK (1);
10246                         n = read32 (ip + 1);
10247                         --sp;
10248                         src1 = sp [0];
10249                         if ((src1->type != STACK_I4) && (src1->type != STACK_PTR)) 
10250                                 UNVERIFIED;
10251
10252                         ip += 5;
10253                         CHECK_OPSIZE (n * sizeof (guint32));
10254                         target = ip + n * sizeof (guint32);
10255
10256                         GET_BBLOCK (cfg, default_bblock, target);
10257                         default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
10258
10259                         targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
10260                         for (i = 0; i < n; ++i) {
10261                                 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
10262                                 targets [i] = tblock;
10263                                 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
10264                                 ip += 4;
10265                         }
10266
10267                         if (sp != stack_start) {
10268                                 /* 
10269                                  * Link the current bb with the targets as well, so handle_stack_args
10270                                  * will set their in_stack correctly.
10271                                  */
10272                                 link_bblock (cfg, cfg->cbb, default_bblock);
10273                                 for (i = 0; i < n; ++i)
10274                                         link_bblock (cfg, cfg->cbb, targets [i]);
10275
10276                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10277                                 sp = stack_start;
10278                                 CHECK_UNVERIFIABLE (cfg);
10279
10280                                 /* Undo the links */
10281                                 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
10282                                 for (i = 0; i < n; ++i)
10283                                         mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
10284                         }
10285
10286                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
10287                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
10288
10289                         for (i = 0; i < n; ++i)
10290                                 link_bblock (cfg, cfg->cbb, targets [i]);
10291
10292                         table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
10293                         table->table = targets;
10294                         table->table_size = n;
10295
10296                         use_op_switch = FALSE;
10297 #ifdef TARGET_ARM
10298                         /* ARM implements SWITCH statements differently */
10299                         /* FIXME: Make it use the generic implementation */
10300                         if (!cfg->compile_aot)
10301                                 use_op_switch = TRUE;
10302 #endif
10303
10304                         if (COMPILE_LLVM (cfg))
10305                                 use_op_switch = TRUE;
10306
10307                         cfg->cbb->has_jump_table = 1;
10308
10309                         if (use_op_switch) {
10310                                 MONO_INST_NEW (cfg, ins, OP_SWITCH);
10311                                 ins->sreg1 = src1->dreg;
10312                                 ins->inst_p0 = table;
10313                                 ins->inst_many_bb = targets;
10314                                 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
10315                                 MONO_ADD_INS (cfg->cbb, ins);
10316                         } else {
10317                                 if (sizeof (gpointer) == 8)
10318                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
10319                                 else
10320                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
10321
10322 #if SIZEOF_REGISTER == 8
10323                                 /* The upper word might not be zero, and we add it to a 64 bit address later */
10324                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
10325 #endif
10326
10327                                 if (cfg->compile_aot) {
10328                                         MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
10329                                 } else {
10330                                         MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
10331                                         ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
10332                                         ins->inst_p0 = table;
10333                                         ins->dreg = table_reg;
10334                                         MONO_ADD_INS (cfg->cbb, ins);
10335                                 }
10336
10337                                 /* FIXME: Use load_memindex */
10338                                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
10339                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
10340                                 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
10341                         }
10342                         start_new_bblock = 1;
10343                         inline_costs += (BRANCH_COST * 2);
10344                         break;
10345                 }
10346                 case CEE_LDIND_I1:
10347                 case CEE_LDIND_U1:
10348                 case CEE_LDIND_I2:
10349                 case CEE_LDIND_U2:
10350                 case CEE_LDIND_I4:
10351                 case CEE_LDIND_U4:
10352                 case CEE_LDIND_I8:
10353                 case CEE_LDIND_I:
10354                 case CEE_LDIND_R4:
10355                 case CEE_LDIND_R8:
10356                 case CEE_LDIND_REF:
10357                         CHECK_STACK (1);
10358                         --sp;
10359
10360                         switch (*ip) {
10361                         case CEE_LDIND_R4:
10362                         case CEE_LDIND_R8:
10363                                 dreg = alloc_freg (cfg);
10364                                 break;
10365                         case CEE_LDIND_I8:
10366                                 dreg = alloc_lreg (cfg);
10367                                 break;
10368                         case CEE_LDIND_REF:
10369                                 dreg = alloc_ireg_ref (cfg);
10370                                 break;
10371                         default:
10372                                 dreg = alloc_preg (cfg);
10373                         }
10374
10375                         NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
10376                         ins->type = ldind_type [*ip - CEE_LDIND_I1];
10377                         if (*ip == CEE_LDIND_R4)
10378                                 ins->type = cfg->r4_stack_type;
10379                         ins->flags |= ins_flag;
10380                         MONO_ADD_INS (cfg->cbb, ins);
10381                         *sp++ = ins;
10382                         if (ins_flag & MONO_INST_VOLATILE) {
10383                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10384                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10385                         }
10386                         ins_flag = 0;
10387                         ++ip;
10388                         break;
10389                 case CEE_STIND_REF:
10390                 case CEE_STIND_I1:
10391                 case CEE_STIND_I2:
10392                 case CEE_STIND_I4:
10393                 case CEE_STIND_I8:
10394                 case CEE_STIND_R4:
10395                 case CEE_STIND_R8:
10396                 case CEE_STIND_I:
10397                         CHECK_STACK (2);
10398                         sp -= 2;
10399
10400                         if (ins_flag & MONO_INST_VOLATILE) {
10401                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10402                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10403                         }
10404
10405                         NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
10406                         ins->flags |= ins_flag;
10407                         ins_flag = 0;
10408
10409                         MONO_ADD_INS (cfg->cbb, ins);
10410
10411                         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)))
10412                                 emit_write_barrier (cfg, sp [0], sp [1]);
10413
10414                         inline_costs += 1;
10415                         ++ip;
10416                         break;
10417
10418                 case CEE_MUL:
10419                         CHECK_STACK (2);
10420
10421                         MONO_INST_NEW (cfg, ins, (*ip));
10422                         sp -= 2;
10423                         ins->sreg1 = sp [0]->dreg;
10424                         ins->sreg2 = sp [1]->dreg;
10425                         type_from_op (cfg, ins, sp [0], sp [1]);
10426                         CHECK_TYPE (ins);
10427                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10428
10429                         /* Use the immediate opcodes if possible */
10430                         if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
10431                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10432                                 if (imm_opcode != -1) {
10433                                         ins->opcode = imm_opcode;
10434                                         ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
10435                                         ins->sreg2 = -1;
10436
10437                                         NULLIFY_INS (sp [1]);
10438                                 }
10439                         }
10440
10441                         MONO_ADD_INS ((cfg)->cbb, (ins));
10442
10443                         *sp++ = mono_decompose_opcode (cfg, ins);
10444                         ip++;
10445                         break;
10446                 case CEE_ADD:
10447                 case CEE_SUB:
10448                 case CEE_DIV:
10449                 case CEE_DIV_UN:
10450                 case CEE_REM:
10451                 case CEE_REM_UN:
10452                 case CEE_AND:
10453                 case CEE_OR:
10454                 case CEE_XOR:
10455                 case CEE_SHL:
10456                 case CEE_SHR:
10457                 case CEE_SHR_UN:
10458                         CHECK_STACK (2);
10459
10460                         MONO_INST_NEW (cfg, ins, (*ip));
10461                         sp -= 2;
10462                         ins->sreg1 = sp [0]->dreg;
10463                         ins->sreg2 = sp [1]->dreg;
10464                         type_from_op (cfg, ins, sp [0], sp [1]);
10465                         CHECK_TYPE (ins);
10466                         add_widen_op (cfg, ins, &sp [0], &sp [1]);
10467                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10468
10469                         /* FIXME: Pass opcode to is_inst_imm */
10470
10471                         /* Use the immediate opcodes if possible */
10472                         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)) {
10473                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10474                                 if (imm_opcode != -1) {
10475                                         ins->opcode = imm_opcode;
10476                                         if (sp [1]->opcode == OP_I8CONST) {
10477 #if SIZEOF_REGISTER == 8
10478                                                 ins->inst_imm = sp [1]->inst_l;
10479 #else
10480                                                 ins->inst_ls_word = sp [1]->inst_ls_word;
10481                                                 ins->inst_ms_word = sp [1]->inst_ms_word;
10482 #endif
10483                                         }
10484                                         else
10485                                                 ins->inst_imm = (gssize)(sp [1]->inst_c0);
10486                                         ins->sreg2 = -1;
10487
10488                                         /* Might be followed by an instruction added by add_widen_op */
10489                                         if (sp [1]->next == NULL)
10490                                                 NULLIFY_INS (sp [1]);
10491                                 }
10492                         }
10493                         MONO_ADD_INS ((cfg)->cbb, (ins));
10494
10495                         *sp++ = mono_decompose_opcode (cfg, ins);
10496                         ip++;
10497                         break;
10498                 case CEE_NEG:
10499                 case CEE_NOT:
10500                 case CEE_CONV_I1:
10501                 case CEE_CONV_I2:
10502                 case CEE_CONV_I4:
10503                 case CEE_CONV_R4:
10504                 case CEE_CONV_R8:
10505                 case CEE_CONV_U4:
10506                 case CEE_CONV_I8:
10507                 case CEE_CONV_U8:
10508                 case CEE_CONV_OVF_I8:
10509                 case CEE_CONV_OVF_U8:
10510                 case CEE_CONV_R_UN:
10511                         CHECK_STACK (1);
10512
10513                         /* Special case this earlier so we have long constants in the IR */
10514                         if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
10515                                 int data = sp [-1]->inst_c0;
10516                                 sp [-1]->opcode = OP_I8CONST;
10517                                 sp [-1]->type = STACK_I8;
10518 #if SIZEOF_REGISTER == 8
10519                                 if ((*ip) == CEE_CONV_U8)
10520                                         sp [-1]->inst_c0 = (guint32)data;
10521                                 else
10522                                         sp [-1]->inst_c0 = data;
10523 #else
10524                                 sp [-1]->inst_ls_word = data;
10525                                 if ((*ip) == CEE_CONV_U8)
10526                                         sp [-1]->inst_ms_word = 0;
10527                                 else
10528                                         sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
10529 #endif
10530                                 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
10531                         }
10532                         else {
10533                                 ADD_UNOP (*ip);
10534                         }
10535                         ip++;
10536                         break;
10537                 case CEE_CONV_OVF_I4:
10538                 case CEE_CONV_OVF_I1:
10539                 case CEE_CONV_OVF_I2:
10540                 case CEE_CONV_OVF_I:
10541                 case CEE_CONV_OVF_U:
10542                         CHECK_STACK (1);
10543
10544                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10545                                 ADD_UNOP (CEE_CONV_OVF_I8);
10546                                 ADD_UNOP (*ip);
10547                         } else {
10548                                 ADD_UNOP (*ip);
10549                         }
10550                         ip++;
10551                         break;
10552                 case CEE_CONV_OVF_U1:
10553                 case CEE_CONV_OVF_U2:
10554                 case CEE_CONV_OVF_U4:
10555                         CHECK_STACK (1);
10556
10557                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10558                                 ADD_UNOP (CEE_CONV_OVF_U8);
10559                                 ADD_UNOP (*ip);
10560                         } else {
10561                                 ADD_UNOP (*ip);
10562                         }
10563                         ip++;
10564                         break;
10565                 case CEE_CONV_OVF_I1_UN:
10566                 case CEE_CONV_OVF_I2_UN:
10567                 case CEE_CONV_OVF_I4_UN:
10568                 case CEE_CONV_OVF_I8_UN:
10569                 case CEE_CONV_OVF_U1_UN:
10570                 case CEE_CONV_OVF_U2_UN:
10571                 case CEE_CONV_OVF_U4_UN:
10572                 case CEE_CONV_OVF_U8_UN:
10573                 case CEE_CONV_OVF_I_UN:
10574                 case CEE_CONV_OVF_U_UN:
10575                 case CEE_CONV_U2:
10576                 case CEE_CONV_U1:
10577                 case CEE_CONV_I:
10578                 case CEE_CONV_U:
10579                         CHECK_STACK (1);
10580                         ADD_UNOP (*ip);
10581                         CHECK_CFG_EXCEPTION;
10582                         ip++;
10583                         break;
10584                 case CEE_ADD_OVF:
10585                 case CEE_ADD_OVF_UN:
10586                 case CEE_MUL_OVF:
10587                 case CEE_MUL_OVF_UN:
10588                 case CEE_SUB_OVF:
10589                 case CEE_SUB_OVF_UN:
10590                         CHECK_STACK (2);
10591                         ADD_BINOP (*ip);
10592                         ip++;
10593                         break;
10594                 case CEE_CPOBJ:
10595                         GSHAREDVT_FAILURE (*ip);
10596                         CHECK_OPSIZE (5);
10597                         CHECK_STACK (2);
10598                         token = read32 (ip + 1);
10599                         klass = mini_get_class (method, token, generic_context);
10600                         CHECK_TYPELOAD (klass);
10601                         sp -= 2;
10602                         if (generic_class_is_reference_type (cfg, klass)) {
10603                                 MonoInst *store, *load;
10604                                 int dreg = alloc_ireg_ref (cfg);
10605
10606                                 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10607                                 load->flags |= ins_flag;
10608                                 MONO_ADD_INS (cfg->cbb, load);
10609
10610                                 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10611                                 store->flags |= ins_flag;
10612                                 MONO_ADD_INS (cfg->cbb, store);
10613
10614                                 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10615                                         emit_write_barrier (cfg, sp [0], sp [1]);
10616                         } else {
10617                                 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10618                         }
10619                         ins_flag = 0;
10620                         ip += 5;
10621                         break;
10622                 case CEE_LDOBJ: {
10623                         int loc_index = -1;
10624                         int stloc_len = 0;
10625
10626                         CHECK_OPSIZE (5);
10627                         CHECK_STACK (1);
10628                         --sp;
10629                         token = read32 (ip + 1);
10630                         klass = mini_get_class (method, token, generic_context);
10631                         CHECK_TYPELOAD (klass);
10632
10633                         /* Optimize the common ldobj+stloc combination */
10634                         switch (ip [5]) {
10635                         case CEE_STLOC_S:
10636                                 loc_index = ip [6];
10637                                 stloc_len = 2;
10638                                 break;
10639                         case CEE_STLOC_0:
10640                         case CEE_STLOC_1:
10641                         case CEE_STLOC_2:
10642                         case CEE_STLOC_3:
10643                                 loc_index = ip [5] - CEE_STLOC_0;
10644                                 stloc_len = 1;
10645                                 break;
10646                         default:
10647                                 break;
10648                         }
10649
10650                         if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10651                                 CHECK_LOCAL (loc_index);
10652
10653                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10654                                 ins->dreg = cfg->locals [loc_index]->dreg;
10655                                 ins->flags |= ins_flag;
10656                                 ip += 5;
10657                                 ip += stloc_len;
10658                                 if (ins_flag & MONO_INST_VOLATILE) {
10659                                         /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10660                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10661                                 }
10662                                 ins_flag = 0;
10663                                 break;
10664                         }
10665
10666                         /* Optimize the ldobj+stobj combination */
10667                         /* The reference case ends up being a load+store anyway */
10668                         /* Skip this if the operation is volatile. */
10669                         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)) {
10670                                 CHECK_STACK (1);
10671
10672                                 sp --;
10673
10674                                 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10675
10676                                 ip += 5 + 5;
10677                                 ins_flag = 0;
10678                                 break;
10679                         }
10680
10681                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10682                         ins->flags |= ins_flag;
10683                         *sp++ = ins;
10684
10685                         if (ins_flag & MONO_INST_VOLATILE) {
10686                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10687                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10688                         }
10689
10690                         ip += 5;
10691                         ins_flag = 0;
10692                         inline_costs += 1;
10693                         break;
10694                 }
10695                 case CEE_LDSTR:
10696                         CHECK_STACK_OVF (1);
10697                         CHECK_OPSIZE (5);
10698                         n = read32 (ip + 1);
10699
10700                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10701                                 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10702                                 ins->type = STACK_OBJ;
10703                                 *sp = ins;
10704                         }
10705                         else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10706                                 MonoInst *iargs [1];
10707                                 char *str = (char *)mono_method_get_wrapper_data (method, n);
10708
10709                                 if (cfg->compile_aot)
10710                                         EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10711                                 else
10712                                         EMIT_NEW_PCONST (cfg, iargs [0], str);
10713                                 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10714                         } else {
10715                                 if (cfg->opt & MONO_OPT_SHARED) {
10716                                         MonoInst *iargs [3];
10717
10718                                         if (cfg->compile_aot) {
10719                                                 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10720                                         }
10721                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10722                                         EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10723                                         EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10724                                         *sp = mono_emit_jit_icall (cfg, mono_ldstr, iargs);
10725                                         mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10726                                 } else {
10727                                         if (cfg->cbb->out_of_line) {
10728                                                 MonoInst *iargs [2];
10729
10730                                                 if (image == mono_defaults.corlib) {
10731                                                         /* 
10732                                                          * Avoid relocations in AOT and save some space by using a 
10733                                                          * version of helper_ldstr specialized to mscorlib.
10734                                                          */
10735                                                         EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10736                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10737                                                 } else {
10738                                                         /* Avoid creating the string object */
10739                                                         EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10740                                                         EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10741                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10742                                                 }
10743                                         } 
10744                                         else
10745                                         if (cfg->compile_aot) {
10746                                                 NEW_LDSTRCONST (cfg, ins, image, n);
10747                                                 *sp = ins;
10748                                                 MONO_ADD_INS (cfg->cbb, ins);
10749                                         } 
10750                                         else {
10751                                                 NEW_PCONST (cfg, ins, NULL);
10752                                                 ins->type = STACK_OBJ;
10753                                                 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10754                                                 if (!ins->inst_p0)
10755                                                         OUT_OF_MEMORY_FAILURE;
10756
10757                                                 *sp = ins;
10758                                                 MONO_ADD_INS (cfg->cbb, ins);
10759                                         }
10760                                 }
10761                         }
10762
10763                         sp++;
10764                         ip += 5;
10765                         break;
10766                 case CEE_NEWOBJ: {
10767                         MonoInst *iargs [2];
10768                         MonoMethodSignature *fsig;
10769                         MonoInst this_ins;
10770                         MonoInst *alloc;
10771                         MonoInst *vtable_arg = NULL;
10772
10773                         CHECK_OPSIZE (5);
10774                         token = read32 (ip + 1);
10775                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10776                         CHECK_CFG_ERROR;
10777
10778                         fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10779                         CHECK_CFG_ERROR;
10780
10781                         mono_save_token_info (cfg, image, token, cmethod);
10782
10783                         if (!mono_class_init (cmethod->klass))
10784                                 TYPE_LOAD_ERROR (cmethod->klass);
10785
10786                         context_used = mini_method_check_context_used (cfg, cmethod);
10787
10788                         if (mono_security_core_clr_enabled ())
10789                                 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10790
10791                         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)) {
10792                                 emit_class_init (cfg, cmethod->klass);
10793                                 CHECK_TYPELOAD (cmethod->klass);
10794                         }
10795
10796                         /*
10797                         if (cfg->gsharedvt) {
10798                                 if (mini_is_gsharedvt_variable_signature (sig))
10799                                         GSHAREDVT_FAILURE (*ip);
10800                         }
10801                         */
10802
10803                         n = fsig->param_count;
10804                         CHECK_STACK (n);
10805
10806                         /* 
10807                          * Generate smaller code for the common newobj <exception> instruction in
10808                          * argument checking code.
10809                          */
10810                         if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10811                                 is_exception_class (cmethod->klass) && n <= 2 &&
10812                                 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) && 
10813                                 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10814                                 MonoInst *iargs [3];
10815
10816                                 sp -= n;
10817
10818                                 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10819                                 switch (n) {
10820                                 case 0:
10821                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10822                                         break;
10823                                 case 1:
10824                                         iargs [1] = sp [0];
10825                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10826                                         break;
10827                                 case 2:
10828                                         iargs [1] = sp [0];
10829                                         iargs [2] = sp [1];
10830                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10831                                         break;
10832                                 default:
10833                                         g_assert_not_reached ();
10834                                 }
10835
10836                                 ip += 5;
10837                                 inline_costs += 5;
10838                                 break;
10839                         }
10840
10841                         /* move the args to allow room for 'this' in the first position */
10842                         while (n--) {
10843                                 --sp;
10844                                 sp [1] = sp [0];
10845                         }
10846
10847                         /* check_call_signature () requires sp[0] to be set */
10848                         this_ins.type = STACK_OBJ;
10849                         sp [0] = &this_ins;
10850                         if (check_call_signature (cfg, fsig, sp))
10851                                 UNVERIFIED;
10852
10853                         iargs [0] = NULL;
10854
10855                         if (mini_class_is_system_array (cmethod->klass)) {
10856                                 *sp = emit_get_rgctx_method (cfg, context_used,
10857                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
10858
10859                                 /* Avoid varargs in the common case */
10860                                 if (fsig->param_count == 1)
10861                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10862                                 else if (fsig->param_count == 2)
10863                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10864                                 else if (fsig->param_count == 3)
10865                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10866                                 else if (fsig->param_count == 4)
10867                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10868                                 else
10869                                         alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10870                         } else if (cmethod->string_ctor) {
10871                                 g_assert (!context_used);
10872                                 g_assert (!vtable_arg);
10873                                 /* we simply pass a null pointer */
10874                                 EMIT_NEW_PCONST (cfg, *sp, NULL); 
10875                                 /* now call the string ctor */
10876                                 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10877                         } else {
10878                                 if (cmethod->klass->valuetype) {
10879                                         iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10880                                         emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10881                                         EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10882
10883                                         alloc = NULL;
10884
10885                                         /* 
10886                                          * The code generated by mini_emit_virtual_call () expects
10887                                          * iargs [0] to be a boxed instance, but luckily the vcall
10888                                          * will be transformed into a normal call there.
10889                                          */
10890                                 } else if (context_used) {
10891                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10892                                         *sp = alloc;
10893                                 } else {
10894                                         MonoVTable *vtable = NULL;
10895
10896                                         if (!cfg->compile_aot)
10897                                                 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10898                                         CHECK_TYPELOAD (cmethod->klass);
10899
10900                                         /*
10901                                          * TypeInitializationExceptions thrown from the mono_runtime_class_init
10902                                          * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10903                                          * As a workaround, we call class cctors before allocating objects.
10904                                          */
10905                                         if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10906                                                 emit_class_init (cfg, cmethod->klass);
10907                                                 if (cfg->verbose_level > 2)
10908                                                         printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10909                                                 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10910                                         }
10911
10912                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10913                                         *sp = alloc;
10914                                 }
10915                                 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10916
10917                                 if (alloc)
10918                                         MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10919
10920                                 /* Now call the actual ctor */
10921                                 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10922                                 CHECK_CFG_EXCEPTION;
10923                         }
10924
10925                         if (alloc == NULL) {
10926                                 /* Valuetype */
10927                                 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10928                                 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10929                                 *sp++= ins;
10930                         } else {
10931                                 *sp++ = alloc;
10932                         }
10933                         
10934                         ip += 5;
10935                         inline_costs += 5;
10936                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10937                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10938                         break;
10939                 }
10940                 case CEE_CASTCLASS:
10941                         CHECK_STACK (1);
10942                         --sp;
10943                         CHECK_OPSIZE (5);
10944                         token = read32 (ip + 1);
10945                         klass = mini_get_class (method, token, generic_context);
10946                         CHECK_TYPELOAD (klass);
10947                         if (sp [0]->type != STACK_OBJ)
10948                                 UNVERIFIED;
10949
10950                         ins = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
10951                         CHECK_CFG_EXCEPTION;
10952
10953                         *sp ++ = ins;
10954                         ip += 5;
10955                         break;
10956                 case CEE_ISINST: {
10957                         CHECK_STACK (1);
10958                         --sp;
10959                         CHECK_OPSIZE (5);
10960                         token = read32 (ip + 1);
10961                         klass = mini_get_class (method, token, generic_context);
10962                         CHECK_TYPELOAD (klass);
10963                         if (sp [0]->type != STACK_OBJ)
10964                                 UNVERIFIED;
10965  
10966                         context_used = mini_class_check_context_used (cfg, klass);
10967
10968                         if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
10969                                 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
10970                                 MonoInst *args [3];
10971                                 int idx;
10972
10973                                 /* obj */
10974                                 args [0] = *sp;
10975
10976                                 /* klass */
10977                                 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
10978
10979                                 /* inline cache*/
10980                                 idx = get_castclass_cache_idx (cfg);
10981                                 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
10982
10983                                 *sp++ = mono_emit_method_call (cfg, mono_isinst, args, NULL);
10984                                 ip += 5;
10985                                 inline_costs += 2;
10986                         } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
10987                                 MonoMethod *mono_isinst;
10988                                 MonoInst *iargs [1];
10989                                 int costs;
10990
10991                                 mono_isinst = mono_marshal_get_isinst (klass); 
10992                                 iargs [0] = sp [0];
10993
10994                                 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), 
10995                                                                            iargs, ip, cfg->real_offset, TRUE);
10996                                 CHECK_CFG_EXCEPTION;
10997                                 g_assert (costs > 0);
10998                                 
10999                                 ip += 5;
11000                                 cfg->real_offset += 5;
11001
11002                                 *sp++= iargs [0];
11003
11004                                 inline_costs += costs;
11005                         }
11006                         else {
11007                                 ins = handle_isinst (cfg, klass, *sp, context_used);
11008                                 CHECK_CFG_EXCEPTION;
11009                                 *sp ++ = ins;
11010                                 ip += 5;
11011                         }
11012                         break;
11013                 }
11014                 case CEE_UNBOX_ANY: {
11015                         MonoInst *res, *addr;
11016
11017                         CHECK_STACK (1);
11018                         --sp;
11019                         CHECK_OPSIZE (5);
11020                         token = read32 (ip + 1);
11021                         klass = mini_get_class (method, token, generic_context);
11022                         CHECK_TYPELOAD (klass);
11023
11024                         mono_save_token_info (cfg, image, token, klass);
11025
11026                         context_used = mini_class_check_context_used (cfg, klass);
11027
11028                         if (mini_is_gsharedvt_klass (klass)) {
11029                                 res = handle_unbox_gsharedvt (cfg, klass, *sp);
11030                                 inline_costs += 2;
11031                         } else if (generic_class_is_reference_type (cfg, klass)) {
11032                                 res = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
11033                                 CHECK_CFG_EXCEPTION;
11034                         } else if (mono_class_is_nullable (klass)) {
11035                                 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
11036                         } else {
11037                                 addr = handle_unbox (cfg, klass, sp, context_used);
11038                                 /* LDOBJ */
11039                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11040                                 res = ins;
11041                                 inline_costs += 2;
11042                         }
11043
11044                         *sp ++ = res;
11045                         ip += 5;
11046                         break;
11047                 }
11048                 case CEE_BOX: {
11049                         MonoInst *val;
11050                         MonoClass *enum_class;
11051                         MonoMethod *has_flag;
11052
11053                         CHECK_STACK (1);
11054                         --sp;
11055                         val = *sp;
11056                         CHECK_OPSIZE (5);
11057                         token = read32 (ip + 1);
11058                         klass = mini_get_class (method, token, generic_context);
11059                         CHECK_TYPELOAD (klass);
11060
11061                         mono_save_token_info (cfg, image, token, klass);
11062
11063                         context_used = mini_class_check_context_used (cfg, klass);
11064
11065                         if (generic_class_is_reference_type (cfg, klass)) {
11066                                 *sp++ = val;
11067                                 ip += 5;
11068                                 break;
11069                         }
11070
11071                         if (klass == mono_defaults.void_class)
11072                                 UNVERIFIED;
11073                         if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
11074                                 UNVERIFIED;
11075                         /* frequent check in generic code: box (struct), brtrue */
11076
11077                         /*
11078                          * Look for:
11079                          *
11080                          *   <push int/long ptr>
11081                          *   <push int/long>
11082                          *   box MyFlags
11083                          *   constrained. MyFlags
11084                          *   callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
11085                          *
11086                          * If we find this sequence and the operand types on box and constrained
11087                          * are equal, we can emit a specialized instruction sequence instead of
11088                          * the very slow HasFlag () call.
11089                          */
11090                         if ((cfg->opt & MONO_OPT_INTRINS) &&
11091                             /* Cheap checks first. */
11092                             ip + 5 + 6 + 5 < end &&
11093                             ip [5] == CEE_PREFIX1 &&
11094                             ip [6] == CEE_CONSTRAINED_ &&
11095                             ip [11] == CEE_CALLVIRT &&
11096                             ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
11097                             mono_class_is_enum (klass) &&
11098                             (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
11099                             (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
11100                             has_flag->klass == mono_defaults.enum_class &&
11101                             !strcmp (has_flag->name, "HasFlag") &&
11102                             has_flag->signature->hasthis &&
11103                             has_flag->signature->param_count == 1) {
11104                                 CHECK_TYPELOAD (enum_class);
11105
11106                                 if (enum_class == klass) {
11107                                         MonoInst *enum_this, *enum_flag;
11108
11109                                         ip += 5 + 6 + 5;
11110                                         --sp;
11111
11112                                         enum_this = sp [0];
11113                                         enum_flag = sp [1];
11114
11115                                         *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
11116                                         break;
11117                                 }
11118                         }
11119
11120                         // FIXME: LLVM can't handle the inconsistent bb linking
11121                         if (!mono_class_is_nullable (klass) &&
11122                                 !mini_is_gsharedvt_klass (klass) &&
11123                                 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11124                                 (ip [5] == CEE_BRTRUE || 
11125                                  ip [5] == CEE_BRTRUE_S ||
11126                                  ip [5] == CEE_BRFALSE ||
11127                                  ip [5] == CEE_BRFALSE_S)) {
11128                                 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
11129                                 int dreg;
11130                                 MonoBasicBlock *true_bb, *false_bb;
11131
11132                                 ip += 5;
11133
11134                                 if (cfg->verbose_level > 3) {
11135                                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
11136                                         printf ("<box+brtrue opt>\n");
11137                                 }
11138
11139                                 switch (*ip) {
11140                                 case CEE_BRTRUE_S:
11141                                 case CEE_BRFALSE_S:
11142                                         CHECK_OPSIZE (2);
11143                                         ip++;
11144                                         target = ip + 1 + (signed char)(*ip);
11145                                         ip++;
11146                                         break;
11147                                 case CEE_BRTRUE:
11148                                 case CEE_BRFALSE:
11149                                         CHECK_OPSIZE (5);
11150                                         ip++;
11151                                         target = ip + 4 + (gint)(read32 (ip));
11152                                         ip += 4;
11153                                         break;
11154                                 default:
11155                                         g_assert_not_reached ();
11156                                 }
11157
11158                                 /* 
11159                                  * We need to link both bblocks, since it is needed for handling stack
11160                                  * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
11161                                  * Branching to only one of them would lead to inconsistencies, so
11162                                  * generate an ICONST+BRTRUE, the branch opts will get rid of them.
11163                                  */
11164                                 GET_BBLOCK (cfg, true_bb, target);
11165                                 GET_BBLOCK (cfg, false_bb, ip);
11166
11167                                 mono_link_bblock (cfg, cfg->cbb, true_bb);
11168                                 mono_link_bblock (cfg, cfg->cbb, false_bb);
11169
11170                                 if (sp != stack_start) {
11171                                         handle_stack_args (cfg, stack_start, sp - stack_start);
11172                                         sp = stack_start;
11173                                         CHECK_UNVERIFIABLE (cfg);
11174                                 }
11175
11176                                 if (COMPILE_LLVM (cfg)) {
11177                                         dreg = alloc_ireg (cfg);
11178                                         MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
11179                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
11180
11181                                         MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
11182                                 } else {
11183                                         /* The JIT can't eliminate the iconst+compare */
11184                                         MONO_INST_NEW (cfg, ins, OP_BR);
11185                                         ins->inst_target_bb = is_true ? true_bb : false_bb;
11186                                         MONO_ADD_INS (cfg->cbb, ins);
11187                                 }
11188
11189                                 start_new_bblock = 1;
11190                                 break;
11191                         }
11192
11193                         *sp++ = handle_box (cfg, val, klass, context_used);
11194
11195                         CHECK_CFG_EXCEPTION;
11196                         ip += 5;
11197                         inline_costs += 1;
11198                         break;
11199                 }
11200                 case CEE_UNBOX: {
11201                         CHECK_STACK (1);
11202                         --sp;
11203                         CHECK_OPSIZE (5);
11204                         token = read32 (ip + 1);
11205                         klass = mini_get_class (method, token, generic_context);
11206                         CHECK_TYPELOAD (klass);
11207
11208                         mono_save_token_info (cfg, image, token, klass);
11209
11210                         context_used = mini_class_check_context_used (cfg, klass);
11211
11212                         if (mono_class_is_nullable (klass)) {
11213                                 MonoInst *val;
11214
11215                                 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
11216                                 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
11217
11218                                 *sp++= ins;
11219                         } else {
11220                                 ins = handle_unbox (cfg, klass, sp, context_used);
11221                                 *sp++ = ins;
11222                         }
11223                         ip += 5;
11224                         inline_costs += 2;
11225                         break;
11226                 }
11227                 case CEE_LDFLD:
11228                 case CEE_LDFLDA:
11229                 case CEE_STFLD:
11230                 case CEE_LDSFLD:
11231                 case CEE_LDSFLDA:
11232                 case CEE_STSFLD: {
11233                         MonoClassField *field;
11234 #ifndef DISABLE_REMOTING
11235                         int costs;
11236 #endif
11237                         guint foffset;
11238                         gboolean is_instance;
11239                         int op;
11240                         gpointer addr = NULL;
11241                         gboolean is_special_static;
11242                         MonoType *ftype;
11243                         MonoInst *store_val = NULL;
11244                         MonoInst *thread_ins;
11245
11246                         op = *ip;
11247                         is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
11248                         if (is_instance) {
11249                                 if (op == CEE_STFLD) {
11250                                         CHECK_STACK (2);
11251                                         sp -= 2;
11252                                         store_val = sp [1];
11253                                 } else {
11254                                         CHECK_STACK (1);
11255                                         --sp;
11256                                 }
11257                                 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
11258                                         UNVERIFIED;
11259                                 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
11260                                         UNVERIFIED;
11261                         } else {
11262                                 if (op == CEE_STSFLD) {
11263                                         CHECK_STACK (1);
11264                                         sp--;
11265                                         store_val = sp [0];
11266                                 }
11267                         }
11268
11269                         CHECK_OPSIZE (5);
11270                         token = read32 (ip + 1);
11271                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
11272                                 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
11273                                 klass = field->parent;
11274                         }
11275                         else {
11276                                 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
11277                                 CHECK_CFG_ERROR;
11278                         }
11279                         if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
11280                                 FIELD_ACCESS_FAILURE (method, field);
11281                         mono_class_init (klass);
11282
11283                         /* if the class is Critical then transparent code cannot access it's fields */
11284                         if (!is_instance && mono_security_core_clr_enabled ())
11285                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
11286
11287                         /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
11288                            any visible *instance* field  (in fact there's a single case for a static field in Marshal) XXX
11289                         if (mono_security_core_clr_enabled ())
11290                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
11291                         */
11292
11293                         ftype = mono_field_get_type (field);
11294
11295                         /*
11296                          * LDFLD etc. is usable on static fields as well, so convert those cases to
11297                          * the static case.
11298                          */
11299                         if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
11300                                 switch (op) {
11301                                 case CEE_LDFLD:
11302                                         op = CEE_LDSFLD;
11303                                         break;
11304                                 case CEE_STFLD:
11305                                         op = CEE_STSFLD;
11306                                         break;
11307                                 case CEE_LDFLDA:
11308                                         op = CEE_LDSFLDA;
11309                                         break;
11310                                 default:
11311                                         g_assert_not_reached ();
11312                                 }
11313                                 is_instance = FALSE;
11314                         }
11315
11316                         context_used = mini_class_check_context_used (cfg, klass);
11317
11318                         /* INSTANCE CASE */
11319
11320                         foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
11321                         if (op == CEE_STFLD) {
11322                                 if (target_type_is_incompatible (cfg, field->type, sp [1]))
11323                                         UNVERIFIED;
11324 #ifndef DISABLE_REMOTING
11325                                 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
11326                                         MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
11327                                         MonoInst *iargs [5];
11328
11329                                         GSHAREDVT_FAILURE (op);
11330
11331                                         iargs [0] = sp [0];
11332                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11333                                         EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11334                                         EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : 
11335                                                     field->offset);
11336                                         iargs [4] = sp [1];
11337
11338                                         if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11339                                                 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), 
11340                                                                                            iargs, ip, cfg->real_offset, TRUE);
11341                                                 CHECK_CFG_EXCEPTION;
11342                                                 g_assert (costs > 0);
11343                                                       
11344                                                 cfg->real_offset += 5;
11345
11346                                                 inline_costs += costs;
11347                                         } else {
11348                                                 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
11349                                         }
11350                                 } else
11351 #endif
11352                                 {
11353                                         MonoInst *store;
11354
11355                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11356
11357                                         if (mini_is_gsharedvt_klass (klass)) {
11358                                                 MonoInst *offset_ins;
11359
11360                                                 context_used = mini_class_check_context_used (cfg, klass);
11361
11362                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11363                                                 /* The value is offset by 1 */
11364                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11365                                                 dreg = alloc_ireg_mp (cfg);
11366                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11367                                                 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
11368                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
11369                                         } else {
11370                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
11371                                         }
11372                                         if (sp [0]->opcode != OP_LDADDR)
11373                                                 store->flags |= MONO_INST_FAULT;
11374
11375                                 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)) {
11376                                         /* insert call to write barrier */
11377                                         MonoInst *ptr;
11378                                         int dreg;
11379
11380                                         dreg = alloc_ireg_mp (cfg);
11381                                         EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11382                                         emit_write_barrier (cfg, ptr, sp [1]);
11383                                 }
11384
11385                                         store->flags |= ins_flag;
11386                                 }
11387                                 ins_flag = 0;
11388                                 ip += 5;
11389                                 break;
11390                         }
11391
11392 #ifndef DISABLE_REMOTING
11393                         if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
11394                                 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type); 
11395                                 MonoInst *iargs [4];
11396
11397                                 GSHAREDVT_FAILURE (op);
11398
11399                                 iargs [0] = sp [0];
11400                                 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11401                                 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11402                                 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
11403                                 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11404                                         costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), 
11405                                                                                    iargs, ip, cfg->real_offset, TRUE);
11406                                         CHECK_CFG_EXCEPTION;
11407                                         g_assert (costs > 0);
11408                                                       
11409                                         cfg->real_offset += 5;
11410
11411                                         *sp++ = iargs [0];
11412
11413                                         inline_costs += costs;
11414                                 } else {
11415                                         ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
11416                                         *sp++ = ins;
11417                                 }
11418                         } else 
11419 #endif
11420                         if (is_instance) {
11421                                 if (sp [0]->type == STACK_VTYPE) {
11422                                         MonoInst *var;
11423
11424                                         /* Have to compute the address of the variable */
11425
11426                                         var = get_vreg_to_inst (cfg, sp [0]->dreg);
11427                                         if (!var)
11428                                                 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
11429                                         else
11430                                                 g_assert (var->klass == klass);
11431                                         
11432                                         EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
11433                                         sp [0] = ins;
11434                                 }
11435
11436                                 if (op == CEE_LDFLDA) {
11437                                         if (sp [0]->type == STACK_OBJ) {
11438                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
11439                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
11440                                         }
11441
11442                                         dreg = alloc_ireg_mp (cfg);
11443
11444                                         if (mini_is_gsharedvt_klass (klass)) {
11445                                                 MonoInst *offset_ins;
11446
11447                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11448                                                 /* The value is offset by 1 */
11449                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11450                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11451                                         } else {
11452                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11453                                         }
11454                                         ins->klass = mono_class_from_mono_type (field->type);
11455                                         ins->type = STACK_MP;
11456                                         *sp++ = ins;
11457                                 } else {
11458                                         MonoInst *load;
11459
11460                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11461
11462                                         if (mini_is_gsharedvt_klass (klass)) {
11463                                                 MonoInst *offset_ins;
11464
11465                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11466                                                 /* The value is offset by 1 */
11467                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11468                                                 dreg = alloc_ireg_mp (cfg);
11469                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11470                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
11471                                         } else {
11472                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
11473                                         }
11474                                         load->flags |= ins_flag;
11475                                         if (sp [0]->opcode != OP_LDADDR)
11476                                                 load->flags |= MONO_INST_FAULT;
11477                                         *sp++ = load;
11478                                 }
11479                         }
11480
11481                         if (is_instance) {
11482                                 ins_flag = 0;
11483                                 ip += 5;
11484                                 break;
11485                         }
11486
11487                         /* STATIC CASE */
11488                         context_used = mini_class_check_context_used (cfg, klass);
11489
11490                         if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL)
11491                                 UNVERIFIED;
11492
11493                         /* The special_static_fields field is init'd in mono_class_vtable, so it needs
11494                          * to be called here.
11495                          */
11496                         if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
11497                                 mono_class_vtable (cfg->domain, klass);
11498                                 CHECK_TYPELOAD (klass);
11499                         }
11500                         mono_domain_lock (cfg->domain);
11501                         if (cfg->domain->special_static_fields)
11502                                 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
11503                         mono_domain_unlock (cfg->domain);
11504
11505                         is_special_static = mono_class_field_is_special_static (field);
11506
11507                         if (is_special_static && ((gsize)addr & 0x80000000) == 0)
11508                                 thread_ins = mono_get_thread_intrinsic (cfg);
11509                         else
11510                                 thread_ins = NULL;
11511
11512                         /* Generate IR to compute the field address */
11513                         if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
11514                                 /*
11515                                  * Fast access to TLS data
11516                                  * Inline version of get_thread_static_data () in
11517                                  * threads.c.
11518                                  */
11519                                 guint32 offset;
11520                                 int idx, static_data_reg, array_reg, dreg;
11521
11522                                 GSHAREDVT_FAILURE (op);
11523
11524                                 MONO_ADD_INS (cfg->cbb, thread_ins);
11525                                 static_data_reg = alloc_ireg (cfg);
11526                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
11527
11528                                 if (cfg->compile_aot) {
11529                                         int offset_reg, offset2_reg, idx_reg;
11530
11531                                         /* For TLS variables, this will return the TLS offset */
11532                                         EMIT_NEW_SFLDACONST (cfg, ins, field);
11533                                         offset_reg = ins->dreg;
11534                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
11535                                         idx_reg = alloc_ireg (cfg);
11536                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
11537                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
11538                                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
11539                                         array_reg = alloc_ireg (cfg);
11540                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
11541                                         offset2_reg = alloc_ireg (cfg);
11542                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
11543                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
11544                                         dreg = alloc_ireg (cfg);
11545                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
11546                                 } else {
11547                                         offset = (gsize)addr & 0x7fffffff;
11548                                         idx = offset & 0x3f;
11549
11550                                         array_reg = alloc_ireg (cfg);
11551                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
11552                                         dreg = alloc_ireg (cfg);
11553                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
11554                                 }
11555                         } else if ((cfg->opt & MONO_OPT_SHARED) ||
11556                                         (cfg->compile_aot && is_special_static) ||
11557                                         (context_used && is_special_static)) {
11558                                 MonoInst *iargs [2];
11559
11560                                 g_assert (field->parent);
11561                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11562                                 if (context_used) {
11563                                         iargs [1] = emit_get_rgctx_field (cfg, context_used,
11564                                                 field, MONO_RGCTX_INFO_CLASS_FIELD);
11565                                 } else {
11566                                         EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11567                                 }
11568                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11569                         } else if (context_used) {
11570                                 MonoInst *static_data;
11571
11572                                 /*
11573                                 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
11574                                         method->klass->name_space, method->klass->name, method->name,
11575                                         depth, field->offset);
11576                                 */
11577
11578                                 if (mono_class_needs_cctor_run (klass, method))
11579                                         emit_class_init (cfg, klass);
11580
11581                                 /*
11582                                  * The pointer we're computing here is
11583                                  *
11584                                  *   super_info.static_data + field->offset
11585                                  */
11586                                 static_data = emit_get_rgctx_klass (cfg, context_used,
11587                                         klass, MONO_RGCTX_INFO_STATIC_DATA);
11588
11589                                 if (mini_is_gsharedvt_klass (klass)) {
11590                                         MonoInst *offset_ins;
11591
11592                                         offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11593                                         /* The value is offset by 1 */
11594                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11595                                         dreg = alloc_ireg_mp (cfg);
11596                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
11597                                 } else if (field->offset == 0) {
11598                                         ins = static_data;
11599                                 } else {
11600                                         int addr_reg = mono_alloc_preg (cfg);
11601                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
11602                                 }
11603                                 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
11604                                 MonoInst *iargs [2];
11605
11606                                 g_assert (field->parent);
11607                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11608                                 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11609                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11610                         } else {
11611                                 MonoVTable *vtable = NULL;
11612
11613                                 if (!cfg->compile_aot)
11614                                         vtable = mono_class_vtable (cfg->domain, klass);
11615                                 CHECK_TYPELOAD (klass);
11616
11617                                 if (!addr) {
11618                                         if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11619                                                 if (!(g_slist_find (class_inits, klass))) {
11620                                                         emit_class_init (cfg, klass);
11621                                                         if (cfg->verbose_level > 2)
11622                                                                 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11623                                                         class_inits = g_slist_prepend (class_inits, klass);
11624                                                 }
11625                                         } else {
11626                                                 if (cfg->run_cctors) {
11627                                                         MonoException *ex;
11628                                                         /* This makes so that inline cannot trigger */
11629                                                         /* .cctors: too many apps depend on them */
11630                                                         /* running with a specific order... */
11631                                                         g_assert (vtable);
11632                                                         if (! vtable->initialized)
11633                                                                 INLINE_FAILURE ("class init");
11634                                                         ex = mono_runtime_class_init_full (vtable, FALSE);
11635                                                         if (ex) {
11636                                                                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
11637                                                                 mono_error_set_exception_instance (&cfg->error, ex);
11638                                                                 g_assert_not_reached ();
11639                                                                 goto exception_exit;
11640                                                         }
11641                                                 }
11642                                         }
11643                                         if (cfg->compile_aot)
11644                                                 EMIT_NEW_SFLDACONST (cfg, ins, field);
11645                                         else {
11646                                                 g_assert (vtable);
11647                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11648                                                 g_assert (addr);
11649                                                 EMIT_NEW_PCONST (cfg, ins, addr);
11650                                         }
11651                                 } else {
11652                                         MonoInst *iargs [1];
11653                                         EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11654                                         ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11655                                 }
11656                         }
11657
11658                         /* Generate IR to do the actual load/store operation */
11659
11660                         if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11661                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11662                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11663                         }
11664
11665                         if (op == CEE_LDSFLDA) {
11666                                 ins->klass = mono_class_from_mono_type (ftype);
11667                                 ins->type = STACK_PTR;
11668                                 *sp++ = ins;
11669                         } else if (op == CEE_STSFLD) {
11670                                 MonoInst *store;
11671
11672                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11673                                 store->flags |= ins_flag;
11674                         } else {
11675                                 gboolean is_const = FALSE;
11676                                 MonoVTable *vtable = NULL;
11677                                 gpointer addr = NULL;
11678
11679                                 if (!context_used) {
11680                                         vtable = mono_class_vtable (cfg->domain, klass);
11681                                         CHECK_TYPELOAD (klass);
11682                                 }
11683                                 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11684                                                 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11685                                         int ro_type = ftype->type;
11686                                         if (!addr)
11687                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11688                                         if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11689                                                 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11690                                         }
11691
11692                                         GSHAREDVT_FAILURE (op);
11693
11694                                         /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11695                                         is_const = TRUE;
11696                                         switch (ro_type) {
11697                                         case MONO_TYPE_BOOLEAN:
11698                                         case MONO_TYPE_U1:
11699                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11700                                                 sp++;
11701                                                 break;
11702                                         case MONO_TYPE_I1:
11703                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11704                                                 sp++;
11705                                                 break;                                          
11706                                         case MONO_TYPE_CHAR:
11707                                         case MONO_TYPE_U2:
11708                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11709                                                 sp++;
11710                                                 break;
11711                                         case MONO_TYPE_I2:
11712                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11713                                                 sp++;
11714                                                 break;
11715                                                 break;
11716                                         case MONO_TYPE_I4:
11717                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11718                                                 sp++;
11719                                                 break;                                          
11720                                         case MONO_TYPE_U4:
11721                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11722                                                 sp++;
11723                                                 break;
11724                                         case MONO_TYPE_I:
11725                                         case MONO_TYPE_U:
11726                                         case MONO_TYPE_PTR:
11727                                         case MONO_TYPE_FNPTR:
11728                                                 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11729                                                 type_to_eval_stack_type ((cfg), field->type, *sp);
11730                                                 sp++;
11731                                                 break;
11732                                         case MONO_TYPE_STRING:
11733                                         case MONO_TYPE_OBJECT:
11734                                         case MONO_TYPE_CLASS:
11735                                         case MONO_TYPE_SZARRAY:
11736                                         case MONO_TYPE_ARRAY:
11737                                                 if (!mono_gc_is_moving ()) {
11738                                                         EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11739                                                         type_to_eval_stack_type ((cfg), field->type, *sp);
11740                                                         sp++;
11741                                                 } else {
11742                                                         is_const = FALSE;
11743                                                 }
11744                                                 break;
11745                                         case MONO_TYPE_I8:
11746                                         case MONO_TYPE_U8:
11747                                                 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11748                                                 sp++;
11749                                                 break;
11750                                         case MONO_TYPE_R4:
11751                                         case MONO_TYPE_R8:
11752                                         case MONO_TYPE_VALUETYPE:
11753                                         default:
11754                                                 is_const = FALSE;
11755                                                 break;
11756                                         }
11757                                 }
11758
11759                                 if (!is_const) {
11760                                         MonoInst *load;
11761
11762                                         CHECK_STACK_OVF (1);
11763
11764                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11765                                         load->flags |= ins_flag;
11766                                         ins_flag = 0;
11767                                         *sp++ = load;
11768                                 }
11769                         }
11770
11771                         if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11772                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11773                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11774                         }
11775
11776                         ins_flag = 0;
11777                         ip += 5;
11778                         break;
11779                 }
11780                 case CEE_STOBJ:
11781                         CHECK_STACK (2);
11782                         sp -= 2;
11783                         CHECK_OPSIZE (5);
11784                         token = read32 (ip + 1);
11785                         klass = mini_get_class (method, token, generic_context);
11786                         CHECK_TYPELOAD (klass);
11787                         if (ins_flag & MONO_INST_VOLATILE) {
11788                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11789                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11790                         }
11791                         /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11792                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11793                         ins->flags |= ins_flag;
11794                         if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11795                                         generic_class_is_reference_type (cfg, klass)) {
11796                                 /* insert call to write barrier */
11797                                 emit_write_barrier (cfg, sp [0], sp [1]);
11798                         }
11799                         ins_flag = 0;
11800                         ip += 5;
11801                         inline_costs += 1;
11802                         break;
11803
11804                         /*
11805                          * Array opcodes
11806                          */
11807                 case CEE_NEWARR: {
11808                         MonoInst *len_ins;
11809                         const char *data_ptr;
11810                         int data_size = 0;
11811                         guint32 field_token;
11812
11813                         CHECK_STACK (1);
11814                         --sp;
11815
11816                         CHECK_OPSIZE (5);
11817                         token = read32 (ip + 1);
11818
11819                         klass = mini_get_class (method, token, generic_context);
11820                         CHECK_TYPELOAD (klass);
11821
11822                         context_used = mini_class_check_context_used (cfg, klass);
11823
11824                         if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11825                                 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11826                                 ins->sreg1 = sp [0]->dreg;
11827                                 ins->type = STACK_I4;
11828                                 ins->dreg = alloc_ireg (cfg);
11829                                 MONO_ADD_INS (cfg->cbb, ins);
11830                                 *sp = mono_decompose_opcode (cfg, ins);
11831                         }
11832
11833                         if (context_used) {
11834                                 MonoInst *args [3];
11835                                 MonoClass *array_class = mono_array_class_get (klass, 1);
11836                                 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11837
11838                                 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11839
11840                                 /* vtable */
11841                                 args [0] = emit_get_rgctx_klass (cfg, context_used,
11842                                         array_class, MONO_RGCTX_INFO_VTABLE);
11843                                 /* array len */
11844                                 args [1] = sp [0];
11845
11846                                 if (managed_alloc)
11847                                         ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11848                                 else
11849                                         ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11850                         } else {
11851                                 if (cfg->opt & MONO_OPT_SHARED) {
11852                                         /* Decompose now to avoid problems with references to the domainvar */
11853                                         MonoInst *iargs [3];
11854
11855                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11856                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11857                                         iargs [2] = sp [0];
11858
11859                                         ins = mono_emit_jit_icall (cfg, mono_array_new, iargs);
11860                                 } else {
11861                                         /* Decompose later since it is needed by abcrem */
11862                                         MonoClass *array_type = mono_array_class_get (klass, 1);
11863                                         mono_class_vtable (cfg->domain, array_type);
11864                                         CHECK_TYPELOAD (array_type);
11865
11866                                         MONO_INST_NEW (cfg, ins, OP_NEWARR);
11867                                         ins->dreg = alloc_ireg_ref (cfg);
11868                                         ins->sreg1 = sp [0]->dreg;
11869                                         ins->inst_newa_class = klass;
11870                                         ins->type = STACK_OBJ;
11871                                         ins->klass = array_type;
11872                                         MONO_ADD_INS (cfg->cbb, ins);
11873                                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11874                                         cfg->cbb->has_array_access = TRUE;
11875
11876                                         /* Needed so mono_emit_load_get_addr () gets called */
11877                                         mono_get_got_var (cfg);
11878                                 }
11879                         }
11880
11881                         len_ins = sp [0];
11882                         ip += 5;
11883                         *sp++ = ins;
11884                         inline_costs += 1;
11885
11886                         /* 
11887                          * we inline/optimize the initialization sequence if possible.
11888                          * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11889                          * for small sizes open code the memcpy
11890                          * ensure the rva field is big enough
11891                          */
11892                         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))) {
11893                                 MonoMethod *memcpy_method = get_memcpy_method ();
11894                                 MonoInst *iargs [3];
11895                                 int add_reg = alloc_ireg_mp (cfg);
11896
11897                                 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11898                                 if (cfg->compile_aot) {
11899                                         EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11900                                 } else {
11901                                         EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11902                                 }
11903                                 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11904                                 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11905                                 ip += 11;
11906                         }
11907
11908                         break;
11909                 }
11910                 case CEE_LDLEN:
11911                         CHECK_STACK (1);
11912                         --sp;
11913                         if (sp [0]->type != STACK_OBJ)
11914                                 UNVERIFIED;
11915
11916                         MONO_INST_NEW (cfg, ins, OP_LDLEN);
11917                         ins->dreg = alloc_preg (cfg);
11918                         ins->sreg1 = sp [0]->dreg;
11919                         ins->type = STACK_I4;
11920                         /* This flag will be inherited by the decomposition */
11921                         ins->flags |= MONO_INST_FAULT;
11922                         MONO_ADD_INS (cfg->cbb, ins);
11923                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11924                         cfg->cbb->has_array_access = TRUE;
11925                         ip ++;
11926                         *sp++ = ins;
11927                         break;
11928                 case CEE_LDELEMA:
11929                         CHECK_STACK (2);
11930                         sp -= 2;
11931                         CHECK_OPSIZE (5);
11932                         if (sp [0]->type != STACK_OBJ)
11933                                 UNVERIFIED;
11934
11935                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11936
11937                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11938                         CHECK_TYPELOAD (klass);
11939                         /* we need to make sure that this array is exactly the type it needs
11940                          * to be for correctness. the wrappers are lax with their usage
11941                          * so we need to ignore them here
11942                          */
11943                         if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11944                                 MonoClass *array_class = mono_array_class_get (klass, 1);
11945                                 mini_emit_check_array_type (cfg, sp [0], array_class);
11946                                 CHECK_TYPELOAD (array_class);
11947                         }
11948
11949                         readonly = FALSE;
11950                         ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11951                         *sp++ = ins;
11952                         ip += 5;
11953                         break;
11954                 case CEE_LDELEM:
11955                 case CEE_LDELEM_I1:
11956                 case CEE_LDELEM_U1:
11957                 case CEE_LDELEM_I2:
11958                 case CEE_LDELEM_U2:
11959                 case CEE_LDELEM_I4:
11960                 case CEE_LDELEM_U4:
11961                 case CEE_LDELEM_I8:
11962                 case CEE_LDELEM_I:
11963                 case CEE_LDELEM_R4:
11964                 case CEE_LDELEM_R8:
11965                 case CEE_LDELEM_REF: {
11966                         MonoInst *addr;
11967
11968                         CHECK_STACK (2);
11969                         sp -= 2;
11970
11971                         if (*ip == CEE_LDELEM) {
11972                                 CHECK_OPSIZE (5);
11973                                 token = read32 (ip + 1);
11974                                 klass = mini_get_class (method, token, generic_context);
11975                                 CHECK_TYPELOAD (klass);
11976                                 mono_class_init (klass);
11977                         }
11978                         else
11979                                 klass = array_access_to_klass (*ip);
11980
11981                         if (sp [0]->type != STACK_OBJ)
11982                                 UNVERIFIED;
11983
11984                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11985
11986                         if (mini_is_gsharedvt_variable_klass (klass)) {
11987                                 // FIXME-VT: OP_ICONST optimization
11988                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11989                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11990                                 ins->opcode = OP_LOADV_MEMBASE;
11991                         } else if (sp [1]->opcode == OP_ICONST) {
11992                                 int array_reg = sp [0]->dreg;
11993                                 int index_reg = sp [1]->dreg;
11994                                 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11995
11996                                 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
11997                                         MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
11998
11999                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
12000                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
12001                         } else {
12002                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12003                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12004                         }
12005                         *sp++ = ins;
12006                         if (*ip == CEE_LDELEM)
12007                                 ip += 5;
12008                         else
12009                                 ++ip;
12010                         break;
12011                 }
12012                 case CEE_STELEM_I:
12013                 case CEE_STELEM_I1:
12014                 case CEE_STELEM_I2:
12015                 case CEE_STELEM_I4:
12016                 case CEE_STELEM_I8:
12017                 case CEE_STELEM_R4:
12018                 case CEE_STELEM_R8:
12019                 case CEE_STELEM_REF:
12020                 case CEE_STELEM: {
12021                         CHECK_STACK (3);
12022                         sp -= 3;
12023
12024                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
12025
12026                         if (*ip == CEE_STELEM) {
12027                                 CHECK_OPSIZE (5);
12028                                 token = read32 (ip + 1);
12029                                 klass = mini_get_class (method, token, generic_context);
12030                                 CHECK_TYPELOAD (klass);
12031                                 mono_class_init (klass);
12032                         }
12033                         else
12034                                 klass = array_access_to_klass (*ip);
12035
12036                         if (sp [0]->type != STACK_OBJ)
12037                                 UNVERIFIED;
12038
12039                         emit_array_store (cfg, klass, sp, TRUE);
12040
12041                         if (*ip == CEE_STELEM)
12042                                 ip += 5;
12043                         else
12044                                 ++ip;
12045                         inline_costs += 1;
12046                         break;
12047                 }
12048                 case CEE_CKFINITE: {
12049                         CHECK_STACK (1);
12050                         --sp;
12051
12052                         if (cfg->llvm_only) {
12053                                 MonoInst *iargs [1];
12054
12055                                 iargs [0] = sp [0];
12056                                 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
12057                         } else  {
12058                                 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
12059                                 ins->sreg1 = sp [0]->dreg;
12060                                 ins->dreg = alloc_freg (cfg);
12061                                 ins->type = STACK_R8;
12062                                 MONO_ADD_INS (cfg->cbb, ins);
12063
12064                                 *sp++ = mono_decompose_opcode (cfg, ins);
12065                         }
12066
12067                         ++ip;
12068                         break;
12069                 }
12070                 case CEE_REFANYVAL: {
12071                         MonoInst *src_var, *src;
12072
12073                         int klass_reg = alloc_preg (cfg);
12074                         int dreg = alloc_preg (cfg);
12075
12076                         GSHAREDVT_FAILURE (*ip);
12077
12078                         CHECK_STACK (1);
12079                         MONO_INST_NEW (cfg, ins, *ip);
12080                         --sp;
12081                         CHECK_OPSIZE (5);
12082                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
12083                         CHECK_TYPELOAD (klass);
12084
12085                         context_used = mini_class_check_context_used (cfg, klass);
12086
12087                         // FIXME:
12088                         src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12089                         if (!src_var)
12090                                 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12091                         EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12092                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
12093
12094                         if (context_used) {
12095                                 MonoInst *klass_ins;
12096
12097                                 klass_ins = emit_get_rgctx_klass (cfg, context_used,
12098                                                 klass, MONO_RGCTX_INFO_KLASS);
12099
12100                                 // FIXME:
12101                                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
12102                                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
12103                         } else {
12104                                 mini_emit_class_check (cfg, klass_reg, klass);
12105                         }
12106                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
12107                         ins->type = STACK_MP;
12108                         ins->klass = klass;
12109                         *sp++ = ins;
12110                         ip += 5;
12111                         break;
12112                 }
12113                 case CEE_MKREFANY: {
12114                         MonoInst *loc, *addr;
12115
12116                         GSHAREDVT_FAILURE (*ip);
12117
12118                         CHECK_STACK (1);
12119                         MONO_INST_NEW (cfg, ins, *ip);
12120                         --sp;
12121                         CHECK_OPSIZE (5);
12122                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
12123                         CHECK_TYPELOAD (klass);
12124
12125                         context_used = mini_class_check_context_used (cfg, klass);
12126
12127                         loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
12128                         EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
12129
12130                         if (context_used) {
12131                                 MonoInst *const_ins;
12132                                 int type_reg = alloc_preg (cfg);
12133
12134                                 const_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
12135                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
12136                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12137                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12138                         } else if (cfg->compile_aot) {
12139                                 int const_reg = alloc_preg (cfg);
12140                                 int type_reg = alloc_preg (cfg);
12141
12142                                 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
12143                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
12144                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12145                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12146                         } else {
12147                                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), &klass->byval_arg);
12148                                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), klass);
12149                         }
12150                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
12151
12152                         EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
12153                         ins->type = STACK_VTYPE;
12154                         ins->klass = mono_defaults.typed_reference_class;
12155                         *sp++ = ins;
12156                         ip += 5;
12157                         break;
12158                 }
12159                 case CEE_LDTOKEN: {
12160                         gpointer handle;
12161                         MonoClass *handle_class;
12162
12163                         CHECK_STACK_OVF (1);
12164
12165                         CHECK_OPSIZE (5);
12166                         n = read32 (ip + 1);
12167
12168                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
12169                                         method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
12170                                 handle = mono_method_get_wrapper_data (method, n);
12171                                 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
12172                                 if (handle_class == mono_defaults.typehandle_class)
12173                                         handle = &((MonoClass*)handle)->byval_arg;
12174                         }
12175                         else {
12176                                 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
12177                                 CHECK_CFG_ERROR;
12178                         }
12179                         if (!handle)
12180                                 LOAD_ERROR;
12181                         mono_class_init (handle_class);
12182                         if (cfg->gshared) {
12183                                 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
12184                                                 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
12185                                         /* This case handles ldtoken
12186                                            of an open type, like for
12187                                            typeof(Gen<>). */
12188                                         context_used = 0;
12189                                 } else if (handle_class == mono_defaults.typehandle_class) {
12190                                         context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
12191                                 } else if (handle_class == mono_defaults.fieldhandle_class)
12192                                         context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
12193                                 else if (handle_class == mono_defaults.methodhandle_class)
12194                                         context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
12195                                 else
12196                                         g_assert_not_reached ();
12197                         }
12198
12199                         if ((cfg->opt & MONO_OPT_SHARED) &&
12200                                         method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
12201                                         method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
12202                                 MonoInst *addr, *vtvar, *iargs [3];
12203                                 int method_context_used;
12204
12205                                 method_context_used = mini_method_check_context_used (cfg, method);
12206
12207                                 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL); 
12208
12209                                 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
12210                                 EMIT_NEW_ICONST (cfg, iargs [1], n);
12211                                 if (method_context_used) {
12212                                         iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
12213                                                 method, MONO_RGCTX_INFO_METHOD);
12214                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
12215                                 } else {
12216                                         EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
12217                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
12218                                 }
12219                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12220
12221                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12222
12223                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12224                         } else {
12225                                 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) && 
12226                                         ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) && 
12227                                         (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
12228                                         (cmethod->klass == mono_defaults.systemtype_class) &&
12229                                         (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
12230                                         MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
12231
12232                                         mono_class_init (tclass);
12233                                         if (context_used) {
12234                                                 ins = emit_get_rgctx_klass (cfg, context_used,
12235                                                         tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
12236                                         } else if (cfg->compile_aot) {
12237                                                 if (method->wrapper_type) {
12238                                                         mono_error_init (&error); //got to do it since there are multiple conditionals below
12239                                                         if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
12240                                                                 /* Special case for static synchronized wrappers */
12241                                                                 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
12242                                                         } else {
12243                                                                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
12244                                                                 /* FIXME: n is not a normal token */
12245                                                                 DISABLE_AOT (cfg);
12246                                                                 EMIT_NEW_PCONST (cfg, ins, NULL);
12247                                                         }
12248                                                 } else {
12249                                                         EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
12250                                                 }
12251                                         } else {
12252                                                 MonoError error;
12253                                                 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &error);
12254                                                 mono_error_raise_exception (&error); /* FIXME don't raise here */
12255
12256                                                 EMIT_NEW_PCONST (cfg, ins, rt);
12257                                         }
12258                                         ins->type = STACK_OBJ;
12259                                         ins->klass = cmethod->klass;
12260                                         ip += 5;
12261                                 } else {
12262                                         MonoInst *addr, *vtvar;
12263
12264                                         vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12265
12266                                         if (context_used) {
12267                                                 if (handle_class == mono_defaults.typehandle_class) {
12268                                                         ins = emit_get_rgctx_klass (cfg, context_used,
12269                                                                         mono_class_from_mono_type ((MonoType *)handle),
12270                                                                         MONO_RGCTX_INFO_TYPE);
12271                                                 } else if (handle_class == mono_defaults.methodhandle_class) {
12272                                                         ins = emit_get_rgctx_method (cfg, context_used,
12273                                                                         (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
12274                                                 } else if (handle_class == mono_defaults.fieldhandle_class) {
12275                                                         ins = emit_get_rgctx_field (cfg, context_used,
12276                                                                         (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
12277                                                 } else {
12278                                                         g_assert_not_reached ();
12279                                                 }
12280                                         } else if (cfg->compile_aot) {
12281                                                 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
12282                                         } else {
12283                                                 EMIT_NEW_PCONST (cfg, ins, handle);
12284                                         }
12285                                         EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12286                                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12287                                         EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12288                                 }
12289                         }
12290
12291                         *sp++ = ins;
12292                         ip += 5;
12293                         break;
12294                 }
12295                 case CEE_THROW:
12296                         CHECK_STACK (1);
12297                         MONO_INST_NEW (cfg, ins, OP_THROW);
12298                         --sp;
12299                         ins->sreg1 = sp [0]->dreg;
12300                         ip++;
12301                         cfg->cbb->out_of_line = TRUE;
12302                         MONO_ADD_INS (cfg->cbb, ins);
12303                         MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12304                         MONO_ADD_INS (cfg->cbb, ins);
12305                         sp = stack_start;
12306                         
12307                         link_bblock (cfg, cfg->cbb, end_bblock);
12308                         start_new_bblock = 1;
12309                         /* This can complicate code generation for llvm since the return value might not be defined */
12310                         if (COMPILE_LLVM (cfg))
12311                                 INLINE_FAILURE ("throw");
12312                         break;
12313                 case CEE_ENDFINALLY:
12314                         /* mono_save_seq_point_info () depends on this */
12315                         if (sp != stack_start)
12316                                 emit_seq_point (cfg, method, ip, FALSE, FALSE);
12317                         MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
12318                         MONO_ADD_INS (cfg->cbb, ins);
12319                         ip++;
12320                         start_new_bblock = 1;
12321
12322                         /*
12323                          * Control will leave the method so empty the stack, otherwise
12324                          * the next basic block will start with a nonempty stack.
12325                          */
12326                         while (sp != stack_start) {
12327                                 sp--;
12328                         }
12329                         break;
12330                 case CEE_LEAVE:
12331                 case CEE_LEAVE_S: {
12332                         GList *handlers;
12333
12334                         if (*ip == CEE_LEAVE) {
12335                                 CHECK_OPSIZE (5);
12336                                 target = ip + 5 + (gint32)read32(ip + 1);
12337                         } else {
12338                                 CHECK_OPSIZE (2);
12339                                 target = ip + 2 + (signed char)(ip [1]);
12340                         }
12341
12342                         /* empty the stack */
12343                         while (sp != stack_start) {
12344                                 sp--;
12345                         }
12346
12347                         /* 
12348                          * If this leave statement is in a catch block, check for a
12349                          * pending exception, and rethrow it if necessary.
12350                          * We avoid doing this in runtime invoke wrappers, since those are called
12351                          * by native code which excepts the wrapper to catch all exceptions.
12352                          */
12353                         for (i = 0; i < header->num_clauses; ++i) {
12354                                 MonoExceptionClause *clause = &header->clauses [i];
12355
12356                                 /* 
12357                                  * Use <= in the final comparison to handle clauses with multiple
12358                                  * leave statements, like in bug #78024.
12359                                  * The ordering of the exception clauses guarantees that we find the
12360                                  * innermost clause.
12361                                  */
12362                                 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) {
12363                                         MonoInst *exc_ins;
12364                                         MonoBasicBlock *dont_throw;
12365
12366                                         /*
12367                                           MonoInst *load;
12368
12369                                           NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
12370                                         */
12371
12372                                         exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
12373
12374                                         NEW_BBLOCK (cfg, dont_throw);
12375
12376                                         /*
12377                                          * Currently, we always rethrow the abort exception, despite the 
12378                                          * fact that this is not correct. See thread6.cs for an example. 
12379                                          * But propagating the abort exception is more important than 
12380                                          * getting the sematics right.
12381                                          */
12382                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
12383                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
12384                                         MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
12385
12386                                         MONO_START_BB (cfg, dont_throw);
12387                                 }
12388                         }
12389
12390 #ifdef ENABLE_LLVM
12391                         cfg->cbb->try_end = (intptr_t)(ip - header->code);
12392 #endif
12393
12394                         if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
12395                                 GList *tmp;
12396                                 MonoExceptionClause *clause;
12397
12398                                 for (tmp = handlers; tmp; tmp = tmp->next) {
12399                                         clause = (MonoExceptionClause *)tmp->data;
12400                                         tblock = cfg->cil_offset_to_bb [clause->handler_offset];
12401                                         g_assert (tblock);
12402                                         link_bblock (cfg, cfg->cbb, tblock);
12403                                         MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
12404                                         ins->inst_target_bb = tblock;
12405                                         ins->inst_eh_block = clause;
12406                                         MONO_ADD_INS (cfg->cbb, ins);
12407                                         cfg->cbb->has_call_handler = 1;
12408                                         if (COMPILE_LLVM (cfg)) {
12409                                                 MonoBasicBlock *target_bb;
12410
12411                                                 /* 
12412                                                  * Link the finally bblock with the target, since it will
12413                                                  * conceptually branch there.
12414                                                  */
12415                                                 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
12416                                                 GET_BBLOCK (cfg, target_bb, target);
12417                                                 link_bblock (cfg, tblock, target_bb);
12418                                         }
12419                                 }
12420                                 g_list_free (handlers);
12421                         } 
12422
12423                         MONO_INST_NEW (cfg, ins, OP_BR);
12424                         MONO_ADD_INS (cfg->cbb, ins);
12425                         GET_BBLOCK (cfg, tblock, target);
12426                         link_bblock (cfg, cfg->cbb, tblock);
12427                         ins->inst_target_bb = tblock;
12428
12429                         start_new_bblock = 1;
12430
12431                         if (*ip == CEE_LEAVE)
12432                                 ip += 5;
12433                         else
12434                                 ip += 2;
12435
12436                         break;
12437                 }
12438
12439                         /*
12440                          * Mono specific opcodes
12441                          */
12442                 case MONO_CUSTOM_PREFIX: {
12443
12444                         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
12445
12446                         CHECK_OPSIZE (2);
12447                         switch (ip [1]) {
12448                         case CEE_MONO_ICALL: {
12449                                 gpointer func;
12450                                 MonoJitICallInfo *info;
12451
12452                                 token = read32 (ip + 2);
12453                                 func = mono_method_get_wrapper_data (method, token);
12454                                 info = mono_find_jit_icall_by_addr (func);
12455                                 if (!info)
12456                                         g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
12457                                 g_assert (info);
12458
12459                                 CHECK_STACK (info->sig->param_count);
12460                                 sp -= info->sig->param_count;
12461
12462                                 ins = mono_emit_jit_icall (cfg, info->func, sp);
12463                                 if (!MONO_TYPE_IS_VOID (info->sig->ret))
12464                                         *sp++ = ins;
12465
12466                                 ip += 6;
12467                                 inline_costs += 10 * num_calls++;
12468
12469                                 break;
12470                         }
12471                         case CEE_MONO_LDPTR_CARD_TABLE:
12472                         case CEE_MONO_LDPTR_NURSERY_START:
12473                         case CEE_MONO_LDPTR_NURSERY_BITS:
12474                         case CEE_MONO_LDPTR_INT_REQ_FLAG: {
12475                                 CHECK_STACK_OVF (1);
12476
12477                                 switch (ip [1]) {
12478                                         case CEE_MONO_LDPTR_CARD_TABLE:
12479                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
12480                                                 break;
12481                                         case CEE_MONO_LDPTR_NURSERY_START:
12482                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
12483                                                 break;
12484                                         case CEE_MONO_LDPTR_NURSERY_BITS:
12485                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
12486                                                 break;
12487                                         case CEE_MONO_LDPTR_INT_REQ_FLAG:
12488                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
12489                                                 break;
12490                                 }
12491
12492                                 *sp++ = ins;
12493                                 ip += 2;
12494                                 inline_costs += 10 * num_calls++;
12495                                 break;
12496                         }
12497                         case CEE_MONO_LDPTR: {
12498                                 gpointer ptr;
12499
12500                                 CHECK_STACK_OVF (1);
12501                                 CHECK_OPSIZE (6);
12502                                 token = read32 (ip + 2);
12503
12504                                 ptr = mono_method_get_wrapper_data (method, token);
12505                                 EMIT_NEW_PCONST (cfg, ins, ptr);
12506                                 *sp++ = ins;
12507                                 ip += 6;
12508                                 inline_costs += 10 * num_calls++;
12509                                 /* Can't embed random pointers into AOT code */
12510                                 DISABLE_AOT (cfg);
12511                                 break;
12512                         }
12513                         case CEE_MONO_JIT_ICALL_ADDR: {
12514                                 MonoJitICallInfo *callinfo;
12515                                 gpointer ptr;
12516
12517                                 CHECK_STACK_OVF (1);
12518                                 CHECK_OPSIZE (6);
12519                                 token = read32 (ip + 2);
12520
12521                                 ptr = mono_method_get_wrapper_data (method, token);
12522                                 callinfo = mono_find_jit_icall_by_addr (ptr);
12523                                 g_assert (callinfo);
12524                                 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
12525                                 *sp++ = ins;
12526                                 ip += 6;
12527                                 inline_costs += 10 * num_calls++;
12528                                 break;
12529                         }
12530                         case CEE_MONO_ICALL_ADDR: {
12531                                 MonoMethod *cmethod;
12532                                 gpointer ptr;
12533
12534                                 CHECK_STACK_OVF (1);
12535                                 CHECK_OPSIZE (6);
12536                                 token = read32 (ip + 2);
12537
12538                                 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
12539
12540                                 if (cfg->compile_aot) {
12541                                         EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
12542                                 } else {
12543                                         ptr = mono_lookup_internal_call (cmethod);
12544                                         g_assert (ptr);
12545                                         EMIT_NEW_PCONST (cfg, ins, ptr);
12546                                 }
12547                                 *sp++ = ins;
12548                                 ip += 6;
12549                                 break;
12550                         }
12551                         case CEE_MONO_VTADDR: {
12552                                 MonoInst *src_var, *src;
12553
12554                                 CHECK_STACK (1);
12555                                 --sp;
12556
12557                                 // FIXME:
12558                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12559                                 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
12560                                 *sp++ = src;
12561                                 ip += 2;
12562                                 break;
12563                         }
12564                         case CEE_MONO_NEWOBJ: {
12565                                 MonoInst *iargs [2];
12566
12567                                 CHECK_STACK_OVF (1);
12568                                 CHECK_OPSIZE (6);
12569                                 token = read32 (ip + 2);
12570                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12571                                 mono_class_init (klass);
12572                                 NEW_DOMAINCONST (cfg, iargs [0]);
12573                                 MONO_ADD_INS (cfg->cbb, iargs [0]);
12574                                 NEW_CLASSCONST (cfg, iargs [1], klass);
12575                                 MONO_ADD_INS (cfg->cbb, iargs [1]);
12576                                 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
12577                                 ip += 6;
12578                                 inline_costs += 10 * num_calls++;
12579                                 break;
12580                         }
12581                         case CEE_MONO_OBJADDR:
12582                                 CHECK_STACK (1);
12583                                 --sp;
12584                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
12585                                 ins->dreg = alloc_ireg_mp (cfg);
12586                                 ins->sreg1 = sp [0]->dreg;
12587                                 ins->type = STACK_MP;
12588                                 MONO_ADD_INS (cfg->cbb, ins);
12589                                 *sp++ = ins;
12590                                 ip += 2;
12591                                 break;
12592                         case CEE_MONO_LDNATIVEOBJ:
12593                                 /*
12594                                  * Similar to LDOBJ, but instead load the unmanaged 
12595                                  * representation of the vtype to the stack.
12596                                  */
12597                                 CHECK_STACK (1);
12598                                 CHECK_OPSIZE (6);
12599                                 --sp;
12600                                 token = read32 (ip + 2);
12601                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12602                                 g_assert (klass->valuetype);
12603                                 mono_class_init (klass);
12604
12605                                 {
12606                                         MonoInst *src, *dest, *temp;
12607
12608                                         src = sp [0];
12609                                         temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12610                                         temp->backend.is_pinvoke = 1;
12611                                         EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12612                                         mini_emit_stobj (cfg, dest, src, klass, TRUE);
12613
12614                                         EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12615                                         dest->type = STACK_VTYPE;
12616                                         dest->klass = klass;
12617
12618                                         *sp ++ = dest;
12619                                         ip += 6;
12620                                 }
12621                                 break;
12622                         case CEE_MONO_RETOBJ: {
12623                                 /*
12624                                  * Same as RET, but return the native representation of a vtype
12625                                  * to the caller.
12626                                  */
12627                                 g_assert (cfg->ret);
12628                                 g_assert (mono_method_signature (method)->pinvoke); 
12629                                 CHECK_STACK (1);
12630                                 --sp;
12631                                 
12632                                 CHECK_OPSIZE (6);
12633                                 token = read32 (ip + 2);    
12634                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12635
12636                                 if (!cfg->vret_addr) {
12637                                         g_assert (cfg->ret_var_is_local);
12638
12639                                         EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12640                                 } else {
12641                                         EMIT_NEW_RETLOADA (cfg, ins);
12642                                 }
12643                                 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12644                                 
12645                                 if (sp != stack_start)
12646                                         UNVERIFIED;
12647                                 
12648                                 MONO_INST_NEW (cfg, ins, OP_BR);
12649                                 ins->inst_target_bb = end_bblock;
12650                                 MONO_ADD_INS (cfg->cbb, ins);
12651                                 link_bblock (cfg, cfg->cbb, end_bblock);
12652                                 start_new_bblock = 1;
12653                                 ip += 6;
12654                                 break;
12655                         }
12656                         case CEE_MONO_CISINST:
12657                         case CEE_MONO_CCASTCLASS: {
12658                                 int token;
12659                                 CHECK_STACK (1);
12660                                 --sp;
12661                                 CHECK_OPSIZE (6);
12662                                 token = read32 (ip + 2);
12663                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12664                                 if (ip [1] == CEE_MONO_CISINST)
12665                                         ins = handle_cisinst (cfg, klass, sp [0]);
12666                                 else
12667                                         ins = handle_ccastclass (cfg, klass, sp [0]);
12668                                 *sp++ = ins;
12669                                 ip += 6;
12670                                 break;
12671                         }
12672                         case CEE_MONO_SAVE_LMF:
12673                         case CEE_MONO_RESTORE_LMF:
12674                                 ip += 2;
12675                                 break;
12676                         case CEE_MONO_CLASSCONST:
12677                                 CHECK_STACK_OVF (1);
12678                                 CHECK_OPSIZE (6);
12679                                 token = read32 (ip + 2);
12680                                 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12681                                 *sp++ = ins;
12682                                 ip += 6;
12683                                 inline_costs += 10 * num_calls++;
12684                                 break;
12685                         case CEE_MONO_NOT_TAKEN:
12686                                 cfg->cbb->out_of_line = TRUE;
12687                                 ip += 2;
12688                                 break;
12689                         case CEE_MONO_TLS: {
12690                                 MonoTlsKey key;
12691
12692                                 CHECK_STACK_OVF (1);
12693                                 CHECK_OPSIZE (6);
12694                                 key = (MonoTlsKey)read32 (ip + 2);
12695                                 g_assert (key < TLS_KEY_NUM);
12696
12697                                 ins = mono_create_tls_get (cfg, key);
12698                                 if (!ins) {
12699                                         if (cfg->compile_aot) {
12700                                                 DISABLE_AOT (cfg);
12701                                                 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
12702                                                 ins->dreg = alloc_preg (cfg);
12703                                                 ins->type = STACK_PTR;
12704                                         } else {
12705                                                 g_assert_not_reached ();
12706                                         }
12707                                 }
12708                                 ins->type = STACK_PTR;
12709                                 MONO_ADD_INS (cfg->cbb, ins);
12710                                 *sp++ = ins;
12711                                 ip += 6;
12712                                 break;
12713                         }
12714                         case CEE_MONO_DYN_CALL: {
12715                                 MonoCallInst *call;
12716
12717                                 /* It would be easier to call a trampoline, but that would put an
12718                                  * extra frame on the stack, confusing exception handling. So
12719                                  * implement it inline using an opcode for now.
12720                                  */
12721
12722                                 if (!cfg->dyn_call_var) {
12723                                         cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12724                                         /* prevent it from being register allocated */
12725                                         cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12726                                 }
12727
12728                                 /* Has to use a call inst since it local regalloc expects it */
12729                                 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12730                                 ins = (MonoInst*)call;
12731                                 sp -= 2;
12732                                 ins->sreg1 = sp [0]->dreg;
12733                                 ins->sreg2 = sp [1]->dreg;
12734                                 MONO_ADD_INS (cfg->cbb, ins);
12735
12736                                 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12737
12738                                 ip += 2;
12739                                 inline_costs += 10 * num_calls++;
12740
12741                                 break;
12742                         }
12743                         case CEE_MONO_MEMORY_BARRIER: {
12744                                 CHECK_OPSIZE (6);
12745                                 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12746                                 ip += 6;
12747                                 break;
12748                         }
12749                         case CEE_MONO_JIT_ATTACH: {
12750                                 MonoInst *args [16], *domain_ins;
12751                                 MonoInst *ad_ins, *jit_tls_ins;
12752                                 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12753
12754                                 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12755
12756                                 EMIT_NEW_PCONST (cfg, ins, NULL);
12757                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12758
12759                                 ad_ins = mono_get_domain_intrinsic (cfg);
12760                                 jit_tls_ins = mono_get_jit_tls_intrinsic (cfg);
12761
12762                                 if (cfg->backend->have_tls_get && ad_ins && jit_tls_ins) {
12763                                         NEW_BBLOCK (cfg, next_bb);
12764                                         NEW_BBLOCK (cfg, call_bb);
12765
12766                                         if (cfg->compile_aot) {
12767                                                 /* AOT code is only used in the root domain */
12768                                                 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12769                                         } else {
12770                                                 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12771                                         }
12772                                         MONO_ADD_INS (cfg->cbb, ad_ins);
12773                                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12774                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12775
12776                                         MONO_ADD_INS (cfg->cbb, jit_tls_ins);
12777                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12778                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12779
12780                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12781                                         MONO_START_BB (cfg, call_bb);
12782                                 }
12783
12784                                 if (cfg->compile_aot) {
12785                                         /* AOT code is only used in the root domain */
12786                                         EMIT_NEW_PCONST (cfg, args [0], NULL);
12787                                 } else {
12788                                         EMIT_NEW_PCONST (cfg, args [0], cfg->domain);
12789                                 }
12790                                 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12791                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12792
12793                                 if (next_bb)
12794                                         MONO_START_BB (cfg, next_bb);
12795                                 ip += 2;
12796                                 break;
12797                         }
12798                         case CEE_MONO_JIT_DETACH: {
12799                                 MonoInst *args [16];
12800
12801                                 /* Restore the original domain */
12802                                 dreg = alloc_ireg (cfg);
12803                                 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12804                                 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12805                                 ip += 2;
12806                                 break;
12807                         }
12808                         case CEE_MONO_CALLI_EXTRA_ARG: {
12809                                 MonoInst *addr;
12810                                 MonoMethodSignature *fsig;
12811                                 MonoInst *arg;
12812
12813                                 /*
12814                                  * This is the same as CEE_CALLI, but passes an additional argument
12815                                  * to the called method in llvmonly mode.
12816                                  * This is only used by delegate invoke wrappers to call the
12817                                  * actual delegate method.
12818                                  */
12819                                 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12820
12821                                 CHECK_OPSIZE (6);
12822                                 token = read32 (ip + 2);
12823
12824                                 ins = NULL;
12825
12826                                 cmethod = NULL;
12827                                 CHECK_STACK (1);
12828                                 --sp;
12829                                 addr = *sp;
12830                                 fsig = mini_get_signature (method, token, generic_context);
12831
12832                                 if (cfg->llvm_only)
12833                                         cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12834
12835                                 n = fsig->param_count + fsig->hasthis + 1;
12836
12837                                 CHECK_STACK (n);
12838
12839                                 sp -= n;
12840                                 arg = sp [n - 1];
12841
12842                                 if (cfg->llvm_only) {
12843                                         /*
12844                                          * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12845                                          * cconv. This is set by mono_init_delegate ().
12846                                          */
12847                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12848                                                 MonoInst *callee = addr;
12849                                                 MonoInst *call, *localloc_ins;
12850                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12851                                                 int low_bit_reg = alloc_preg (cfg);
12852
12853                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12854                                                 NEW_BBLOCK (cfg, end_bb);
12855
12856                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12857                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12858                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12859
12860                                                 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12861                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12862                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12863                                                 /*
12864                                                  * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12865                                                  */
12866                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12867                                                 ins->dreg = alloc_preg (cfg);
12868                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12869                                                 MONO_ADD_INS (cfg->cbb, ins);
12870                                                 localloc_ins = ins;
12871                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12872                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12873                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12874
12875                                                 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12876                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12877
12878                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12879                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12880                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12881                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12882                                                 ins->dreg = call->dreg;
12883
12884                                                 MONO_START_BB (cfg, end_bb);
12885                                         } else {
12886                                                 /* Caller uses a normal calling conv */
12887
12888                                                 MonoInst *callee = addr;
12889                                                 MonoInst *call, *localloc_ins;
12890                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12891                                                 int low_bit_reg = alloc_preg (cfg);
12892
12893                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12894                                                 NEW_BBLOCK (cfg, end_bb);
12895
12896                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12897                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12898                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12899
12900                                                 /* Normal case: callee uses a normal cconv, no conversion is needed */
12901                                                 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12902                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12903                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12904                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12905                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12906                                                 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12907                                                 MONO_ADD_INS (cfg->cbb, addr);
12908                                                 /*
12909                                                  * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12910                                                  */
12911                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12912                                                 ins->dreg = alloc_preg (cfg);
12913                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12914                                                 MONO_ADD_INS (cfg->cbb, ins);
12915                                                 localloc_ins = ins;
12916                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12917                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12918                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12919
12920                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12921                                                 ins->dreg = call->dreg;
12922                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12923
12924                                                 MONO_START_BB (cfg, end_bb);
12925                                         }
12926                                 } else {
12927                                         /* Same as CEE_CALLI */
12928                                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12929                                                 /*
12930                                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
12931                                                  */
12932                                                 MonoInst *callee = addr;
12933
12934                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12935                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12936                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12937                                         } else {
12938                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12939                                         }
12940                                 }
12941
12942                                 if (!MONO_TYPE_IS_VOID (fsig->ret))
12943                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12944
12945                                 CHECK_CFG_EXCEPTION;
12946
12947                                 ip += 6;
12948                                 ins_flag = 0;
12949                                 constrained_class = NULL;
12950                                 break;
12951                         }
12952                         default:
12953                                 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12954                                 break;
12955                         }
12956                         break;
12957                 }
12958
12959                 case CEE_PREFIX1: {
12960                         CHECK_OPSIZE (2);
12961                         switch (ip [1]) {
12962                         case CEE_ARGLIST: {
12963                                 /* somewhat similar to LDTOKEN */
12964                                 MonoInst *addr, *vtvar;
12965                                 CHECK_STACK_OVF (1);
12966                                 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL); 
12967
12968                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12969                                 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12970
12971                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12972                                 ins->type = STACK_VTYPE;
12973                                 ins->klass = mono_defaults.argumenthandle_class;
12974                                 *sp++ = ins;
12975                                 ip += 2;
12976                                 break;
12977                         }
12978                         case CEE_CEQ:
12979                         case CEE_CGT:
12980                         case CEE_CGT_UN:
12981                         case CEE_CLT:
12982                         case CEE_CLT_UN: {
12983                                 MonoInst *cmp, *arg1, *arg2;
12984
12985                                 CHECK_STACK (2);
12986                                 sp -= 2;
12987                                 arg1 = sp [0];
12988                                 arg2 = sp [1];
12989
12990                                 /*
12991                                  * The following transforms:
12992                                  *    CEE_CEQ    into OP_CEQ
12993                                  *    CEE_CGT    into OP_CGT
12994                                  *    CEE_CGT_UN into OP_CGT_UN
12995                                  *    CEE_CLT    into OP_CLT
12996                                  *    CEE_CLT_UN into OP_CLT_UN
12997                                  */
12998                                 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12999
13000                                 MONO_INST_NEW (cfg, ins, cmp->opcode);
13001                                 cmp->sreg1 = arg1->dreg;
13002                                 cmp->sreg2 = arg2->dreg;
13003                                 type_from_op (cfg, cmp, arg1, arg2);
13004                                 CHECK_TYPE (cmp);
13005                                 add_widen_op (cfg, cmp, &arg1, &arg2);
13006                                 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
13007                                         cmp->opcode = OP_LCOMPARE;
13008                                 else if (arg1->type == STACK_R4)
13009                                         cmp->opcode = OP_RCOMPARE;
13010                                 else if (arg1->type == STACK_R8)
13011                                         cmp->opcode = OP_FCOMPARE;
13012                                 else
13013                                         cmp->opcode = OP_ICOMPARE;
13014                                 MONO_ADD_INS (cfg->cbb, cmp);
13015                                 ins->type = STACK_I4;
13016                                 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
13017                                 type_from_op (cfg, ins, arg1, arg2);
13018
13019                                 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
13020                                         /*
13021                                          * The backends expect the fceq opcodes to do the
13022                                          * comparison too.
13023                                          */
13024                                         ins->sreg1 = cmp->sreg1;
13025                                         ins->sreg2 = cmp->sreg2;
13026                                         NULLIFY_INS (cmp);
13027                                 }
13028                                 MONO_ADD_INS (cfg->cbb, ins);
13029                                 *sp++ = ins;
13030                                 ip += 2;
13031                                 break;
13032                         }
13033                         case CEE_LDFTN: {
13034                                 MonoInst *argconst;
13035                                 MonoMethod *cil_method;
13036
13037                                 CHECK_STACK_OVF (1);
13038                                 CHECK_OPSIZE (6);
13039                                 n = read32 (ip + 2);
13040                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13041                                 CHECK_CFG_ERROR;
13042
13043                                 mono_class_init (cmethod->klass);
13044
13045                                 mono_save_token_info (cfg, image, n, cmethod);
13046
13047                                 context_used = mini_method_check_context_used (cfg, cmethod);
13048
13049                                 cil_method = cmethod;
13050                                 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
13051                                         METHOD_ACCESS_FAILURE (method, cil_method);
13052
13053                                 if (mono_security_core_clr_enabled ())
13054                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13055
13056                                 /* 
13057                                  * Optimize the common case of ldftn+delegate creation
13058                                  */
13059                                 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
13060                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13061                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13062                                                 MonoInst *target_ins, *handle_ins;
13063                                                 MonoMethod *invoke;
13064                                                 int invoke_context_used;
13065
13066                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
13067                                                 if (!invoke || !mono_method_signature (invoke))
13068                                                         LOAD_ERROR;
13069
13070                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13071
13072                                                 target_ins = sp [-1];
13073
13074                                                 if (mono_security_core_clr_enabled ())
13075                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13076
13077                                                 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
13078                                                         /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
13079                                                         if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
13080                                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
13081                                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
13082                                                         }
13083                                                 }
13084
13085                                                 /* FIXME: SGEN support */
13086                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
13087                                                         ip += 6;
13088                                                         if (cfg->verbose_level > 3)
13089                                                                 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));
13090                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
13091                                                                 sp --;
13092                                                                 *sp = handle_ins;
13093                                                                 CHECK_CFG_EXCEPTION;
13094                                                                 ip += 5;
13095                                                                 sp ++;
13096                                                                 break;
13097                                                         }
13098                                                         ip -= 6;
13099                                                 }
13100                                         }
13101                                 }
13102
13103                                 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
13104                                 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
13105                                 *sp++ = ins;
13106                                 
13107                                 ip += 6;
13108                                 inline_costs += 10 * num_calls++;
13109                                 break;
13110                         }
13111                         case CEE_LDVIRTFTN: {
13112                                 MonoInst *args [2];
13113
13114                                 CHECK_STACK (1);
13115                                 CHECK_OPSIZE (6);
13116                                 n = read32 (ip + 2);
13117                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13118                                 CHECK_CFG_ERROR;
13119
13120                                 mono_class_init (cmethod->klass);
13121  
13122                                 context_used = mini_method_check_context_used (cfg, cmethod);
13123
13124                                 if (mono_security_core_clr_enabled ())
13125                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13126
13127                                 /*
13128                                  * Optimize the common case of ldvirtftn+delegate creation
13129                                  */
13130                                 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)) {
13131                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13132                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13133                                                 MonoInst *target_ins, *handle_ins;
13134                                                 MonoMethod *invoke;
13135                                                 int invoke_context_used;
13136                                                 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
13137
13138                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
13139                                                 if (!invoke || !mono_method_signature (invoke))
13140                                                         LOAD_ERROR;
13141
13142                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13143
13144                                                 target_ins = sp [-1];
13145
13146                                                 if (mono_security_core_clr_enabled ())
13147                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13148
13149                                                 /* FIXME: SGEN support */
13150                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
13151                                                         ip += 6;
13152                                                         if (cfg->verbose_level > 3)
13153                                                                 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));
13154                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
13155                                                                 sp -= 2;
13156                                                                 *sp = handle_ins;
13157                                                                 CHECK_CFG_EXCEPTION;
13158                                                                 ip += 5;
13159                                                                 sp ++;
13160                                                                 break;
13161                                                         }
13162                                                         ip -= 6;
13163                                                 }
13164                                         }
13165                                 }
13166
13167                                 --sp;
13168                                 args [0] = *sp;
13169
13170                                 args [1] = emit_get_rgctx_method (cfg, context_used,
13171                                                                                                   cmethod, MONO_RGCTX_INFO_METHOD);
13172
13173                                 if (context_used)
13174                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
13175                                 else
13176                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
13177
13178                                 ip += 6;
13179                                 inline_costs += 10 * num_calls++;
13180                                 break;
13181                         }
13182                         case CEE_LDARG:
13183                                 CHECK_STACK_OVF (1);
13184                                 CHECK_OPSIZE (4);
13185                                 n = read16 (ip + 2);
13186                                 CHECK_ARG (n);
13187                                 EMIT_NEW_ARGLOAD (cfg, ins, n);
13188                                 *sp++ = ins;
13189                                 ip += 4;
13190                                 break;
13191                         case CEE_LDARGA:
13192                                 CHECK_STACK_OVF (1);
13193                                 CHECK_OPSIZE (4);
13194                                 n = read16 (ip + 2);
13195                                 CHECK_ARG (n);
13196                                 NEW_ARGLOADA (cfg, ins, n);
13197                                 MONO_ADD_INS (cfg->cbb, ins);
13198                                 *sp++ = ins;
13199                                 ip += 4;
13200                                 break;
13201                         case CEE_STARG:
13202                                 CHECK_STACK (1);
13203                                 --sp;
13204                                 CHECK_OPSIZE (4);
13205                                 n = read16 (ip + 2);
13206                                 CHECK_ARG (n);
13207                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
13208                                         UNVERIFIED;
13209                                 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
13210                                 ip += 4;
13211                                 break;
13212                         case CEE_LDLOC:
13213                                 CHECK_STACK_OVF (1);
13214                                 CHECK_OPSIZE (4);
13215                                 n = read16 (ip + 2);
13216                                 CHECK_LOCAL (n);
13217                                 EMIT_NEW_LOCLOAD (cfg, ins, n);
13218                                 *sp++ = ins;
13219                                 ip += 4;
13220                                 break;
13221                         case CEE_LDLOCA: {
13222                                 unsigned char *tmp_ip;
13223                                 CHECK_STACK_OVF (1);
13224                                 CHECK_OPSIZE (4);
13225                                 n = read16 (ip + 2);
13226                                 CHECK_LOCAL (n);
13227
13228                                 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
13229                                         ip = tmp_ip;
13230                                         inline_costs += 1;
13231                                         break;
13232                                 }                       
13233                                 
13234                                 EMIT_NEW_LOCLOADA (cfg, ins, n);
13235                                 *sp++ = ins;
13236                                 ip += 4;
13237                                 break;
13238                         }
13239                         case CEE_STLOC:
13240                                 CHECK_STACK (1);
13241                                 --sp;
13242                                 CHECK_OPSIZE (4);
13243                                 n = read16 (ip + 2);
13244                                 CHECK_LOCAL (n);
13245                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
13246                                         UNVERIFIED;
13247                                 emit_stloc_ir (cfg, sp, header, n);
13248                                 ip += 4;
13249                                 inline_costs += 1;
13250                                 break;
13251                         case CEE_LOCALLOC:
13252                                 CHECK_STACK (1);
13253                                 --sp;
13254                                 if (sp != stack_start) 
13255                                         UNVERIFIED;
13256                                 if (cfg->method != method) 
13257                                         /* 
13258                                          * Inlining this into a loop in a parent could lead to 
13259                                          * stack overflows which is different behavior than the
13260                                          * non-inlined case, thus disable inlining in this case.
13261                                          */
13262                                         INLINE_FAILURE("localloc");
13263
13264                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
13265                                 ins->dreg = alloc_preg (cfg);
13266                                 ins->sreg1 = sp [0]->dreg;
13267                                 ins->type = STACK_PTR;
13268                                 MONO_ADD_INS (cfg->cbb, ins);
13269
13270                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
13271                                 if (init_locals)
13272                                         ins->flags |= MONO_INST_INIT;
13273
13274                                 *sp++ = ins;
13275                                 ip += 2;
13276                                 break;
13277                         case CEE_ENDFILTER: {
13278                                 MonoExceptionClause *clause, *nearest;
13279                                 int cc;
13280
13281                                 CHECK_STACK (1);
13282                                 --sp;
13283                                 if ((sp != stack_start) || (sp [0]->type != STACK_I4)) 
13284                                         UNVERIFIED;
13285                                 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
13286                                 ins->sreg1 = (*sp)->dreg;
13287                                 MONO_ADD_INS (cfg->cbb, ins);
13288                                 start_new_bblock = 1;
13289                                 ip += 2;
13290
13291                                 nearest = NULL;
13292                                 for (cc = 0; cc < header->num_clauses; ++cc) {
13293                                         clause = &header->clauses [cc];
13294                                         if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
13295                                                 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
13296                                             (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
13297                                                 nearest = clause;
13298                                 }
13299                                 g_assert (nearest);
13300                                 if ((ip - header->code) != nearest->handler_offset)
13301                                         UNVERIFIED;
13302
13303                                 break;
13304                         }
13305                         case CEE_UNALIGNED_:
13306                                 ins_flag |= MONO_INST_UNALIGNED;
13307                                 /* FIXME: record alignment? we can assume 1 for now */
13308                                 CHECK_OPSIZE (3);
13309                                 ip += 3;
13310                                 break;
13311                         case CEE_VOLATILE_:
13312                                 ins_flag |= MONO_INST_VOLATILE;
13313                                 ip += 2;
13314                                 break;
13315                         case CEE_TAIL_:
13316                                 ins_flag   |= MONO_INST_TAILCALL;
13317                                 cfg->flags |= MONO_CFG_HAS_TAIL;
13318                                 /* Can't inline tail calls at this time */
13319                                 inline_costs += 100000;
13320                                 ip += 2;
13321                                 break;
13322                         case CEE_INITOBJ:
13323                                 CHECK_STACK (1);
13324                                 --sp;
13325                                 CHECK_OPSIZE (6);
13326                                 token = read32 (ip + 2);
13327                                 klass = mini_get_class (method, token, generic_context);
13328                                 CHECK_TYPELOAD (klass);
13329                                 if (generic_class_is_reference_type (cfg, klass))
13330                                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
13331                                 else
13332                                         mini_emit_initobj (cfg, *sp, NULL, klass);
13333                                 ip += 6;
13334                                 inline_costs += 1;
13335                                 break;
13336                         case CEE_CONSTRAINED_:
13337                                 CHECK_OPSIZE (6);
13338                                 token = read32 (ip + 2);
13339                                 constrained_class = mini_get_class (method, token, generic_context);
13340                                 CHECK_TYPELOAD (constrained_class);
13341                                 ip += 6;
13342                                 break;
13343                         case CEE_CPBLK:
13344                         case CEE_INITBLK: {
13345                                 MonoInst *iargs [3];
13346                                 CHECK_STACK (3);
13347                                 sp -= 3;
13348
13349                                 /* Skip optimized paths for volatile operations. */
13350                                 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)) {
13351                                         mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
13352                                 } 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)) {
13353                                         /* emit_memset only works when val == 0 */
13354                                         mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
13355                                 } else {
13356                                         MonoInst *call;
13357                                         iargs [0] = sp [0];
13358                                         iargs [1] = sp [1];
13359                                         iargs [2] = sp [2];
13360                                         if (ip [1] == CEE_CPBLK) {
13361                                                 /*
13362                                                  * FIXME: It's unclear whether we should be emitting both the acquire
13363                                                  * and release barriers for cpblk. It is technically both a load and
13364                                                  * store operation, so it seems like that's the sensible thing to do.
13365                                                  *
13366                                                  * FIXME: We emit full barriers on both sides of the operation for
13367                                                  * simplicity. We should have a separate atomic memcpy method instead.
13368                                                  */
13369                                                 MonoMethod *memcpy_method = get_memcpy_method ();
13370
13371                                                 if (ins_flag & MONO_INST_VOLATILE)
13372                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13373
13374                                                 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
13375                                                 call->flags |= ins_flag;
13376
13377                                                 if (ins_flag & MONO_INST_VOLATILE)
13378                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13379                                         } else {
13380                                                 MonoMethod *memset_method = get_memset_method ();
13381                                                 if (ins_flag & MONO_INST_VOLATILE) {
13382                                                         /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
13383                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
13384                                                 }
13385                                                 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
13386                                                 call->flags |= ins_flag;
13387                                         }
13388                                 }
13389                                 ip += 2;
13390                                 ins_flag = 0;
13391                                 inline_costs += 1;
13392                                 break;
13393                         }
13394                         case CEE_NO_:
13395                                 CHECK_OPSIZE (3);
13396                                 if (ip [2] & 0x1)
13397                                         ins_flag |= MONO_INST_NOTYPECHECK;
13398                                 if (ip [2] & 0x2)
13399                                         ins_flag |= MONO_INST_NORANGECHECK;
13400                                 /* we ignore the no-nullcheck for now since we
13401                                  * really do it explicitly only when doing callvirt->call
13402                                  */
13403                                 ip += 3;
13404                                 break;
13405                         case CEE_RETHROW: {
13406                                 MonoInst *load;
13407                                 int handler_offset = -1;
13408
13409                                 for (i = 0; i < header->num_clauses; ++i) {
13410                                         MonoExceptionClause *clause = &header->clauses [i];
13411                                         if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
13412                                                 handler_offset = clause->handler_offset;
13413                                                 break;
13414                                         }
13415                                 }
13416
13417                                 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
13418
13419                                 if (handler_offset == -1)
13420                                         UNVERIFIED;
13421
13422                                 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
13423                                 MONO_INST_NEW (cfg, ins, OP_RETHROW);
13424                                 ins->sreg1 = load->dreg;
13425                                 MONO_ADD_INS (cfg->cbb, ins);
13426
13427                                 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
13428                                 MONO_ADD_INS (cfg->cbb, ins);
13429
13430                                 sp = stack_start;
13431                                 link_bblock (cfg, cfg->cbb, end_bblock);
13432                                 start_new_bblock = 1;
13433                                 ip += 2;
13434                                 break;
13435                         }
13436                         case CEE_SIZEOF: {
13437                                 guint32 val;
13438                                 int ialign;
13439
13440                                 CHECK_STACK_OVF (1);
13441                                 CHECK_OPSIZE (6);
13442                                 token = read32 (ip + 2);
13443                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
13444                                         MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
13445                                         CHECK_CFG_ERROR;
13446
13447                                         val = mono_type_size (type, &ialign);
13448                                 } else {
13449                                         MonoClass *klass = mini_get_class (method, token, generic_context);
13450                                         CHECK_TYPELOAD (klass);
13451
13452                                         val = mono_type_size (&klass->byval_arg, &ialign);
13453
13454                                         if (mini_is_gsharedvt_klass (klass))
13455                                                 GSHAREDVT_FAILURE (*ip);
13456                                 }
13457                                 EMIT_NEW_ICONST (cfg, ins, val);
13458                                 *sp++= ins;
13459                                 ip += 6;
13460                                 break;
13461                         }
13462                         case CEE_REFANYTYPE: {
13463                                 MonoInst *src_var, *src;
13464
13465                                 GSHAREDVT_FAILURE (*ip);
13466
13467                                 CHECK_STACK (1);
13468                                 --sp;
13469
13470                                 // FIXME:
13471                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
13472                                 if (!src_var)
13473                                         src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
13474                                 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
13475                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
13476                                 *sp++ = ins;
13477                                 ip += 2;
13478                                 break;
13479                         }
13480                         case CEE_READONLY_:
13481                                 readonly = TRUE;
13482                                 ip += 2;
13483                                 break;
13484
13485                         case CEE_UNUSED56:
13486                         case CEE_UNUSED57:
13487                         case CEE_UNUSED70:
13488                         case CEE_UNUSED:
13489                         case CEE_UNUSED99:
13490                                 UNVERIFIED;
13491                                 
13492                         default:
13493                                 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
13494                                 UNVERIFIED;
13495                         }
13496                         break;
13497                 }
13498                 case CEE_UNUSED58:
13499                 case CEE_UNUSED1:
13500                         UNVERIFIED;
13501
13502                 default:
13503                         g_warning ("opcode 0x%02x not handled", *ip);
13504                         UNVERIFIED;
13505                 }
13506         }
13507         if (start_new_bblock != 1)
13508                 UNVERIFIED;
13509
13510         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
13511         if (cfg->cbb->next_bb) {
13512                 /* This could already be set because of inlining, #693905 */
13513                 MonoBasicBlock *bb = cfg->cbb;
13514
13515                 while (bb->next_bb)
13516                         bb = bb->next_bb;
13517                 bb->next_bb = end_bblock;
13518         } else {
13519                 cfg->cbb->next_bb = end_bblock;
13520         }
13521
13522         if (cfg->method == method && cfg->domainvar) {
13523                 MonoInst *store;
13524                 MonoInst *get_domain;
13525
13526                 cfg->cbb = init_localsbb;
13527
13528                 if ((get_domain = mono_get_domain_intrinsic (cfg))) {
13529                         MONO_ADD_INS (cfg->cbb, get_domain);
13530                 } else {
13531                         get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
13532                 }
13533                 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
13534                 MONO_ADD_INS (cfg->cbb, store);
13535         }
13536
13537 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
13538         if (cfg->compile_aot)
13539                 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
13540                 mono_get_got_var (cfg);
13541 #endif
13542
13543         if (cfg->method == method && cfg->got_var)
13544                 mono_emit_load_got_addr (cfg);
13545
13546         if (init_localsbb) {
13547                 cfg->cbb = init_localsbb;
13548                 cfg->ip = NULL;
13549                 for (i = 0; i < header->num_locals; ++i) {
13550                         emit_init_local (cfg, i, header->locals [i], init_locals);
13551                 }
13552         }
13553
13554         if (cfg->init_ref_vars && cfg->method == method) {
13555                 /* Emit initialization for ref vars */
13556                 // FIXME: Avoid duplication initialization for IL locals.
13557                 for (i = 0; i < cfg->num_varinfo; ++i) {
13558                         MonoInst *ins = cfg->varinfo [i];
13559
13560                         if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
13561                                 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
13562                 }
13563         }
13564
13565         if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
13566                 cfg->cbb = init_localsbb;
13567                 emit_push_lmf (cfg);
13568         }
13569
13570         cfg->cbb = init_localsbb;
13571         emit_instrumentation_call (cfg, mono_profiler_method_enter);
13572
13573         if (seq_points) {
13574                 MonoBasicBlock *bb;
13575
13576                 /*
13577                  * Make seq points at backward branch targets interruptable.
13578                  */
13579                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
13580                         if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
13581                                 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
13582         }
13583
13584         /* Add a sequence point for method entry/exit events */
13585         if (seq_points && cfg->gen_sdb_seq_points) {
13586                 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
13587                 MONO_ADD_INS (init_localsbb, ins);
13588                 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
13589                 MONO_ADD_INS (cfg->bb_exit, ins);
13590         }
13591
13592         /*
13593          * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
13594          * the code they refer to was dead (#11880).
13595          */
13596         if (sym_seq_points) {
13597                 for (i = 0; i < header->code_size; ++i) {
13598                         if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
13599                                 MonoInst *ins;
13600
13601                                 NEW_SEQ_POINT (cfg, ins, i, FALSE);
13602                                 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
13603                         }
13604                 }
13605         }
13606
13607         cfg->ip = NULL;
13608
13609         if (cfg->method == method) {
13610                 MonoBasicBlock *bb;
13611                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13612                         bb->region = mono_find_block_region (cfg, bb->real_offset);
13613                         if (cfg->spvars)
13614                                 mono_create_spvar_for_region (cfg, bb->region);
13615                         if (cfg->verbose_level > 2)
13616                                 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
13617                 }
13618         }
13619
13620         if (inline_costs < 0) {
13621                 char *mname;
13622
13623                 /* Method is too large */
13624                 mname = mono_method_full_name (method, TRUE);
13625                 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
13626                 g_free (mname);
13627         }
13628
13629         if ((cfg->verbose_level > 2) && (cfg->method == method)) 
13630                 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13631
13632         goto cleanup;
13633
13634 mono_error_exit:
13635         g_assert (!mono_error_ok (&cfg->error));
13636         goto cleanup;
13637  
13638  exception_exit:
13639         g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13640         goto cleanup;
13641
13642  unverified:
13643         set_exception_type_from_invalid_il (cfg, method, ip);
13644         goto cleanup;
13645
13646  cleanup:
13647         g_slist_free (class_inits);
13648         mono_basic_block_free (original_bb);
13649         cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13650         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
13651         if (cfg->exception_type)
13652                 return -1;
13653         else
13654                 return inline_costs;
13655 }
13656
13657 static int
13658 store_membase_reg_to_store_membase_imm (int opcode)
13659 {
13660         switch (opcode) {
13661         case OP_STORE_MEMBASE_REG:
13662                 return OP_STORE_MEMBASE_IMM;
13663         case OP_STOREI1_MEMBASE_REG:
13664                 return OP_STOREI1_MEMBASE_IMM;
13665         case OP_STOREI2_MEMBASE_REG:
13666                 return OP_STOREI2_MEMBASE_IMM;
13667         case OP_STOREI4_MEMBASE_REG:
13668                 return OP_STOREI4_MEMBASE_IMM;
13669         case OP_STOREI8_MEMBASE_REG:
13670                 return OP_STOREI8_MEMBASE_IMM;
13671         default:
13672                 g_assert_not_reached ();
13673         }
13674
13675         return -1;
13676 }               
13677
13678 int
13679 mono_op_to_op_imm (int opcode)
13680 {
13681         switch (opcode) {
13682         case OP_IADD:
13683                 return OP_IADD_IMM;
13684         case OP_ISUB:
13685                 return OP_ISUB_IMM;
13686         case OP_IDIV:
13687                 return OP_IDIV_IMM;
13688         case OP_IDIV_UN:
13689                 return OP_IDIV_UN_IMM;
13690         case OP_IREM:
13691                 return OP_IREM_IMM;
13692         case OP_IREM_UN:
13693                 return OP_IREM_UN_IMM;
13694         case OP_IMUL:
13695                 return OP_IMUL_IMM;
13696         case OP_IAND:
13697                 return OP_IAND_IMM;
13698         case OP_IOR:
13699                 return OP_IOR_IMM;
13700         case OP_IXOR:
13701                 return OP_IXOR_IMM;
13702         case OP_ISHL:
13703                 return OP_ISHL_IMM;
13704         case OP_ISHR:
13705                 return OP_ISHR_IMM;
13706         case OP_ISHR_UN:
13707                 return OP_ISHR_UN_IMM;
13708
13709         case OP_LADD:
13710                 return OP_LADD_IMM;
13711         case OP_LSUB:
13712                 return OP_LSUB_IMM;
13713         case OP_LAND:
13714                 return OP_LAND_IMM;
13715         case OP_LOR:
13716                 return OP_LOR_IMM;
13717         case OP_LXOR:
13718                 return OP_LXOR_IMM;
13719         case OP_LSHL:
13720                 return OP_LSHL_IMM;
13721         case OP_LSHR:
13722                 return OP_LSHR_IMM;
13723         case OP_LSHR_UN:
13724                 return OP_LSHR_UN_IMM;
13725 #if SIZEOF_REGISTER == 8
13726         case OP_LREM:
13727                 return OP_LREM_IMM;
13728 #endif
13729
13730         case OP_COMPARE:
13731                 return OP_COMPARE_IMM;
13732         case OP_ICOMPARE:
13733                 return OP_ICOMPARE_IMM;
13734         case OP_LCOMPARE:
13735                 return OP_LCOMPARE_IMM;
13736
13737         case OP_STORE_MEMBASE_REG:
13738                 return OP_STORE_MEMBASE_IMM;
13739         case OP_STOREI1_MEMBASE_REG:
13740                 return OP_STOREI1_MEMBASE_IMM;
13741         case OP_STOREI2_MEMBASE_REG:
13742                 return OP_STOREI2_MEMBASE_IMM;
13743         case OP_STOREI4_MEMBASE_REG:
13744                 return OP_STOREI4_MEMBASE_IMM;
13745
13746 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13747         case OP_X86_PUSH:
13748                 return OP_X86_PUSH_IMM;
13749         case OP_X86_COMPARE_MEMBASE_REG:
13750                 return OP_X86_COMPARE_MEMBASE_IMM;
13751 #endif
13752 #if defined(TARGET_AMD64)
13753         case OP_AMD64_ICOMPARE_MEMBASE_REG:
13754                 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13755 #endif
13756         case OP_VOIDCALL_REG:
13757                 return OP_VOIDCALL;
13758         case OP_CALL_REG:
13759                 return OP_CALL;
13760         case OP_LCALL_REG:
13761                 return OP_LCALL;
13762         case OP_FCALL_REG:
13763                 return OP_FCALL;
13764         case OP_LOCALLOC:
13765                 return OP_LOCALLOC_IMM;
13766         }
13767
13768         return -1;
13769 }
13770
13771 static int
13772 ldind_to_load_membase (int opcode)
13773 {
13774         switch (opcode) {
13775         case CEE_LDIND_I1:
13776                 return OP_LOADI1_MEMBASE;
13777         case CEE_LDIND_U1:
13778                 return OP_LOADU1_MEMBASE;
13779         case CEE_LDIND_I2:
13780                 return OP_LOADI2_MEMBASE;
13781         case CEE_LDIND_U2:
13782                 return OP_LOADU2_MEMBASE;
13783         case CEE_LDIND_I4:
13784                 return OP_LOADI4_MEMBASE;
13785         case CEE_LDIND_U4:
13786                 return OP_LOADU4_MEMBASE;
13787         case CEE_LDIND_I:
13788                 return OP_LOAD_MEMBASE;
13789         case CEE_LDIND_REF:
13790                 return OP_LOAD_MEMBASE;
13791         case CEE_LDIND_I8:
13792                 return OP_LOADI8_MEMBASE;
13793         case CEE_LDIND_R4:
13794                 return OP_LOADR4_MEMBASE;
13795         case CEE_LDIND_R8:
13796                 return OP_LOADR8_MEMBASE;
13797         default:
13798                 g_assert_not_reached ();
13799         }
13800
13801         return -1;
13802 }
13803
13804 static int
13805 stind_to_store_membase (int opcode)
13806 {
13807         switch (opcode) {
13808         case CEE_STIND_I1:
13809                 return OP_STOREI1_MEMBASE_REG;
13810         case CEE_STIND_I2:
13811                 return OP_STOREI2_MEMBASE_REG;
13812         case CEE_STIND_I4:
13813                 return OP_STOREI4_MEMBASE_REG;
13814         case CEE_STIND_I:
13815         case CEE_STIND_REF:
13816                 return OP_STORE_MEMBASE_REG;
13817         case CEE_STIND_I8:
13818                 return OP_STOREI8_MEMBASE_REG;
13819         case CEE_STIND_R4:
13820                 return OP_STORER4_MEMBASE_REG;
13821         case CEE_STIND_R8:
13822                 return OP_STORER8_MEMBASE_REG;
13823         default:
13824                 g_assert_not_reached ();
13825         }
13826
13827         return -1;
13828 }
13829
13830 int
13831 mono_load_membase_to_load_mem (int opcode)
13832 {
13833         // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13834 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13835         switch (opcode) {
13836         case OP_LOAD_MEMBASE:
13837                 return OP_LOAD_MEM;
13838         case OP_LOADU1_MEMBASE:
13839                 return OP_LOADU1_MEM;
13840         case OP_LOADU2_MEMBASE:
13841                 return OP_LOADU2_MEM;
13842         case OP_LOADI4_MEMBASE:
13843                 return OP_LOADI4_MEM;
13844         case OP_LOADU4_MEMBASE:
13845                 return OP_LOADU4_MEM;
13846 #if SIZEOF_REGISTER == 8
13847         case OP_LOADI8_MEMBASE:
13848                 return OP_LOADI8_MEM;
13849 #endif
13850         }
13851 #endif
13852
13853         return -1;
13854 }
13855
13856 static inline int
13857 op_to_op_dest_membase (int store_opcode, int opcode)
13858 {
13859 #if defined(TARGET_X86)
13860         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13861                 return -1;
13862
13863         switch (opcode) {
13864         case OP_IADD:
13865                 return OP_X86_ADD_MEMBASE_REG;
13866         case OP_ISUB:
13867                 return OP_X86_SUB_MEMBASE_REG;
13868         case OP_IAND:
13869                 return OP_X86_AND_MEMBASE_REG;
13870         case OP_IOR:
13871                 return OP_X86_OR_MEMBASE_REG;
13872         case OP_IXOR:
13873                 return OP_X86_XOR_MEMBASE_REG;
13874         case OP_ADD_IMM:
13875         case OP_IADD_IMM:
13876                 return OP_X86_ADD_MEMBASE_IMM;
13877         case OP_SUB_IMM:
13878         case OP_ISUB_IMM:
13879                 return OP_X86_SUB_MEMBASE_IMM;
13880         case OP_AND_IMM:
13881         case OP_IAND_IMM:
13882                 return OP_X86_AND_MEMBASE_IMM;
13883         case OP_OR_IMM:
13884         case OP_IOR_IMM:
13885                 return OP_X86_OR_MEMBASE_IMM;
13886         case OP_XOR_IMM:
13887         case OP_IXOR_IMM:
13888                 return OP_X86_XOR_MEMBASE_IMM;
13889         case OP_MOVE:
13890                 return OP_NOP;
13891         }
13892 #endif
13893
13894 #if defined(TARGET_AMD64)
13895         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13896                 return -1;
13897
13898         switch (opcode) {
13899         case OP_IADD:
13900                 return OP_X86_ADD_MEMBASE_REG;
13901         case OP_ISUB:
13902                 return OP_X86_SUB_MEMBASE_REG;
13903         case OP_IAND:
13904                 return OP_X86_AND_MEMBASE_REG;
13905         case OP_IOR:
13906                 return OP_X86_OR_MEMBASE_REG;
13907         case OP_IXOR:
13908                 return OP_X86_XOR_MEMBASE_REG;
13909         case OP_IADD_IMM:
13910                 return OP_X86_ADD_MEMBASE_IMM;
13911         case OP_ISUB_IMM:
13912                 return OP_X86_SUB_MEMBASE_IMM;
13913         case OP_IAND_IMM:
13914                 return OP_X86_AND_MEMBASE_IMM;
13915         case OP_IOR_IMM:
13916                 return OP_X86_OR_MEMBASE_IMM;
13917         case OP_IXOR_IMM:
13918                 return OP_X86_XOR_MEMBASE_IMM;
13919         case OP_LADD:
13920                 return OP_AMD64_ADD_MEMBASE_REG;
13921         case OP_LSUB:
13922                 return OP_AMD64_SUB_MEMBASE_REG;
13923         case OP_LAND:
13924                 return OP_AMD64_AND_MEMBASE_REG;
13925         case OP_LOR:
13926                 return OP_AMD64_OR_MEMBASE_REG;
13927         case OP_LXOR:
13928                 return OP_AMD64_XOR_MEMBASE_REG;
13929         case OP_ADD_IMM:
13930         case OP_LADD_IMM:
13931                 return OP_AMD64_ADD_MEMBASE_IMM;
13932         case OP_SUB_IMM:
13933         case OP_LSUB_IMM:
13934                 return OP_AMD64_SUB_MEMBASE_IMM;
13935         case OP_AND_IMM:
13936         case OP_LAND_IMM:
13937                 return OP_AMD64_AND_MEMBASE_IMM;
13938         case OP_OR_IMM:
13939         case OP_LOR_IMM:
13940                 return OP_AMD64_OR_MEMBASE_IMM;
13941         case OP_XOR_IMM:
13942         case OP_LXOR_IMM:
13943                 return OP_AMD64_XOR_MEMBASE_IMM;
13944         case OP_MOVE:
13945                 return OP_NOP;
13946         }
13947 #endif
13948
13949         return -1;
13950 }
13951
13952 static inline int
13953 op_to_op_store_membase (int store_opcode, int opcode)
13954 {
13955 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13956         switch (opcode) {
13957         case OP_ICEQ:
13958                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13959                         return OP_X86_SETEQ_MEMBASE;
13960         case OP_CNE:
13961                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13962                         return OP_X86_SETNE_MEMBASE;
13963         }
13964 #endif
13965
13966         return -1;
13967 }
13968
13969 static inline int
13970 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13971 {
13972 #ifdef TARGET_X86
13973         /* FIXME: This has sign extension issues */
13974         /*
13975         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13976                 return OP_X86_COMPARE_MEMBASE8_IMM;
13977         */
13978
13979         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13980                 return -1;
13981
13982         switch (opcode) {
13983         case OP_X86_PUSH:
13984                 return OP_X86_PUSH_MEMBASE;
13985         case OP_COMPARE_IMM:
13986         case OP_ICOMPARE_IMM:
13987                 return OP_X86_COMPARE_MEMBASE_IMM;
13988         case OP_COMPARE:
13989         case OP_ICOMPARE:
13990                 return OP_X86_COMPARE_MEMBASE_REG;
13991         }
13992 #endif
13993
13994 #ifdef TARGET_AMD64
13995         /* FIXME: This has sign extension issues */
13996         /*
13997         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13998                 return OP_X86_COMPARE_MEMBASE8_IMM;
13999         */
14000
14001         switch (opcode) {
14002         case OP_X86_PUSH:
14003                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14004                         return OP_X86_PUSH_MEMBASE;
14005                 break;
14006                 /* FIXME: This only works for 32 bit immediates
14007         case OP_COMPARE_IMM:
14008         case OP_LCOMPARE_IMM:
14009                 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
14010                         return OP_AMD64_COMPARE_MEMBASE_IMM;
14011                 */
14012         case OP_ICOMPARE_IMM:
14013                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14014                         return OP_AMD64_ICOMPARE_MEMBASE_IMM;
14015                 break;
14016         case OP_COMPARE:
14017         case OP_LCOMPARE:
14018                 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
14019                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
14020                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14021                         return OP_AMD64_COMPARE_MEMBASE_REG;
14022                 break;
14023         case OP_ICOMPARE:
14024                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14025                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
14026                 break;
14027         }
14028 #endif
14029
14030         return -1;
14031 }
14032
14033 static inline int
14034 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
14035 {
14036 #ifdef TARGET_X86
14037         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14038                 return -1;
14039         
14040         switch (opcode) {
14041         case OP_COMPARE:
14042         case OP_ICOMPARE:
14043                 return OP_X86_COMPARE_REG_MEMBASE;
14044         case OP_IADD:
14045                 return OP_X86_ADD_REG_MEMBASE;
14046         case OP_ISUB:
14047                 return OP_X86_SUB_REG_MEMBASE;
14048         case OP_IAND:
14049                 return OP_X86_AND_REG_MEMBASE;
14050         case OP_IOR:
14051                 return OP_X86_OR_REG_MEMBASE;
14052         case OP_IXOR:
14053                 return OP_X86_XOR_REG_MEMBASE;
14054         }
14055 #endif
14056
14057 #ifdef TARGET_AMD64
14058         if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
14059                 switch (opcode) {
14060                 case OP_ICOMPARE:
14061                         return OP_AMD64_ICOMPARE_REG_MEMBASE;
14062                 case OP_IADD:
14063                         return OP_X86_ADD_REG_MEMBASE;
14064                 case OP_ISUB:
14065                         return OP_X86_SUB_REG_MEMBASE;
14066                 case OP_IAND:
14067                         return OP_X86_AND_REG_MEMBASE;
14068                 case OP_IOR:
14069                         return OP_X86_OR_REG_MEMBASE;
14070                 case OP_IXOR:
14071                         return OP_X86_XOR_REG_MEMBASE;
14072                 }
14073         } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
14074                 switch (opcode) {
14075                 case OP_COMPARE:
14076                 case OP_LCOMPARE:
14077                         return OP_AMD64_COMPARE_REG_MEMBASE;
14078                 case OP_LADD:
14079                         return OP_AMD64_ADD_REG_MEMBASE;
14080                 case OP_LSUB:
14081                         return OP_AMD64_SUB_REG_MEMBASE;
14082                 case OP_LAND:
14083                         return OP_AMD64_AND_REG_MEMBASE;
14084                 case OP_LOR:
14085                         return OP_AMD64_OR_REG_MEMBASE;
14086                 case OP_LXOR:
14087                         return OP_AMD64_XOR_REG_MEMBASE;
14088                 }
14089         }
14090 #endif
14091
14092         return -1;
14093 }
14094
14095 int
14096 mono_op_to_op_imm_noemul (int opcode)
14097 {
14098         switch (opcode) {
14099 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
14100         case OP_LSHR:
14101         case OP_LSHL:
14102         case OP_LSHR_UN:
14103                 return -1;
14104 #endif
14105 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14106         case OP_IDIV:
14107         case OP_IDIV_UN:
14108         case OP_IREM:
14109         case OP_IREM_UN:
14110                 return -1;
14111 #endif
14112 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
14113         case OP_IMUL:
14114                 return -1;
14115 #endif
14116         default:
14117                 return mono_op_to_op_imm (opcode);
14118         }
14119 }
14120
14121 /**
14122  * mono_handle_global_vregs:
14123  *
14124  *   Make vregs used in more than one bblock 'global', i.e. allocate a variable
14125  * for them.
14126  */
14127 void
14128 mono_handle_global_vregs (MonoCompile *cfg)
14129 {
14130         gint32 *vreg_to_bb;
14131         MonoBasicBlock *bb;
14132         int i, pos;
14133
14134         vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
14135
14136 #ifdef MONO_ARCH_SIMD_INTRINSICS
14137         if (cfg->uses_simd_intrinsics)
14138                 mono_simd_simplify_indirection (cfg);
14139 #endif
14140
14141         /* Find local vregs used in more than one bb */
14142         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14143                 MonoInst *ins = bb->code;       
14144                 int block_num = bb->block_num;
14145
14146                 if (cfg->verbose_level > 2)
14147                         printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
14148
14149                 cfg->cbb = bb;
14150                 for (; ins; ins = ins->next) {
14151                         const char *spec = INS_INFO (ins->opcode);
14152                         int regtype = 0, regindex;
14153                         gint32 prev_bb;
14154
14155                         if (G_UNLIKELY (cfg->verbose_level > 2))
14156                                 mono_print_ins (ins);
14157
14158                         g_assert (ins->opcode >= MONO_CEE_LAST);
14159
14160                         for (regindex = 0; regindex < 4; regindex ++) {
14161                                 int vreg = 0;
14162
14163                                 if (regindex == 0) {
14164                                         regtype = spec [MONO_INST_DEST];
14165                                         if (regtype == ' ')
14166                                                 continue;
14167                                         vreg = ins->dreg;
14168                                 } else if (regindex == 1) {
14169                                         regtype = spec [MONO_INST_SRC1];
14170                                         if (regtype == ' ')
14171                                                 continue;
14172                                         vreg = ins->sreg1;
14173                                 } else if (regindex == 2) {
14174                                         regtype = spec [MONO_INST_SRC2];
14175                                         if (regtype == ' ')
14176                                                 continue;
14177                                         vreg = ins->sreg2;
14178                                 } else if (regindex == 3) {
14179                                         regtype = spec [MONO_INST_SRC3];
14180                                         if (regtype == ' ')
14181                                                 continue;
14182                                         vreg = ins->sreg3;
14183                                 }
14184
14185 #if SIZEOF_REGISTER == 4
14186                                 /* In the LLVM case, the long opcodes are not decomposed */
14187                                 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
14188                                         /*
14189                                          * Since some instructions reference the original long vreg,
14190                                          * and some reference the two component vregs, it is quite hard
14191                                          * to determine when it needs to be global. So be conservative.
14192                                          */
14193                                         if (!get_vreg_to_inst (cfg, vreg)) {
14194                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14195
14196                                                 if (cfg->verbose_level > 2)
14197                                                         printf ("LONG VREG R%d made global.\n", vreg);
14198                                         }
14199
14200                                         /*
14201                                          * Make the component vregs volatile since the optimizations can
14202                                          * get confused otherwise.
14203                                          */
14204                                         get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
14205                                         get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
14206                                 }
14207 #endif
14208
14209                                 g_assert (vreg != -1);
14210
14211                                 prev_bb = vreg_to_bb [vreg];
14212                                 if (prev_bb == 0) {
14213                                         /* 0 is a valid block num */
14214                                         vreg_to_bb [vreg] = block_num + 1;
14215                                 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
14216                                         if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
14217                                                 continue;
14218
14219                                         if (!get_vreg_to_inst (cfg, vreg)) {
14220                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14221                                                         printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
14222
14223                                                 switch (regtype) {
14224                                                 case 'i':
14225                                                         if (vreg_is_ref (cfg, vreg))
14226                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
14227                                                         else
14228                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
14229                                                         break;
14230                                                 case 'l':
14231                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14232                                                         break;
14233                                                 case 'f':
14234                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
14235                                                         break;
14236                                                 case 'v':
14237                                                         mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
14238                                                         break;
14239                                                 default:
14240                                                         g_assert_not_reached ();
14241                                                 }
14242                                         }
14243
14244                                         /* Flag as having been used in more than one bb */
14245                                         vreg_to_bb [vreg] = -1;
14246                                 }
14247                         }
14248                 }
14249         }
14250
14251         /* If a variable is used in only one bblock, convert it into a local vreg */
14252         for (i = 0; i < cfg->num_varinfo; i++) {
14253                 MonoInst *var = cfg->varinfo [i];
14254                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
14255
14256                 switch (var->type) {
14257                 case STACK_I4:
14258                 case STACK_OBJ:
14259                 case STACK_PTR:
14260                 case STACK_MP:
14261                 case STACK_VTYPE:
14262 #if SIZEOF_REGISTER == 8
14263                 case STACK_I8:
14264 #endif
14265 #if !defined(TARGET_X86)
14266                 /* Enabling this screws up the fp stack on x86 */
14267                 case STACK_R8:
14268 #endif
14269                         if (mono_arch_is_soft_float ())
14270                                 break;
14271
14272                         /*
14273                         if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
14274                                 break;
14275                         */
14276
14277                         /* Arguments are implicitly global */
14278                         /* Putting R4 vars into registers doesn't work currently */
14279                         /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
14280                         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) {
14281                                 /* 
14282                                  * Make that the variable's liveness interval doesn't contain a call, since
14283                                  * that would cause the lvreg to be spilled, making the whole optimization
14284                                  * useless.
14285                                  */
14286                                 /* This is too slow for JIT compilation */
14287 #if 0
14288                                 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
14289                                         MonoInst *ins;
14290                                         int def_index, call_index, ins_index;
14291                                         gboolean spilled = FALSE;
14292
14293                                         def_index = -1;
14294                                         call_index = -1;
14295                                         ins_index = 0;
14296                                         for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
14297                                                 const char *spec = INS_INFO (ins->opcode);
14298
14299                                                 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
14300                                                         def_index = ins_index;
14301
14302                                                 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
14303                                                         ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
14304                                                         if (call_index > def_index) {
14305                                                                 spilled = TRUE;
14306                                                                 break;
14307                                                         }
14308                                                 }
14309
14310                                                 if (MONO_IS_CALL (ins))
14311                                                         call_index = ins_index;
14312
14313                                                 ins_index ++;
14314                                         }
14315
14316                                         if (spilled)
14317                                                 break;
14318                                 }
14319 #endif
14320
14321                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14322                                         printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
14323                                 var->flags |= MONO_INST_IS_DEAD;
14324                                 cfg->vreg_to_inst [var->dreg] = NULL;
14325                         }
14326                         break;
14327                 }
14328         }
14329
14330         /* 
14331          * Compress the varinfo and vars tables so the liveness computation is faster and
14332          * takes up less space.
14333          */
14334         pos = 0;
14335         for (i = 0; i < cfg->num_varinfo; ++i) {
14336                 MonoInst *var = cfg->varinfo [i];
14337                 if (pos < i && cfg->locals_start == i)
14338                         cfg->locals_start = pos;
14339                 if (!(var->flags & MONO_INST_IS_DEAD)) {
14340                         if (pos < i) {
14341                                 cfg->varinfo [pos] = cfg->varinfo [i];
14342                                 cfg->varinfo [pos]->inst_c0 = pos;
14343                                 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
14344                                 cfg->vars [pos].idx = pos;
14345 #if SIZEOF_REGISTER == 4
14346                                 if (cfg->varinfo [pos]->type == STACK_I8) {
14347                                         /* Modify the two component vars too */
14348                                         MonoInst *var1;
14349
14350                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
14351                                         var1->inst_c0 = pos;
14352                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
14353                                         var1->inst_c0 = pos;
14354                                 }
14355 #endif
14356                         }
14357                         pos ++;
14358                 }
14359         }
14360         cfg->num_varinfo = pos;
14361         if (cfg->locals_start > cfg->num_varinfo)
14362                 cfg->locals_start = cfg->num_varinfo;
14363 }
14364
14365 /*
14366  * mono_allocate_gsharedvt_vars:
14367  *
14368  *   Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
14369  * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
14370  */
14371 void
14372 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
14373 {
14374         int i;
14375
14376         cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
14377
14378         for (i = 0; i < cfg->num_varinfo; ++i) {
14379                 MonoInst *ins = cfg->varinfo [i];
14380                 int idx;
14381
14382                 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
14383                         if (i >= cfg->locals_start) {
14384                                 /* Local */
14385                                 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
14386                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
14387                                 ins->opcode = OP_GSHAREDVT_LOCAL;
14388                                 ins->inst_imm = idx;
14389                         } else {
14390                                 /* Arg */
14391                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
14392                                 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
14393                         }
14394                 }
14395         }
14396 }
14397
14398 /**
14399  * mono_spill_global_vars:
14400  *
14401  *   Generate spill code for variables which are not allocated to registers, 
14402  * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
14403  * code is generated which could be optimized by the local optimization passes.
14404  */
14405 void
14406 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
14407 {
14408         MonoBasicBlock *bb;
14409         char spec2 [16];
14410         int orig_next_vreg;
14411         guint32 *vreg_to_lvreg;
14412         guint32 *lvregs;
14413         guint32 i, lvregs_len;
14414         gboolean dest_has_lvreg = FALSE;
14415         MonoStackType stacktypes [128];
14416         MonoInst **live_range_start, **live_range_end;
14417         MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
14418
14419         *need_local_opts = FALSE;
14420
14421         memset (spec2, 0, sizeof (spec2));
14422
14423         /* FIXME: Move this function to mini.c */
14424         stacktypes ['i'] = STACK_PTR;
14425         stacktypes ['l'] = STACK_I8;
14426         stacktypes ['f'] = STACK_R8;
14427 #ifdef MONO_ARCH_SIMD_INTRINSICS
14428         stacktypes ['x'] = STACK_VTYPE;
14429 #endif
14430
14431 #if SIZEOF_REGISTER == 4
14432         /* Create MonoInsts for longs */
14433         for (i = 0; i < cfg->num_varinfo; i++) {
14434                 MonoInst *ins = cfg->varinfo [i];
14435
14436                 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
14437                         switch (ins->type) {
14438                         case STACK_R8:
14439                         case STACK_I8: {
14440                                 MonoInst *tree;
14441
14442                                 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
14443                                         break;
14444
14445                                 g_assert (ins->opcode == OP_REGOFFSET);
14446
14447                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
14448                                 g_assert (tree);
14449                                 tree->opcode = OP_REGOFFSET;
14450                                 tree->inst_basereg = ins->inst_basereg;
14451                                 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
14452
14453                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
14454                                 g_assert (tree);
14455                                 tree->opcode = OP_REGOFFSET;
14456                                 tree->inst_basereg = ins->inst_basereg;
14457                                 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
14458                                 break;
14459                         }
14460                         default:
14461                                 break;
14462                         }
14463                 }
14464         }
14465 #endif
14466
14467         if (cfg->compute_gc_maps) {
14468                 /* registers need liveness info even for !non refs */
14469                 for (i = 0; i < cfg->num_varinfo; i++) {
14470                         MonoInst *ins = cfg->varinfo [i];
14471
14472                         if (ins->opcode == OP_REGVAR)
14473                                 ins->flags |= MONO_INST_GC_TRACK;
14474                 }
14475         }
14476                 
14477         /* FIXME: widening and truncation */
14478
14479         /*
14480          * As an optimization, when a variable allocated to the stack is first loaded into 
14481          * an lvreg, we will remember the lvreg and use it the next time instead of loading
14482          * the variable again.
14483          */
14484         orig_next_vreg = cfg->next_vreg;
14485         vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
14486         lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
14487         lvregs_len = 0;
14488
14489         /* 
14490          * These arrays contain the first and last instructions accessing a given
14491          * variable.
14492          * Since we emit bblocks in the same order we process them here, and we
14493          * don't split live ranges, these will precisely describe the live range of
14494          * the variable, i.e. the instruction range where a valid value can be found
14495          * in the variables location.
14496          * The live range is computed using the liveness info computed by the liveness pass.
14497          * We can't use vmv->range, since that is an abstract live range, and we need
14498          * one which is instruction precise.
14499          * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
14500          */
14501         /* FIXME: Only do this if debugging info is requested */
14502         live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
14503         live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
14504         live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14505         live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14506         
14507         /* Add spill loads/stores */
14508         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14509                 MonoInst *ins;
14510
14511                 if (cfg->verbose_level > 2)
14512                         printf ("\nSPILL BLOCK %d:\n", bb->block_num);
14513
14514                 /* Clear vreg_to_lvreg array */
14515                 for (i = 0; i < lvregs_len; i++)
14516                         vreg_to_lvreg [lvregs [i]] = 0;
14517                 lvregs_len = 0;
14518
14519                 cfg->cbb = bb;
14520                 MONO_BB_FOR_EACH_INS (bb, ins) {
14521                         const char *spec = INS_INFO (ins->opcode);
14522                         int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
14523                         gboolean store, no_lvreg;
14524                         int sregs [MONO_MAX_SRC_REGS];
14525
14526                         if (G_UNLIKELY (cfg->verbose_level > 2))
14527                                 mono_print_ins (ins);
14528
14529                         if (ins->opcode == OP_NOP)
14530                                 continue;
14531
14532                         /* 
14533                          * We handle LDADDR here as well, since it can only be decomposed
14534                          * when variable addresses are known.
14535                          */
14536                         if (ins->opcode == OP_LDADDR) {
14537                                 MonoInst *var = (MonoInst *)ins->inst_p0;
14538
14539                                 if (var->opcode == OP_VTARG_ADDR) {
14540                                         /* Happens on SPARC/S390 where vtypes are passed by reference */
14541                                         MonoInst *vtaddr = var->inst_left;
14542                                         if (vtaddr->opcode == OP_REGVAR) {
14543                                                 ins->opcode = OP_MOVE;
14544                                                 ins->sreg1 = vtaddr->dreg;
14545                                         }
14546                                         else if (var->inst_left->opcode == OP_REGOFFSET) {
14547                                                 ins->opcode = OP_LOAD_MEMBASE;
14548                                                 ins->inst_basereg = vtaddr->inst_basereg;
14549                                                 ins->inst_offset = vtaddr->inst_offset;
14550                                         } else
14551                                                 NOT_IMPLEMENTED;
14552                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
14553                                         /* gsharedvt arg passed by ref */
14554                                         g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
14555
14556                                         ins->opcode = OP_LOAD_MEMBASE;
14557                                         ins->inst_basereg = var->inst_basereg;
14558                                         ins->inst_offset = var->inst_offset;
14559                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
14560                                         MonoInst *load, *load2, *load3;
14561                                         int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
14562                                         int reg1, reg2, reg3;
14563                                         MonoInst *info_var = cfg->gsharedvt_info_var;
14564                                         MonoInst *locals_var = cfg->gsharedvt_locals_var;
14565
14566                                         /*
14567                                          * gsharedvt local.
14568                                          * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
14569                                          */
14570
14571                                         g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
14572
14573                                         g_assert (info_var);
14574                                         g_assert (locals_var);
14575
14576                                         /* Mark the instruction used to compute the locals var as used */
14577                                         cfg->gsharedvt_locals_var_ins = NULL;
14578
14579                                         /* Load the offset */
14580                                         if (info_var->opcode == OP_REGOFFSET) {
14581                                                 reg1 = alloc_ireg (cfg);
14582                                                 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
14583                                         } else if (info_var->opcode == OP_REGVAR) {
14584                                                 load = NULL;
14585                                                 reg1 = info_var->dreg;
14586                                         } else {
14587                                                 g_assert_not_reached ();
14588                                         }
14589                                         reg2 = alloc_ireg (cfg);
14590                                         NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
14591                                         /* Load the locals area address */
14592                                         reg3 = alloc_ireg (cfg);
14593                                         if (locals_var->opcode == OP_REGOFFSET) {
14594                                                 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
14595                                         } else if (locals_var->opcode == OP_REGVAR) {
14596                                                 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
14597                                         } else {
14598                                                 g_assert_not_reached ();
14599                                         }
14600                                         /* Compute the address */
14601                                         ins->opcode = OP_PADD;
14602                                         ins->sreg1 = reg3;
14603                                         ins->sreg2 = reg2;
14604
14605                                         mono_bblock_insert_before_ins (bb, ins, load3);
14606                                         mono_bblock_insert_before_ins (bb, load3, load2);
14607                                         if (load)
14608                                                 mono_bblock_insert_before_ins (bb, load2, load);
14609                                 } else {
14610                                         g_assert (var->opcode == OP_REGOFFSET);
14611
14612                                         ins->opcode = OP_ADD_IMM;
14613                                         ins->sreg1 = var->inst_basereg;
14614                                         ins->inst_imm = var->inst_offset;
14615                                 }
14616
14617                                 *need_local_opts = TRUE;
14618                                 spec = INS_INFO (ins->opcode);
14619                         }
14620
14621                         if (ins->opcode < MONO_CEE_LAST) {
14622                                 mono_print_ins (ins);
14623                                 g_assert_not_reached ();
14624                         }
14625
14626                         /*
14627                          * Store opcodes have destbasereg in the dreg, but in reality, it is an
14628                          * src register.
14629                          * FIXME:
14630                          */
14631                         if (MONO_IS_STORE_MEMBASE (ins)) {
14632                                 tmp_reg = ins->dreg;
14633                                 ins->dreg = ins->sreg2;
14634                                 ins->sreg2 = tmp_reg;
14635                                 store = TRUE;
14636
14637                                 spec2 [MONO_INST_DEST] = ' ';
14638                                 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14639                                 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14640                                 spec2 [MONO_INST_SRC3] = ' ';
14641                                 spec = spec2;
14642                         } else if (MONO_IS_STORE_MEMINDEX (ins))
14643                                 g_assert_not_reached ();
14644                         else
14645                                 store = FALSE;
14646                         no_lvreg = FALSE;
14647
14648                         if (G_UNLIKELY (cfg->verbose_level > 2)) {
14649                                 printf ("\t %.3s %d", spec, ins->dreg);
14650                                 num_sregs = mono_inst_get_src_registers (ins, sregs);
14651                                 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14652                                         printf (" %d", sregs [srcindex]);
14653                                 printf ("\n");
14654                         }
14655
14656                         /***************/
14657                         /*    DREG     */
14658                         /***************/
14659                         regtype = spec [MONO_INST_DEST];
14660                         g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14661                         prev_dreg = -1;
14662
14663                         if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14664                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14665                                 MonoInst *store_ins;
14666                                 int store_opcode;
14667                                 MonoInst *def_ins = ins;
14668                                 int dreg = ins->dreg; /* The original vreg */
14669
14670                                 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14671
14672                                 if (var->opcode == OP_REGVAR) {
14673                                         ins->dreg = var->dreg;
14674                                 } 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)) {
14675                                         /* 
14676                                          * Instead of emitting a load+store, use a _membase opcode.
14677                                          */
14678                                         g_assert (var->opcode == OP_REGOFFSET);
14679                                         if (ins->opcode == OP_MOVE) {
14680                                                 NULLIFY_INS (ins);
14681                                                 def_ins = NULL;
14682                                         } else {
14683                                                 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14684                                                 ins->inst_basereg = var->inst_basereg;
14685                                                 ins->inst_offset = var->inst_offset;
14686                                                 ins->dreg = -1;
14687                                         }
14688                                         spec = INS_INFO (ins->opcode);
14689                                 } else {
14690                                         guint32 lvreg;
14691
14692                                         g_assert (var->opcode == OP_REGOFFSET);
14693
14694                                         prev_dreg = ins->dreg;
14695
14696                                         /* Invalidate any previous lvreg for this vreg */
14697                                         vreg_to_lvreg [ins->dreg] = 0;
14698
14699                                         lvreg = 0;
14700
14701                                         if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14702                                                 regtype = 'l';
14703                                                 store_opcode = OP_STOREI8_MEMBASE_REG;
14704                                         }
14705
14706                                         ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14707
14708 #if SIZEOF_REGISTER != 8
14709                                         if (regtype == 'l') {
14710                                                 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));
14711                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
14712                                                 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));
14713                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
14714                                                 def_ins = store_ins;
14715                                         }
14716                                         else
14717 #endif
14718                                         {
14719                                                 g_assert (store_opcode != OP_STOREV_MEMBASE);
14720
14721                                                 /* Try to fuse the store into the instruction itself */
14722                                                 /* FIXME: Add more instructions */
14723                                                 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14724                                                         ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14725                                                         ins->inst_imm = ins->inst_c0;
14726                                                         ins->inst_destbasereg = var->inst_basereg;
14727                                                         ins->inst_offset = var->inst_offset;
14728                                                         spec = INS_INFO (ins->opcode);
14729                                                 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14730                                                         ins->opcode = store_opcode;
14731                                                         ins->inst_destbasereg = var->inst_basereg;
14732                                                         ins->inst_offset = var->inst_offset;
14733
14734                                                         no_lvreg = TRUE;
14735
14736                                                         tmp_reg = ins->dreg;
14737                                                         ins->dreg = ins->sreg2;
14738                                                         ins->sreg2 = tmp_reg;
14739                                                         store = TRUE;
14740
14741                                                         spec2 [MONO_INST_DEST] = ' ';
14742                                                         spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14743                                                         spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14744                                                         spec2 [MONO_INST_SRC3] = ' ';
14745                                                         spec = spec2;
14746                                                 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14747                                                         // FIXME: The backends expect the base reg to be in inst_basereg
14748                                                         ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14749                                                         ins->dreg = -1;
14750                                                         ins->inst_basereg = var->inst_basereg;
14751                                                         ins->inst_offset = var->inst_offset;
14752                                                         spec = INS_INFO (ins->opcode);
14753                                                 } else {
14754                                                         /* printf ("INS: "); mono_print_ins (ins); */
14755                                                         /* Create a store instruction */
14756                                                         NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14757
14758                                                         /* Insert it after the instruction */
14759                                                         mono_bblock_insert_after_ins (bb, ins, store_ins);
14760
14761                                                         def_ins = store_ins;
14762
14763                                                         /* 
14764                                                          * We can't assign ins->dreg to var->dreg here, since the
14765                                                          * sregs could use it. So set a flag, and do it after
14766                                                          * the sregs.
14767                                                          */
14768                                                         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)))
14769                                                                 dest_has_lvreg = TRUE;
14770                                                 }
14771                                         }
14772                                 }
14773
14774                                 if (def_ins && !live_range_start [dreg]) {
14775                                         live_range_start [dreg] = def_ins;
14776                                         live_range_start_bb [dreg] = bb;
14777                                 }
14778
14779                                 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14780                                         MonoInst *tmp;
14781
14782                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14783                                         tmp->inst_c1 = dreg;
14784                                         mono_bblock_insert_after_ins (bb, def_ins, tmp);
14785                                 }
14786                         }
14787
14788                         /************/
14789                         /*  SREGS   */
14790                         /************/
14791                         num_sregs = mono_inst_get_src_registers (ins, sregs);
14792                         for (srcindex = 0; srcindex < 3; ++srcindex) {
14793                                 regtype = spec [MONO_INST_SRC1 + srcindex];
14794                                 sreg = sregs [srcindex];
14795
14796                                 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14797                                 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14798                                         MonoInst *var = get_vreg_to_inst (cfg, sreg);
14799                                         MonoInst *use_ins = ins;
14800                                         MonoInst *load_ins;
14801                                         guint32 load_opcode;
14802
14803                                         if (var->opcode == OP_REGVAR) {
14804                                                 sregs [srcindex] = var->dreg;
14805                                                 //mono_inst_set_src_registers (ins, sregs);
14806                                                 live_range_end [sreg] = use_ins;
14807                                                 live_range_end_bb [sreg] = bb;
14808
14809                                                 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14810                                                         MonoInst *tmp;
14811
14812                                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14813                                                         /* var->dreg is a hreg */
14814                                                         tmp->inst_c1 = sreg;
14815                                                         mono_bblock_insert_after_ins (bb, ins, tmp);
14816                                                 }
14817
14818                                                 continue;
14819                                         }
14820
14821                                         g_assert (var->opcode == OP_REGOFFSET);
14822                                                 
14823                                         load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14824
14825                                         g_assert (load_opcode != OP_LOADV_MEMBASE);
14826
14827                                         if (vreg_to_lvreg [sreg]) {
14828                                                 g_assert (vreg_to_lvreg [sreg] != -1);
14829
14830                                                 /* The variable is already loaded to an lvreg */
14831                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14832                                                         printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14833                                                 sregs [srcindex] = vreg_to_lvreg [sreg];
14834                                                 //mono_inst_set_src_registers (ins, sregs);
14835                                                 continue;
14836                                         }
14837
14838                                         /* Try to fuse the load into the instruction */
14839                                         if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14840                                                 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14841                                                 sregs [0] = var->inst_basereg;
14842                                                 //mono_inst_set_src_registers (ins, sregs);
14843                                                 ins->inst_offset = var->inst_offset;
14844                                         } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14845                                                 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14846                                                 sregs [1] = var->inst_basereg;
14847                                                 //mono_inst_set_src_registers (ins, sregs);
14848                                                 ins->inst_offset = var->inst_offset;
14849                                         } else {
14850                                                 if (MONO_IS_REAL_MOVE (ins)) {
14851                                                         ins->opcode = OP_NOP;
14852                                                         sreg = ins->dreg;
14853                                                 } else {
14854                                                         //printf ("%d ", srcindex); mono_print_ins (ins);
14855
14856                                                         sreg = alloc_dreg (cfg, stacktypes [regtype]);
14857
14858                                                         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) {
14859                                                                 if (var->dreg == prev_dreg) {
14860                                                                         /*
14861                                                                          * sreg refers to the value loaded by the load
14862                                                                          * emitted below, but we need to use ins->dreg
14863                                                                          * since it refers to the store emitted earlier.
14864                                                                          */
14865                                                                         sreg = ins->dreg;
14866                                                                 }
14867                                                                 g_assert (sreg != -1);
14868                                                                 vreg_to_lvreg [var->dreg] = sreg;
14869                                                                 g_assert (lvregs_len < 1024);
14870                                                                 lvregs [lvregs_len ++] = var->dreg;
14871                                                         }
14872                                                 }
14873
14874                                                 sregs [srcindex] = sreg;
14875                                                 //mono_inst_set_src_registers (ins, sregs);
14876
14877 #if SIZEOF_REGISTER != 8
14878                                                 if (regtype == 'l') {
14879                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14880                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14881                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14882                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14883                                                         use_ins = load_ins;
14884                                                 }
14885                                                 else
14886 #endif
14887                                                 {
14888 #if SIZEOF_REGISTER == 4
14889                                                         g_assert (load_opcode != OP_LOADI8_MEMBASE);
14890 #endif
14891                                                         NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14892                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14893                                                         use_ins = load_ins;
14894                                                 }
14895                                         }
14896
14897                                         if (var->dreg < orig_next_vreg) {
14898                                                 live_range_end [var->dreg] = use_ins;
14899                                                 live_range_end_bb [var->dreg] = bb;
14900                                         }
14901
14902                                         if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14903                                                 MonoInst *tmp;
14904
14905                                                 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14906                                                 tmp->inst_c1 = var->dreg;
14907                                                 mono_bblock_insert_after_ins (bb, ins, tmp);
14908                                         }
14909                                 }
14910                         }
14911                         mono_inst_set_src_registers (ins, sregs);
14912
14913                         if (dest_has_lvreg) {
14914                                 g_assert (ins->dreg != -1);
14915                                 vreg_to_lvreg [prev_dreg] = ins->dreg;
14916                                 g_assert (lvregs_len < 1024);
14917                                 lvregs [lvregs_len ++] = prev_dreg;
14918                                 dest_has_lvreg = FALSE;
14919                         }
14920
14921                         if (store) {
14922                                 tmp_reg = ins->dreg;
14923                                 ins->dreg = ins->sreg2;
14924                                 ins->sreg2 = tmp_reg;
14925                         }
14926
14927                         if (MONO_IS_CALL (ins)) {
14928                                 /* Clear vreg_to_lvreg array */
14929                                 for (i = 0; i < lvregs_len; i++)
14930                                         vreg_to_lvreg [lvregs [i]] = 0;
14931                                 lvregs_len = 0;
14932                         } else if (ins->opcode == OP_NOP) {
14933                                 ins->dreg = -1;
14934                                 MONO_INST_NULLIFY_SREGS (ins);
14935                         }
14936
14937                         if (cfg->verbose_level > 2)
14938                                 mono_print_ins_index (1, ins);
14939                 }
14940
14941                 /* Extend the live range based on the liveness info */
14942                 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14943                         for (i = 0; i < cfg->num_varinfo; i ++) {
14944                                 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14945
14946                                 if (vreg_is_volatile (cfg, vi->vreg))
14947                                         /* The liveness info is incomplete */
14948                                         continue;
14949
14950                                 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14951                                         /* Live from at least the first ins of this bb */
14952                                         live_range_start [vi->vreg] = bb->code;
14953                                         live_range_start_bb [vi->vreg] = bb;
14954                                 }
14955
14956                                 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14957                                         /* Live at least until the last ins of this bb */
14958                                         live_range_end [vi->vreg] = bb->last_ins;
14959                                         live_range_end_bb [vi->vreg] = bb;
14960                                 }
14961                         }
14962                 }
14963         }
14964         
14965         /*
14966          * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14967          * by storing the current native offset into MonoMethodVar->live_range_start/end.
14968          */
14969         if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14970                 for (i = 0; i < cfg->num_varinfo; ++i) {
14971                         int vreg = MONO_VARINFO (cfg, i)->vreg;
14972                         MonoInst *ins;
14973
14974                         if (live_range_start [vreg]) {
14975                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14976                                 ins->inst_c0 = i;
14977                                 ins->inst_c1 = vreg;
14978                                 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14979                         }
14980                         if (live_range_end [vreg]) {
14981                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14982                                 ins->inst_c0 = i;
14983                                 ins->inst_c1 = vreg;
14984                                 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14985                                         mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14986                                 else
14987                                         mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14988                         }
14989                 }
14990         }
14991
14992         if (cfg->gsharedvt_locals_var_ins) {
14993                 /* Nullify if unused */
14994                 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14995                 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14996         }
14997
14998         g_free (live_range_start);
14999         g_free (live_range_end);
15000         g_free (live_range_start_bb);
15001         g_free (live_range_end_bb);
15002 }
15003
15004 /**
15005  * FIXME:
15006  * - use 'iadd' instead of 'int_add'
15007  * - handling ovf opcodes: decompose in method_to_ir.
15008  * - unify iregs/fregs
15009  *   -> partly done, the missing parts are:
15010  *   - a more complete unification would involve unifying the hregs as well, so
15011  *     code wouldn't need if (fp) all over the place. but that would mean the hregs
15012  *     would no longer map to the machine hregs, so the code generators would need to
15013  *     be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
15014  *     wouldn't work any more. Duplicating the code in mono_local_regalloc () into
15015  *     fp/non-fp branches speeds it up by about 15%.
15016  * - use sext/zext opcodes instead of shifts
15017  * - add OP_ICALL
15018  * - get rid of TEMPLOADs if possible and use vregs instead
15019  * - clean up usage of OP_P/OP_ opcodes
15020  * - cleanup usage of DUMMY_USE
15021  * - cleanup the setting of ins->type for MonoInst's which are pushed on the 
15022  *   stack
15023  * - set the stack type and allocate a dreg in the EMIT_NEW macros
15024  * - get rid of all the <foo>2 stuff when the new JIT is ready.
15025  * - make sure handle_stack_args () is called before the branch is emitted
15026  * - when the new IR is done, get rid of all unused stuff
15027  * - COMPARE/BEQ as separate instructions or unify them ?
15028  *   - keeping them separate allows specialized compare instructions like
15029  *     compare_imm, compare_membase
15030  *   - most back ends unify fp compare+branch, fp compare+ceq
15031  * - integrate mono_save_args into inline_method
15032  * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
15033  * - handle long shift opts on 32 bit platforms somehow: they require 
15034  *   3 sregs (2 for arg1 and 1 for arg2)
15035  * - make byref a 'normal' type.
15036  * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
15037  *   variable if needed.
15038  * - do not start a new IL level bblock when cfg->cbb is changed by a function call
15039  *   like inline_method.
15040  * - remove inlining restrictions
15041  * - fix LNEG and enable cfold of INEG
15042  * - generalize x86 optimizations like ldelema as a peephole optimization
15043  * - add store_mem_imm for amd64
15044  * - optimize the loading of the interruption flag in the managed->native wrappers
15045  * - avoid special handling of OP_NOP in passes
15046  * - move code inserting instructions into one function/macro.
15047  * - try a coalescing phase after liveness analysis
15048  * - add float -> vreg conversion + local optimizations on !x86
15049  * - figure out how to handle decomposed branches during optimizations, ie.
15050  *   compare+branch, op_jump_table+op_br etc.
15051  * - promote RuntimeXHandles to vregs
15052  * - vtype cleanups:
15053  *   - add a NEW_VARLOADA_VREG macro
15054  * - the vtype optimizations are blocked by the LDADDR opcodes generated for 
15055  *   accessing vtype fields.
15056  * - get rid of I8CONST on 64 bit platforms
15057  * - dealing with the increase in code size due to branches created during opcode
15058  *   decomposition:
15059  *   - use extended basic blocks
15060  *     - all parts of the JIT
15061  *     - handle_global_vregs () && local regalloc
15062  *   - avoid introducing global vregs during decomposition, like 'vtable' in isinst
15063  * - sources of increase in code size:
15064  *   - vtypes
15065  *   - long compares
15066  *   - isinst and castclass
15067  *   - lvregs not allocated to global registers even if used multiple times
15068  * - call cctors outside the JIT, to make -v output more readable and JIT timings more
15069  *   meaningful.
15070  * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
15071  * - add all micro optimizations from the old JIT
15072  * - put tree optimizations into the deadce pass
15073  * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
15074  *   specific function.
15075  * - unify the float comparison opcodes with the other comparison opcodes, i.e.
15076  *   fcompare + branchCC.
15077  * - create a helper function for allocating a stack slot, taking into account 
15078  *   MONO_CFG_HAS_SPILLUP.
15079  * - merge r68207.
15080  * - merge the ia64 switch changes.
15081  * - optimize mono_regstate2_alloc_int/float.
15082  * - fix the pessimistic handling of variables accessed in exception handler blocks.
15083  * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
15084  *   parts of the tree could be separated by other instructions, killing the tree
15085  *   arguments, or stores killing loads etc. Also, should we fold loads into other
15086  *   instructions if the result of the load is used multiple times ?
15087  * - make the REM_IMM optimization in mini-x86.c arch-independent.
15088  * - LAST MERGE: 108395.
15089  * - when returning vtypes in registers, generate IR and append it to the end of the
15090  *   last bb instead of doing it in the epilog.
15091  * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
15092  */
15093
15094 /*
15095
15096 NOTES
15097 -----
15098
15099 - When to decompose opcodes:
15100   - earlier: this makes some optimizations hard to implement, since the low level IR
15101   no longer contains the neccessary information. But it is easier to do.
15102   - later: harder to implement, enables more optimizations.
15103 - Branches inside bblocks:
15104   - created when decomposing complex opcodes. 
15105     - branches to another bblock: harmless, but not tracked by the branch 
15106       optimizations, so need to branch to a label at the start of the bblock.
15107     - branches to inside the same bblock: very problematic, trips up the local
15108       reg allocator. Can be fixed by spitting the current bblock, but that is a
15109       complex operation, since some local vregs can become global vregs etc.
15110 - Local/global vregs:
15111   - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
15112     local register allocator.
15113   - global vregs: used in more than one bblock. Have an associated MonoMethodVar
15114     structure, created by mono_create_var (). Assigned to hregs or the stack by
15115     the global register allocator.
15116 - When to do optimizations like alu->alu_imm:
15117   - earlier -> saves work later on since the IR will be smaller/simpler
15118   - later -> can work on more instructions
15119 - Handling of valuetypes:
15120   - When a vtype is pushed on the stack, a new temporary is created, an 
15121     instruction computing its address (LDADDR) is emitted and pushed on
15122     the stack. Need to optimize cases when the vtype is used immediately as in
15123     argument passing, stloc etc.
15124 - Instead of the to_end stuff in the old JIT, simply call the function handling
15125   the values on the stack before emitting the last instruction of the bb.
15126 */
15127
15128 #endif /* DISABLE_JIT */