[jit] In native-to-managed wrappers, emit the initlocals code for vtypes after the...
[mono.git] / mono / mini / method-to-ir.c
1 /**
2  * \file
3  * Convert CIL to the JIT internal representation
4  *
5  * Author:
6  *   Paolo Molaro (lupus@ximian.com)
7  *   Dietmar Maurer (dietmar@ximian.com)
8  *
9  * (C) 2002 Ximian, Inc.
10  * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
11  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14
15 #include <config.h>
16 #include <mono/utils/mono-compiler.h>
17 #include "mini.h"
18
19 #ifndef DISABLE_JIT
20
21 #include <signal.h>
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #include <math.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34
35 #ifdef HAVE_ALLOCA_H
36 #include <alloca.h>
37 #endif
38
39 #include <mono/utils/memcheck.h>
40 #include <mono/metadata/abi-details.h>
41 #include <mono/metadata/assembly.h>
42 #include <mono/metadata/attrdefs.h>
43 #include <mono/metadata/loader.h>
44 #include <mono/metadata/tabledefs.h>
45 #include <mono/metadata/class.h>
46 #include <mono/metadata/object.h>
47 #include <mono/metadata/exception.h>
48 #include <mono/metadata/opcodes.h>
49 #include <mono/metadata/mono-endian.h>
50 #include <mono/metadata/tokentype.h>
51 #include <mono/metadata/tabledefs.h>
52 #include <mono/metadata/marshal.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/metadata/debug-internals.h>
55 #include <mono/metadata/gc-internals.h>
56 #include <mono/metadata/security-manager.h>
57 #include <mono/metadata/threads-types.h>
58 #include <mono/metadata/security-core-clr.h>
59 #include <mono/metadata/profiler-private.h>
60 #include <mono/metadata/profiler.h>
61 #include <mono/metadata/monitor.h>
62 #include <mono/utils/mono-memory-model.h>
63 #include <mono/utils/mono-error-internals.h>
64 #include <mono/metadata/mono-basic-block.h>
65 #include <mono/metadata/reflection-internals.h>
66 #include <mono/utils/mono-threads-coop.h>
67
68 #include "trace.h"
69
70 #include "ir-emit.h"
71
72 #include "jit-icalls.h"
73 #include "jit.h"
74 #include "debugger-agent.h"
75 #include "seq-points.h"
76 #include "aot-compiler.h"
77 #include "mini-llvm.h"
78
79 #define BRANCH_COST 10
80 #define INLINE_LENGTH_LIMIT 20
81
82 /* These have 'cfg' as an implicit argument */
83 #define INLINE_FAILURE(msg) do {                                                                        \
84         if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
85                 inline_failure (cfg, msg);                                                                              \
86                 goto exception_exit;                                                                                    \
87         } \
88         } while (0)
89 #define CHECK_CFG_EXCEPTION do {\
90                 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
91                         goto exception_exit;                                            \
92         } while (0)
93 #define FIELD_ACCESS_FAILURE(method, field) do {                                        \
94                 field_access_failure ((cfg), (method), (field));                        \
95                 goto exception_exit;    \
96         } while (0)
97 #define GENERIC_SHARING_FAILURE(opcode) do {            \
98                 if (cfg->gshared) {                                                                     \
99                         gshared_failure (cfg, opcode, __FILE__, __LINE__);      \
100                         goto exception_exit;    \
101                 }                       \
102         } while (0)
103 #define GSHAREDVT_FAILURE(opcode) do {          \
104         if (cfg->gsharedvt) {                                                                                           \
105                 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__);                    \
106                 goto exception_exit;                                                                                    \
107         }                                                                                                                                       \
108         } while (0)
109 #define OUT_OF_MEMORY_FAILURE do {      \
110                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);                \
111                 mono_error_set_out_of_memory (&cfg->error, "");                                 \
112                 goto exception_exit;    \
113         } while (0)
114 #define DISABLE_AOT(cfg) do { \
115                 if ((cfg)->verbose_level >= 2)                                            \
116                         printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__);   \
117                 (cfg)->disable_aot = TRUE;                                                        \
118         } while (0)
119 #define LOAD_ERROR do { \
120                 break_on_unverified ();                                                         \
121                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
122                 goto exception_exit;                                                                    \
123         } while (0)
124
125 #define TYPE_LOAD_ERROR(klass) do { \
126                 cfg->exception_ptr = klass; \
127                 LOAD_ERROR;                                     \
128         } while (0)
129
130 #define CHECK_CFG_ERROR do {\
131                 if (!mono_error_ok (&cfg->error)) { \
132                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);        \
133                         goto mono_error_exit; \
134                 } \
135         } while (0)
136
137 /* Determine whenever 'ins' represents a load of the 'this' argument */
138 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
139
140 static int ldind_to_load_membase (int opcode);
141 static int stind_to_store_membase (int opcode);
142
143 int mono_op_to_op_imm (int opcode);
144 int mono_op_to_op_imm_noemul (int opcode);
145
146 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
147                                                   guchar *ip, guint real_offset, gboolean inline_always);
148 static MonoInst*
149 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
150
151 /* helper methods signatures */
152 static MonoMethodSignature *helper_sig_domain_get;
153 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
154 static MonoMethodSignature *helper_sig_llvmonly_imt_trampoline;
155 static MonoMethodSignature *helper_sig_jit_thread_attach;
156 static MonoMethodSignature *helper_sig_get_tls_tramp;
157 static MonoMethodSignature *helper_sig_set_tls_tramp;
158
159 /* type loading helpers */
160 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, "System.Runtime.CompilerServices", "RuntimeHelpers")
161 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, "System.Diagnostics", "DebuggableAttribute")
162
163 /*
164  * Instruction metadata
165  */
166 #ifdef MINI_OP
167 #undef MINI_OP
168 #endif
169 #ifdef MINI_OP3
170 #undef MINI_OP3
171 #endif
172 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
173 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
174 #define NONE ' '
175 #define IREG 'i'
176 #define FREG 'f'
177 #define VREG 'v'
178 #define XREG 'x'
179 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
180 #define LREG IREG
181 #else
182 #define LREG 'l'
183 #endif
184 /* keep in sync with the enum in mini.h */
185 const char
186 ins_info[] = {
187 #include "mini-ops.h"
188 };
189 #undef MINI_OP
190 #undef MINI_OP3
191
192 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
193 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
194 /* 
195  * This should contain the index of the last sreg + 1. This is not the same
196  * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
197  */
198 const gint8 ins_sreg_counts[] = {
199 #include "mini-ops.h"
200 };
201 #undef MINI_OP
202 #undef MINI_OP3
203
204 guint32
205 mono_alloc_ireg (MonoCompile *cfg)
206 {
207         return alloc_ireg (cfg);
208 }
209
210 guint32
211 mono_alloc_lreg (MonoCompile *cfg)
212 {
213         return alloc_lreg (cfg);
214 }
215
216 guint32
217 mono_alloc_freg (MonoCompile *cfg)
218 {
219         return alloc_freg (cfg);
220 }
221
222 guint32
223 mono_alloc_preg (MonoCompile *cfg)
224 {
225         return alloc_preg (cfg);
226 }
227
228 guint32
229 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
230 {
231         return alloc_dreg (cfg, stack_type);
232 }
233
234 /*
235  * mono_alloc_ireg_ref:
236  *
237  *   Allocate an IREG, and mark it as holding a GC ref.
238  */
239 guint32
240 mono_alloc_ireg_ref (MonoCompile *cfg)
241 {
242         return alloc_ireg_ref (cfg);
243 }
244
245 /*
246  * mono_alloc_ireg_mp:
247  *
248  *   Allocate an IREG, and mark it as holding a managed pointer.
249  */
250 guint32
251 mono_alloc_ireg_mp (MonoCompile *cfg)
252 {
253         return alloc_ireg_mp (cfg);
254 }
255
256 /*
257  * mono_alloc_ireg_copy:
258  *
259  *   Allocate an IREG with the same GC type as VREG.
260  */
261 guint32
262 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
263 {
264         if (vreg_is_ref (cfg, vreg))
265                 return alloc_ireg_ref (cfg);
266         else if (vreg_is_mp (cfg, vreg))
267                 return alloc_ireg_mp (cfg);
268         else
269                 return alloc_ireg (cfg);
270 }
271
272 guint
273 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
274 {
275         if (type->byref)
276                 return OP_MOVE;
277
278         type = mini_get_underlying_type (type);
279 handle_enum:
280         switch (type->type) {
281         case MONO_TYPE_I1:
282         case MONO_TYPE_U1:
283                 return OP_MOVE;
284         case MONO_TYPE_I2:
285         case MONO_TYPE_U2:
286                 return OP_MOVE;
287         case MONO_TYPE_I4:
288         case MONO_TYPE_U4:
289                 return OP_MOVE;
290         case MONO_TYPE_I:
291         case MONO_TYPE_U:
292         case MONO_TYPE_PTR:
293         case MONO_TYPE_FNPTR:
294                 return OP_MOVE;
295         case MONO_TYPE_CLASS:
296         case MONO_TYPE_STRING:
297         case MONO_TYPE_OBJECT:
298         case MONO_TYPE_SZARRAY:
299         case MONO_TYPE_ARRAY:    
300                 return OP_MOVE;
301         case MONO_TYPE_I8:
302         case MONO_TYPE_U8:
303 #if SIZEOF_REGISTER == 8
304                 return OP_MOVE;
305 #else
306                 return OP_LMOVE;
307 #endif
308         case MONO_TYPE_R4:
309                 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
310         case MONO_TYPE_R8:
311                 return OP_FMOVE;
312         case MONO_TYPE_VALUETYPE:
313                 if (type->data.klass->enumtype) {
314                         type = mono_class_enum_basetype (type->data.klass);
315                         goto handle_enum;
316                 }
317                 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
318                         return OP_XMOVE;
319                 return OP_VMOVE;
320         case MONO_TYPE_TYPEDBYREF:
321                 return OP_VMOVE;
322         case MONO_TYPE_GENERICINST:
323                 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
324                         return OP_XMOVE;
325                 type = &type->data.generic_class->container_class->byval_arg;
326                 goto handle_enum;
327         case MONO_TYPE_VAR:
328         case MONO_TYPE_MVAR:
329                 g_assert (cfg->gshared);
330                 if (mini_type_var_is_vt (type))
331                         return OP_VMOVE;
332                 else
333                         return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
334         default:
335                 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
336         }
337         return -1;
338 }
339
340 void
341 mono_print_bb (MonoBasicBlock *bb, const char *msg)
342 {
343         int i;
344         MonoInst *tree;
345         GString *str = g_string_new ("");
346
347         g_string_append_printf (str, "%s %d: [IN: ", msg, bb->block_num);
348         for (i = 0; i < bb->in_count; ++i)
349                 g_string_append_printf (str, " BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
350         g_string_append_printf (str, ", OUT: ");
351         for (i = 0; i < bb->out_count; ++i)
352                 g_string_append_printf (str, " BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
353         g_string_append_printf (str, " ]\n");
354
355         g_print ("%s", str->str);
356         g_string_free (str, TRUE);
357
358         for (tree = bb->code; tree; tree = tree->next)
359                 mono_print_ins_index (-1, tree);
360 }
361
362 void
363 mono_create_helper_signatures (void)
364 {
365         helper_sig_domain_get = mono_create_icall_signature ("ptr");
366         helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
367         helper_sig_llvmonly_imt_trampoline = mono_create_icall_signature ("ptr ptr ptr");
368         helper_sig_jit_thread_attach = mono_create_icall_signature ("ptr ptr");
369         helper_sig_get_tls_tramp = mono_create_icall_signature ("ptr");
370         helper_sig_set_tls_tramp = mono_create_icall_signature ("void ptr");
371 }
372
373 static MONO_NEVER_INLINE void
374 break_on_unverified (void)
375 {
376         if (mini_get_debug_options ()->break_on_unverified)
377                 G_BREAKPOINT ();
378 }
379
380 static MONO_NEVER_INLINE void
381 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
382 {
383         char *method_fname = mono_method_full_name (method, TRUE);
384         char *field_fname = mono_field_full_name (field);
385         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
386         mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
387         g_free (method_fname);
388         g_free (field_fname);
389 }
390
391 static MONO_NEVER_INLINE void
392 inline_failure (MonoCompile *cfg, const char *msg)
393 {
394         if (cfg->verbose_level >= 2)
395                 printf ("inline failed: %s\n", msg);
396         mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
397 }
398
399 static MONO_NEVER_INLINE void
400 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
401 {
402         if (cfg->verbose_level > 2)                                                                                     \
403                 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);
404         mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
405 }
406
407 static MONO_NEVER_INLINE void
408 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
409 {
410         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);
411         if (cfg->verbose_level >= 2)
412                 printf ("%s\n", cfg->exception_message);
413         mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
414 }
415
416 /*
417  * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e. 
418  * foo<T> (int i) { ldarg.0; box T; }
419  */
420 #define UNVERIFIED do { \
421         if (cfg->gsharedvt) { \
422                 if (cfg->verbose_level > 2)                                                                     \
423                         printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
424                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
425                 goto exception_exit;                                                                                    \
426         }                                                                                                                                       \
427         break_on_unverified ();                                                                                         \
428         goto unverified;                                                                                                        \
429 } while (0)
430
431 #define GET_BBLOCK(cfg,tblock,ip) do {  \
432                 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
433                 if (!(tblock)) {        \
434                         if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
435             NEW_BBLOCK (cfg, (tblock)); \
436                         (tblock)->cil_code = (ip);      \
437                         ADD_BBLOCK (cfg, (tblock));     \
438                 } \
439         } while (0)
440
441 #if defined(TARGET_X86) || defined(TARGET_AMD64)
442 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
443                 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
444                 (dest)->dreg = alloc_ireg_mp ((cfg)); \
445                 (dest)->sreg1 = (sr1); \
446                 (dest)->sreg2 = (sr2); \
447                 (dest)->inst_imm = (imm); \
448                 (dest)->backend.shift_amount = (shift); \
449                 MONO_ADD_INS ((cfg)->cbb, (dest)); \
450         } while (0)
451 #endif
452
453 /* Emit conversions so both operands of a binary opcode are of the same type */
454 static void
455 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
456 {
457         MonoInst *arg1 = *arg1_ref;
458         MonoInst *arg2 = *arg2_ref;
459
460         if (cfg->r4fp &&
461                 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
462                  (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
463                 MonoInst *conv;
464
465                 /* Mixing r4/r8 is allowed by the spec */
466                 if (arg1->type == STACK_R4) {
467                         int dreg = alloc_freg (cfg);
468
469                         EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
470                         conv->type = STACK_R8;
471                         ins->sreg1 = dreg;
472                         *arg1_ref = conv;
473                 }
474                 if (arg2->type == STACK_R4) {
475                         int dreg = alloc_freg (cfg);
476
477                         EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
478                         conv->type = STACK_R8;
479                         ins->sreg2 = dreg;
480                         *arg2_ref = conv;
481                 }
482         }
483
484 #if SIZEOF_REGISTER == 8
485         /* FIXME: Need to add many more cases */
486         if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
487                 MonoInst *widen;
488
489                 int dr = alloc_preg (cfg);
490                 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
491                 (ins)->sreg2 = widen->dreg;
492         }
493 #endif
494 }
495
496 #define ADD_BINOP(op) do {      \
497                 MONO_INST_NEW (cfg, ins, (op)); \
498                 sp -= 2;        \
499                 ins->sreg1 = sp [0]->dreg;      \
500                 ins->sreg2 = sp [1]->dreg;      \
501                 type_from_op (cfg, ins, sp [0], sp [1]);        \
502                 CHECK_TYPE (ins);       \
503                 /* Have to insert a widening op */               \
504         add_widen_op (cfg, ins, &sp [0], &sp [1]);               \
505         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
506         MONO_ADD_INS ((cfg)->cbb, (ins)); \
507         *sp++ = mono_decompose_opcode ((cfg), (ins));   \
508         } while (0)
509
510 #define ADD_UNOP(op) do {       \
511                 MONO_INST_NEW (cfg, ins, (op)); \
512                 sp--;   \
513                 ins->sreg1 = sp [0]->dreg;      \
514                 type_from_op (cfg, ins, sp [0], NULL);  \
515                 CHECK_TYPE (ins);       \
516         (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
517         MONO_ADD_INS ((cfg)->cbb, (ins)); \
518                 *sp++ = mono_decompose_opcode (cfg, ins);       \
519         } while (0)
520
521 #define ADD_BINCOND(next_block) do {    \
522                 MonoInst *cmp;  \
523                 sp -= 2; \
524                 MONO_INST_NEW(cfg, cmp, OP_COMPARE);    \
525                 cmp->sreg1 = sp [0]->dreg;      \
526                 cmp->sreg2 = sp [1]->dreg;      \
527                 type_from_op (cfg, cmp, sp [0], sp [1]);        \
528                 CHECK_TYPE (cmp);       \
529                 add_widen_op (cfg, cmp, &sp [0], &sp [1]);                                              \
530                 type_from_op (cfg, ins, sp [0], sp [1]);                                                        \
531                 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);   \
532                 GET_BBLOCK (cfg, tblock, target);               \
533                 link_bblock (cfg, cfg->cbb, tblock);    \
534                 ins->inst_true_bb = tblock;     \
535                 if ((next_block)) {     \
536                         link_bblock (cfg, cfg->cbb, (next_block));      \
537                         ins->inst_false_bb = (next_block);      \
538                         start_new_bblock = 1;   \
539                 } else {        \
540                         GET_BBLOCK (cfg, tblock, ip);           \
541                         link_bblock (cfg, cfg->cbb, tblock);    \
542                         ins->inst_false_bb = tblock;    \
543                         start_new_bblock = 2;   \
544                 }       \
545                 if (sp != stack_start) {                                                                        \
546                     handle_stack_args (cfg, stack_start, sp - stack_start); \
547                         CHECK_UNVERIFIABLE (cfg); \
548                 } \
549         MONO_ADD_INS (cfg->cbb, cmp); \
550                 MONO_ADD_INS (cfg->cbb, ins);   \
551         } while (0)
552
553 /* *
554  * link_bblock: Links two basic blocks
555  *
556  * links two basic blocks in the control flow graph, the 'from'
557  * argument is the starting block and the 'to' argument is the block
558  * the control flow ends to after 'from'.
559  */
560 static void
561 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
562 {
563         MonoBasicBlock **newa;
564         int i, found;
565
566 #if 0
567         if (from->cil_code) {
568                 if (to->cil_code)
569                         printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
570                 else
571                         printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
572         } else {
573                 if (to->cil_code)
574                         printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
575                 else
576                         printf ("edge from entry to exit\n");
577         }
578 #endif
579
580         found = FALSE;
581         for (i = 0; i < from->out_count; ++i) {
582                 if (to == from->out_bb [i]) {
583                         found = TRUE;
584                         break;
585                 }
586         }
587         if (!found) {
588                 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
589                 for (i = 0; i < from->out_count; ++i) {
590                         newa [i] = from->out_bb [i];
591                 }
592                 newa [i] = to;
593                 from->out_count++;
594                 from->out_bb = newa;
595         }
596
597         found = FALSE;
598         for (i = 0; i < to->in_count; ++i) {
599                 if (from == to->in_bb [i]) {
600                         found = TRUE;
601                         break;
602                 }
603         }
604         if (!found) {
605                 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
606                 for (i = 0; i < to->in_count; ++i) {
607                         newa [i] = to->in_bb [i];
608                 }
609                 newa [i] = from;
610                 to->in_count++;
611                 to->in_bb = newa;
612         }
613 }
614
615 void
616 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
617 {
618         link_bblock (cfg, from, to);
619 }
620
621 /**
622  * mono_find_block_region:
623  *
624  *   We mark each basic block with a region ID. We use that to avoid BB
625  *   optimizations when blocks are in different regions.
626  *
627  * Returns:
628  *   A region token that encodes where this region is, and information
629  *   about the clause owner for this block.
630  *
631  *   The region encodes the try/catch/filter clause that owns this block
632  *   as well as the type.  -1 is a special value that represents a block
633  *   that is in none of try/catch/filter.
634  */
635 static int
636 mono_find_block_region (MonoCompile *cfg, int offset)
637 {
638         MonoMethodHeader *header = cfg->header;
639         MonoExceptionClause *clause;
640         int i;
641
642         for (i = 0; i < header->num_clauses; ++i) {
643                 clause = &header->clauses [i];
644                 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
645                     (offset < (clause->handler_offset)))
646                         return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
647                            
648                 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
649                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
650                                 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
651                         else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
652                                 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
653                         else
654                                 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
655                 }
656         }
657         for (i = 0; i < header->num_clauses; ++i) {
658                 clause = &header->clauses [i];
659
660                 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
661                         return ((i + 1) << 8) | clause->flags;
662         }
663
664         return -1;
665 }
666
667 static gboolean
668 ip_in_finally_clause (MonoCompile *cfg, int offset)
669 {
670         MonoMethodHeader *header = cfg->header;
671         MonoExceptionClause *clause;
672         int i;
673
674         for (i = 0; i < header->num_clauses; ++i) {
675                 clause = &header->clauses [i];
676                 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FAULT)
677                         continue;
678
679                 if (MONO_OFFSET_IN_HANDLER (clause, offset))
680                         return TRUE;
681         }
682         return FALSE;
683 }
684
685 static GList*
686 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
687 {
688         MonoMethodHeader *header = cfg->header;
689         MonoExceptionClause *clause;
690         int i;
691         GList *res = NULL;
692
693         for (i = 0; i < header->num_clauses; ++i) {
694                 clause = &header->clauses [i];
695                 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) && 
696                     (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
697                         if (clause->flags == type)
698                                 res = g_list_append (res, clause);
699                 }
700         }
701         return res;
702 }
703
704 static void
705 mono_create_spvar_for_region (MonoCompile *cfg, int region)
706 {
707         MonoInst *var;
708
709         var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
710         if (var)
711                 return;
712
713         var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
714         /* prevent it from being register allocated */
715         var->flags |= MONO_INST_VOLATILE;
716
717         g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
718 }
719
720 MonoInst *
721 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
722 {
723         return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
724 }
725
726 static MonoInst*
727 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
728 {
729         MonoInst *var;
730
731         var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
732         if (var)
733                 return var;
734
735         var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
736         /* prevent it from being register allocated */
737         var->flags |= MONO_INST_VOLATILE;
738
739         g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
740
741         return var;
742 }
743
744 /*
745  * Returns the type used in the eval stack when @type is loaded.
746  * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
747  */
748 void
749 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
750 {
751         MonoClass *klass;
752
753         type = mini_get_underlying_type (type);
754         inst->klass = klass = mono_class_from_mono_type (type);
755         if (type->byref) {
756                 inst->type = STACK_MP;
757                 return;
758         }
759
760 handle_enum:
761         switch (type->type) {
762         case MONO_TYPE_VOID:
763                 inst->type = STACK_INV;
764                 return;
765         case MONO_TYPE_I1:
766         case MONO_TYPE_U1:
767         case MONO_TYPE_I2:
768         case MONO_TYPE_U2:
769         case MONO_TYPE_I4:
770         case MONO_TYPE_U4:
771                 inst->type = STACK_I4;
772                 return;
773         case MONO_TYPE_I:
774         case MONO_TYPE_U:
775         case MONO_TYPE_PTR:
776         case MONO_TYPE_FNPTR:
777                 inst->type = STACK_PTR;
778                 return;
779         case MONO_TYPE_CLASS:
780         case MONO_TYPE_STRING:
781         case MONO_TYPE_OBJECT:
782         case MONO_TYPE_SZARRAY:
783         case MONO_TYPE_ARRAY:    
784                 inst->type = STACK_OBJ;
785                 return;
786         case MONO_TYPE_I8:
787         case MONO_TYPE_U8:
788                 inst->type = STACK_I8;
789                 return;
790         case MONO_TYPE_R4:
791                 inst->type = cfg->r4_stack_type;
792                 break;
793         case MONO_TYPE_R8:
794                 inst->type = STACK_R8;
795                 return;
796         case MONO_TYPE_VALUETYPE:
797                 if (type->data.klass->enumtype) {
798                         type = mono_class_enum_basetype (type->data.klass);
799                         goto handle_enum;
800                 } else {
801                         inst->klass = klass;
802                         inst->type = STACK_VTYPE;
803                         return;
804                 }
805         case MONO_TYPE_TYPEDBYREF:
806                 inst->klass = mono_defaults.typed_reference_class;
807                 inst->type = STACK_VTYPE;
808                 return;
809         case MONO_TYPE_GENERICINST:
810                 type = &type->data.generic_class->container_class->byval_arg;
811                 goto handle_enum;
812         case MONO_TYPE_VAR:
813         case MONO_TYPE_MVAR:
814                 g_assert (cfg->gshared);
815                 if (mini_is_gsharedvt_type (type)) {
816                         g_assert (cfg->gsharedvt);
817                         inst->type = STACK_VTYPE;
818                 } else {
819                         type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
820                 }
821                 return;
822         default:
823                 g_error ("unknown type 0x%02x in eval stack type", type->type);
824         }
825 }
826
827 /*
828  * The following tables are used to quickly validate the IL code in type_from_op ().
829  */
830 static const char
831 bin_num_table [STACK_MAX] [STACK_MAX] = {
832         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
833         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
834         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
835         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
836         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8,  STACK_INV, STACK_INV, STACK_INV, STACK_R8},
837         {STACK_INV, STACK_MP,  STACK_INV, STACK_MP,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
838         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
839         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
840         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
841 };
842
843 static const char 
844 neg_table [] = {
845         STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
846 };
847
848 /* reduce the size of this table */
849 static const char
850 bin_int_table [STACK_MAX] [STACK_MAX] = {
851         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
852         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
853         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
854         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
855         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
856         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
857         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
858         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
859 };
860
861 static const char
862 bin_comp_table [STACK_MAX] [STACK_MAX] = {
863 /*      Inv i  L  p  F  &  O  vt r4 */
864         {0},
865         {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
866         {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
867         {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
868         {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
869         {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
870         {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
871         {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
872         {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
873 };
874
875 /* reduce the size of this table */
876 static const char
877 shift_table [STACK_MAX] [STACK_MAX] = {
878         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
879         {STACK_INV, STACK_I4,  STACK_INV, STACK_I4,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
880         {STACK_INV, STACK_I8,  STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
881         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
882         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
883         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
884         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
885         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
886 };
887
888 /*
889  * Tables to map from the non-specific opcode to the matching
890  * type-specific opcode.
891  */
892 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
893 static const guint16
894 binops_op_map [STACK_MAX] = {
895         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
896 };
897
898 /* handles from CEE_NEG to CEE_CONV_U8 */
899 static const guint16
900 unops_op_map [STACK_MAX] = {
901         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
902 };
903
904 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
905 static const guint16
906 ovfops_op_map [STACK_MAX] = {
907         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
908 };
909
910 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
911 static const guint16
912 ovf2ops_op_map [STACK_MAX] = {
913         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
914 };
915
916 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
917 static const guint16
918 ovf3ops_op_map [STACK_MAX] = {
919         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
920 };
921
922 /* handles from CEE_BEQ to CEE_BLT_UN */
923 static const guint16
924 beqops_op_map [STACK_MAX] = {
925         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
926 };
927
928 /* handles from CEE_CEQ to CEE_CLT_UN */
929 static const guint16
930 ceqops_op_map [STACK_MAX] = {
931         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
932 };
933
934 /*
935  * Sets ins->type (the type on the eval stack) according to the
936  * type of the opcode and the arguments to it.
937  * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
938  *
939  * FIXME: this function sets ins->type unconditionally in some cases, but
940  * it should set it to invalid for some types (a conv.x on an object)
941  */
942 static void
943 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
944 {
945         switch (ins->opcode) {
946         /* binops */
947         case CEE_ADD:
948         case CEE_SUB:
949         case CEE_MUL:
950         case CEE_DIV:
951         case CEE_REM:
952                 /* FIXME: check unverifiable args for STACK_MP */
953                 ins->type = bin_num_table [src1->type] [src2->type];
954                 ins->opcode += binops_op_map [ins->type];
955                 break;
956         case CEE_DIV_UN:
957         case CEE_REM_UN:
958         case CEE_AND:
959         case CEE_OR:
960         case CEE_XOR:
961                 ins->type = bin_int_table [src1->type] [src2->type];
962                 ins->opcode += binops_op_map [ins->type];
963                 break;
964         case CEE_SHL:
965         case CEE_SHR:
966         case CEE_SHR_UN:
967                 ins->type = shift_table [src1->type] [src2->type];
968                 ins->opcode += binops_op_map [ins->type];
969                 break;
970         case OP_COMPARE:
971         case OP_LCOMPARE:
972         case OP_ICOMPARE:
973                 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
974                 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
975                         ins->opcode = OP_LCOMPARE;
976                 else if (src1->type == STACK_R4)
977                         ins->opcode = OP_RCOMPARE;
978                 else if (src1->type == STACK_R8)
979                         ins->opcode = OP_FCOMPARE;
980                 else
981                         ins->opcode = OP_ICOMPARE;
982                 break;
983         case OP_ICOMPARE_IMM:
984                 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
985                 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
986                         ins->opcode = OP_LCOMPARE_IMM;          
987                 break;
988         case CEE_BEQ:
989         case CEE_BGE:
990         case CEE_BGT:
991         case CEE_BLE:
992         case CEE_BLT:
993         case CEE_BNE_UN:
994         case CEE_BGE_UN:
995         case CEE_BGT_UN:
996         case CEE_BLE_UN:
997         case CEE_BLT_UN:
998                 ins->opcode += beqops_op_map [src1->type];
999                 break;
1000         case OP_CEQ:
1001                 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
1002                 ins->opcode += ceqops_op_map [src1->type];
1003                 break;
1004         case OP_CGT:
1005         case OP_CGT_UN:
1006         case OP_CLT:
1007         case OP_CLT_UN:
1008                 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1009                 ins->opcode += ceqops_op_map [src1->type];
1010                 break;
1011         /* unops */
1012         case CEE_NEG:
1013                 ins->type = neg_table [src1->type];
1014                 ins->opcode += unops_op_map [ins->type];
1015                 break;
1016         case CEE_NOT:
1017                 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1018                         ins->type = src1->type;
1019                 else
1020                         ins->type = STACK_INV;
1021                 ins->opcode += unops_op_map [ins->type];
1022                 break;
1023         case CEE_CONV_I1:
1024         case CEE_CONV_I2:
1025         case CEE_CONV_I4:
1026         case CEE_CONV_U4:
1027                 ins->type = STACK_I4;
1028                 ins->opcode += unops_op_map [src1->type];
1029                 break;
1030         case CEE_CONV_R_UN:
1031                 ins->type = STACK_R8;
1032                 switch (src1->type) {
1033                 case STACK_I4:
1034                 case STACK_PTR:
1035                         ins->opcode = OP_ICONV_TO_R_UN;
1036                         break;
1037                 case STACK_I8:
1038                         ins->opcode = OP_LCONV_TO_R_UN; 
1039                         break;
1040                 }
1041                 break;
1042         case CEE_CONV_OVF_I1:
1043         case CEE_CONV_OVF_U1:
1044         case CEE_CONV_OVF_I2:
1045         case CEE_CONV_OVF_U2:
1046         case CEE_CONV_OVF_I4:
1047         case CEE_CONV_OVF_U4:
1048                 ins->type = STACK_I4;
1049                 ins->opcode += ovf3ops_op_map [src1->type];
1050                 break;
1051         case CEE_CONV_OVF_I_UN:
1052         case CEE_CONV_OVF_U_UN:
1053                 ins->type = STACK_PTR;
1054                 ins->opcode += ovf2ops_op_map [src1->type];
1055                 break;
1056         case CEE_CONV_OVF_I1_UN:
1057         case CEE_CONV_OVF_I2_UN:
1058         case CEE_CONV_OVF_I4_UN:
1059         case CEE_CONV_OVF_U1_UN:
1060         case CEE_CONV_OVF_U2_UN:
1061         case CEE_CONV_OVF_U4_UN:
1062                 ins->type = STACK_I4;
1063                 ins->opcode += ovf2ops_op_map [src1->type];
1064                 break;
1065         case CEE_CONV_U:
1066                 ins->type = STACK_PTR;
1067                 switch (src1->type) {
1068                 case STACK_I4:
1069                         ins->opcode = OP_ICONV_TO_U;
1070                         break;
1071                 case STACK_PTR:
1072                 case STACK_MP:
1073                 case STACK_OBJ:
1074 #if SIZEOF_VOID_P == 8
1075                         ins->opcode = OP_LCONV_TO_U;
1076 #else
1077                         ins->opcode = OP_MOVE;
1078 #endif
1079                         break;
1080                 case STACK_I8:
1081                         ins->opcode = OP_LCONV_TO_U;
1082                         break;
1083                 case STACK_R8:
1084                         ins->opcode = OP_FCONV_TO_U;
1085                         break;
1086                 }
1087                 break;
1088         case CEE_CONV_I8:
1089         case CEE_CONV_U8:
1090                 ins->type = STACK_I8;
1091                 ins->opcode += unops_op_map [src1->type];
1092                 break;
1093         case CEE_CONV_OVF_I8:
1094         case CEE_CONV_OVF_U8:
1095                 ins->type = STACK_I8;
1096                 ins->opcode += ovf3ops_op_map [src1->type];
1097                 break;
1098         case CEE_CONV_OVF_U8_UN:
1099         case CEE_CONV_OVF_I8_UN:
1100                 ins->type = STACK_I8;
1101                 ins->opcode += ovf2ops_op_map [src1->type];
1102                 break;
1103         case CEE_CONV_R4:
1104                 ins->type = cfg->r4_stack_type;
1105                 ins->opcode += unops_op_map [src1->type];
1106                 break;
1107         case CEE_CONV_R8:
1108                 ins->type = STACK_R8;
1109                 ins->opcode += unops_op_map [src1->type];
1110                 break;
1111         case OP_CKFINITE:
1112                 ins->type = STACK_R8;           
1113                 break;
1114         case CEE_CONV_U2:
1115         case CEE_CONV_U1:
1116                 ins->type = STACK_I4;
1117                 ins->opcode += ovfops_op_map [src1->type];
1118                 break;
1119         case CEE_CONV_I:
1120         case CEE_CONV_OVF_I:
1121         case CEE_CONV_OVF_U:
1122                 ins->type = STACK_PTR;
1123                 ins->opcode += ovfops_op_map [src1->type];
1124                 break;
1125         case CEE_ADD_OVF:
1126         case CEE_ADD_OVF_UN:
1127         case CEE_MUL_OVF:
1128         case CEE_MUL_OVF_UN:
1129         case CEE_SUB_OVF:
1130         case CEE_SUB_OVF_UN:
1131                 ins->type = bin_num_table [src1->type] [src2->type];
1132                 ins->opcode += ovfops_op_map [src1->type];
1133                 if (ins->type == STACK_R8)
1134                         ins->type = STACK_INV;
1135                 break;
1136         case OP_LOAD_MEMBASE:
1137                 ins->type = STACK_PTR;
1138                 break;
1139         case OP_LOADI1_MEMBASE:
1140         case OP_LOADU1_MEMBASE:
1141         case OP_LOADI2_MEMBASE:
1142         case OP_LOADU2_MEMBASE:
1143         case OP_LOADI4_MEMBASE:
1144         case OP_LOADU4_MEMBASE:
1145                 ins->type = STACK_PTR;
1146                 break;
1147         case OP_LOADI8_MEMBASE:
1148                 ins->type = STACK_I8;
1149                 break;
1150         case OP_LOADR4_MEMBASE:
1151                 ins->type = cfg->r4_stack_type;
1152                 break;
1153         case OP_LOADR8_MEMBASE:
1154                 ins->type = STACK_R8;
1155                 break;
1156         default:
1157                 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1158                 break;
1159         }
1160
1161         if (ins->type == STACK_MP)
1162                 ins->klass = mono_defaults.object_class;
1163 }
1164
1165 static MonoClass*
1166 ldind_to_type (int op)
1167 {
1168         switch (op) {
1169         case CEE_LDIND_I1: return mono_defaults.sbyte_class;
1170         case CEE_LDIND_U1: return mono_defaults.byte_class;
1171         case CEE_LDIND_I2: return mono_defaults.int16_class;
1172         case CEE_LDIND_U2: return mono_defaults.uint16_class;
1173         case CEE_LDIND_I4: return mono_defaults.int32_class;
1174         case CEE_LDIND_U4: return mono_defaults.uint32_class;
1175         case CEE_LDIND_I8: return mono_defaults.int64_class;
1176         case CEE_LDIND_I: return mono_defaults.int_class;
1177         case CEE_LDIND_R4: return mono_defaults.single_class;
1178         case CEE_LDIND_R8: return mono_defaults.double_class;
1179         case CEE_LDIND_REF:return mono_defaults.object_class; //FIXME we should try to return a more specific type
1180         default: g_error ("Unknown ldind type %d", op);
1181         }
1182 }
1183
1184 #if 0
1185
1186 static const char
1187 param_table [STACK_MAX] [STACK_MAX] = {
1188         {0},
1189 };
1190
1191 static int
1192 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1193 {
1194         int i;
1195
1196         if (sig->hasthis) {
1197                 switch (args->type) {
1198                 case STACK_I4:
1199                 case STACK_I8:
1200                 case STACK_R8:
1201                 case STACK_VTYPE:
1202                 case STACK_INV:
1203                         return 0;
1204                 }
1205                 args++;
1206         }
1207         for (i = 0; i < sig->param_count; ++i) {
1208                 switch (args [i].type) {
1209                 case STACK_INV:
1210                         return 0;
1211                 case STACK_MP:
1212                         if (!sig->params [i]->byref)
1213                                 return 0;
1214                         continue;
1215                 case STACK_OBJ:
1216                         if (sig->params [i]->byref)
1217                                 return 0;
1218                         switch (sig->params [i]->type) {
1219                         case MONO_TYPE_CLASS:
1220                         case MONO_TYPE_STRING:
1221                         case MONO_TYPE_OBJECT:
1222                         case MONO_TYPE_SZARRAY:
1223                         case MONO_TYPE_ARRAY:
1224                                 break;
1225                         default:
1226                                 return 0;
1227                         }
1228                         continue;
1229                 case STACK_R8:
1230                         if (sig->params [i]->byref)
1231                                 return 0;
1232                         if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1233                                 return 0;
1234                         continue;
1235                 case STACK_PTR:
1236                 case STACK_I4:
1237                 case STACK_I8:
1238                 case STACK_VTYPE:
1239                         break;
1240                 }
1241                 /*if (!param_table [args [i].type] [sig->params [i]->type])
1242                         return 0;*/
1243         }
1244         return 1;
1245 }
1246 #endif
1247
1248 /*
1249  * When we need a pointer to the current domain many times in a method, we
1250  * call mono_domain_get() once and we store the result in a local variable.
1251  * This function returns the variable that represents the MonoDomain*.
1252  */
1253 inline static MonoInst *
1254 mono_get_domainvar (MonoCompile *cfg)
1255 {
1256         if (!cfg->domainvar)
1257                 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1258         return cfg->domainvar;
1259 }
1260
1261 /*
1262  * The got_var contains the address of the Global Offset Table when AOT 
1263  * compiling.
1264  */
1265 MonoInst *
1266 mono_get_got_var (MonoCompile *cfg)
1267 {
1268         if (!cfg->compile_aot || !cfg->backend->need_got_var || cfg->llvm_only)
1269                 return NULL;
1270         if (!cfg->got_var) {
1271                 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1272         }
1273         return cfg->got_var;
1274 }
1275
1276 static void
1277 mono_create_rgctx_var (MonoCompile *cfg)
1278 {
1279         if (!cfg->rgctx_var) {
1280                 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1281                 /* force the var to be stack allocated */
1282                 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1283         }
1284 }
1285
1286 static MonoInst *
1287 mono_get_vtable_var (MonoCompile *cfg)
1288 {
1289         g_assert (cfg->gshared);
1290
1291         mono_create_rgctx_var (cfg);
1292
1293         return cfg->rgctx_var;
1294 }
1295
1296 static MonoType*
1297 type_from_stack_type (MonoInst *ins) {
1298         switch (ins->type) {
1299         case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1300         case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1301         case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1302         case STACK_R4: return &mono_defaults.single_class->byval_arg;
1303         case STACK_R8: return &mono_defaults.double_class->byval_arg;
1304         case STACK_MP:
1305                 return &ins->klass->this_arg;
1306         case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1307         case STACK_VTYPE: return &ins->klass->byval_arg;
1308         default:
1309                 g_error ("stack type %d to monotype not handled\n", ins->type);
1310         }
1311         return NULL;
1312 }
1313
1314 static G_GNUC_UNUSED int
1315 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1316 {
1317         t = mono_type_get_underlying_type (t);
1318         switch (t->type) {
1319         case MONO_TYPE_I1:
1320         case MONO_TYPE_U1:
1321         case MONO_TYPE_I2:
1322         case MONO_TYPE_U2:
1323         case MONO_TYPE_I4:
1324         case MONO_TYPE_U4:
1325                 return STACK_I4;
1326         case MONO_TYPE_I:
1327         case MONO_TYPE_U:
1328         case MONO_TYPE_PTR:
1329         case MONO_TYPE_FNPTR:
1330                 return STACK_PTR;
1331         case MONO_TYPE_CLASS:
1332         case MONO_TYPE_STRING:
1333         case MONO_TYPE_OBJECT:
1334         case MONO_TYPE_SZARRAY:
1335         case MONO_TYPE_ARRAY:    
1336                 return STACK_OBJ;
1337         case MONO_TYPE_I8:
1338         case MONO_TYPE_U8:
1339                 return STACK_I8;
1340         case MONO_TYPE_R4:
1341                 return cfg->r4_stack_type;
1342         case MONO_TYPE_R8:
1343                 return STACK_R8;
1344         case MONO_TYPE_VALUETYPE:
1345         case MONO_TYPE_TYPEDBYREF:
1346                 return STACK_VTYPE;
1347         case MONO_TYPE_GENERICINST:
1348                 if (mono_type_generic_inst_is_valuetype (t))
1349                         return STACK_VTYPE;
1350                 else
1351                         return STACK_OBJ;
1352                 break;
1353         default:
1354                 g_assert_not_reached ();
1355         }
1356
1357         return -1;
1358 }
1359
1360 static MonoClass*
1361 array_access_to_klass (int opcode)
1362 {
1363         switch (opcode) {
1364         case CEE_LDELEM_U1:
1365                 return mono_defaults.byte_class;
1366         case CEE_LDELEM_U2:
1367                 return mono_defaults.uint16_class;
1368         case CEE_LDELEM_I:
1369         case CEE_STELEM_I:
1370                 return mono_defaults.int_class;
1371         case CEE_LDELEM_I1:
1372         case CEE_STELEM_I1:
1373                 return mono_defaults.sbyte_class;
1374         case CEE_LDELEM_I2:
1375         case CEE_STELEM_I2:
1376                 return mono_defaults.int16_class;
1377         case CEE_LDELEM_I4:
1378         case CEE_STELEM_I4:
1379                 return mono_defaults.int32_class;
1380         case CEE_LDELEM_U4:
1381                 return mono_defaults.uint32_class;
1382         case CEE_LDELEM_I8:
1383         case CEE_STELEM_I8:
1384                 return mono_defaults.int64_class;
1385         case CEE_LDELEM_R4:
1386         case CEE_STELEM_R4:
1387                 return mono_defaults.single_class;
1388         case CEE_LDELEM_R8:
1389         case CEE_STELEM_R8:
1390                 return mono_defaults.double_class;
1391         case CEE_LDELEM_REF:
1392         case CEE_STELEM_REF:
1393                 return mono_defaults.object_class;
1394         default:
1395                 g_assert_not_reached ();
1396         }
1397         return NULL;
1398 }
1399
1400 /*
1401  * We try to share variables when possible
1402  */
1403 static MonoInst *
1404 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1405 {
1406         MonoInst *res;
1407         int pos, vnum;
1408
1409         /* inlining can result in deeper stacks */ 
1410         if (slot >= cfg->header->max_stack)
1411                 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1412
1413         pos = ins->type - 1 + slot * STACK_MAX;
1414
1415         switch (ins->type) {
1416         case STACK_I4:
1417         case STACK_I8:
1418         case STACK_R8:
1419         case STACK_PTR:
1420         case STACK_MP:
1421         case STACK_OBJ:
1422                 if ((vnum = cfg->intvars [pos]))
1423                         return cfg->varinfo [vnum];
1424                 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1425                 cfg->intvars [pos] = res->inst_c0;
1426                 break;
1427         default:
1428                 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1429         }
1430         return res;
1431 }
1432
1433 static void
1434 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1435 {
1436         /* 
1437          * Don't use this if a generic_context is set, since that means AOT can't
1438          * look up the method using just the image+token.
1439          * table == 0 means this is a reference made from a wrapper.
1440          */
1441         if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1442                 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1443                 jump_info_token->image = image;
1444                 jump_info_token->token = token;
1445                 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1446         }
1447 }
1448
1449 /*
1450  * This function is called to handle items that are left on the evaluation stack
1451  * at basic block boundaries. What happens is that we save the values to local variables
1452  * and we reload them later when first entering the target basic block (with the
1453  * handle_loaded_temps () function).
1454  * A single joint point will use the same variables (stored in the array bb->out_stack or
1455  * bb->in_stack, if the basic block is before or after the joint point).
1456  *
1457  * This function needs to be called _before_ emitting the last instruction of
1458  * the bb (i.e. before emitting a branch).
1459  * If the stack merge fails at a join point, cfg->unverifiable is set.
1460  */
1461 static void
1462 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1463 {
1464         int i, bindex;
1465         MonoBasicBlock *bb = cfg->cbb;
1466         MonoBasicBlock *outb;
1467         MonoInst *inst, **locals;
1468         gboolean found;
1469
1470         if (!count)
1471                 return;
1472         if (cfg->verbose_level > 3)
1473                 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1474         if (!bb->out_scount) {
1475                 bb->out_scount = count;
1476                 //printf ("bblock %d has out:", bb->block_num);
1477                 found = FALSE;
1478                 for (i = 0; i < bb->out_count; ++i) {
1479                         outb = bb->out_bb [i];
1480                         /* exception handlers are linked, but they should not be considered for stack args */
1481                         if (outb->flags & BB_EXCEPTION_HANDLER)
1482                                 continue;
1483                         //printf (" %d", outb->block_num);
1484                         if (outb->in_stack) {
1485                                 found = TRUE;
1486                                 bb->out_stack = outb->in_stack;
1487                                 break;
1488                         }
1489                 }
1490                 //printf ("\n");
1491                 if (!found) {
1492                         bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1493                         for (i = 0; i < count; ++i) {
1494                                 /* 
1495                                  * try to reuse temps already allocated for this purpouse, if they occupy the same
1496                                  * stack slot and if they are of the same type.
1497                                  * This won't cause conflicts since if 'local' is used to 
1498                                  * store one of the values in the in_stack of a bblock, then
1499                                  * the same variable will be used for the same outgoing stack 
1500                                  * slot as well. 
1501                                  * This doesn't work when inlining methods, since the bblocks
1502                                  * in the inlined methods do not inherit their in_stack from
1503                                  * the bblock they are inlined to. See bug #58863 for an
1504                                  * example.
1505                                  */
1506                                 if (cfg->inlined_method)
1507                                         bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1508                                 else
1509                                         bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1510                         }
1511                 }
1512         }
1513
1514         for (i = 0; i < bb->out_count; ++i) {
1515                 outb = bb->out_bb [i];
1516                 /* exception handlers are linked, but they should not be considered for stack args */
1517                 if (outb->flags & BB_EXCEPTION_HANDLER)
1518                         continue;
1519                 if (outb->in_scount) {
1520                         if (outb->in_scount != bb->out_scount) {
1521                                 cfg->unverifiable = TRUE;
1522                                 return;
1523                         }
1524                         continue; /* check they are the same locals */
1525                 }
1526                 outb->in_scount = count;
1527                 outb->in_stack = bb->out_stack;
1528         }
1529
1530         locals = bb->out_stack;
1531         cfg->cbb = bb;
1532         for (i = 0; i < count; ++i) {
1533                 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1534                 inst->cil_code = sp [i]->cil_code;
1535                 sp [i] = locals [i];
1536                 if (cfg->verbose_level > 3)
1537                         printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1538         }
1539
1540         /*
1541          * It is possible that the out bblocks already have in_stack assigned, and
1542          * the in_stacks differ. In this case, we will store to all the different 
1543          * in_stacks.
1544          */
1545
1546         found = TRUE;
1547         bindex = 0;
1548         while (found) {
1549                 /* Find a bblock which has a different in_stack */
1550                 found = FALSE;
1551                 while (bindex < bb->out_count) {
1552                         outb = bb->out_bb [bindex];
1553                         /* exception handlers are linked, but they should not be considered for stack args */
1554                         if (outb->flags & BB_EXCEPTION_HANDLER) {
1555                                 bindex++;
1556                                 continue;
1557                         }
1558                         if (outb->in_stack != locals) {
1559                                 for (i = 0; i < count; ++i) {
1560                                         EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1561                                         inst->cil_code = sp [i]->cil_code;
1562                                         sp [i] = locals [i];
1563                                         if (cfg->verbose_level > 3)
1564                                                 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1565                                 }
1566                                 locals = outb->in_stack;
1567                                 found = TRUE;
1568                                 break;
1569                         }
1570                         bindex ++;
1571                 }
1572         }
1573 }
1574
1575 MonoInst*
1576 mini_emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1577 {
1578         MonoInst *ins;
1579
1580         if (cfg->compile_aot) {
1581                 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1582         } else {
1583                 MonoJumpInfo ji;
1584                 gpointer target;
1585                 MonoError error;
1586
1587                 ji.type = patch_type;
1588                 ji.data.target = data;
1589                 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1590                 mono_error_assert_ok (&error);
1591
1592                 EMIT_NEW_PCONST (cfg, ins, target);
1593         }
1594         return ins;
1595 }
1596
1597 static MonoInst*
1598 mono_create_fast_tls_getter (MonoCompile *cfg, MonoTlsKey key)
1599 {
1600         int tls_offset = mono_tls_get_tls_offset (key);
1601
1602         if (cfg->compile_aot)
1603                 return NULL;
1604
1605         if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1606                 MonoInst *ins;
1607                 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
1608                 ins->dreg = mono_alloc_preg (cfg);
1609                 ins->inst_offset = tls_offset;
1610                 return ins;
1611         }
1612         return NULL;
1613 }
1614
1615 static MonoInst*
1616 mono_create_fast_tls_setter (MonoCompile *cfg, MonoInst* value, MonoTlsKey key)
1617 {
1618         int tls_offset = mono_tls_get_tls_offset (key);
1619
1620         if (cfg->compile_aot)
1621                 return NULL;
1622
1623         if (tls_offset != -1 && mono_arch_have_fast_tls ()) {
1624                 MonoInst *ins;
1625                 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1626                 ins->sreg1 = value->dreg;
1627                 ins->inst_offset = tls_offset;
1628                 return ins;
1629         }
1630         return NULL;
1631 }
1632
1633
1634 MonoInst*
1635 mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
1636 {
1637         MonoInst *fast_tls = NULL;
1638
1639         if (!mini_get_debug_options ()->use_fallback_tls)
1640                 fast_tls = mono_create_fast_tls_getter (cfg, key);
1641
1642         if (fast_tls) {
1643                 MONO_ADD_INS (cfg->cbb, fast_tls);
1644                 return fast_tls;
1645         }
1646
1647         if (cfg->compile_aot) {
1648                 MonoInst *addr;
1649                 /*
1650                  * tls getters are critical pieces of code and we don't want to resolve them
1651                  * through the standard plt/tramp mechanism since we might expose ourselves
1652                  * to crashes and infinite recursions.
1653                  */
1654                 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GET_TLS_TRAMP, (void*)key);
1655                 return mini_emit_calli (cfg, helper_sig_get_tls_tramp, NULL, addr, NULL, NULL);
1656         } else {
1657                 gpointer getter = mono_tls_get_tls_getter (key, FALSE);
1658                 return mono_emit_jit_icall (cfg, getter, NULL);
1659         }
1660 }
1661
1662 static MonoInst*
1663 mono_create_tls_set (MonoCompile *cfg, MonoInst *value, MonoTlsKey key)
1664 {
1665         MonoInst *fast_tls = NULL;
1666
1667         if (!mini_get_debug_options ()->use_fallback_tls)
1668                 fast_tls = mono_create_fast_tls_setter (cfg, value, key);
1669
1670         if (fast_tls) {
1671                 MONO_ADD_INS (cfg->cbb, fast_tls);
1672                 return fast_tls;
1673         }
1674
1675         if (cfg->compile_aot) {
1676                 MonoInst *addr;
1677                 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_SET_TLS_TRAMP, (void*)key);
1678                 return mini_emit_calli (cfg, helper_sig_set_tls_tramp, &value, addr, NULL, NULL);
1679         } else {
1680                 gpointer setter = mono_tls_get_tls_setter (key, FALSE);
1681                 return mono_emit_jit_icall (cfg, setter, &value);
1682         }
1683 }
1684
1685 /*
1686  * emit_push_lmf:
1687  *
1688  *   Emit IR to push the current LMF onto the LMF stack.
1689  */
1690 static void
1691 emit_push_lmf (MonoCompile *cfg)
1692 {
1693         /*
1694          * Emit IR to push the LMF:
1695          * lmf_addr = <lmf_addr from tls>
1696          * lmf->lmf_addr = lmf_addr
1697          * lmf->prev_lmf = *lmf_addr
1698          * *lmf_addr = lmf
1699          */
1700         MonoInst *ins, *lmf_ins;
1701
1702         if (!cfg->lmf_ir)
1703                 return;
1704
1705         int lmf_reg, prev_lmf_reg;
1706         /*
1707          * Store lmf_addr in a variable, so it can be allocated to a global register.
1708          */
1709         if (!cfg->lmf_addr_var)
1710                 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1711
1712 #ifdef HOST_WIN32
1713         ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
1714         g_assert (ins);
1715         int jit_tls_dreg = ins->dreg;
1716
1717         lmf_reg = alloc_preg (cfg);
1718         EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
1719 #else
1720         lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF_ADDR);
1721         g_assert (lmf_ins);
1722 #endif
1723         lmf_ins->dreg = cfg->lmf_addr_var->dreg;
1724
1725         EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1726         lmf_reg = ins->dreg;
1727
1728         prev_lmf_reg = alloc_preg (cfg);
1729         /* Save previous_lmf */
1730         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
1731         EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
1732         /* Set new lmf */
1733         EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
1734 }
1735
1736 /*
1737  * emit_pop_lmf:
1738  *
1739  *   Emit IR to pop the current LMF from the LMF stack.
1740  */
1741 static void
1742 emit_pop_lmf (MonoCompile *cfg)
1743 {
1744         int lmf_reg, lmf_addr_reg;
1745         MonoInst *ins;
1746
1747         if (!cfg->lmf_ir)
1748                 return;
1749
1750         EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1751         lmf_reg = ins->dreg;
1752
1753         int prev_lmf_reg;
1754         /*
1755          * Emit IR to pop the LMF:
1756          * *(lmf->lmf_addr) = lmf->prev_lmf
1757          */
1758         /* This could be called before emit_push_lmf () */
1759         if (!cfg->lmf_addr_var)
1760                 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1761         lmf_addr_reg = cfg->lmf_addr_var->dreg;
1762
1763         prev_lmf_reg = alloc_preg (cfg);
1764         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1765         EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
1766 }
1767
1768 static int
1769 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
1770 {
1771 handle_enum:
1772         type = mini_get_underlying_type (type);
1773         switch (type->type) {
1774         case MONO_TYPE_VOID:
1775                 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
1776         case MONO_TYPE_I1:
1777         case MONO_TYPE_U1:
1778         case MONO_TYPE_I2:
1779         case MONO_TYPE_U2:
1780         case MONO_TYPE_I4:
1781         case MONO_TYPE_U4:
1782                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1783         case MONO_TYPE_I:
1784         case MONO_TYPE_U:
1785         case MONO_TYPE_PTR:
1786         case MONO_TYPE_FNPTR:
1787                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1788         case MONO_TYPE_CLASS:
1789         case MONO_TYPE_STRING:
1790         case MONO_TYPE_OBJECT:
1791         case MONO_TYPE_SZARRAY:
1792         case MONO_TYPE_ARRAY:    
1793                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1794         case MONO_TYPE_I8:
1795         case MONO_TYPE_U8:
1796                 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
1797         case MONO_TYPE_R4:
1798                 if (cfg->r4fp)
1799                         return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
1800                 else
1801                         return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1802         case MONO_TYPE_R8:
1803                 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1804         case MONO_TYPE_VALUETYPE:
1805                 if (type->data.klass->enumtype) {
1806                         type = mono_class_enum_basetype (type->data.klass);
1807                         goto handle_enum;
1808                 } else
1809                         return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1810         case MONO_TYPE_TYPEDBYREF:
1811                 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1812         case MONO_TYPE_GENERICINST:
1813                 type = &type->data.generic_class->container_class->byval_arg;
1814                 goto handle_enum;
1815         case MONO_TYPE_VAR:
1816         case MONO_TYPE_MVAR:
1817                 /* gsharedvt */
1818                 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1819         default:
1820                 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1821         }
1822         return -1;
1823 }
1824
1825 //XXX this ignores if t is byref
1826 #define MONO_TYPE_IS_PRIMITIVE_SCALAR(t) ((((((t)->type >= MONO_TYPE_BOOLEAN && (t)->type <= MONO_TYPE_U8) || ((t)->type >= MONO_TYPE_I && (t)->type <= MONO_TYPE_U)))))
1827
1828 /*
1829  * target_type_is_incompatible:
1830  * @cfg: MonoCompile context
1831  *
1832  * Check that the item @arg on the evaluation stack can be stored
1833  * in the target type (can be a local, or field, etc).
1834  * The cfg arg can be used to check if we need verification or just
1835  * validity checks.
1836  *
1837  * Returns: non-0 value if arg can't be stored on a target.
1838  */
1839 static int
1840 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
1841 {
1842         MonoType *simple_type;
1843         MonoClass *klass;
1844
1845         if (target->byref) {
1846                 /* FIXME: check that the pointed to types match */
1847                 if (arg->type == STACK_MP) {
1848                         /* This is needed to handle gshared types + ldaddr. We lower the types so we can handle enums and other typedef-like types. */
1849                         MonoClass *target_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&mono_class_from_mono_type (target)->byval_arg));
1850                         MonoClass *source_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg));
1851
1852                         /* if the target is native int& or same type */
1853                         if (target->type == MONO_TYPE_I || target_class_lowered == source_class_lowered)
1854                                 return 0;
1855
1856                         /* Both are primitive type byrefs and the source points to a larger type that the destination */
1857                         if (MONO_TYPE_IS_PRIMITIVE_SCALAR (&target_class_lowered->byval_arg) && MONO_TYPE_IS_PRIMITIVE_SCALAR (&source_class_lowered->byval_arg) &&
1858                                 mono_class_instance_size (target_class_lowered) <= mono_class_instance_size (source_class_lowered))
1859                                 return 0;
1860                         return 1;
1861                 }
1862                 if (arg->type == STACK_PTR)
1863                         return 0;
1864                 return 1;
1865         }
1866
1867         simple_type = mini_get_underlying_type (target);
1868         switch (simple_type->type) {
1869         case MONO_TYPE_VOID:
1870                 return 1;
1871         case MONO_TYPE_I1:
1872         case MONO_TYPE_U1:
1873         case MONO_TYPE_I2:
1874         case MONO_TYPE_U2:
1875         case MONO_TYPE_I4:
1876         case MONO_TYPE_U4:
1877                 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
1878                         return 1;
1879                 return 0;
1880         case MONO_TYPE_PTR:
1881                 /* STACK_MP is needed when setting pinned locals */
1882                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
1883                         return 1;
1884                 return 0;
1885         case MONO_TYPE_I:
1886         case MONO_TYPE_U:
1887         case MONO_TYPE_FNPTR:
1888                 /* 
1889                  * Some opcodes like ldloca returns 'transient pointers' which can be stored in
1890                  * in native int. (#688008).
1891                  */
1892                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
1893                         return 1;
1894                 return 0;
1895         case MONO_TYPE_CLASS:
1896         case MONO_TYPE_STRING:
1897         case MONO_TYPE_OBJECT:
1898         case MONO_TYPE_SZARRAY:
1899         case MONO_TYPE_ARRAY:    
1900                 if (arg->type != STACK_OBJ)
1901                         return 1;
1902                 /* FIXME: check type compatibility */
1903                 return 0;
1904         case MONO_TYPE_I8:
1905         case MONO_TYPE_U8:
1906                 if (arg->type != STACK_I8)
1907                         return 1;
1908                 return 0;
1909         case MONO_TYPE_R4:
1910                 if (arg->type != cfg->r4_stack_type)
1911                         return 1;
1912                 return 0;
1913         case MONO_TYPE_R8:
1914                 if (arg->type != STACK_R8)
1915                         return 1;
1916                 return 0;
1917         case MONO_TYPE_VALUETYPE:
1918                 if (arg->type != STACK_VTYPE)
1919                         return 1;
1920                 klass = mono_class_from_mono_type (simple_type);
1921                 if (klass != arg->klass)
1922                         return 1;
1923                 return 0;
1924         case MONO_TYPE_TYPEDBYREF:
1925                 if (arg->type != STACK_VTYPE)
1926                         return 1;
1927                 klass = mono_class_from_mono_type (simple_type);
1928                 if (klass != arg->klass)
1929                         return 1;
1930                 return 0;
1931         case MONO_TYPE_GENERICINST:
1932                 if (mono_type_generic_inst_is_valuetype (simple_type)) {
1933                         MonoClass *target_class;
1934                         if (arg->type != STACK_VTYPE)
1935                                 return 1;
1936                         klass = mono_class_from_mono_type (simple_type);
1937                         target_class = mono_class_from_mono_type (target);
1938                         /* The second cases is needed when doing partial sharing */
1939                         if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
1940                                 return 1;
1941                         return 0;
1942                 } else {
1943                         if (arg->type != STACK_OBJ)
1944                                 return 1;
1945                         /* FIXME: check type compatibility */
1946                         return 0;
1947                 }
1948         case MONO_TYPE_VAR:
1949         case MONO_TYPE_MVAR:
1950                 g_assert (cfg->gshared);
1951                 if (mini_type_var_is_vt (simple_type)) {
1952                         if (arg->type != STACK_VTYPE)
1953                                 return 1;
1954                 } else {
1955                         if (arg->type != STACK_OBJ)
1956                                 return 1;
1957                 }
1958                 return 0;
1959         default:
1960                 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
1961         }
1962         return 1;
1963 }
1964
1965 /*
1966  * Prepare arguments for passing to a function call.
1967  * Return a non-zero value if the arguments can't be passed to the given
1968  * signature.
1969  * The type checks are not yet complete and some conversions may need
1970  * casts on 32 or 64 bit architectures.
1971  *
1972  * FIXME: implement this using target_type_is_incompatible ()
1973  */
1974 static int
1975 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1976 {
1977         MonoType *simple_type;
1978         int i;
1979
1980         if (sig->hasthis) {
1981                 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1982                         return 1;
1983                 args++;
1984         }
1985         for (i = 0; i < sig->param_count; ++i) {
1986                 if (sig->params [i]->byref) {
1987                         if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1988                                 return 1;
1989                         continue;
1990                 }
1991                 simple_type = mini_get_underlying_type (sig->params [i]);
1992 handle_enum:
1993                 switch (simple_type->type) {
1994                 case MONO_TYPE_VOID:
1995                         return 1;
1996                         continue;
1997                 case MONO_TYPE_I1:
1998                 case MONO_TYPE_U1:
1999                 case MONO_TYPE_I2:
2000                 case MONO_TYPE_U2:
2001                 case MONO_TYPE_I4:
2002                 case MONO_TYPE_U4:
2003                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2004                                 return 1;
2005                         continue;
2006                 case MONO_TYPE_I:
2007                 case MONO_TYPE_U:
2008                 case MONO_TYPE_PTR:
2009                 case MONO_TYPE_FNPTR:
2010                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2011                                 return 1;
2012                         continue;
2013                 case MONO_TYPE_CLASS:
2014                 case MONO_TYPE_STRING:
2015                 case MONO_TYPE_OBJECT:
2016                 case MONO_TYPE_SZARRAY:
2017                 case MONO_TYPE_ARRAY:    
2018                         if (args [i]->type != STACK_OBJ)
2019                                 return 1;
2020                         continue;
2021                 case MONO_TYPE_I8:
2022                 case MONO_TYPE_U8:
2023                         if (args [i]->type != STACK_I8)
2024                                 return 1;
2025                         continue;
2026                 case MONO_TYPE_R4:
2027                         if (args [i]->type != cfg->r4_stack_type)
2028                                 return 1;
2029                         continue;
2030                 case MONO_TYPE_R8:
2031                         if (args [i]->type != STACK_R8)
2032                                 return 1;
2033                         continue;
2034                 case MONO_TYPE_VALUETYPE:
2035                         if (simple_type->data.klass->enumtype) {
2036                                 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2037                                 goto handle_enum;
2038                         }
2039                         if (args [i]->type != STACK_VTYPE)
2040                                 return 1;
2041                         continue;
2042                 case MONO_TYPE_TYPEDBYREF:
2043                         if (args [i]->type != STACK_VTYPE)
2044                                 return 1;
2045                         continue;
2046                 case MONO_TYPE_GENERICINST:
2047                         simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2048                         goto handle_enum;
2049                 case MONO_TYPE_VAR:
2050                 case MONO_TYPE_MVAR:
2051                         /* gsharedvt */
2052                         if (args [i]->type != STACK_VTYPE)
2053                                 return 1;
2054                         continue;
2055                 default:
2056                         g_error ("unknown type 0x%02x in check_call_signature",
2057                                  simple_type->type);
2058                 }
2059         }
2060         return 0;
2061 }
2062
2063 static int
2064 callvirt_to_call (int opcode)
2065 {
2066         switch (opcode) {
2067         case OP_CALL_MEMBASE:
2068                 return OP_CALL;
2069         case OP_VOIDCALL_MEMBASE:
2070                 return OP_VOIDCALL;
2071         case OP_FCALL_MEMBASE:
2072                 return OP_FCALL;
2073         case OP_RCALL_MEMBASE:
2074                 return OP_RCALL;
2075         case OP_VCALL_MEMBASE:
2076                 return OP_VCALL;
2077         case OP_LCALL_MEMBASE:
2078                 return OP_LCALL;
2079         default:
2080                 g_assert_not_reached ();
2081         }
2082
2083         return -1;
2084 }
2085
2086 static int
2087 callvirt_to_call_reg (int opcode)
2088 {
2089         switch (opcode) {
2090         case OP_CALL_MEMBASE:
2091                 return OP_CALL_REG;
2092         case OP_VOIDCALL_MEMBASE:
2093                 return OP_VOIDCALL_REG;
2094         case OP_FCALL_MEMBASE:
2095                 return OP_FCALL_REG;
2096         case OP_RCALL_MEMBASE:
2097                 return OP_RCALL_REG;
2098         case OP_VCALL_MEMBASE:
2099                 return OP_VCALL_REG;
2100         case OP_LCALL_MEMBASE:
2101                 return OP_LCALL_REG;
2102         default:
2103                 g_assert_not_reached ();
2104         }
2105
2106         return -1;
2107 }
2108
2109 /* Either METHOD or IMT_ARG needs to be set */
2110 static void
2111 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2112 {
2113         int method_reg;
2114
2115         if (COMPILE_LLVM (cfg)) {
2116                 if (imt_arg) {
2117                         method_reg = alloc_preg (cfg);
2118                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2119                 } else {
2120                         MonoInst *ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2121                         method_reg = ins->dreg;
2122                 }
2123
2124 #ifdef ENABLE_LLVM
2125                 call->imt_arg_reg = method_reg;
2126 #endif
2127                 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2128                 return;
2129         }
2130
2131         if (imt_arg) {
2132                 method_reg = alloc_preg (cfg);
2133                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2134         } else {
2135                 MonoInst *ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2136                 method_reg = ins->dreg;
2137         }
2138
2139         mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2140 }
2141
2142 static MonoJumpInfo *
2143 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2144 {
2145         MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2146
2147         ji->ip.i = ip;
2148         ji->type = type;
2149         ji->data.target = target;
2150
2151         return ji;
2152 }
2153
2154 int
2155 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2156 {
2157         if (cfg->gshared)
2158                 return mono_class_check_context_used (klass);
2159         else
2160                 return 0;
2161 }
2162
2163 static int
2164 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2165 {
2166         if (cfg->gshared)
2167                 return mono_method_check_context_used (method);
2168         else
2169                 return 0;
2170 }
2171
2172 /*
2173  * check_method_sharing:
2174  *
2175  *   Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2176  */
2177 static void
2178 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2179 {
2180         gboolean pass_vtable = FALSE;
2181         gboolean pass_mrgctx = FALSE;
2182
2183         if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2184                 (mono_class_is_ginst (cmethod->klass) || mono_class_is_gtd (cmethod->klass))) {
2185                 gboolean sharable = FALSE;
2186
2187                 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2188                         sharable = TRUE;
2189
2190                 /*
2191                  * Pass vtable iff target method might
2192                  * be shared, which means that sharing
2193                  * is enabled for its class and its
2194                  * context is sharable (and it's not a
2195                  * generic method).
2196                  */
2197                 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2198                         pass_vtable = TRUE;
2199         }
2200
2201         if (mini_method_get_context (cmethod) &&
2202                 mini_method_get_context (cmethod)->method_inst) {
2203                 g_assert (!pass_vtable);
2204
2205                 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2206                         pass_mrgctx = TRUE;
2207                 } else {
2208                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2209                                 pass_mrgctx = TRUE;
2210                 }
2211         }
2212
2213         if (out_pass_vtable)
2214                 *out_pass_vtable = pass_vtable;
2215         if (out_pass_mrgctx)
2216                 *out_pass_mrgctx = pass_mrgctx;
2217 }
2218
2219 inline static MonoCallInst *
2220 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig, 
2221                                          MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline, MonoMethod *target)
2222 {
2223         MonoType *sig_ret;
2224         MonoCallInst *call;
2225 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2226         int i;
2227 #endif
2228
2229         if (cfg->llvm_only)
2230                 tail = FALSE;
2231
2232         if (tail) {
2233                 mini_profiler_emit_tail_call (cfg, target);
2234
2235                 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2236         } else
2237                 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2238
2239         call->args = args;
2240         call->signature = sig;
2241         call->rgctx_reg = rgctx;
2242         sig_ret = mini_get_underlying_type (sig->ret);
2243
2244         type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2245
2246         if (tail) {
2247                 if (mini_type_is_vtype (sig_ret)) {
2248                         call->vret_var = cfg->vret_addr;
2249                         //g_assert_not_reached ();
2250                 }
2251         } else if (mini_type_is_vtype (sig_ret)) {
2252                 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2253                 MonoInst *loada;
2254
2255                 temp->backend.is_pinvoke = sig->pinvoke;
2256
2257                 /*
2258                  * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2259                  * address of return value to increase optimization opportunities.
2260                  * Before vtype decomposition, the dreg of the call ins itself represents the
2261                  * fact the call modifies the return value. After decomposition, the call will
2262                  * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2263                  * will be transformed into an LDADDR.
2264                  */
2265                 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2266                 loada->dreg = alloc_preg (cfg);
2267                 loada->inst_p0 = temp;
2268                 /* We reference the call too since call->dreg could change during optimization */
2269                 loada->inst_p1 = call;
2270                 MONO_ADD_INS (cfg->cbb, loada);
2271
2272                 call->inst.dreg = temp->dreg;
2273
2274                 call->vret_var = loada;
2275         } else if (!MONO_TYPE_IS_VOID (sig_ret))
2276                 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2277
2278 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2279         if (COMPILE_SOFT_FLOAT (cfg)) {
2280                 /* 
2281                  * If the call has a float argument, we would need to do an r8->r4 conversion using 
2282                  * an icall, but that cannot be done during the call sequence since it would clobber
2283                  * the call registers + the stack. So we do it before emitting the call.
2284                  */
2285                 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2286                         MonoType *t;
2287                         MonoInst *in = call->args [i];
2288
2289                         if (i >= sig->hasthis)
2290                                 t = sig->params [i - sig->hasthis];
2291                         else
2292                                 t = &mono_defaults.int_class->byval_arg;
2293                         t = mono_type_get_underlying_type (t);
2294
2295                         if (!t->byref && t->type == MONO_TYPE_R4) {
2296                                 MonoInst *iargs [1];
2297                                 MonoInst *conv;
2298
2299                                 iargs [0] = in;
2300                                 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2301
2302                                 /* The result will be in an int vreg */
2303                                 call->args [i] = conv;
2304                         }
2305                 }
2306         }
2307 #endif
2308
2309         call->need_unbox_trampoline = unbox_trampoline;
2310
2311 #ifdef ENABLE_LLVM
2312         if (COMPILE_LLVM (cfg))
2313                 mono_llvm_emit_call (cfg, call);
2314         else
2315                 mono_arch_emit_call (cfg, call);
2316 #else
2317         mono_arch_emit_call (cfg, call);
2318 #endif
2319
2320         cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2321         cfg->flags |= MONO_CFG_HAS_CALLS;
2322         
2323         return call;
2324 }
2325
2326 static void
2327 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2328 {
2329         mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2330         cfg->uses_rgctx_reg = TRUE;
2331         call->rgctx_reg = TRUE;
2332 #ifdef ENABLE_LLVM
2333         call->rgctx_arg_reg = rgctx_reg;
2334 #endif
2335 }       
2336
2337 MonoInst*
2338 mini_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2339 {
2340         MonoCallInst *call;
2341         MonoInst *ins;
2342         int rgctx_reg = -1;
2343         gboolean check_sp = FALSE;
2344
2345         if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2346                 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2347
2348                 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2349                         check_sp = TRUE;
2350         }
2351
2352         if (rgctx_arg) {
2353                 rgctx_reg = mono_alloc_preg (cfg);
2354                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2355         }
2356
2357         if (check_sp) {
2358                 if (!cfg->stack_inbalance_var)
2359                         cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2360
2361                 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2362                 ins->dreg = cfg->stack_inbalance_var->dreg;
2363                 MONO_ADD_INS (cfg->cbb, ins);
2364         }
2365
2366         call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE, NULL);
2367
2368         call->inst.sreg1 = addr->dreg;
2369
2370         if (imt_arg)
2371                 emit_imt_argument (cfg, call, NULL, imt_arg);
2372
2373         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2374
2375         if (check_sp) {
2376                 int sp_reg;
2377
2378                 sp_reg = mono_alloc_preg (cfg);
2379
2380                 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2381                 ins->dreg = sp_reg;
2382                 MONO_ADD_INS (cfg->cbb, ins);
2383
2384                 /* Restore the stack so we don't crash when throwing the exception */
2385                 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2386                 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2387                 MONO_ADD_INS (cfg->cbb, ins);
2388
2389                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2390                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2391         }
2392
2393         if (rgctx_arg)
2394                 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2395
2396         return (MonoInst*)call;
2397 }
2398
2399 static MonoInst*
2400 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2401
2402 static MonoInst*
2403 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2404                                                         MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2405 {
2406 #ifndef DISABLE_REMOTING
2407         gboolean might_be_remote = FALSE;
2408 #endif
2409         gboolean virtual_ = this_ins != NULL;
2410         gboolean enable_for_aot = TRUE;
2411         int context_used;
2412         MonoCallInst *call;
2413         MonoInst *call_target = NULL;
2414         int rgctx_reg = 0;
2415         gboolean need_unbox_trampoline;
2416
2417         if (!sig)
2418                 sig = mono_method_signature (method);
2419
2420         if (cfg->llvm_only && (mono_class_is_interface (method->klass)))
2421                 g_assert_not_reached ();
2422
2423         if (rgctx_arg) {
2424                 rgctx_reg = mono_alloc_preg (cfg);
2425                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2426         }
2427
2428         if (method->string_ctor) {
2429                 /* Create the real signature */
2430                 /* FIXME: Cache these */
2431                 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2432                 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2433
2434                 sig = ctor_sig;
2435         }
2436
2437         context_used = mini_method_check_context_used (cfg, method);
2438
2439 #ifndef DISABLE_REMOTING
2440         might_be_remote = this_ins && sig->hasthis &&
2441                 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2442                 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2443
2444         if (might_be_remote && context_used) {
2445                 MonoInst *addr;
2446
2447                 g_assert (cfg->gshared);
2448
2449                 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2450
2451                 return mini_emit_calli (cfg, sig, args, addr, NULL, NULL);
2452         }
2453 #endif
2454
2455         if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2456                 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2457
2458         need_unbox_trampoline = method->klass == mono_defaults.object_class || mono_class_is_interface (method->klass);
2459
2460         call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline, method);
2461
2462 #ifndef DISABLE_REMOTING
2463         if (might_be_remote)
2464                 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2465         else
2466 #endif
2467                 call->method = method;
2468         call->inst.flags |= MONO_INST_HAS_METHOD;
2469         call->inst.inst_left = this_ins;
2470         call->tail_call = tail;
2471
2472         if (virtual_) {
2473                 int vtable_reg, slot_reg, this_reg;
2474                 int offset;
2475
2476                 this_reg = this_ins->dreg;
2477
2478                 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2479                         MonoInst *dummy_use;
2480
2481                         MONO_EMIT_NULL_CHECK (cfg, this_reg);
2482
2483                         /* Make a call to delegate->invoke_impl */
2484                         call->inst.inst_basereg = this_reg;
2485                         call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2486                         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2487
2488                         /* We must emit a dummy use here because the delegate trampoline will
2489                         replace the 'this' argument with the delegate target making this activation
2490                         no longer a root for the delegate.
2491                         This is an issue for delegates that target collectible code such as dynamic
2492                         methods of GC'able assemblies.
2493
2494                         For a test case look into #667921.
2495
2496                         FIXME: a dummy use is not the best way to do it as the local register allocator
2497                         will put it on a caller save register and spil it around the call. 
2498                         Ideally, we would either put it on a callee save register or only do the store part.  
2499                          */
2500                         EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2501
2502                         return (MonoInst*)call;
2503                 }
2504
2505                 if ((!cfg->compile_aot || enable_for_aot) && 
2506                         (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) || 
2507                          (MONO_METHOD_IS_FINAL (method) &&
2508                           method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2509                         !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2510                         /* 
2511                          * the method is not virtual, we just need to ensure this is not null
2512                          * and then we can call the method directly.
2513                          */
2514 #ifndef DISABLE_REMOTING
2515                         if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2516                                 /* 
2517                                  * The check above ensures method is not gshared, this is needed since
2518                                  * gshared methods can't have wrappers.
2519                                  */
2520                                 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2521                         }
2522 #endif
2523
2524                         if (!method->string_ctor)
2525                                 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2526
2527                         call->inst.opcode = callvirt_to_call (call->inst.opcode);
2528                 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2529                         /*
2530                          * the method is virtual, but we can statically dispatch since either
2531                          * it's class or the method itself are sealed.
2532                          * But first we need to ensure it's not a null reference.
2533                          */
2534                         MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2535
2536                         call->inst.opcode = callvirt_to_call (call->inst.opcode);
2537                 } else if (call_target) {
2538                         vtable_reg = alloc_preg (cfg);
2539                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2540
2541                         call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2542                         call->inst.sreg1 = call_target->dreg;
2543                         call->inst.flags &= !MONO_INST_HAS_METHOD;
2544                 } else {
2545                         vtable_reg = alloc_preg (cfg);
2546                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2547                         if (mono_class_is_interface (method->klass)) {
2548                                 guint32 imt_slot = mono_method_get_imt_slot (method);
2549                                 emit_imt_argument (cfg, call, call->method, imt_arg);
2550                                 slot_reg = vtable_reg;
2551                                 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2552                         } else {
2553                                 slot_reg = vtable_reg;
2554                                 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2555                                         ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2556                                 if (imt_arg) {
2557                                         g_assert (mono_method_signature (method)->generic_param_count);
2558                                         emit_imt_argument (cfg, call, call->method, imt_arg);
2559                                 }
2560                         }
2561
2562                         call->inst.sreg1 = slot_reg;
2563                         call->inst.inst_offset = offset;
2564                         call->is_virtual = TRUE;
2565                 }
2566         }
2567
2568         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2569
2570         if (rgctx_arg)
2571                 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2572
2573         return (MonoInst*)call;
2574 }
2575
2576 MonoInst*
2577 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2578 {
2579         return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2580 }
2581
2582 MonoInst*
2583 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2584                                            MonoInst **args)
2585 {
2586         MonoCallInst *call;
2587
2588         g_assert (sig);
2589
2590         call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE, NULL);
2591         call->fptr = func;
2592
2593         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2594
2595         return (MonoInst*)call;
2596 }
2597
2598 MonoInst*
2599 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2600 {
2601         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2602
2603         g_assert (info);
2604
2605         return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2606 }
2607
2608 /*
2609  * mono_emit_abs_call:
2610  *
2611  *   Emit a call to the runtime function described by PATCH_TYPE and DATA.
2612  */
2613 inline static MonoInst*
2614 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data, 
2615                                         MonoMethodSignature *sig, MonoInst **args)
2616 {
2617         MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2618         MonoInst *ins;
2619
2620         /* 
2621          * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2622          * handle it.
2623          */
2624         if (cfg->abs_patches == NULL)
2625                 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2626         g_hash_table_insert (cfg->abs_patches, ji, ji);
2627         ins = mono_emit_native_call (cfg, ji, sig, args);
2628         ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2629         return ins;
2630 }
2631
2632 static MonoMethodSignature*
2633 sig_to_rgctx_sig (MonoMethodSignature *sig)
2634 {
2635         // FIXME: memory allocation
2636         MonoMethodSignature *res;
2637         int i;
2638
2639         res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
2640         memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
2641         res->param_count = sig->param_count + 1;
2642         for (i = 0; i < sig->param_count; ++i)
2643                 res->params [i] = sig->params [i];
2644         res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
2645         return res;
2646 }
2647
2648 /* Make an indirect call to FSIG passing an additional argument */
2649 static MonoInst*
2650 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
2651 {
2652         MonoMethodSignature *csig;
2653         MonoInst *args_buf [16];
2654         MonoInst **args;
2655         int i, pindex, tmp_reg;
2656
2657         /* Make a call with an rgctx/extra arg */
2658         if (fsig->param_count + 2 < 16)
2659                 args = args_buf;
2660         else
2661                 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
2662         pindex = 0;
2663         if (fsig->hasthis)
2664                 args [pindex ++] = orig_args [0];
2665         for (i = 0; i < fsig->param_count; ++i)
2666                 args [pindex ++] = orig_args [fsig->hasthis + i];
2667         tmp_reg = alloc_preg (cfg);
2668         EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
2669         csig = sig_to_rgctx_sig (fsig);
2670         return mini_emit_calli (cfg, csig, args, call_target, NULL, NULL);
2671 }
2672
2673 /* Emit an indirect call to the function descriptor ADDR */
2674 static MonoInst*
2675 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
2676 {
2677         int addr_reg, arg_reg;
2678         MonoInst *call_target;
2679
2680         g_assert (cfg->llvm_only);
2681
2682         /*
2683          * addr points to a <addr, arg> pair, load both of them, and
2684          * make a call to addr, passing arg as an extra arg.
2685          */
2686         addr_reg = alloc_preg (cfg);
2687         EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
2688         arg_reg = alloc_preg (cfg);
2689         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
2690
2691         return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
2692 }
2693
2694 static gboolean
2695 direct_icalls_enabled (MonoCompile *cfg)
2696 {
2697         return FALSE;
2698
2699         /* LLVM on amd64 can't handle calls to non-32 bit addresses */
2700 #ifdef TARGET_AMD64
2701         if (cfg->compile_llvm && !cfg->llvm_only)
2702                 return FALSE;
2703 #endif
2704         if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
2705                 return FALSE;
2706         return TRUE;
2707 }
2708
2709 MonoInst*
2710 mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args)
2711 {
2712         /*
2713          * Call the jit icall without a wrapper if possible.
2714          * The wrapper is needed for the following reasons:
2715          * - to handle exceptions thrown using mono_raise_exceptions () from the
2716          *   icall function. The EH code needs the lmf frame pushed by the
2717          *   wrapper to be able to unwind back to managed code.
2718          * - to be able to do stack walks for asynchronously suspended
2719          *   threads when debugging.
2720          */
2721         if (info->no_raise && direct_icalls_enabled (cfg)) {
2722                 char *name;
2723                 int costs;
2724
2725                 if (!info->wrapper_method) {
2726                         name = g_strdup_printf ("__icall_wrapper_%s", info->name);
2727                         info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
2728                         g_free (name);
2729                         mono_memory_barrier ();
2730                 }
2731
2732                 /*
2733                  * Inline the wrapper method, which is basically a call to the C icall, and
2734                  * an exception check.
2735                  */
2736                 costs = inline_method (cfg, info->wrapper_method, NULL,
2737                                                            args, NULL, il_offset, TRUE);
2738                 g_assert (costs > 0);
2739                 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
2740
2741                 return args [0];
2742         } else {
2743                 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2744         }
2745 }
2746  
2747 static MonoInst*
2748 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
2749 {
2750         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2751                 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
2752                         int widen_op = -1;
2753
2754                         /* 
2755                          * Native code might return non register sized integers 
2756                          * without initializing the upper bits.
2757                          */
2758                         switch (mono_type_to_load_membase (cfg, fsig->ret)) {
2759                         case OP_LOADI1_MEMBASE:
2760                                 widen_op = OP_ICONV_TO_I1;
2761                                 break;
2762                         case OP_LOADU1_MEMBASE:
2763                                 widen_op = OP_ICONV_TO_U1;
2764                                 break;
2765                         case OP_LOADI2_MEMBASE:
2766                                 widen_op = OP_ICONV_TO_I2;
2767                                 break;
2768                         case OP_LOADU2_MEMBASE:
2769                                 widen_op = OP_ICONV_TO_U2;
2770                                 break;
2771                         default:
2772                                 break;
2773                         }
2774
2775                         if (widen_op != -1) {
2776                                 int dreg = alloc_preg (cfg);
2777                                 MonoInst *widen;
2778
2779                                 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
2780                                 widen->type = ins->type;
2781                                 ins = widen;
2782                         }
2783                 }
2784         }
2785
2786         return ins;
2787 }
2788
2789
2790 static void
2791 emit_method_access_failure (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
2792 {
2793         MonoInst *args [16];
2794
2795         args [0] = emit_get_rgctx_method (cfg, mono_method_check_context_used (caller), caller, MONO_RGCTX_INFO_METHOD);
2796         args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (callee), callee, MONO_RGCTX_INFO_METHOD);
2797
2798         mono_emit_jit_icall (cfg, mono_throw_method_access, args);
2799 }
2800
2801 MonoMethod*
2802 mini_get_memcpy_method (void)
2803 {
2804         static MonoMethod *memcpy_method = NULL;
2805         if (!memcpy_method) {
2806                 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2807                 if (!memcpy_method)
2808                         g_error ("Old corlib found. Install a new one");
2809         }
2810         return memcpy_method;
2811 }
2812
2813 void
2814 mini_emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
2815 {
2816         int card_table_shift_bits;
2817         gpointer card_table_mask;
2818         guint8 *card_table;
2819         MonoInst *dummy_use;
2820         int nursery_shift_bits;
2821         size_t nursery_size;
2822
2823         if (!cfg->gen_write_barriers)
2824                 return;
2825
2826         //method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1])
2827
2828         card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
2829
2830         mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
2831
2832         if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
2833                 MonoInst *wbarrier;
2834
2835                 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
2836                 wbarrier->sreg1 = ptr->dreg;
2837                 wbarrier->sreg2 = value->dreg;
2838                 MONO_ADD_INS (cfg->cbb, wbarrier);
2839         } else if (card_table) {
2840                 int offset_reg = alloc_preg (cfg);
2841                 int card_reg;
2842                 MonoInst *ins;
2843
2844                 /*
2845                  * We emit a fast light weight write barrier. This always marks cards as in the concurrent
2846                  * collector case, so, for the serial collector, it might slightly slow down nursery
2847                  * collections. We also expect that the host system and the target system have the same card
2848                  * table configuration, which is the case if they have the same pointer size.
2849                  */
2850
2851                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
2852                 if (card_table_mask)
2853                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
2854
2855                 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
2856                  * IMM's larger than 32bits.
2857                  */
2858                 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
2859                 card_reg = ins->dreg;
2860
2861                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
2862                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
2863         } else {
2864                 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
2865                 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
2866         }
2867
2868         EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
2869 }
2870
2871 MonoMethod*
2872 mini_get_memset_method (void)
2873 {
2874         static MonoMethod *memset_method = NULL;
2875         if (!memset_method) {
2876                 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2877                 if (!memset_method)
2878                         g_error ("Old corlib found. Install a new one");
2879         }
2880         return memset_method;
2881 }
2882
2883 void
2884 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
2885 {
2886         MonoInst *iargs [3];
2887         int n;
2888         guint32 align;
2889         MonoMethod *memset_method;
2890         MonoInst *size_ins = NULL;
2891         MonoInst *bzero_ins = NULL;
2892         static MonoMethod *bzero_method;
2893
2894         /* FIXME: Optimize this for the case when dest is an LDADDR */
2895         mono_class_init (klass);
2896         if (mini_is_gsharedvt_klass (klass)) {
2897                 size_ins = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
2898                 bzero_ins = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
2899                 if (!bzero_method)
2900                         bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
2901                 g_assert (bzero_method);
2902                 iargs [0] = dest;
2903                 iargs [1] = size_ins;
2904                 mini_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
2905                 return;
2906         }
2907
2908         klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
2909
2910         n = mono_class_value_size (klass, &align);
2911
2912         if (n <= sizeof (gpointer) * 8) {
2913                 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
2914         }
2915         else {
2916                 memset_method = mini_get_memset_method ();
2917                 iargs [0] = dest;
2918                 EMIT_NEW_ICONST (cfg, iargs [1], 0);
2919                 EMIT_NEW_ICONST (cfg, iargs [2], n);
2920                 mono_emit_method_call (cfg, memset_method, iargs, NULL);
2921         }
2922 }
2923
2924 /*
2925  * emit_get_rgctx:
2926  *
2927  *   Emit IR to return either the this pointer for instance method,
2928  * or the mrgctx for static methods.
2929  */
2930 static MonoInst*
2931 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
2932 {
2933         MonoInst *this_ins = NULL;
2934
2935         g_assert (cfg->gshared);
2936
2937         if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
2938                         !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
2939                 !method->klass->valuetype)
2940                 EMIT_NEW_VARLOAD (cfg, this_ins, cfg->this_arg, &mono_defaults.object_class->byval_arg);
2941
2942         if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
2943                 MonoInst *mrgctx_loc, *mrgctx_var;
2944
2945                 g_assert (!this_ins);
2946                 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
2947
2948                 mrgctx_loc = mono_get_vtable_var (cfg);
2949                 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
2950
2951                 return mrgctx_var;
2952         } else if (MONO_CLASS_IS_INTERFACE (cfg->method->klass)) {
2953                 MonoInst *mrgctx_loc, *mrgctx_var;
2954
2955                 /* Default interface methods need an mrgctx since the vtabke at runtime points at an implementing class */
2956                 mrgctx_loc = mono_get_vtable_var (cfg);
2957                 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
2958
2959                 g_assert (mono_method_needs_static_rgctx_invoke (cfg->method, TRUE));
2960
2961                 return mrgctx_var;
2962         } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
2963                 MonoInst *vtable_loc, *vtable_var;
2964
2965                 g_assert (!this_ins);
2966
2967                 vtable_loc = mono_get_vtable_var (cfg);
2968                 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
2969
2970                 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
2971                         MonoInst *mrgctx_var = vtable_var;
2972                         int vtable_reg;
2973
2974                         vtable_reg = alloc_preg (cfg);
2975                         EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
2976                         vtable_var->type = STACK_PTR;
2977                 }
2978
2979                 return vtable_var;
2980         } else {
2981                 MonoInst *ins;
2982                 int vtable_reg;
2983         
2984                 vtable_reg = alloc_preg (cfg);
2985                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2986                 return ins;
2987         }
2988 }
2989
2990 static MonoJumpInfoRgctxEntry *
2991 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
2992 {
2993         MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2994         res->method = method;
2995         res->in_mrgctx = in_mrgctx;
2996         res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2997         res->data->type = patch_type;
2998         res->data->data.target = patch_data;
2999         res->info_type = info_type;
3000
3001         return res;
3002 }
3003
3004 static inline MonoInst*
3005 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3006 {
3007         MonoInst *args [16];
3008         MonoInst *call;
3009
3010         // FIXME: No fastpath since the slot is not a compile time constant
3011         args [0] = rgctx;
3012         EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3013         if (entry->in_mrgctx)
3014                 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3015         else
3016                 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3017         return call;
3018 #if 0
3019         /*
3020          * FIXME: This can be called during decompose, which is a problem since it creates
3021          * new bblocks.
3022          * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3023          */
3024         int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3025         gboolean mrgctx;
3026         MonoBasicBlock *is_null_bb, *end_bb;
3027         MonoInst *res, *ins, *call;
3028         MonoInst *args[16];
3029
3030         slot = mini_get_rgctx_entry_slot (entry);
3031
3032         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3033         index = MONO_RGCTX_SLOT_INDEX (slot);
3034         if (mrgctx)
3035                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3036         for (depth = 0; ; ++depth) {
3037                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3038
3039                 if (index < size - 1)
3040                         break;
3041                 index -= size - 1;
3042         }
3043
3044         NEW_BBLOCK (cfg, end_bb);
3045         NEW_BBLOCK (cfg, is_null_bb);
3046
3047         if (mrgctx) {
3048                 rgctx_reg = rgctx->dreg;
3049         } else {
3050                 rgctx_reg = alloc_preg (cfg);
3051
3052                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3053                 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3054                 NEW_BBLOCK (cfg, is_null_bb);
3055
3056                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3057                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3058         }
3059
3060         for (i = 0; i < depth; ++i) {
3061                 int array_reg = alloc_preg (cfg);
3062
3063                 /* load ptr to next array */
3064                 if (mrgctx && i == 0)
3065                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3066                 else
3067                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3068                 rgctx_reg = array_reg;
3069                 /* is the ptr null? */
3070                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3071                 /* if yes, jump to actual trampoline */
3072                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3073         }
3074
3075         /* fetch slot */
3076         val_reg = alloc_preg (cfg);
3077         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3078         /* is the slot null? */
3079         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3080         /* if yes, jump to actual trampoline */
3081         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3082
3083         /* Fastpath */
3084         res_reg = alloc_preg (cfg);
3085         MONO_INST_NEW (cfg, ins, OP_MOVE);
3086         ins->dreg = res_reg;
3087         ins->sreg1 = val_reg;
3088         MONO_ADD_INS (cfg->cbb, ins);
3089         res = ins;
3090         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3091
3092         /* Slowpath */
3093         MONO_START_BB (cfg, is_null_bb);
3094         args [0] = rgctx;
3095         EMIT_NEW_ICONST (cfg, args [1], index);
3096         if (mrgctx)
3097                 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3098         else
3099                 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3100         MONO_INST_NEW (cfg, ins, OP_MOVE);
3101         ins->dreg = res_reg;
3102         ins->sreg1 = call->dreg;
3103         MONO_ADD_INS (cfg->cbb, ins);
3104         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3105
3106         MONO_START_BB (cfg, end_bb);
3107
3108         return res;
3109 #endif
3110 }
3111
3112 /*
3113  * emit_rgctx_fetch:
3114  *
3115  *   Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3116  * given by RGCTX.
3117  */
3118 static inline MonoInst*
3119 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3120 {
3121         if (cfg->llvm_only)
3122                 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3123         else
3124                 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3125 }
3126
3127 MonoInst*
3128 mini_emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3129                                           MonoClass *klass, MonoRgctxInfoType rgctx_type)
3130 {
3131         MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_CLASS, klass, rgctx_type);
3132         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3133
3134         return emit_rgctx_fetch (cfg, rgctx, entry);
3135 }
3136
3137 static MonoInst*
3138 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3139                                         MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3140 {
3141         MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_SIGNATURE, sig, rgctx_type);
3142         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3143
3144         return emit_rgctx_fetch (cfg, rgctx, entry);
3145 }
3146
3147 static MonoInst*
3148 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3149                                                            MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3150 {
3151         MonoJumpInfoGSharedVtCall *call_info;
3152         MonoJumpInfoRgctxEntry *entry;
3153         MonoInst *rgctx;
3154
3155         call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3156         call_info->sig = sig;
3157         call_info->method = cmethod;
3158
3159         entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_CALL, call_info, rgctx_type);
3160         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3161
3162         return emit_rgctx_fetch (cfg, rgctx, entry);
3163 }
3164
3165 /*
3166  * emit_get_rgctx_virt_method:
3167  *
3168  *   Return data for method VIRT_METHOD for a receiver of type KLASS.
3169  */
3170 static MonoInst*
3171 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3172                                                         MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3173 {
3174         MonoJumpInfoVirtMethod *info;
3175         MonoJumpInfoRgctxEntry *entry;
3176         MonoInst *rgctx;
3177
3178         info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3179         info->klass = klass;
3180         info->method = virt_method;
3181
3182         entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_VIRT_METHOD, info, rgctx_type);
3183         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3184
3185         return emit_rgctx_fetch (cfg, rgctx, entry);
3186 }
3187
3188 static MonoInst*
3189 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3190                                                                  MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3191 {
3192         MonoJumpInfoRgctxEntry *entry;
3193         MonoInst *rgctx;
3194
3195         entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_METHOD, info, MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO);
3196         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3197
3198         return emit_rgctx_fetch (cfg, rgctx, entry);
3199 }
3200
3201 /*
3202  * emit_get_rgctx_method:
3203  *
3204  *   Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3205  * normal constants, else emit a load from the rgctx.
3206  */
3207 static MonoInst*
3208 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3209                                            MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3210 {
3211         if (!context_used) {
3212                 MonoInst *ins;
3213
3214                 switch (rgctx_type) {
3215                 case MONO_RGCTX_INFO_METHOD:
3216                         EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3217                         return ins;
3218                 case MONO_RGCTX_INFO_METHOD_RGCTX:
3219                         EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3220                         return ins;
3221                 default:
3222                         g_assert_not_reached ();
3223                 }
3224         } else {
3225                 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_METHODCONST, cmethod, rgctx_type);
3226                 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3227
3228                 return emit_rgctx_fetch (cfg, rgctx, entry);
3229         }
3230 }
3231
3232 static MonoInst*
3233 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3234                                           MonoClassField *field, MonoRgctxInfoType rgctx_type)
3235 {
3236         MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_FIELD, field, rgctx_type);
3237         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3238
3239         return emit_rgctx_fetch (cfg, rgctx, entry);
3240 }
3241
3242 static int
3243 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3244 {
3245         MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3246         MonoRuntimeGenericContextInfoTemplate *template_;
3247         int i, idx;
3248
3249         g_assert (info);
3250
3251         for (i = 0; i < info->num_entries; ++i) {
3252                 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3253
3254                 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3255                         return i;
3256         }
3257
3258         if (info->num_entries == info->count_entries) {
3259                 MonoRuntimeGenericContextInfoTemplate *new_entries;
3260                 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3261
3262                 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3263
3264                 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3265                 info->entries = new_entries;
3266                 info->count_entries = new_count_entries;
3267         }
3268
3269         idx = info->num_entries;
3270         template_ = &info->entries [idx];
3271         template_->info_type = rgctx_type;
3272         template_->data = data;
3273
3274         info->num_entries ++;
3275
3276         return idx;
3277 }
3278
3279 /*
3280  * emit_get_gsharedvt_info:
3281  *
3282  *   This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3283  */
3284 static MonoInst*
3285 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3286 {
3287         MonoInst *ins;
3288         int idx, dreg;
3289
3290         idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3291         /* Load info->entries [idx] */
3292         dreg = alloc_preg (cfg);
3293         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3294
3295         return ins;
3296 }
3297
3298 MonoInst*
3299 mini_emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3300 {
3301         return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3302 }
3303
3304 /*
3305  * On return the caller must check @klass for load errors.
3306  */
3307 static void
3308 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3309 {
3310         MonoInst *vtable_arg;
3311         int context_used;
3312
3313         context_used = mini_class_check_context_used (cfg, klass);
3314
3315         if (context_used) {
3316                 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
3317                                                                                    klass, MONO_RGCTX_INFO_VTABLE);
3318         } else {
3319                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3320
3321                 if (!vtable)
3322                         return;
3323                 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3324         }
3325
3326         if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3327                 MonoInst *ins;
3328
3329                 /*
3330                  * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3331                  * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3332                  */
3333                 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3334                 ins->sreg1 = vtable_arg->dreg;
3335                 MONO_ADD_INS (cfg->cbb, ins);
3336         } else {
3337                 int inited_reg;
3338                 MonoBasicBlock *inited_bb;
3339                 MonoInst *args [16];
3340
3341                 inited_reg = alloc_ireg (cfg);
3342
3343                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, inited_reg, vtable_arg->dreg, MONO_STRUCT_OFFSET (MonoVTable, initialized));
3344
3345                 NEW_BBLOCK (cfg, inited_bb);
3346
3347                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3348                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3349
3350                 args [0] = vtable_arg;
3351                 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3352
3353                 MONO_START_BB (cfg, inited_bb);
3354         }
3355 }
3356
3357 static void
3358 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3359 {
3360         MonoInst *ins;
3361
3362         if (cfg->gen_seq_points && cfg->method == method) {
3363                 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3364                 if (nonempty_stack)
3365                         ins->flags |= MONO_INST_NONEMPTY_STACK;
3366                 MONO_ADD_INS (cfg->cbb, ins);
3367         }
3368 }
3369
3370 void
3371 mini_save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3372 {
3373         if (mini_get_debug_options ()->better_cast_details) {
3374                 int vtable_reg = alloc_preg (cfg);
3375                 int klass_reg = alloc_preg (cfg);
3376                 MonoBasicBlock *is_null_bb = NULL;
3377                 MonoInst *tls_get;
3378                 int to_klass_reg, context_used;
3379
3380                 if (null_check) {
3381                         NEW_BBLOCK (cfg, is_null_bb);
3382
3383                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3384                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3385                 }
3386
3387                 tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3388                 if (!tls_get) {
3389                         fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3390                         exit (1);
3391                 }
3392
3393                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3394                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3395
3396                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3397
3398                 context_used = mini_class_check_context_used (cfg, klass);
3399                 if (context_used) {
3400                         MonoInst *class_ins;
3401
3402                         class_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3403                         to_klass_reg = class_ins->dreg;
3404                 } else {
3405                         to_klass_reg = alloc_preg (cfg);
3406                         MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3407                 }
3408                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3409
3410                 if (null_check)
3411                         MONO_START_BB (cfg, is_null_bb);
3412         }
3413 }
3414
3415 void
3416 mini_reset_cast_details (MonoCompile *cfg)
3417 {
3418         /* Reset the variables holding the cast details */
3419         if (mini_get_debug_options ()->better_cast_details) {
3420                 MonoInst *tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3421                 /* It is enough to reset the from field */
3422                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3423         }
3424 }
3425
3426 /*
3427  * On return the caller must check @array_class for load errors
3428  */
3429 static void
3430 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3431 {
3432         int vtable_reg = alloc_preg (cfg);
3433         int context_used;
3434
3435         context_used = mini_class_check_context_used (cfg, array_class);
3436
3437         mini_save_cast_details (cfg, array_class, obj->dreg, FALSE);
3438
3439         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3440
3441         if (cfg->opt & MONO_OPT_SHARED) {
3442                 int class_reg = alloc_preg (cfg);
3443                 MonoInst *ins;
3444
3445                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3446                 ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3447                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3448         } else if (context_used) {
3449                 MonoInst *vtable_ins;
3450
3451                 vtable_ins = mini_emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3452                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3453         } else {
3454                 if (cfg->compile_aot) {
3455                         int vt_reg;
3456                         MonoVTable *vtable;
3457
3458                         if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3459                                 return;
3460                         vt_reg = alloc_preg (cfg);
3461                         MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3462                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3463                 } else {
3464                         MonoVTable *vtable;
3465                         if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3466                                 return;
3467                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3468                 }
3469         }
3470         
3471         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3472
3473         mini_reset_cast_details (cfg);
3474 }
3475
3476 /**
3477  * Handles unbox of a Nullable<T>. If context_used is non zero, then shared 
3478  * generic code is generated.
3479  */
3480 static MonoInst*
3481 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
3482 {
3483         MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
3484
3485         if (context_used) {
3486                 MonoInst *rgctx, *addr;
3487
3488                 /* FIXME: What if the class is shared?  We might not
3489                    have to get the address of the method from the
3490                    RGCTX. */
3491                 addr = emit_get_rgctx_method (cfg, context_used, method,
3492                                                                           MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3493                 if (cfg->llvm_only) {
3494                         cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, mono_method_signature (method));
3495                         return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
3496                 } else {
3497                         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3498
3499                         return mini_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3500                 }
3501         } else {
3502                 gboolean pass_vtable, pass_mrgctx;
3503                 MonoInst *rgctx_arg = NULL;
3504
3505                 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3506                 g_assert (!pass_mrgctx);
3507
3508                 if (pass_vtable) {
3509                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3510
3511                         g_assert (vtable);
3512                         EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3513                 }
3514
3515                 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3516         }
3517 }
3518
3519 static MonoInst*
3520 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
3521 {
3522         MonoInst *add;
3523         int obj_reg;
3524         int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
3525         int klass_reg = alloc_dreg (cfg ,STACK_PTR);
3526         int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
3527         int rank_reg = alloc_dreg (cfg ,STACK_I4);
3528
3529         obj_reg = sp [0]->dreg;
3530         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3531         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
3532
3533         /* FIXME: generics */
3534         g_assert (klass->rank == 0);
3535                         
3536         // Check rank == 0
3537         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
3538         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3539
3540         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3541         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
3542
3543         if (context_used) {
3544                 MonoInst *element_class;
3545
3546                 /* This assertion is from the unboxcast insn */
3547                 g_assert (klass->rank == 0);
3548
3549                 element_class = mini_emit_get_rgctx_klass (cfg, context_used,
3550                                 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
3551
3552                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
3553                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3554         } else {
3555                 mini_save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
3556                 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
3557                 mini_reset_cast_details (cfg);
3558         }
3559
3560         NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
3561         MONO_ADD_INS (cfg->cbb, add);
3562         add->type = STACK_MP;
3563         add->klass = klass;
3564
3565         return add;
3566 }
3567
3568 static MonoInst*
3569 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
3570 {
3571         MonoInst *addr, *klass_inst, *is_ref, *args[16];
3572         MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3573         MonoInst *ins;
3574         int dreg, addr_reg;
3575
3576         klass_inst = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
3577
3578         /* obj */
3579         args [0] = obj;
3580
3581         /* klass */
3582         args [1] = klass_inst;
3583
3584         /* CASTCLASS */
3585         obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
3586
3587         NEW_BBLOCK (cfg, is_ref_bb);
3588         NEW_BBLOCK (cfg, is_nullable_bb);
3589         NEW_BBLOCK (cfg, end_bb);
3590         is_ref = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3591         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
3592         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3593
3594         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
3595         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3596
3597         /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
3598         addr_reg = alloc_dreg (cfg, STACK_MP);
3599
3600         /* Non-ref case */
3601         /* UNBOX */
3602         NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
3603         MONO_ADD_INS (cfg->cbb, addr);
3604
3605         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3606
3607         /* Ref case */
3608         MONO_START_BB (cfg, is_ref_bb);
3609
3610         /* Save the ref to a temporary */
3611         dreg = alloc_ireg (cfg);
3612         EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
3613         addr->dreg = addr_reg;
3614         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
3615         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3616
3617         /* Nullable case */
3618         MONO_START_BB (cfg, is_nullable_bb);
3619
3620         {
3621                 MonoInst *addr = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
3622                 MonoInst *unbox_call;
3623                 MonoMethodSignature *unbox_sig;
3624
3625                 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3626                 unbox_sig->ret = &klass->byval_arg;
3627                 unbox_sig->param_count = 1;
3628                 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
3629
3630                 if (cfg->llvm_only)
3631                         unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
3632                 else
3633                         unbox_call = mini_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
3634
3635                 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
3636                 addr->dreg = addr_reg;
3637         }
3638
3639         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3640
3641         /* End */
3642         MONO_START_BB (cfg, end_bb);
3643
3644         /* LDOBJ */
3645         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
3646
3647         return ins;
3648 }
3649
3650 /*
3651  * Returns NULL and set the cfg exception on error.
3652  */
3653 static MonoInst*
3654 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
3655 {
3656         MonoInst *iargs [2];
3657         void *alloc_ftn;
3658
3659         if (context_used) {
3660                 MonoInst *data;
3661                 MonoRgctxInfoType rgctx_info;
3662                 MonoInst *iargs [2];
3663                 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
3664
3665                 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
3666
3667                 if (cfg->opt & MONO_OPT_SHARED)
3668                         rgctx_info = MONO_RGCTX_INFO_KLASS;
3669                 else
3670                         rgctx_info = MONO_RGCTX_INFO_VTABLE;
3671                 data = mini_emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
3672
3673                 if (cfg->opt & MONO_OPT_SHARED) {
3674                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
3675                         iargs [1] = data;
3676                         alloc_ftn = ves_icall_object_new;
3677                 } else {
3678                         iargs [0] = data;
3679                         alloc_ftn = ves_icall_object_new_specific;
3680                 }
3681
3682                 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
3683                         if (known_instance_size) {
3684                                 int size = mono_class_instance_size (klass);
3685                                 if (size < sizeof (MonoObject))
3686                                         g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
3687
3688                                 EMIT_NEW_ICONST (cfg, iargs [1], size);
3689                         }
3690                         return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
3691                 }
3692
3693                 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
3694         }
3695
3696         if (cfg->opt & MONO_OPT_SHARED) {
3697                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
3698                 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
3699
3700                 alloc_ftn = ves_icall_object_new;
3701         } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !mono_class_is_ginst (klass)) {
3702                 /* This happens often in argument checking code, eg. throw new FooException... */
3703                 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
3704                 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
3705                 alloc_ftn = mono_helper_newobj_mscorlib;
3706         } else {
3707                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3708                 MonoMethod *managed_alloc = NULL;
3709
3710                 if (!vtable) {
3711                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
3712                         cfg->exception_ptr = klass;
3713                         return NULL;
3714                 }
3715
3716                 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
3717
3718                 if (managed_alloc) {
3719                         int size = mono_class_instance_size (klass);
3720                         if (size < sizeof (MonoObject))
3721                                 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
3722
3723                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
3724                         EMIT_NEW_ICONST (cfg, iargs [1], size);
3725                         return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
3726                 }
3727                 alloc_ftn = ves_icall_object_new_specific;
3728                 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
3729         }
3730
3731         return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
3732 }
3733         
3734 /*
3735  * Returns NULL and set the cfg exception on error.
3736  */     
3737 static MonoInst*
3738 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
3739 {
3740         MonoInst *alloc, *ins;
3741
3742         if (mono_class_is_nullable (klass)) {
3743                 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
3744
3745                 if (context_used) {
3746                         if (cfg->llvm_only && cfg->gsharedvt) {
3747                                 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
3748                                                                                                                 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3749                                 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
3750                         } else {
3751                                 /* FIXME: What if the class is shared?  We might not
3752                                    have to get the method address from the RGCTX. */
3753                                 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
3754                                                                                                                 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3755                                 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3756
3757                                 return mini_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3758                         }
3759                 } else {
3760                         gboolean pass_vtable, pass_mrgctx;
3761                         MonoInst *rgctx_arg = NULL;
3762
3763                         check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3764                         g_assert (!pass_mrgctx);
3765
3766                         if (pass_vtable) {
3767                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3768
3769                                 g_assert (vtable);
3770                                 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3771                         }
3772
3773                         return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3774                 }
3775         }
3776
3777         if (mini_is_gsharedvt_klass (klass)) {
3778                 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3779                 MonoInst *res, *is_ref, *src_var, *addr;
3780                 int dreg;
3781
3782                 dreg = alloc_ireg (cfg);
3783
3784                 NEW_BBLOCK (cfg, is_ref_bb);
3785                 NEW_BBLOCK (cfg, is_nullable_bb);
3786                 NEW_BBLOCK (cfg, end_bb);
3787                 is_ref = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3788                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
3789                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3790
3791                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
3792                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3793
3794                 /* Non-ref case */
3795                 alloc = handle_alloc (cfg, klass, TRUE, context_used);
3796                 if (!alloc)
3797                         return NULL;
3798                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
3799                 ins->opcode = OP_STOREV_MEMBASE;
3800
3801                 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
3802                 res->type = STACK_OBJ;
3803                 res->klass = klass;
3804                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3805                 
3806                 /* Ref case */
3807                 MONO_START_BB (cfg, is_ref_bb);
3808
3809                 /* val is a vtype, so has to load the value manually */
3810                 src_var = get_vreg_to_inst (cfg, val->dreg);
3811                 if (!src_var)
3812                         src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
3813                 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
3814                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
3815                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3816
3817                 /* Nullable case */
3818                 MONO_START_BB (cfg, is_nullable_bb);
3819
3820                 {
3821                         MonoInst *addr = mini_emit_get_gsharedvt_info_klass (cfg, klass,
3822                                                                                                         MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
3823                         MonoInst *box_call;
3824                         MonoMethodSignature *box_sig;
3825
3826                         /*
3827                          * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
3828                          * construct that method at JIT time, so have to do things by hand.
3829                          */
3830                         box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3831                         box_sig->ret = &mono_defaults.object_class->byval_arg;
3832                         box_sig->param_count = 1;
3833                         box_sig->params [0] = &klass->byval_arg;
3834
3835                         if (cfg->llvm_only)
3836                                 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
3837                         else
3838                                 box_call = mini_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
3839                         EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
3840                         res->type = STACK_OBJ;
3841                         res->klass = klass;
3842                 }
3843
3844                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3845
3846                 MONO_START_BB (cfg, end_bb);
3847
3848                 return res;
3849         } else {
3850                 alloc = handle_alloc (cfg, klass, TRUE, context_used);
3851                 if (!alloc)
3852                         return NULL;
3853
3854                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
3855                 return alloc;
3856         }
3857 }
3858
3859 static GHashTable* direct_icall_type_hash;
3860
3861 static gboolean
3862 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
3863 {
3864         /* LLVM on amd64 can't handle calls to non-32 bit addresses */
3865         if (!direct_icalls_enabled (cfg))
3866                 return FALSE;
3867
3868         /*
3869          * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
3870          * Whitelist a few icalls for now.
3871          */
3872         if (!direct_icall_type_hash) {
3873                 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
3874
3875                 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
3876                 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
3877                 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
3878                 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
3879                 mono_memory_barrier ();
3880                 direct_icall_type_hash = h;
3881         }
3882
3883         if (cmethod->klass == mono_defaults.math_class)
3884                 return TRUE;
3885         /* No locking needed */
3886         if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
3887                 return TRUE;
3888         return FALSE;
3889 }
3890
3891 static gboolean
3892 method_needs_stack_walk (MonoCompile *cfg, MonoMethod *cmethod)
3893 {
3894         if (cmethod->klass == mono_defaults.systemtype_class) {
3895                 if (!strcmp (cmethod->name, "GetType"))
3896                         return TRUE;
3897         }
3898         return FALSE;
3899 }
3900
3901 static G_GNUC_UNUSED MonoInst*
3902 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
3903 {
3904         MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
3905         guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
3906         gboolean is_i4;
3907
3908         switch (enum_type->type) {
3909         case MONO_TYPE_I8:
3910         case MONO_TYPE_U8:
3911 #if SIZEOF_REGISTER == 8
3912         case MONO_TYPE_I:
3913         case MONO_TYPE_U:
3914 #endif
3915                 is_i4 = FALSE;
3916                 break;
3917         default:
3918                 is_i4 = TRUE;
3919                 break;
3920         }
3921
3922         {
3923                 MonoInst *load, *and_, *cmp, *ceq;
3924                 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
3925                 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
3926                 int dest_reg = alloc_ireg (cfg);
3927
3928                 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
3929                 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
3930                 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
3931                 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
3932
3933                 ceq->type = STACK_I4;
3934
3935                 if (!is_i4) {
3936                         load = mono_decompose_opcode (cfg, load);
3937                         and_ = mono_decompose_opcode (cfg, and_);
3938                         cmp = mono_decompose_opcode (cfg, cmp);
3939                         ceq = mono_decompose_opcode (cfg, ceq);
3940                 }
3941
3942                 return ceq;
3943         }
3944 }
3945
3946 /*
3947  * Returns NULL and set the cfg exception on error.
3948  */
3949 static G_GNUC_UNUSED MonoInst*
3950 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
3951 {
3952         MonoInst *ptr;
3953         int dreg;
3954         gpointer trampoline;
3955         MonoInst *obj, *method_ins, *tramp_ins;
3956         MonoDomain *domain;
3957         guint8 **code_slot;
3958
3959         if (virtual_ && !cfg->llvm_only) {
3960                 MonoMethod *invoke = mono_get_delegate_invoke (klass);
3961                 g_assert (invoke);
3962
3963                 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
3964                         return NULL;
3965         }
3966
3967         obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
3968         if (!obj)
3969                 return NULL;
3970
3971         /* Inline the contents of mono_delegate_ctor */
3972
3973         /* Set target field */
3974         /* Optimize away setting of NULL target */
3975         if (!MONO_INS_IS_PCONST_NULL (target)) {
3976                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target->dreg, 0);
3977                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
3978                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
3979                 if (cfg->gen_write_barriers) {
3980                         dreg = alloc_preg (cfg);
3981                         EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
3982                         mini_emit_write_barrier (cfg, ptr, target);
3983                 }
3984         }
3985
3986         /* Set method field */
3987         method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
3988         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
3989
3990         /* 
3991          * To avoid looking up the compiled code belonging to the target method
3992          * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
3993          * store it, and we fill it after the method has been compiled.
3994          */
3995         if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
3996                 MonoInst *code_slot_ins;
3997
3998                 if (context_used) {
3999                         code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
4000                 } else {
4001                         domain = mono_domain_get ();
4002                         mono_domain_lock (domain);
4003                         if (!domain_jit_info (domain)->method_code_hash)
4004                                 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
4005                         code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
4006                         if (!code_slot) {
4007                                 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
4008                                 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
4009                         }
4010                         mono_domain_unlock (domain);
4011
4012                         code_slot_ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
4013                 }
4014                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);                
4015         }
4016
4017         if (cfg->llvm_only) {
4018                 MonoInst *args [16];
4019
4020                 if (virtual_) {
4021                         args [0] = obj;
4022                         args [1] = target;
4023                         args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4024                         mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
4025                 } else {
4026                         args [0] = obj;
4027                         mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
4028                 }
4029
4030                 return obj;
4031         }
4032
4033         if (cfg->compile_aot) {
4034                 MonoDelegateClassMethodPair *del_tramp;
4035
4036                 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
4037                 del_tramp->klass = klass;
4038                 del_tramp->method = context_used ? NULL : method;
4039                 del_tramp->is_virtual = virtual_;
4040                 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
4041         } else {
4042                 if (virtual_)
4043                         trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
4044                 else
4045                         trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
4046                 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
4047         }
4048
4049         /* Set invoke_impl field */
4050         if (virtual_) {
4051                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
4052         } else {
4053                 dreg = alloc_preg (cfg);
4054                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
4055                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
4056
4057                 dreg = alloc_preg (cfg);
4058                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
4059                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
4060         }
4061
4062         dreg = alloc_preg (cfg);
4063         MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
4064         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
4065
4066         /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
4067
4068         return obj;
4069 }
4070
4071 static MonoInst*
4072 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
4073 {
4074         MonoJitICallInfo *info;
4075
4076         /* Need to register the icall so it gets an icall wrapper */
4077         info = mono_get_array_new_va_icall (rank);
4078
4079         cfg->flags |= MONO_CFG_HAS_VARARGS;
4080
4081         /* mono_array_new_va () needs a vararg calling convention */
4082         cfg->exception_message = g_strdup ("array-new");
4083         cfg->disable_llvm = TRUE;
4084
4085         /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
4086         return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
4087 }
4088
4089 /*
4090  * handle_constrained_gsharedvt_call:
4091  *
4092  *   Handle constrained calls where the receiver is a gsharedvt type.
4093  * Return the instruction representing the call. Set the cfg exception on failure.
4094  */
4095 static MonoInst*
4096 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
4097                                                                    gboolean *ref_emit_widen)
4098 {
4099         MonoInst *ins = NULL;
4100         gboolean emit_widen = *ref_emit_widen;
4101
4102         /*
4103          * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
4104          * 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
4105          * pack the arguments into an array, and do the rest of the work in in an icall.
4106          */
4107         if (((cmethod->klass == mono_defaults.object_class) || mono_class_is_interface (cmethod->klass) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
4108                 (MONO_TYPE_IS_VOID (fsig->ret) || MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_IS_REFERENCE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mono_class_is_enum (mono_class_from_mono_type (fsig->ret)) || mini_is_gsharedvt_type (fsig->ret)) &&
4109                 (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]))))) {
4110                 MonoInst *args [16];
4111
4112                 /*
4113                  * This case handles calls to
4114                  * - object:ToString()/Equals()/GetHashCode(),
4115                  * - System.IComparable<T>:CompareTo()
4116                  * - System.IEquatable<T>:Equals ()
4117                  * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
4118                  */
4119
4120                 args [0] = sp [0];
4121                 if (mono_method_check_context_used (cmethod))
4122                         args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
4123                 else
4124                         EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
4125                 args [2] = mini_emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
4126
4127                 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
4128                 if (fsig->hasthis && fsig->param_count) {
4129                         /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
4130                         MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
4131                         ins->dreg = alloc_preg (cfg);
4132                         ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
4133                         MONO_ADD_INS (cfg->cbb, ins);
4134                         args [4] = ins;
4135
4136                         if (mini_is_gsharedvt_type (fsig->params [0])) {
4137                                 int addr_reg, deref_arg_reg;
4138
4139                                 ins = mini_emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4140                                 deref_arg_reg = alloc_preg (cfg);
4141                                 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
4142                                 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
4143
4144                                 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
4145                                 addr_reg = ins->dreg;
4146                                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
4147                         } else {
4148                                 EMIT_NEW_ICONST (cfg, args [3], 0);
4149                                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
4150                         }
4151                 } else {
4152                         EMIT_NEW_ICONST (cfg, args [3], 0);
4153                         EMIT_NEW_ICONST (cfg, args [4], 0);
4154                 }
4155                 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
4156                 emit_widen = FALSE;
4157
4158                 if (mini_is_gsharedvt_type (fsig->ret)) {
4159                         ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
4160                 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mono_class_is_enum (mono_class_from_mono_type (fsig->ret))) {
4161                         MonoInst *add;
4162
4163                         /* Unbox */
4164                         NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
4165                         MONO_ADD_INS (cfg->cbb, add);
4166                         /* Load value */
4167                         NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
4168                         MONO_ADD_INS (cfg->cbb, ins);
4169                         /* ins represents the call result */
4170                 }
4171         } else {
4172                 GSHAREDVT_FAILURE (CEE_CALLVIRT);
4173         }
4174
4175         *ref_emit_widen = emit_widen;
4176
4177         return ins;
4178
4179  exception_exit:
4180         return NULL;
4181 }
4182
4183 static void
4184 mono_emit_load_got_addr (MonoCompile *cfg)
4185 {
4186         MonoInst *getaddr, *dummy_use;
4187
4188         if (!cfg->got_var || cfg->got_var_allocated)
4189                 return;
4190
4191         MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
4192         getaddr->cil_code = cfg->header->code;
4193         getaddr->dreg = cfg->got_var->dreg;
4194
4195         /* Add it to the start of the first bblock */
4196         if (cfg->bb_entry->code) {
4197                 getaddr->next = cfg->bb_entry->code;
4198                 cfg->bb_entry->code = getaddr;
4199         }
4200         else
4201                 MONO_ADD_INS (cfg->bb_entry, getaddr);
4202
4203         cfg->got_var_allocated = TRUE;
4204
4205         /* 
4206          * Add a dummy use to keep the got_var alive, since real uses might
4207          * only be generated by the back ends.
4208          * Add it to end_bblock, so the variable's lifetime covers the whole
4209          * method.
4210          * It would be better to make the usage of the got var explicit in all
4211          * cases when the backend needs it (i.e. calls, throw etc.), so this
4212          * wouldn't be needed.
4213          */
4214         NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
4215         MONO_ADD_INS (cfg->bb_exit, dummy_use);
4216 }
4217
4218 static int inline_limit;
4219 static gboolean inline_limit_inited;
4220
4221 static gboolean
4222 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
4223 {
4224         MonoMethodHeaderSummary header;
4225         MonoVTable *vtable;
4226 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4227         MonoMethodSignature *sig = mono_method_signature (method);
4228         int i;
4229 #endif
4230
4231         if (cfg->disable_inline)
4232                 return FALSE;
4233         if (cfg->gsharedvt)
4234                 return FALSE;
4235
4236         if (cfg->inline_depth > 10)
4237                 return FALSE;
4238
4239         if (!mono_method_get_header_summary (method, &header))
4240                 return FALSE;
4241
4242         /*runtime, icall and pinvoke are checked by summary call*/
4243         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
4244             (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
4245             (mono_class_is_marshalbyref (method->klass)) ||
4246             header.has_clauses)
4247                 return FALSE;
4248
4249         /* also consider num_locals? */
4250         /* Do the size check early to avoid creating vtables */
4251         if (!inline_limit_inited) {
4252                 char *inlinelimit;
4253                 if ((inlinelimit = g_getenv ("MONO_INLINELIMIT"))) {
4254                         inline_limit = atoi (inlinelimit);
4255                         g_free (inlinelimit);
4256                 } else
4257                         inline_limit = INLINE_LENGTH_LIMIT;
4258                 inline_limit_inited = TRUE;
4259         }
4260         if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
4261                 return FALSE;
4262
4263         /*
4264          * if we can initialize the class of the method right away, we do,
4265          * otherwise we don't allow inlining if the class needs initialization,
4266          * since it would mean inserting a call to mono_runtime_class_init()
4267          * inside the inlined code
4268          */
4269         if (cfg->gshared && method->klass->has_cctor && mini_class_check_context_used (cfg, method->klass))
4270                 return FALSE;
4271
4272         if (!(cfg->opt & MONO_OPT_SHARED)) {
4273                 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
4274                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
4275                         if (method->klass->has_cctor) {
4276                                 vtable = mono_class_vtable (cfg->domain, method->klass);
4277                                 if (!vtable)
4278                                         return FALSE;
4279                                 if (!cfg->compile_aot) {
4280                                         MonoError error;
4281                                         if (!mono_runtime_class_init_full (vtable, &error)) {
4282                                                 mono_error_cleanup (&error);
4283                                                 return FALSE;
4284                                         }
4285                                 }
4286                         }
4287                 } else if (mono_class_is_before_field_init (method->klass)) {
4288                         if (cfg->run_cctors && method->klass->has_cctor) {
4289                                 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
4290                                 if (!method->klass->runtime_info)
4291                                         /* No vtable created yet */
4292                                         return FALSE;
4293                                 vtable = mono_class_vtable (cfg->domain, method->klass);
4294                                 if (!vtable)
4295                                         return FALSE;
4296                                 /* This makes so that inline cannot trigger */
4297                                 /* .cctors: too many apps depend on them */
4298                                 /* running with a specific order... */
4299                                 if (! vtable->initialized)
4300                                         return FALSE;
4301                                 MonoError error;
4302                                 if (!mono_runtime_class_init_full (vtable, &error)) {
4303                                         mono_error_cleanup (&error);
4304                                         return FALSE;
4305                                 }
4306                         }
4307                 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
4308                         if (!method->klass->runtime_info)
4309                                 /* No vtable created yet */
4310                                 return FALSE;
4311                         vtable = mono_class_vtable (cfg->domain, method->klass);
4312                         if (!vtable)
4313                                 return FALSE;
4314                         if (!vtable->initialized)
4315                                 return FALSE;
4316                 }
4317         } else {
4318                 /* 
4319                  * If we're compiling for shared code
4320                  * the cctor will need to be run at aot method load time, for example,
4321                  * or at the end of the compilation of the inlining method.
4322                  */
4323                 if (mono_class_needs_cctor_run (method->klass, NULL) && !mono_class_is_before_field_init (method->klass))
4324                         return FALSE;
4325         }
4326
4327 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4328         if (mono_arch_is_soft_float ()) {
4329                 /* FIXME: */
4330                 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
4331                         return FALSE;
4332                 for (i = 0; i < sig->param_count; ++i)
4333                         if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
4334                                 return FALSE;
4335         }
4336 #endif
4337
4338         if (g_list_find (cfg->dont_inline, method))
4339                 return FALSE;
4340
4341         if (mono_profiler_get_call_instrumentation_flags (method))
4342                 return FALSE;
4343
4344         return TRUE;
4345 }
4346
4347 static gboolean
4348 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
4349 {
4350         if (!cfg->compile_aot) {
4351                 g_assert (vtable);
4352                 if (vtable->initialized)
4353                         return FALSE;
4354         }
4355
4356         if (mono_class_is_before_field_init (klass)) {
4357                 if (cfg->method == method)
4358                         return FALSE;
4359         }
4360
4361         if (!mono_class_needs_cctor_run (klass, method))
4362                 return FALSE;
4363
4364         if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
4365                 /* The initialization is already done before the method is called */
4366                 return FALSE;
4367
4368         return TRUE;
4369 }
4370
4371 MonoInst*
4372 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
4373 {
4374         MonoInst *ins;
4375         guint32 size;
4376         int mult_reg, add_reg, array_reg, index_reg, index2_reg;
4377         int context_used;
4378
4379         if (mini_is_gsharedvt_variable_klass (klass)) {
4380                 size = -1;
4381         } else {
4382                 mono_class_init (klass);
4383                 size = mono_class_array_element_size (klass);
4384         }
4385
4386         mult_reg = alloc_preg (cfg);
4387         array_reg = arr->dreg;
4388         index_reg = index->dreg;
4389
4390 #if SIZEOF_REGISTER == 8
4391         /* The array reg is 64 bits but the index reg is only 32 */
4392         if (COMPILE_LLVM (cfg)) {
4393                 /*
4394                  * abcrem can't handle the OP_SEXT_I4, so add this after abcrem,
4395                  * during OP_BOUNDS_CHECK decomposition, and in the implementation
4396                  * of OP_X86_LEA for llvm.
4397                  */
4398                 index2_reg = index_reg;
4399         } else {
4400                 index2_reg = alloc_preg (cfg);
4401                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
4402         }
4403 #else
4404         if (index->type == STACK_I8) {
4405                 index2_reg = alloc_preg (cfg);
4406                 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
4407         } else {
4408                 index2_reg = index_reg;
4409         }
4410 #endif
4411
4412         if (bcheck)
4413                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
4414
4415 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4416         if (size == 1 || size == 2 || size == 4 || size == 8) {
4417                 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
4418
4419                 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
4420                 ins->klass = mono_class_get_element_class (klass);
4421                 ins->type = STACK_MP;
4422
4423                 return ins;
4424         }
4425 #endif          
4426
4427         add_reg = alloc_ireg_mp (cfg);
4428
4429         if (size == -1) {
4430                 MonoInst *rgctx_ins;
4431
4432                 /* gsharedvt */
4433                 g_assert (cfg->gshared);
4434                 context_used = mini_class_check_context_used (cfg, klass);
4435                 g_assert (context_used);
4436                 rgctx_ins = mini_emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
4437                 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
4438         } else {
4439                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
4440         }
4441         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
4442         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4443         ins->klass = mono_class_get_element_class (klass);
4444         ins->type = STACK_MP;
4445         MONO_ADD_INS (cfg->cbb, ins);
4446
4447         return ins;
4448 }
4449
4450 static MonoInst*
4451 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
4452 {
4453         int bounds_reg = alloc_preg (cfg);
4454         int add_reg = alloc_ireg_mp (cfg);
4455         int mult_reg = alloc_preg (cfg);
4456         int mult2_reg = alloc_preg (cfg);
4457         int low1_reg = alloc_preg (cfg);
4458         int low2_reg = alloc_preg (cfg);
4459         int high1_reg = alloc_preg (cfg);
4460         int high2_reg = alloc_preg (cfg);
4461         int realidx1_reg = alloc_preg (cfg);
4462         int realidx2_reg = alloc_preg (cfg);
4463         int sum_reg = alloc_preg (cfg);
4464         int index1, index2, tmpreg;
4465         MonoInst *ins;
4466         guint32 size;
4467
4468         mono_class_init (klass);
4469         size = mono_class_array_element_size (klass);
4470
4471         index1 = index_ins1->dreg;
4472         index2 = index_ins2->dreg;
4473
4474 #if SIZEOF_REGISTER == 8
4475         /* The array reg is 64 bits but the index reg is only 32 */
4476         if (COMPILE_LLVM (cfg)) {
4477                 /* Not needed */
4478         } else {
4479                 tmpreg = alloc_preg (cfg);
4480                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
4481                 index1 = tmpreg;
4482                 tmpreg = alloc_preg (cfg);
4483                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
4484                 index2 = tmpreg;
4485         }
4486 #else
4487         // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
4488         tmpreg = -1;
4489 #endif
4490
4491         /* range checking */
4492         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, 
4493                                        arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4494
4495         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg, 
4496                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4497         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
4498         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg, 
4499                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4500         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
4501         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4502
4503         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg, 
4504                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4505         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
4506         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg, 
4507                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4508         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
4509         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4510
4511         MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
4512         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
4513         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
4514         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
4515         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4516
4517         ins->type = STACK_MP;
4518         ins->klass = klass;
4519         MONO_ADD_INS (cfg->cbb, ins);
4520
4521         return ins;
4522 }
4523
4524 static MonoInst*
4525 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
4526 {
4527         int rank;
4528         MonoInst *addr;
4529         MonoMethod *addr_method;
4530         int element_size;
4531         MonoClass *eclass = cmethod->klass->element_class;
4532
4533         rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
4534
4535         if (rank == 1)
4536                 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
4537
4538         /* emit_ldelema_2 depends on OP_LMUL */
4539         if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
4540                 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
4541         }
4542
4543         if (mini_is_gsharedvt_variable_klass (eclass))
4544                 element_size = 0;
4545         else
4546                 element_size = mono_class_array_element_size (eclass);
4547         addr_method = mono_marshal_get_array_address (rank, element_size);
4548         addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
4549
4550         return addr;
4551 }
4552
4553 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
4554 static MonoInst*
4555 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4556 {
4557         MonoInst *addr, *store, *load;
4558         MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
4559
4560         /* the bounds check is already done by the callers */
4561         addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4562         if (is_set) {
4563                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
4564                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
4565                 if (mini_type_is_reference (&eklass->byval_arg))
4566                         mini_emit_write_barrier (cfg, addr, load);
4567         } else {
4568                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
4569                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
4570         }
4571         return store;
4572 }
4573
4574
4575 static gboolean
4576 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4577 {
4578         return mini_type_is_reference (&klass->byval_arg);
4579 }
4580
4581 static MonoInst*
4582 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
4583 {
4584         if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
4585                 !(MONO_INS_IS_PCONST_NULL (sp [2]))) {
4586                 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
4587                 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
4588                 MonoInst *iargs [3];
4589
4590                 if (!helper->slot)
4591                         mono_class_setup_vtable (obj_array);
4592                 g_assert (helper->slot);
4593
4594                 if (sp [0]->type != STACK_OBJ)
4595                         return NULL;
4596                 if (sp [2]->type != STACK_OBJ)
4597                         return NULL;
4598
4599                 iargs [2] = sp [2];
4600                 iargs [1] = sp [1];
4601                 iargs [0] = sp [0];
4602
4603                 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
4604         } else {
4605                 MonoInst *ins;
4606
4607                 if (mini_is_gsharedvt_variable_klass (klass)) {
4608                         MonoInst *addr;
4609
4610                         // FIXME-VT: OP_ICONST optimization
4611                         addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
4612                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4613                         ins->opcode = OP_STOREV_MEMBASE;
4614                 } else if (sp [1]->opcode == OP_ICONST) {
4615                         int array_reg = sp [0]->dreg;
4616                         int index_reg = sp [1]->dreg;
4617                         int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
4618
4619                         if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
4620                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
4621
4622                         if (safety_checks)
4623                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
4624                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
4625                 } else {
4626                         MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
4627                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4628                         if (generic_class_is_reference_type (cfg, klass))
4629                                 mini_emit_write_barrier (cfg, addr, sp [2]);
4630                 }
4631                 return ins;
4632         }
4633 }
4634
4635 static MonoInst*
4636 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4637 {
4638         MonoClass *eklass;
4639         
4640         if (is_set)
4641                 eklass = mono_class_from_mono_type (fsig->params [2]);
4642         else
4643                 eklass = mono_class_from_mono_type (fsig->ret);
4644
4645         if (is_set) {
4646                 return emit_array_store (cfg, eklass, args, FALSE);
4647         } else {
4648                 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4649                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
4650                 return ins;
4651         }
4652 }
4653
4654 static gboolean
4655 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
4656 {
4657         uint32_t align;
4658         int param_size, return_size;
4659
4660         param_klass = mono_class_from_mono_type (mini_get_underlying_type (&param_klass->byval_arg));
4661         return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
4662
4663         if (cfg->verbose_level > 3)
4664                 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
4665
4666         //Don't allow mixing reference types with value types
4667         if (param_klass->valuetype != return_klass->valuetype) {
4668                 if (cfg->verbose_level > 3)
4669                         printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
4670                 return FALSE;
4671         }
4672
4673         if (!param_klass->valuetype) {
4674                 if (cfg->verbose_level > 3)
4675                         printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
4676                 return TRUE;
4677         }
4678
4679         //That are blitable
4680         if (param_klass->has_references || return_klass->has_references)
4681                 return FALSE;
4682
4683         /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
4684         if ((MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
4685                 (!MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
4686                         if (cfg->verbose_level > 3)
4687                                 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
4688                 return FALSE;
4689         }
4690
4691         if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
4692                 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
4693                 if (cfg->verbose_level > 3)
4694                         printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
4695                 return FALSE;
4696         }
4697
4698         param_size = mono_class_value_size (param_klass, &align);
4699         return_size = mono_class_value_size (return_klass, &align);
4700
4701         //We can do it if sizes match
4702         if (param_size == return_size) {
4703                 if (cfg->verbose_level > 3)
4704                         printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
4705                 return TRUE;
4706         }
4707
4708         //No simple way to handle struct if sizes don't match
4709         if (MONO_TYPE_ISSTRUCT (&param_klass->byval_arg)) {
4710                 if (cfg->verbose_level > 3)
4711                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
4712                 return FALSE;
4713         }
4714
4715         /*
4716          * Same reg size category.
4717          * A quick note on why we don't require widening here.
4718          * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
4719          *
4720          * Since the source value comes from a function argument, the JIT will already have
4721          * the value in a VREG and performed any widening needed before (say, when loading from a field).
4722          */
4723         if (param_size <= 4 && return_size <= 4) {
4724                 if (cfg->verbose_level > 3)
4725                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
4726                 return TRUE;
4727         }
4728
4729         return FALSE;
4730 }
4731
4732 static MonoInst*
4733 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
4734 {
4735         MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
4736         MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
4737
4738         if (mini_is_gsharedvt_variable_type (fsig->ret))
4739                 return NULL;
4740
4741         //Valuetypes that are semantically equivalent or numbers than can be widened to
4742         if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
4743                 return args [0];
4744
4745         //Arrays of valuetypes that are semantically equivalent
4746         if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
4747                 return args [0];
4748
4749         return NULL;
4750 }
4751
4752 static MonoInst*
4753 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4754 {
4755 #ifdef MONO_ARCH_SIMD_INTRINSICS
4756         MonoInst *ins = NULL;
4757
4758         if (cfg->opt & MONO_OPT_SIMD) {
4759                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
4760                 if (ins)
4761                         return ins;
4762         }
4763 #endif
4764
4765         return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
4766 }
4767
4768 MonoInst*
4769 mini_emit_memory_barrier (MonoCompile *cfg, int kind)
4770 {
4771         MonoInst *ins = NULL;
4772         MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4773         MONO_ADD_INS (cfg->cbb, ins);
4774         ins->backend.memory_barrier_kind = kind;
4775
4776         return ins;
4777 }
4778
4779 static MonoInst*
4780 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4781 {
4782         MonoInst *ins = NULL;
4783         int opcode = 0;
4784
4785         /* The LLVM backend supports these intrinsics */
4786         if (cmethod->klass == mono_defaults.math_class) {
4787                 if (strcmp (cmethod->name, "Sin") == 0) {
4788                         opcode = OP_SIN;
4789                 } else if (strcmp (cmethod->name, "Cos") == 0) {
4790                         opcode = OP_COS;
4791                 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
4792                         opcode = OP_SQRT;
4793                 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
4794                         opcode = OP_ABS;
4795                 }
4796
4797                 if (opcode && fsig->param_count == 1) {
4798                         MONO_INST_NEW (cfg, ins, opcode);
4799                         ins->type = STACK_R8;
4800                         ins->dreg = mono_alloc_dreg (cfg, ins->type);
4801                         ins->sreg1 = args [0]->dreg;
4802                         MONO_ADD_INS (cfg->cbb, ins);
4803                 }
4804
4805                 opcode = 0;
4806                 if (cfg->opt & MONO_OPT_CMOV) {
4807                         if (strcmp (cmethod->name, "Min") == 0) {
4808                                 if (fsig->params [0]->type == MONO_TYPE_I4)
4809                                         opcode = OP_IMIN;
4810                                 if (fsig->params [0]->type == MONO_TYPE_U4)
4811                                         opcode = OP_IMIN_UN;
4812                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
4813                                         opcode = OP_LMIN;
4814                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
4815                                         opcode = OP_LMIN_UN;
4816                         } else if (strcmp (cmethod->name, "Max") == 0) {
4817                                 if (fsig->params [0]->type == MONO_TYPE_I4)
4818                                         opcode = OP_IMAX;
4819                                 if (fsig->params [0]->type == MONO_TYPE_U4)
4820                                         opcode = OP_IMAX_UN;
4821                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
4822                                         opcode = OP_LMAX;
4823                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
4824                                         opcode = OP_LMAX_UN;
4825                         }
4826                 }
4827
4828                 if (opcode && fsig->param_count == 2) {
4829                         MONO_INST_NEW (cfg, ins, opcode);
4830                         ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
4831                         ins->dreg = mono_alloc_dreg (cfg, ins->type);
4832                         ins->sreg1 = args [0]->dreg;
4833                         ins->sreg2 = args [1]->dreg;
4834                         MONO_ADD_INS (cfg->cbb, ins);
4835                 }
4836         }
4837
4838         return ins;
4839 }
4840
4841 static MonoInst*
4842 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4843 {
4844         if (cmethod->klass == mono_defaults.array_class) {
4845                 if (strcmp (cmethod->name, "UnsafeStore") == 0)
4846                         return emit_array_unsafe_access (cfg, fsig, args, TRUE);
4847                 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
4848                         return emit_array_unsafe_access (cfg, fsig, args, FALSE);
4849                 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
4850                         return emit_array_unsafe_mov (cfg, fsig, args);
4851         }
4852
4853         return NULL;
4854 }
4855
4856
4857 static gboolean
4858 mono_type_is_native_blittable (MonoType *t)
4859 {
4860         if (MONO_TYPE_IS_REFERENCE (t))
4861                 return FALSE;
4862
4863         if (MONO_TYPE_IS_PRIMITIVE_SCALAR (t))
4864                 return TRUE;
4865
4866         MonoClass *klass = mono_class_from_mono_type (t);
4867
4868         //MonoClass::blitable depends on mono_class_setup_fields being done.
4869         mono_class_setup_fields (klass);
4870         if (!klass->blittable)
4871                 return FALSE;
4872
4873         // If the native marshal size is different we can't convert PtrToStructure to a type load
4874         if (mono_class_native_size (klass, NULL) != mono_class_value_size (klass, NULL))
4875                 return FALSE;
4876
4877         return TRUE;
4878 }
4879
4880
4881 static MonoInst*
4882 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4883 {
4884         MonoInst *ins = NULL;
4885         MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
4886
4887         if (cmethod->klass == mono_defaults.string_class) {
4888                 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
4889                         int dreg = alloc_ireg (cfg);
4890                         int index_reg = alloc_preg (cfg);
4891                         int add_reg = alloc_preg (cfg);
4892
4893 #if SIZEOF_REGISTER == 8
4894                         if (COMPILE_LLVM (cfg)) {
4895                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
4896                         } else {
4897                                 /* The array reg is 64 bits but the index reg is only 32 */
4898                                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
4899                         }
4900 #else
4901                         index_reg = args [1]->dreg;
4902 #endif  
4903                         MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
4904
4905 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4906                         EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
4907                         add_reg = ins->dreg;
4908                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
4909                                                                    add_reg, 0);
4910 #else
4911                         int mult_reg = alloc_preg (cfg);
4912                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
4913                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
4914                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
4915                                                                    add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
4916 #endif
4917                         type_from_op (cfg, ins, NULL, NULL);
4918                         return ins;
4919                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
4920                         int dreg = alloc_ireg (cfg);
4921                         /* Decompose later to allow more optimizations */
4922                         EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
4923                         ins->type = STACK_I4;
4924                         ins->flags |= MONO_INST_FAULT;
4925                         cfg->cbb->has_array_access = TRUE;
4926                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
4927
4928                         return ins;
4929                 } else 
4930                         return NULL;
4931         } else if (cmethod->klass == mono_defaults.object_class) {
4932                 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
4933                         int dreg = alloc_ireg_ref (cfg);
4934                         int vt_reg = alloc_preg (cfg);
4935                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4936                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
4937                         type_from_op (cfg, ins, NULL, NULL);
4938
4939                         return ins;
4940                 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
4941                         int dreg = alloc_ireg (cfg);
4942                         int t1 = alloc_ireg (cfg);
4943         
4944                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
4945                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
4946                         ins->type = STACK_I4;
4947
4948                         return ins;
4949                 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
4950                         MONO_INST_NEW (cfg, ins, OP_NOP);
4951                         MONO_ADD_INS (cfg->cbb, ins);
4952                         return ins;
4953                 } else
4954                         return NULL;
4955         } else if (cmethod->klass == mono_defaults.array_class) {
4956                 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
4957                         return emit_array_generic_access (cfg, fsig, args, FALSE);
4958                 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
4959                         return emit_array_generic_access (cfg, fsig, args, TRUE);
4960
4961 #ifndef MONO_BIG_ARRAYS
4962                 /*
4963                  * This is an inline version of GetLength/GetLowerBound(0) used frequently in
4964                  * Array methods.
4965                  */
4966                 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
4967                          (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
4968                          args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
4969                         int dreg = alloc_ireg (cfg);
4970                         int bounds_reg = alloc_ireg_mp (cfg);
4971                         MonoBasicBlock *end_bb, *szarray_bb;
4972                         gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
4973
4974                         NEW_BBLOCK (cfg, end_bb);
4975                         NEW_BBLOCK (cfg, szarray_bb);
4976
4977                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
4978                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4979                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
4980                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
4981                         /* Non-szarray case */
4982                         if (get_length)
4983                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
4984                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4985                         else
4986                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
4987                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4988                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4989                         MONO_START_BB (cfg, szarray_bb);
4990                         /* Szarray case */
4991                         if (get_length)
4992                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
4993                                                                            args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
4994                         else
4995                                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4996                         MONO_START_BB (cfg, end_bb);
4997
4998                         EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
4999                         ins->type = STACK_I4;
5000                         
5001                         return ins;
5002                 }
5003 #endif
5004
5005                 if (cmethod->name [0] != 'g')
5006                         return NULL;
5007
5008                 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
5009                         int dreg = alloc_ireg (cfg);
5010                         int vtable_reg = alloc_preg (cfg);
5011                         MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg, 
5012                                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5013                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
5014                                                                    vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
5015                         type_from_op (cfg, ins, NULL, NULL);
5016
5017                         return ins;
5018                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5019                         int dreg = alloc_ireg (cfg);
5020
5021                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg, 
5022                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5023                         type_from_op (cfg, ins, NULL, NULL);
5024
5025                         return ins;
5026                 } else
5027                         return NULL;
5028         } else if (cmethod->klass == runtime_helpers_class) {
5029                 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
5030                         EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
5031                         return ins;
5032                 } else if (strcmp (cmethod->name, "IsReferenceOrContainsReferences") == 0 && fsig->param_count == 0) {
5033                         MonoGenericContext *ctx = mono_method_get_context (cmethod);
5034                         g_assert (ctx);
5035                         g_assert (ctx->method_inst);
5036                         g_assert (ctx->method_inst->type_argc == 1);
5037                         MonoType *arg_type = ctx->method_inst->type_argv [0];
5038                         MonoType *t;
5039                         MonoClass *klass;
5040
5041                         ins = NULL;
5042
5043                         /* Resolve the argument class as possible so we can handle common cases fast */
5044                         t = mini_get_underlying_type (arg_type);
5045                         klass = mono_class_from_mono_type (t);
5046                         mono_class_init (klass);
5047                         if (MONO_TYPE_IS_REFERENCE (t))
5048                                 EMIT_NEW_ICONST (cfg, ins, 1);
5049                         else if (MONO_TYPE_IS_PRIMITIVE (t))
5050                                 EMIT_NEW_ICONST (cfg, ins, 0);
5051                         else if (cfg->gshared && (t->type == MONO_TYPE_VAR || t->type == MONO_TYPE_MVAR) && !mini_type_var_is_vt (t))
5052                                 EMIT_NEW_ICONST (cfg, ins, 1);
5053                         else if (!cfg->gshared || !mini_class_check_context_used (cfg, klass))
5054                                 EMIT_NEW_ICONST (cfg, ins, klass->has_references ? 1 : 0);
5055                         else {
5056                                 g_assert (cfg->gshared);
5057
5058                                 /* Have to use the original argument class here */
5059                                 MonoClass *arg_class = mono_class_from_mono_type (arg_type);
5060                                 int context_used = mini_class_check_context_used (cfg, arg_class);
5061
5062                                 /* This returns 1 or 2 */
5063                                 MonoInst *info = mini_emit_get_rgctx_klass (cfg, context_used, arg_class, MONO_RGCTX_INFO_CLASS_IS_REF_OR_CONTAINS_REFS);
5064                                 int dreg = alloc_ireg (cfg);
5065                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ISUB_IMM, dreg, info->dreg, 1);
5066                         }
5067
5068                         return ins;
5069                 } else
5070                         return NULL;
5071         } else if (cmethod->klass == mono_defaults.monitor_class) {
5072                 gboolean is_enter = FALSE;
5073                 gboolean is_v4 = FALSE;
5074
5075                 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 2 && fsig->params [1]->byref) {
5076                         is_enter = TRUE;
5077                         is_v4 = TRUE;
5078                 }
5079                 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 1)
5080                         is_enter = TRUE;
5081
5082                 if (is_enter) {
5083                         /*
5084                          * To make async stack traces work, icalls which can block should have a wrapper.
5085                          * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
5086                          */
5087                         MonoBasicBlock *end_bb;
5088
5089                         NEW_BBLOCK (cfg, end_bb);
5090
5091                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
5092                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
5093                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
5094                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_internal : (gpointer)mono_monitor_enter_internal, args);
5095                         MONO_START_BB (cfg, end_bb);
5096                         return ins;
5097                 }
5098         } else if (cmethod->klass == mono_defaults.thread_class) {
5099                 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
5100                         MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
5101                         MONO_ADD_INS (cfg->cbb, ins);
5102                         return ins;
5103                 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
5104                         return mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5105                 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
5106                         guint32 opcode = 0;
5107                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5108
5109                         if (fsig->params [0]->type == MONO_TYPE_I1)
5110                                 opcode = OP_LOADI1_MEMBASE;
5111                         else if (fsig->params [0]->type == MONO_TYPE_U1)
5112                                 opcode = OP_LOADU1_MEMBASE;
5113                         else if (fsig->params [0]->type == MONO_TYPE_I2)
5114                                 opcode = OP_LOADI2_MEMBASE;
5115                         else if (fsig->params [0]->type == MONO_TYPE_U2)
5116                                 opcode = OP_LOADU2_MEMBASE;
5117                         else if (fsig->params [0]->type == MONO_TYPE_I4)
5118                                 opcode = OP_LOADI4_MEMBASE;
5119                         else if (fsig->params [0]->type == MONO_TYPE_U4)
5120                                 opcode = OP_LOADU4_MEMBASE;
5121                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5122                                 opcode = OP_LOADI8_MEMBASE;
5123                         else if (fsig->params [0]->type == MONO_TYPE_R4)
5124                                 opcode = OP_LOADR4_MEMBASE;
5125                         else if (fsig->params [0]->type == MONO_TYPE_R8)
5126                                 opcode = OP_LOADR8_MEMBASE;
5127                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5128                                 opcode = OP_LOAD_MEMBASE;
5129
5130                         if (opcode) {
5131                                 MONO_INST_NEW (cfg, ins, opcode);
5132                                 ins->inst_basereg = args [0]->dreg;
5133                                 ins->inst_offset = 0;
5134                                 MONO_ADD_INS (cfg->cbb, ins);
5135
5136                                 switch (fsig->params [0]->type) {
5137                                 case MONO_TYPE_I1:
5138                                 case MONO_TYPE_U1:
5139                                 case MONO_TYPE_I2:
5140                                 case MONO_TYPE_U2:
5141                                 case MONO_TYPE_I4:
5142                                 case MONO_TYPE_U4:
5143                                         ins->dreg = mono_alloc_ireg (cfg);
5144                                         ins->type = STACK_I4;
5145                                         break;
5146                                 case MONO_TYPE_I8:
5147                                 case MONO_TYPE_U8:
5148                                         ins->dreg = mono_alloc_lreg (cfg);
5149                                         ins->type = STACK_I8;
5150                                         break;
5151                                 case MONO_TYPE_I:
5152                                 case MONO_TYPE_U:
5153                                         ins->dreg = mono_alloc_ireg (cfg);
5154 #if SIZEOF_REGISTER == 8
5155                                         ins->type = STACK_I8;
5156 #else
5157                                         ins->type = STACK_I4;
5158 #endif
5159                                         break;
5160                                 case MONO_TYPE_R4:
5161                                 case MONO_TYPE_R8:
5162                                         ins->dreg = mono_alloc_freg (cfg);
5163                                         ins->type = STACK_R8;
5164                                         break;
5165                                 default:
5166                                         g_assert (mini_type_is_reference (fsig->params [0]));
5167                                         ins->dreg = mono_alloc_ireg_ref (cfg);
5168                                         ins->type = STACK_OBJ;
5169                                         break;
5170                                 }
5171
5172                                 if (opcode == OP_LOADI8_MEMBASE)
5173                                         ins = mono_decompose_opcode (cfg, ins);
5174
5175                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5176
5177                                 return ins;
5178                         }
5179                 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
5180                         guint32 opcode = 0;
5181                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5182
5183                         if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
5184                                 opcode = OP_STOREI1_MEMBASE_REG;
5185                         else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
5186                                 opcode = OP_STOREI2_MEMBASE_REG;
5187                         else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
5188                                 opcode = OP_STOREI4_MEMBASE_REG;
5189                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5190                                 opcode = OP_STOREI8_MEMBASE_REG;
5191                         else if (fsig->params [0]->type == MONO_TYPE_R4)
5192                                 opcode = OP_STORER4_MEMBASE_REG;
5193                         else if (fsig->params [0]->type == MONO_TYPE_R8)
5194                                 opcode = OP_STORER8_MEMBASE_REG;
5195                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5196                                 opcode = OP_STORE_MEMBASE_REG;
5197
5198                         if (opcode) {
5199                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5200
5201                                 MONO_INST_NEW (cfg, ins, opcode);
5202                                 ins->sreg1 = args [1]->dreg;
5203                                 ins->inst_destbasereg = args [0]->dreg;
5204                                 ins->inst_offset = 0;
5205                                 MONO_ADD_INS (cfg->cbb, ins);
5206
5207                                 if (opcode == OP_STOREI8_MEMBASE_REG)
5208                                         ins = mono_decompose_opcode (cfg, ins);
5209
5210                                 return ins;
5211                         }
5212                 }
5213         } else if (cmethod->klass->image == mono_defaults.corlib &&
5214                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5215                            (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
5216                 ins = NULL;
5217
5218 #if SIZEOF_REGISTER == 8
5219                 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
5220                         if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
5221                                 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
5222                                 ins->dreg = mono_alloc_preg (cfg);
5223                                 ins->sreg1 = args [0]->dreg;
5224                                 ins->type = STACK_I8;
5225                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
5226                                 MONO_ADD_INS (cfg->cbb, ins);
5227                         } else {
5228                                 MonoInst *load_ins;
5229
5230                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5231
5232                                 /* 64 bit reads are already atomic */
5233                                 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
5234                                 load_ins->dreg = mono_alloc_preg (cfg);
5235                                 load_ins->inst_basereg = args [0]->dreg;
5236                                 load_ins->inst_offset = 0;
5237                                 load_ins->type = STACK_I8;
5238                                 MONO_ADD_INS (cfg->cbb, load_ins);
5239
5240                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5241
5242                                 ins = load_ins;
5243                         }
5244                 }
5245 #endif
5246
5247                 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
5248                         MonoInst *ins_iconst;
5249                         guint32 opcode = 0;
5250
5251                         if (fsig->params [0]->type == MONO_TYPE_I4) {
5252                                 opcode = OP_ATOMIC_ADD_I4;
5253                                 cfg->has_atomic_add_i4 = TRUE;
5254                         }
5255 #if SIZEOF_REGISTER == 8
5256                         else if (fsig->params [0]->type == MONO_TYPE_I8)
5257                                 opcode = OP_ATOMIC_ADD_I8;
5258 #endif
5259                         if (opcode) {
5260                                 if (!mono_arch_opcode_supported (opcode))
5261                                         return NULL;
5262                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5263                                 ins_iconst->inst_c0 = 1;
5264                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
5265                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
5266
5267                                 MONO_INST_NEW (cfg, ins, opcode);
5268                                 ins->dreg = mono_alloc_ireg (cfg);
5269                                 ins->inst_basereg = args [0]->dreg;
5270                                 ins->inst_offset = 0;
5271                                 ins->sreg2 = ins_iconst->dreg;
5272                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5273                                 MONO_ADD_INS (cfg->cbb, ins);
5274                         }
5275                 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
5276                         MonoInst *ins_iconst;
5277                         guint32 opcode = 0;
5278
5279                         if (fsig->params [0]->type == MONO_TYPE_I4) {
5280                                 opcode = OP_ATOMIC_ADD_I4;
5281                                 cfg->has_atomic_add_i4 = TRUE;
5282                         }
5283 #if SIZEOF_REGISTER == 8
5284                         else if (fsig->params [0]->type == MONO_TYPE_I8)
5285                                 opcode = OP_ATOMIC_ADD_I8;
5286 #endif
5287                         if (opcode) {
5288                                 if (!mono_arch_opcode_supported (opcode))
5289                                         return NULL;
5290                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5291                                 ins_iconst->inst_c0 = -1;
5292                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
5293                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
5294
5295                                 MONO_INST_NEW (cfg, ins, opcode);
5296                                 ins->dreg = mono_alloc_ireg (cfg);
5297                                 ins->inst_basereg = args [0]->dreg;
5298                                 ins->inst_offset = 0;
5299                                 ins->sreg2 = ins_iconst->dreg;
5300                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5301                                 MONO_ADD_INS (cfg->cbb, ins);
5302                         }
5303                 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
5304                         guint32 opcode = 0;
5305
5306                         if (fsig->params [0]->type == MONO_TYPE_I4) {
5307                                 opcode = OP_ATOMIC_ADD_I4;
5308                                 cfg->has_atomic_add_i4 = TRUE;
5309                         }
5310 #if SIZEOF_REGISTER == 8
5311                         else if (fsig->params [0]->type == MONO_TYPE_I8)
5312                                 opcode = OP_ATOMIC_ADD_I8;
5313 #endif
5314                         if (opcode) {
5315                                 if (!mono_arch_opcode_supported (opcode))
5316                                         return NULL;
5317                                 MONO_INST_NEW (cfg, ins, opcode);
5318                                 ins->dreg = mono_alloc_ireg (cfg);
5319                                 ins->inst_basereg = args [0]->dreg;
5320                                 ins->inst_offset = 0;
5321                                 ins->sreg2 = args [1]->dreg;
5322                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5323                                 MONO_ADD_INS (cfg->cbb, ins);
5324                         }
5325                 }
5326                 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
5327                         MonoInst *f2i = NULL, *i2f;
5328                         guint32 opcode, f2i_opcode, i2f_opcode;
5329                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5330                         gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
5331
5332                         if (fsig->params [0]->type == MONO_TYPE_I4 ||
5333                             fsig->params [0]->type == MONO_TYPE_R4) {
5334                                 opcode = OP_ATOMIC_EXCHANGE_I4;
5335                                 f2i_opcode = OP_MOVE_F_TO_I4;
5336                                 i2f_opcode = OP_MOVE_I4_TO_F;
5337                                 cfg->has_atomic_exchange_i4 = TRUE;
5338                         }
5339 #if SIZEOF_REGISTER == 8
5340                         else if (is_ref ||
5341                                  fsig->params [0]->type == MONO_TYPE_I8 ||
5342                                  fsig->params [0]->type == MONO_TYPE_R8 ||
5343                                  fsig->params [0]->type == MONO_TYPE_I) {
5344                                 opcode = OP_ATOMIC_EXCHANGE_I8;
5345                                 f2i_opcode = OP_MOVE_F_TO_I8;
5346                                 i2f_opcode = OP_MOVE_I8_TO_F;
5347                         }
5348 #else
5349                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
5350                                 opcode = OP_ATOMIC_EXCHANGE_I4;
5351                                 cfg->has_atomic_exchange_i4 = TRUE;
5352                         }
5353 #endif
5354                         else
5355                                 return NULL;
5356
5357                         if (!mono_arch_opcode_supported (opcode))
5358                                 return NULL;
5359
5360                         if (is_float) {
5361                                 /* TODO: Decompose these opcodes instead of bailing here. */
5362                                 if (COMPILE_SOFT_FLOAT (cfg))
5363                                         return NULL;
5364
5365                                 MONO_INST_NEW (cfg, f2i, f2i_opcode);
5366                                 f2i->dreg = mono_alloc_ireg (cfg);
5367                                 f2i->sreg1 = args [1]->dreg;
5368                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
5369                                         f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5370                                 MONO_ADD_INS (cfg->cbb, f2i);
5371                         }
5372
5373                         MONO_INST_NEW (cfg, ins, opcode);
5374                         ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
5375                         ins->inst_basereg = args [0]->dreg;
5376                         ins->inst_offset = 0;
5377                         ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
5378                         MONO_ADD_INS (cfg->cbb, ins);
5379
5380                         switch (fsig->params [0]->type) {
5381                         case MONO_TYPE_I4:
5382                                 ins->type = STACK_I4;
5383                                 break;
5384                         case MONO_TYPE_I8:
5385                                 ins->type = STACK_I8;
5386                                 break;
5387                         case MONO_TYPE_I:
5388 #if SIZEOF_REGISTER == 8
5389                                 ins->type = STACK_I8;
5390 #else
5391                                 ins->type = STACK_I4;
5392 #endif
5393                                 break;
5394                         case MONO_TYPE_R4:
5395                         case MONO_TYPE_R8:
5396                                 ins->type = STACK_R8;
5397                                 break;
5398                         default:
5399                                 g_assert (mini_type_is_reference (fsig->params [0]));
5400                                 ins->type = STACK_OBJ;
5401                                 break;
5402                         }
5403
5404                         if (is_float) {
5405                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5406                                 i2f->dreg = mono_alloc_freg (cfg);
5407                                 i2f->sreg1 = ins->dreg;
5408                                 i2f->type = STACK_R8;
5409                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
5410                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5411                                 MONO_ADD_INS (cfg->cbb, i2f);
5412
5413                                 ins = i2f;
5414                         }
5415
5416                         if (cfg->gen_write_barriers && is_ref)
5417                                 mini_emit_write_barrier (cfg, args [0], args [1]);
5418                 }
5419                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
5420                         MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
5421                         guint32 opcode, f2i_opcode, i2f_opcode;
5422                         gboolean is_ref = mini_type_is_reference (fsig->params [1]);
5423                         gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
5424
5425                         if (fsig->params [1]->type == MONO_TYPE_I4 ||
5426                             fsig->params [1]->type == MONO_TYPE_R4) {
5427                                 opcode = OP_ATOMIC_CAS_I4;
5428                                 f2i_opcode = OP_MOVE_F_TO_I4;
5429                                 i2f_opcode = OP_MOVE_I4_TO_F;
5430                                 cfg->has_atomic_cas_i4 = TRUE;
5431                         }
5432 #if SIZEOF_REGISTER == 8
5433                         else if (is_ref ||
5434                                  fsig->params [1]->type == MONO_TYPE_I8 ||
5435                                  fsig->params [1]->type == MONO_TYPE_R8 ||
5436                                  fsig->params [1]->type == MONO_TYPE_I) {
5437                                 opcode = OP_ATOMIC_CAS_I8;
5438                                 f2i_opcode = OP_MOVE_F_TO_I8;
5439                                 i2f_opcode = OP_MOVE_I8_TO_F;
5440                         }
5441 #else
5442                         else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
5443                                 opcode = OP_ATOMIC_CAS_I4;
5444                                 cfg->has_atomic_cas_i4 = TRUE;
5445                         }
5446 #endif
5447                         else
5448                                 return NULL;
5449
5450                         if (!mono_arch_opcode_supported (opcode))
5451                                 return NULL;
5452
5453                         if (is_float) {
5454                                 /* TODO: Decompose these opcodes instead of bailing here. */
5455                                 if (COMPILE_SOFT_FLOAT (cfg))
5456                                         return NULL;
5457
5458                                 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
5459                                 f2i_new->dreg = mono_alloc_ireg (cfg);
5460                                 f2i_new->sreg1 = args [1]->dreg;
5461                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
5462                                         f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5463                                 MONO_ADD_INS (cfg->cbb, f2i_new);
5464
5465                                 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
5466                                 f2i_cmp->dreg = mono_alloc_ireg (cfg);
5467                                 f2i_cmp->sreg1 = args [2]->dreg;
5468                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
5469                                         f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5470                                 MONO_ADD_INS (cfg->cbb, f2i_cmp);
5471                         }
5472
5473                         MONO_INST_NEW (cfg, ins, opcode);
5474                         ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
5475                         ins->sreg1 = args [0]->dreg;
5476                         ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
5477                         ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
5478                         MONO_ADD_INS (cfg->cbb, ins);
5479
5480                         switch (fsig->params [1]->type) {
5481                         case MONO_TYPE_I4:
5482                                 ins->type = STACK_I4;
5483                                 break;
5484                         case MONO_TYPE_I8:
5485                                 ins->type = STACK_I8;
5486                                 break;
5487                         case MONO_TYPE_I:
5488 #if SIZEOF_REGISTER == 8
5489                                 ins->type = STACK_I8;
5490 #else
5491                                 ins->type = STACK_I4;
5492 #endif
5493                                 break;
5494                         case MONO_TYPE_R4:
5495                                 ins->type = cfg->r4_stack_type;
5496                                 break;
5497                         case MONO_TYPE_R8:
5498                                 ins->type = STACK_R8;
5499                                 break;
5500                         default:
5501                                 g_assert (mini_type_is_reference (fsig->params [1]));
5502                                 ins->type = STACK_OBJ;
5503                                 break;
5504                         }
5505
5506                         if (is_float) {
5507                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5508                                 i2f->dreg = mono_alloc_freg (cfg);
5509                                 i2f->sreg1 = ins->dreg;
5510                                 i2f->type = STACK_R8;
5511                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
5512                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5513                                 MONO_ADD_INS (cfg->cbb, i2f);
5514
5515                                 ins = i2f;
5516                         }
5517
5518                         if (cfg->gen_write_barriers && is_ref)
5519                                 mini_emit_write_barrier (cfg, args [0], args [1]);
5520                 }
5521                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
5522                          fsig->params [1]->type == MONO_TYPE_I4) {
5523                         MonoInst *cmp, *ceq;
5524
5525                         if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
5526                                 return NULL;
5527
5528                         /* int32 r = CAS (location, value, comparand); */
5529                         MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
5530                         ins->dreg = alloc_ireg (cfg);
5531                         ins->sreg1 = args [0]->dreg;
5532                         ins->sreg2 = args [1]->dreg;
5533                         ins->sreg3 = args [2]->dreg;
5534                         ins->type = STACK_I4;
5535                         MONO_ADD_INS (cfg->cbb, ins);
5536
5537                         /* bool result = r == comparand; */
5538                         MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
5539                         cmp->sreg1 = ins->dreg;
5540                         cmp->sreg2 = args [2]->dreg;
5541                         cmp->type = STACK_I4;
5542                         MONO_ADD_INS (cfg->cbb, cmp);
5543
5544                         MONO_INST_NEW (cfg, ceq, OP_ICEQ);
5545                         ceq->dreg = alloc_ireg (cfg);
5546                         ceq->type = STACK_I4;
5547                         MONO_ADD_INS (cfg->cbb, ceq);
5548
5549                         /* *success = result; */
5550                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
5551
5552                         cfg->has_atomic_cas_i4 = TRUE;
5553                 }
5554                 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
5555                         ins = mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5556
5557                 if (ins)
5558                         return ins;
5559         } else if (cmethod->klass->image == mono_defaults.corlib &&
5560                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5561                            (strcmp (cmethod->klass->name, "Volatile") == 0)) {
5562                 ins = NULL;
5563
5564                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
5565                         guint32 opcode = 0;
5566                         MonoType *t = fsig->params [0];
5567                         gboolean is_ref;
5568                         gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
5569
5570                         g_assert (t->byref);
5571                         /* t is a byref type, so the reference check is more complicated */
5572                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5573                         if (t->type == MONO_TYPE_I1)
5574                                 opcode = OP_ATOMIC_LOAD_I1;
5575                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5576                                 opcode = OP_ATOMIC_LOAD_U1;
5577                         else if (t->type == MONO_TYPE_I2)
5578                                 opcode = OP_ATOMIC_LOAD_I2;
5579                         else if (t->type == MONO_TYPE_U2)
5580                                 opcode = OP_ATOMIC_LOAD_U2;
5581                         else if (t->type == MONO_TYPE_I4)
5582                                 opcode = OP_ATOMIC_LOAD_I4;
5583                         else if (t->type == MONO_TYPE_U4)
5584                                 opcode = OP_ATOMIC_LOAD_U4;
5585                         else if (t->type == MONO_TYPE_R4)
5586                                 opcode = OP_ATOMIC_LOAD_R4;
5587                         else if (t->type == MONO_TYPE_R8)
5588                                 opcode = OP_ATOMIC_LOAD_R8;
5589 #if SIZEOF_REGISTER == 8
5590                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5591                                 opcode = OP_ATOMIC_LOAD_I8;
5592                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5593                                 opcode = OP_ATOMIC_LOAD_U8;
5594 #else
5595                         else if (t->type == MONO_TYPE_I)
5596                                 opcode = OP_ATOMIC_LOAD_I4;
5597                         else if (is_ref || t->type == MONO_TYPE_U)
5598                                 opcode = OP_ATOMIC_LOAD_U4;
5599 #endif
5600
5601                         if (opcode) {
5602                                 if (!mono_arch_opcode_supported (opcode))
5603                                         return NULL;
5604
5605                                 MONO_INST_NEW (cfg, ins, opcode);
5606                                 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
5607                                 ins->sreg1 = args [0]->dreg;
5608                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
5609                                 MONO_ADD_INS (cfg->cbb, ins);
5610
5611                                 switch (t->type) {
5612                                 case MONO_TYPE_BOOLEAN:
5613                                 case MONO_TYPE_I1:
5614                                 case MONO_TYPE_U1:
5615                                 case MONO_TYPE_I2:
5616                                 case MONO_TYPE_U2:
5617                                 case MONO_TYPE_I4:
5618                                 case MONO_TYPE_U4:
5619                                         ins->type = STACK_I4;
5620                                         break;
5621                                 case MONO_TYPE_I8:
5622                                 case MONO_TYPE_U8:
5623                                         ins->type = STACK_I8;
5624                                         break;
5625                                 case MONO_TYPE_I:
5626                                 case MONO_TYPE_U:
5627 #if SIZEOF_REGISTER == 8
5628                                         ins->type = STACK_I8;
5629 #else
5630                                         ins->type = STACK_I4;
5631 #endif
5632                                         break;
5633                                 case MONO_TYPE_R4:
5634                                         ins->type = cfg->r4_stack_type;
5635                                         break;
5636                                 case MONO_TYPE_R8:
5637                                         ins->type = STACK_R8;
5638                                         break;
5639                                 default:
5640                                         g_assert (is_ref);
5641                                         ins->type = STACK_OBJ;
5642                                         break;
5643                                 }
5644                         }
5645                 }
5646
5647                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
5648                         guint32 opcode = 0;
5649                         MonoType *t = fsig->params [0];
5650                         gboolean is_ref;
5651
5652                         g_assert (t->byref);
5653                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5654                         if (t->type == MONO_TYPE_I1)
5655                                 opcode = OP_ATOMIC_STORE_I1;
5656                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5657                                 opcode = OP_ATOMIC_STORE_U1;
5658                         else if (t->type == MONO_TYPE_I2)
5659                                 opcode = OP_ATOMIC_STORE_I2;
5660                         else if (t->type == MONO_TYPE_U2)
5661                                 opcode = OP_ATOMIC_STORE_U2;
5662                         else if (t->type == MONO_TYPE_I4)
5663                                 opcode = OP_ATOMIC_STORE_I4;
5664                         else if (t->type == MONO_TYPE_U4)
5665                                 opcode = OP_ATOMIC_STORE_U4;
5666                         else if (t->type == MONO_TYPE_R4)
5667                                 opcode = OP_ATOMIC_STORE_R4;
5668                         else if (t->type == MONO_TYPE_R8)
5669                                 opcode = OP_ATOMIC_STORE_R8;
5670 #if SIZEOF_REGISTER == 8
5671                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5672                                 opcode = OP_ATOMIC_STORE_I8;
5673                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5674                                 opcode = OP_ATOMIC_STORE_U8;
5675 #else
5676                         else if (t->type == MONO_TYPE_I)
5677                                 opcode = OP_ATOMIC_STORE_I4;
5678                         else if (is_ref || t->type == MONO_TYPE_U)
5679                                 opcode = OP_ATOMIC_STORE_U4;
5680 #endif
5681
5682                         if (opcode) {
5683                                 if (!mono_arch_opcode_supported (opcode))
5684                                         return NULL;
5685
5686                                 MONO_INST_NEW (cfg, ins, opcode);
5687                                 ins->dreg = args [0]->dreg;
5688                                 ins->sreg1 = args [1]->dreg;
5689                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
5690                                 MONO_ADD_INS (cfg->cbb, ins);
5691
5692                                 if (cfg->gen_write_barriers && is_ref)
5693                                         mini_emit_write_barrier (cfg, args [0], args [1]);
5694                         }
5695                 }
5696
5697                 if (ins)
5698                         return ins;
5699         } else if (cmethod->klass->image == mono_defaults.corlib &&
5700                            (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
5701                            (strcmp (cmethod->klass->name, "Debugger") == 0)) {
5702                 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
5703                         if (mini_should_insert_breakpoint (cfg->method)) {
5704                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
5705                         } else {
5706                                 MONO_INST_NEW (cfg, ins, OP_NOP);
5707                                 MONO_ADD_INS (cfg->cbb, ins);
5708                         }
5709                         return ins;
5710                 }
5711         } else if (cmethod->klass->image == mono_defaults.corlib &&
5712                    (strcmp (cmethod->klass->name_space, "System") == 0) &&
5713                    (strcmp (cmethod->klass->name, "Environment") == 0)) {
5714                 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
5715 #ifdef TARGET_WIN32
5716                         EMIT_NEW_ICONST (cfg, ins, 1);
5717 #else
5718                         EMIT_NEW_ICONST (cfg, ins, 0);
5719 #endif
5720                 }
5721         } else if (cmethod->klass->image == mono_defaults.corlib &&
5722                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
5723                            (strcmp (cmethod->klass->name, "Assembly") == 0)) {
5724                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
5725                         /* No stack walks are currently available, so implement this as an intrinsic */
5726                         MonoInst *assembly_ins;
5727
5728                         EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
5729                         ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
5730                         return ins;
5731                 }
5732         } else if (cmethod->klass->image == mono_defaults.corlib &&
5733                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
5734                            (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
5735                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
5736                         /* No stack walks are currently available, so implement this as an intrinsic */
5737                         MonoInst *method_ins;
5738                         MonoMethod *declaring = cfg->method;
5739
5740                         /* This returns the declaring generic method */
5741                         if (declaring->is_inflated)
5742                                 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
5743                         EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
5744                         ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
5745                         cfg->no_inline = TRUE;
5746                         if (cfg->method != cfg->current_method)
5747                                 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
5748                         return ins;
5749                 }
5750         } else if (cmethod->klass == mono_defaults.math_class) {
5751                 /* 
5752                  * There is general branchless code for Min/Max, but it does not work for 
5753                  * all inputs:
5754                  * http://everything2.com/?node_id=1051618
5755                  */
5756         } else if (cmethod->klass == mono_defaults.systemtype_class && !strcmp (cmethod->name, "op_Equality")) {
5757                 EMIT_NEW_BIALU (cfg, ins, OP_COMPARE, -1, args [0]->dreg, args [1]->dreg);
5758                 MONO_INST_NEW (cfg, ins, OP_PCEQ);
5759                 ins->dreg = alloc_preg (cfg);
5760                 ins->type = STACK_I4;
5761                 MONO_ADD_INS (cfg->cbb, ins);
5762                 return ins;
5763         } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
5764                     !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
5765                                 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
5766                                 !strcmp (cmethod->klass->name, "Selector")) ||
5767                            ((!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") ||
5768                                  !strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.Mac")) &&
5769                                 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
5770                                 !strcmp (cmethod->klass->name, "Selector"))
5771                            ) {
5772                 if ((cfg->backend->have_objc_get_selector || cfg->compile_llvm) &&
5773                         !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
5774                     (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
5775                     cfg->compile_aot) {
5776                         MonoInst *pi;
5777                         MonoJumpInfoToken *ji;
5778                         char *s;
5779
5780                         if (args [0]->opcode == OP_GOT_ENTRY) {
5781                                 pi = (MonoInst *)args [0]->inst_p1;
5782                                 g_assert (pi->opcode == OP_PATCH_INFO);
5783                                 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
5784                                 ji = (MonoJumpInfoToken *)pi->inst_p0;
5785                         } else {
5786                                 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
5787                                 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
5788                         }
5789
5790                         NULLIFY_INS (args [0]);
5791
5792                         s = mono_ldstr_utf8 (ji->image, mono_metadata_token_index (ji->token), &cfg->error);
5793                         return_val_if_nok (&cfg->error, NULL);
5794
5795                         MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
5796                         ins->dreg = mono_alloc_ireg (cfg);
5797                         // FIXME: Leaks
5798                         ins->inst_p0 = s;
5799                         MONO_ADD_INS (cfg->cbb, ins);
5800                         return ins;
5801                 }
5802         } else if (cmethod->klass->image == mono_defaults.corlib &&
5803                         (strcmp (cmethod->klass->name_space, "System.Runtime.InteropServices") == 0) &&
5804                         (strcmp (cmethod->klass->name, "Marshal") == 0)) {
5805                 //Convert Marshal.PtrToStructure<T> of blittable T to direct loads
5806                 if (strcmp (cmethod->name, "PtrToStructure") == 0 &&
5807                                 cmethod->is_inflated &&
5808                                 fsig->param_count == 1 &&
5809                                 !mini_method_check_context_used (cfg, cmethod)) {
5810
5811                         MonoGenericContext *method_context = mono_method_get_context (cmethod);
5812                         MonoType *arg0 = method_context->method_inst->type_argv [0];
5813                         if (mono_type_is_native_blittable (arg0))
5814                                 return mini_emit_memory_load (cfg, arg0, args [0], 0, 0);
5815                 }
5816         }
5817
5818 #ifdef MONO_ARCH_SIMD_INTRINSICS
5819         if (cfg->opt & MONO_OPT_SIMD) {
5820                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5821                 if (ins)
5822                         return ins;
5823         }
5824 #endif
5825
5826         ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5827         if (ins)
5828                 return ins;
5829
5830         if (COMPILE_LLVM (cfg)) {
5831                 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
5832                 if (ins)
5833                         return ins;
5834         }
5835
5836         return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
5837 }
5838
5839 /*
5840  * This entry point could be used later for arbitrary method
5841  * redirection.
5842  */
5843 inline static MonoInst*
5844 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,  
5845                                         MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
5846 {
5847         if (method->klass == mono_defaults.string_class) {
5848                 /* managed string allocation support */
5849                 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(cfg->opt & MONO_OPT_SHARED)) {
5850                         MonoInst *iargs [2];
5851                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
5852                         MonoMethod *managed_alloc = NULL;
5853
5854                         g_assert (vtable); /*Should not fail since it System.String*/
5855 #ifndef MONO_CROSS_COMPILE
5856                         managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
5857 #endif
5858                         if (!managed_alloc)
5859                                 return NULL;
5860                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
5861                         iargs [1] = args [0];
5862                         return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
5863                 }
5864         }
5865         return NULL;
5866 }
5867
5868 static void
5869 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
5870 {
5871         MonoInst *store, *temp;
5872         int i;
5873
5874         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5875                 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
5876
5877                 /*
5878                  * FIXME: We should use *args++ = sp [0], but that would mean the arg
5879                  * would be different than the MonoInst's used to represent arguments, and
5880                  * the ldelema implementation can't deal with that.
5881                  * Solution: When ldelema is used on an inline argument, create a var for 
5882                  * it, emit ldelema on that var, and emit the saving code below in
5883                  * inline_method () if needed.
5884                  */
5885                 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
5886                 cfg->args [i] = temp;
5887                 /* This uses cfg->args [i] which is set by the preceeding line */
5888                 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
5889                 store->cil_code = sp [0]->cil_code;
5890                 sp++;
5891         }
5892 }
5893
5894 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
5895 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
5896
5897 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
5898 static gboolean
5899 check_inline_called_method_name_limit (MonoMethod *called_method)
5900 {
5901         int strncmp_result;
5902         static const char *limit = NULL;
5903         
5904         if (limit == NULL) {
5905                 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
5906
5907                 if (limit_string != NULL)
5908                         limit = limit_string;
5909                 else
5910                         limit = "";
5911         }
5912
5913         if (limit [0] != '\0') {
5914                 char *called_method_name = mono_method_full_name (called_method, TRUE);
5915
5916                 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
5917                 g_free (called_method_name);
5918         
5919                 //return (strncmp_result <= 0);
5920                 return (strncmp_result == 0);
5921         } else {
5922                 return TRUE;
5923         }
5924 }
5925 #endif
5926
5927 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
5928 static gboolean
5929 check_inline_caller_method_name_limit (MonoMethod *caller_method)
5930 {
5931         int strncmp_result;
5932         static const char *limit = NULL;
5933         
5934         if (limit == NULL) {
5935                 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
5936                 if (limit_string != NULL) {
5937                         limit = limit_string;
5938                 } else {
5939                         limit = "";
5940                 }
5941         }
5942
5943         if (limit [0] != '\0') {
5944                 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
5945
5946                 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
5947                 g_free (caller_method_name);
5948         
5949                 //return (strncmp_result <= 0);
5950                 return (strncmp_result == 0);
5951         } else {
5952                 return TRUE;
5953         }
5954 }
5955 #endif
5956
5957 static void
5958 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
5959 {
5960         static double r8_0 = 0.0;
5961         static float r4_0 = 0.0;
5962         MonoInst *ins;
5963         int t;
5964
5965         rtype = mini_get_underlying_type (rtype);
5966         t = rtype->type;
5967
5968         if (rtype->byref) {
5969                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
5970         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5971                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
5972         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5973                 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
5974         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
5975                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
5976                 ins->type = STACK_R4;
5977                 ins->inst_p0 = (void*)&r4_0;
5978                 ins->dreg = dreg;
5979                 MONO_ADD_INS (cfg->cbb, ins);
5980         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5981                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5982                 ins->type = STACK_R8;
5983                 ins->inst_p0 = (void*)&r8_0;
5984                 ins->dreg = dreg;
5985                 MONO_ADD_INS (cfg->cbb, ins);
5986         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
5987                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
5988                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
5989         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
5990                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
5991         } else {
5992                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
5993         }
5994 }
5995
5996 static void
5997 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
5998 {
5999         int t;
6000
6001         rtype = mini_get_underlying_type (rtype);
6002         t = rtype->type;
6003
6004         if (rtype->byref) {
6005                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6006         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6007                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
6008         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6009                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
6010         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6011                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
6012         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6013                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
6014         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6015                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6016                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6017         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6018                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6019         } else {
6020                 emit_init_rvar (cfg, dreg, rtype);
6021         }
6022 }
6023
6024 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
6025 static void
6026 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
6027 {
6028         MonoInst *var = cfg->locals [local];
6029         if (COMPILE_SOFT_FLOAT (cfg)) {
6030                 MonoInst *store;
6031                 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
6032                 emit_init_rvar (cfg, reg, type);
6033                 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
6034         } else {
6035                 if (init)
6036                         emit_init_rvar (cfg, var->dreg, type);
6037                 else
6038                         emit_dummy_init_rvar (cfg, var->dreg, type);
6039         }
6040 }
6041
6042 int
6043 mini_inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, guchar *ip, guint real_offset, gboolean inline_always)
6044 {
6045         return inline_method (cfg, cmethod, fsig, sp, ip, real_offset, inline_always);
6046 }
6047
6048 /*
6049  * inline_method:
6050  *
6051  * Return the cost of inlining CMETHOD, or zero if it should not be inlined.
6052  */
6053 static int
6054 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
6055                guchar *ip, guint real_offset, gboolean inline_always)
6056 {
6057         MonoError error;
6058         MonoInst *ins, *rvar = NULL;
6059         MonoMethodHeader *cheader;
6060         MonoBasicBlock *ebblock, *sbblock;
6061         int i, costs;
6062         MonoMethod *prev_inlined_method;
6063         MonoInst **prev_locals, **prev_args;
6064         MonoType **prev_arg_types;
6065         guint prev_real_offset;
6066         GHashTable *prev_cbb_hash;
6067         MonoBasicBlock **prev_cil_offset_to_bb;
6068         MonoBasicBlock *prev_cbb;
6069         const unsigned char *prev_ip;
6070         unsigned char *prev_cil_start;
6071         guint32 prev_cil_offset_to_bb_len;
6072         MonoMethod *prev_current_method;
6073         MonoGenericContext *prev_generic_context;
6074         gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
6075
6076         g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
6077
6078 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6079         if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
6080                 return 0;
6081 #endif
6082 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6083         if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
6084                 return 0;
6085 #endif
6086
6087         if (!fsig)
6088                 fsig = mono_method_signature (cmethod);
6089
6090         if (cfg->verbose_level > 2)
6091                 printf ("INLINE START %p %s -> %s\n", cmethod,  mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6092
6093         if (!cmethod->inline_info) {
6094                 cfg->stat_inlineable_methods++;
6095                 cmethod->inline_info = 1;
6096         }
6097
6098         /* allocate local variables */
6099         cheader = mono_method_get_header_checked (cmethod, &error);
6100         if (!cheader) {
6101                 if (inline_always) {
6102                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
6103                         mono_error_move (&cfg->error, &error);
6104                 } else {
6105                         mono_error_cleanup (&error);
6106                 }
6107                 return 0;
6108         }
6109
6110         /*Must verify before creating locals as it can cause the JIT to assert.*/
6111         if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
6112                 mono_metadata_free_mh (cheader);
6113                 return 0;
6114         }
6115
6116         /* allocate space to store the return value */
6117         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6118                 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
6119         }
6120
6121         prev_locals = cfg->locals;
6122         cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
6123         for (i = 0; i < cheader->num_locals; ++i)
6124                 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
6125
6126         /* allocate start and end blocks */
6127         /* This is needed so if the inline is aborted, we can clean up */
6128         NEW_BBLOCK (cfg, sbblock);
6129         sbblock->real_offset = real_offset;
6130
6131         NEW_BBLOCK (cfg, ebblock);
6132         ebblock->block_num = cfg->num_bblocks++;
6133         ebblock->real_offset = real_offset;
6134
6135         prev_args = cfg->args;
6136         prev_arg_types = cfg->arg_types;
6137         prev_inlined_method = cfg->inlined_method;
6138         prev_ret_var_set = cfg->ret_var_set;
6139         prev_real_offset = cfg->real_offset;
6140         prev_cbb_hash = cfg->cbb_hash;
6141         prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
6142         prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
6143         prev_cil_start = cfg->cil_start;
6144         prev_ip = cfg->ip;
6145         prev_cbb = cfg->cbb;
6146         prev_current_method = cfg->current_method;
6147         prev_generic_context = cfg->generic_context;
6148         prev_disable_inline = cfg->disable_inline;
6149
6150         cfg->inlined_method = cmethod;
6151         cfg->ret_var_set = FALSE;
6152         cfg->inline_depth ++;
6153
6154         if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
6155                 virtual_ = TRUE;
6156
6157         costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
6158
6159         ret_var_set = cfg->ret_var_set;
6160
6161         cfg->inlined_method = prev_inlined_method;
6162         cfg->real_offset = prev_real_offset;
6163         cfg->cbb_hash = prev_cbb_hash;
6164         cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
6165         cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
6166         cfg->cil_start = prev_cil_start;
6167         cfg->ip = prev_ip;
6168         cfg->locals = prev_locals;
6169         cfg->args = prev_args;
6170         cfg->arg_types = prev_arg_types;
6171         cfg->current_method = prev_current_method;
6172         cfg->generic_context = prev_generic_context;
6173         cfg->ret_var_set = prev_ret_var_set;
6174         cfg->disable_inline = prev_disable_inline;
6175         cfg->inline_depth --;
6176
6177         if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
6178                 if (cfg->verbose_level > 2)
6179                         printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6180
6181                 cfg->stat_inlined_methods++;
6182
6183                 /* always add some code to avoid block split failures */
6184                 MONO_INST_NEW (cfg, ins, OP_NOP);
6185                 MONO_ADD_INS (prev_cbb, ins);
6186
6187                 prev_cbb->next_bb = sbblock;
6188                 link_bblock (cfg, prev_cbb, sbblock);
6189
6190                 /* 
6191                  * Get rid of the begin and end bblocks if possible to aid local
6192                  * optimizations.
6193                  */
6194                 if (prev_cbb->out_count == 1)
6195                         mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
6196
6197                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
6198                         mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
6199
6200                 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
6201                         MonoBasicBlock *prev = ebblock->in_bb [0];
6202
6203                         if (prev->next_bb == ebblock) {
6204                                 mono_merge_basic_blocks (cfg, prev, ebblock);
6205                                 cfg->cbb = prev;
6206                                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
6207                                         mono_merge_basic_blocks (cfg, prev_cbb, prev);
6208                                         cfg->cbb = prev_cbb;
6209                                 }
6210                         } else {
6211                                 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
6212                                 cfg->cbb = ebblock;
6213                         }
6214                 } else {
6215                         /* 
6216                          * Its possible that the rvar is set in some prev bblock, but not in others.
6217                          * (#1835).
6218                          */
6219                         if (rvar) {
6220                                 MonoBasicBlock *bb;
6221
6222                                 for (i = 0; i < ebblock->in_count; ++i) {
6223                                         bb = ebblock->in_bb [i];
6224
6225                                         if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
6226                                                 cfg->cbb = bb;
6227
6228                                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6229                                         }
6230                                 }
6231                         }
6232
6233                         cfg->cbb = ebblock;
6234                 }
6235
6236                 if (rvar) {
6237                         /*
6238                          * If the inlined method contains only a throw, then the ret var is not 
6239                          * set, so set it to a dummy value.
6240                          */
6241                         if (!ret_var_set)
6242                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6243
6244                         EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
6245                         *sp++ = ins;
6246                 }
6247                 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6248                 return costs + 1;
6249         } else {
6250                 if (cfg->verbose_level > 2)
6251                         printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
6252                 cfg->exception_type = MONO_EXCEPTION_NONE;
6253
6254                 /* This gets rid of the newly added bblocks */
6255                 cfg->cbb = prev_cbb;
6256         }
6257         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6258         return 0;
6259 }
6260
6261 /*
6262  * Some of these comments may well be out-of-date.
6263  * Design decisions: we do a single pass over the IL code (and we do bblock 
6264  * splitting/merging in the few cases when it's required: a back jump to an IL
6265  * address that was not already seen as bblock starting point).
6266  * Code is validated as we go (full verification is still better left to metadata/verify.c).
6267  * Complex operations are decomposed in simpler ones right away. We need to let the 
6268  * arch-specific code peek and poke inside this process somehow (except when the 
6269  * optimizations can take advantage of the full semantic info of coarse opcodes).
6270  * All the opcodes of the form opcode.s are 'normalized' to opcode.
6271  * MonoInst->opcode initially is the IL opcode or some simplification of that 
6272  * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific 
6273  * opcode with value bigger than OP_LAST.
6274  * At this point the IR can be handed over to an interpreter, a dumb code generator
6275  * or to the optimizing code generator that will translate it to SSA form.
6276  *
6277  * Profiling directed optimizations.
6278  * We may compile by default with few or no optimizations and instrument the code
6279  * or the user may indicate what methods to optimize the most either in a config file
6280  * or through repeated runs where the compiler applies offline the optimizations to 
6281  * each method and then decides if it was worth it.
6282  */
6283
6284 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
6285 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
6286 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
6287 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
6288 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
6289 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
6290 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
6291 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
6292
6293 /* offset from br.s -> br like opcodes */
6294 #define BIG_BRANCH_OFFSET 13
6295
6296 static gboolean
6297 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
6298 {
6299         MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
6300
6301         return b == NULL || b == bb;
6302 }
6303
6304 static int
6305 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
6306 {
6307         unsigned char *ip = start;
6308         unsigned char *target;
6309         int i;
6310         guint cli_addr;
6311         MonoBasicBlock *bblock;
6312         const MonoOpcode *opcode;
6313
6314         while (ip < end) {
6315                 cli_addr = ip - start;
6316                 i = mono_opcode_value ((const guint8 **)&ip, end);
6317                 if (i < 0)
6318                         UNVERIFIED;
6319                 opcode = &mono_opcodes [i];
6320                 switch (opcode->argument) {
6321                 case MonoInlineNone:
6322                         ip++; 
6323                         break;
6324                 case MonoInlineString:
6325                 case MonoInlineType:
6326                 case MonoInlineField:
6327                 case MonoInlineMethod:
6328                 case MonoInlineTok:
6329                 case MonoInlineSig:
6330                 case MonoShortInlineR:
6331                 case MonoInlineI:
6332                         ip += 5;
6333                         break;
6334                 case MonoInlineVar:
6335                         ip += 3;
6336                         break;
6337                 case MonoShortInlineVar:
6338                 case MonoShortInlineI:
6339                         ip += 2;
6340                         break;
6341                 case MonoShortInlineBrTarget:
6342                         target = start + cli_addr + 2 + (signed char)ip [1];
6343                         GET_BBLOCK (cfg, bblock, target);
6344                         ip += 2;
6345                         if (ip < end)
6346                                 GET_BBLOCK (cfg, bblock, ip);
6347                         break;
6348                 case MonoInlineBrTarget:
6349                         target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
6350                         GET_BBLOCK (cfg, bblock, target);
6351                         ip += 5;
6352                         if (ip < end)
6353                                 GET_BBLOCK (cfg, bblock, ip);
6354                         break;
6355                 case MonoInlineSwitch: {
6356                         guint32 n = read32 (ip + 1);
6357                         guint32 j;
6358                         ip += 5;
6359                         cli_addr += 5 + 4 * n;
6360                         target = start + cli_addr;
6361                         GET_BBLOCK (cfg, bblock, target);
6362                         
6363                         for (j = 0; j < n; ++j) {
6364                                 target = start + cli_addr + (gint32)read32 (ip);
6365                                 GET_BBLOCK (cfg, bblock, target);
6366                                 ip += 4;
6367                         }
6368                         break;
6369                 }
6370                 case MonoInlineR:
6371                 case MonoInlineI8:
6372                         ip += 9;
6373                         break;
6374                 default:
6375                         g_assert_not_reached ();
6376                 }
6377
6378                 if (i == CEE_THROW) {
6379                         unsigned char *bb_start = ip - 1;
6380                         
6381                         /* Find the start of the bblock containing the throw */
6382                         bblock = NULL;
6383                         while ((bb_start >= start) && !bblock) {
6384                                 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
6385                                 bb_start --;
6386                         }
6387                         if (bblock)
6388                                 bblock->out_of_line = 1;
6389                 }
6390         }
6391         return 0;
6392 unverified:
6393 exception_exit:
6394         *pos = ip;
6395         return 1;
6396 }
6397
6398 static inline MonoMethod *
6399 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
6400 {
6401         MonoMethod *method;
6402
6403         error_init (error);
6404
6405         if (m->wrapper_type != MONO_WRAPPER_NONE) {
6406                 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
6407                 if (context) {
6408                         method = mono_class_inflate_generic_method_checked (method, context, error);
6409                 }
6410         } else {
6411                 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
6412         }
6413
6414         return method;
6415 }
6416
6417 static inline MonoMethod *
6418 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
6419 {
6420         MonoError error;
6421         MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
6422
6423         if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
6424                 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
6425                 method = NULL;
6426         }
6427
6428         if (!method && !cfg)
6429                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6430
6431         return method;
6432 }
6433
6434 static inline MonoMethodSignature*
6435 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context, MonoError *error)
6436 {
6437         MonoMethodSignature *fsig;
6438
6439         error_init (error);
6440         if (method->wrapper_type != MONO_WRAPPER_NONE) {
6441                 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6442         } else {
6443                 fsig = mono_metadata_parse_signature_checked (method->klass->image, token, error);
6444                 return_val_if_nok (error, NULL);
6445         }
6446         if (context) {
6447                 fsig = mono_inflate_generic_signature(fsig, context, error);
6448         }
6449         return fsig;
6450 }
6451
6452 static MonoMethod*
6453 throw_exception (void)
6454 {
6455         static MonoMethod *method = NULL;
6456
6457         if (!method) {
6458                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
6459                 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
6460         }
6461         g_assert (method);
6462         return method;
6463 }
6464
6465 static void
6466 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
6467 {
6468         MonoMethod *thrower = throw_exception ();
6469         MonoInst *args [1];
6470
6471         EMIT_NEW_PCONST (cfg, args [0], ex);
6472         mono_emit_method_call (cfg, thrower, args, NULL);
6473 }
6474
6475 /*
6476  * Return the original method is a wrapper is specified. We can only access 
6477  * the custom attributes from the original method.
6478  */
6479 static MonoMethod*
6480 get_original_method (MonoMethod *method)
6481 {
6482         if (method->wrapper_type == MONO_WRAPPER_NONE)
6483                 return method;
6484
6485         /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
6486         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
6487                 return NULL;
6488
6489         /* in other cases we need to find the original method */
6490         return mono_marshal_method_from_wrapper (method);
6491 }
6492
6493 static void
6494 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
6495 {
6496         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6497         MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
6498         if (ex)
6499                 emit_throw_exception (cfg, ex);
6500 }
6501
6502 static void
6503 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
6504 {
6505         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6506         MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
6507         if (ex)
6508                 emit_throw_exception (cfg, ex);
6509 }
6510
6511 /*
6512  * Check that the IL instructions at ip are the array initialization
6513  * sequence and return the pointer to the data and the size.
6514  */
6515 static const char*
6516 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
6517 {
6518         /*
6519          * newarr[System.Int32]
6520          * dup
6521          * ldtoken field valuetype ...
6522          * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
6523          */
6524         if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
6525                 MonoError error;
6526                 guint32 token = read32 (ip + 7);
6527                 guint32 field_token = read32 (ip + 2);
6528                 guint32 field_index = field_token & 0xffffff;
6529                 guint32 rva;
6530                 const char *data_ptr;
6531                 int size = 0;
6532                 MonoMethod *cmethod;
6533                 MonoClass *dummy_class;
6534                 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
6535                 int dummy_align;
6536
6537                 if (!field) {
6538                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
6539                         return NULL;
6540                 }
6541
6542                 *out_field_token = field_token;
6543
6544                 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
6545                 if (!cmethod)
6546                         return NULL;
6547                 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
6548                         return NULL;
6549                 switch (mini_get_underlying_type (&klass->byval_arg)->type) {
6550                 case MONO_TYPE_I1:
6551                 case MONO_TYPE_U1:
6552                         size = 1; break;
6553                 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
6554 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
6555                 case MONO_TYPE_I2:
6556                 case MONO_TYPE_U2:
6557                         size = 2; break;
6558                 case MONO_TYPE_I4:
6559                 case MONO_TYPE_U4:
6560                 case MONO_TYPE_R4:
6561                         size = 4; break;
6562                 case MONO_TYPE_R8:
6563                 case MONO_TYPE_I8:
6564                 case MONO_TYPE_U8:
6565                         size = 8; break;
6566 #endif
6567                 default:
6568                         return NULL;
6569                 }
6570                 size *= len;
6571                 if (size > mono_type_size (field->type, &dummy_align))
6572                     return NULL;
6573                 *out_size = size;
6574                 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
6575                 if (!image_is_dynamic (method->klass->image)) {
6576                         field_index = read32 (ip + 2) & 0xffffff;
6577                         mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
6578                         data_ptr = mono_image_rva_map (method->klass->image, rva);
6579                         /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
6580                         /* for aot code we do the lookup on load */
6581                         if (aot && data_ptr)
6582                                 return (const char *)GUINT_TO_POINTER (rva);
6583                 } else {
6584                         /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */ 
6585                         g_assert (!aot);
6586                         data_ptr = mono_field_get_data (field);
6587                 }
6588                 return data_ptr;
6589         }
6590         return NULL;
6591 }
6592
6593 static void
6594 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
6595 {
6596         MonoError error;
6597         char *method_fname = mono_method_full_name (method, TRUE);
6598         char *method_code;
6599         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
6600
6601         if (!header) {
6602                 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
6603                 mono_error_cleanup (&error);
6604         } else if (header->code_size == 0)
6605                 method_code = g_strdup ("method body is empty.");
6606         else
6607                 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
6608         mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
6609         g_free (method_fname);
6610         g_free (method_code);
6611         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
6612 }
6613
6614 static guint32
6615 mono_type_to_stloc_coerce (MonoType *type)
6616 {
6617         if (type->byref)
6618                 return 0;
6619
6620         type = mini_get_underlying_type (type);
6621 handle_enum:
6622         switch (type->type) {
6623         case MONO_TYPE_I1:
6624                 return OP_ICONV_TO_I1;
6625         case MONO_TYPE_U1:
6626                 return OP_ICONV_TO_U1;
6627         case MONO_TYPE_I2:
6628                 return OP_ICONV_TO_I2;
6629         case MONO_TYPE_U2:
6630                 return OP_ICONV_TO_U2;
6631         case MONO_TYPE_I4:
6632         case MONO_TYPE_U4:
6633         case MONO_TYPE_I:
6634         case MONO_TYPE_U:
6635         case MONO_TYPE_PTR:
6636         case MONO_TYPE_FNPTR:
6637         case MONO_TYPE_CLASS:
6638         case MONO_TYPE_STRING:
6639         case MONO_TYPE_OBJECT:
6640         case MONO_TYPE_SZARRAY:
6641         case MONO_TYPE_ARRAY:
6642         case MONO_TYPE_I8:
6643         case MONO_TYPE_U8:
6644         case MONO_TYPE_R4:
6645         case MONO_TYPE_R8:
6646         case MONO_TYPE_TYPEDBYREF:
6647         case MONO_TYPE_GENERICINST:
6648                 return 0;
6649         case MONO_TYPE_VALUETYPE:
6650                 if (type->data.klass->enumtype) {
6651                         type = mono_class_enum_basetype (type->data.klass);
6652                         goto handle_enum;
6653                 }
6654                 return 0;
6655         case MONO_TYPE_VAR:
6656         case MONO_TYPE_MVAR: //TODO I believe we don't need to handle gsharedvt as there won't be match and, for example, u1 is not covariant to u32
6657                 return 0;
6658         default:
6659                 g_error ("unknown type 0x%02x in mono_type_to_stloc_coerce", type->type);
6660         }
6661         return -1;
6662 }
6663
6664 static void
6665 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
6666 {
6667         MonoInst *ins;
6668         guint32 coerce_op = mono_type_to_stloc_coerce (header->locals [n]);
6669
6670         if (coerce_op) {
6671                 if (cfg->cbb->last_ins == sp [0] && sp [0]->opcode == coerce_op) {
6672                         if (cfg->verbose_level > 2)
6673                                 printf ("Found existing coercing is enough for stloc\n");
6674                 } else {
6675                         MONO_INST_NEW (cfg, ins, coerce_op);
6676                         ins->dreg = alloc_ireg (cfg);
6677                         ins->sreg1 = sp [0]->dreg;
6678                         ins->type = STACK_I4;
6679                         ins->klass = mono_class_from_mono_type (header->locals [n]);
6680                         MONO_ADD_INS (cfg->cbb, ins);
6681                         *sp = mono_decompose_opcode (cfg, ins);
6682                 }
6683         }
6684
6685
6686         guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
6687         if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0]  &&
6688                         ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
6689                 /* Optimize reg-reg moves away */
6690                 /* 
6691                  * Can't optimize other opcodes, since sp[0] might point to
6692                  * the last ins of a decomposed opcode.
6693                  */
6694                 sp [0]->dreg = (cfg)->locals [n]->dreg;
6695         } else {
6696                 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
6697         }
6698 }
6699
6700 static void
6701 emit_starg_ir (MonoCompile *cfg, MonoInst **sp, int n)
6702 {
6703         MonoInst *ins;
6704         guint32 coerce_op = mono_type_to_stloc_coerce (cfg->arg_types [n]);
6705
6706         if (coerce_op) {
6707                 if (cfg->cbb->last_ins == sp [0] && sp [0]->opcode == coerce_op) {
6708                         if (cfg->verbose_level > 2)
6709                                 printf ("Found existing coercing is enough for starg\n");
6710                 } else {
6711                         MONO_INST_NEW (cfg, ins, coerce_op);
6712                         ins->dreg = alloc_ireg (cfg);
6713                         ins->sreg1 = sp [0]->dreg;
6714                         ins->type = STACK_I4;
6715                         ins->klass = mono_class_from_mono_type (cfg->arg_types [n]);
6716                         MONO_ADD_INS (cfg->cbb, ins);
6717                         *sp = mono_decompose_opcode (cfg, ins);
6718                 }
6719         }
6720
6721         EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
6722 }
6723
6724 /*
6725  * ldloca inhibits many optimizations so try to get rid of it in common
6726  * cases.
6727  */
6728 static inline unsigned char *
6729 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
6730 {
6731         int local, token;
6732         MonoClass *klass;
6733         MonoType *type;
6734
6735         if (size == 1) {
6736                 local = ip [1];
6737                 ip += 2;
6738         } else {
6739                 local = read16 (ip + 2);
6740                 ip += 4;
6741         }
6742         
6743         if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
6744                 /* From the INITOBJ case */
6745                 token = read32 (ip + 2);
6746                 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
6747                 CHECK_TYPELOAD (klass);
6748                 type = mini_get_underlying_type (&klass->byval_arg);
6749                 emit_init_local (cfg, local, type, TRUE);
6750                 return ip + 6;
6751         }
6752  exception_exit:
6753         return NULL;
6754 }
6755
6756 static MonoInst*
6757 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
6758 {
6759         MonoInst *icall_args [16];
6760         MonoInst *call_target, *ins, *vtable_ins;
6761         int arg_reg, this_reg, vtable_reg;
6762         gboolean is_iface = mono_class_is_interface (cmethod->klass);
6763         gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
6764         gboolean variant_iface = FALSE;
6765         guint32 slot;
6766         int offset;
6767         gboolean special_array_interface = cmethod->klass->is_array_special_interface;
6768
6769         /*
6770          * In llvm-only mode, vtables contain function descriptors instead of
6771          * method addresses/trampolines.
6772          */
6773         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
6774
6775         if (is_iface)
6776                 slot = mono_method_get_imt_slot (cmethod);
6777         else
6778                 slot = mono_method_get_vtable_index (cmethod);
6779
6780         this_reg = sp [0]->dreg;
6781
6782         if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
6783                 variant_iface = TRUE;
6784
6785         if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
6786                 /*
6787                  * The simplest case, a normal virtual call.
6788                  */
6789                 int slot_reg = alloc_preg (cfg);
6790                 int addr_reg = alloc_preg (cfg);
6791                 int arg_reg = alloc_preg (cfg);
6792                 MonoBasicBlock *non_null_bb;
6793
6794                 vtable_reg = alloc_preg (cfg);
6795                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6796                 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
6797
6798                 /* Load the vtable slot, which contains a function descriptor. */
6799                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
6800
6801                 NEW_BBLOCK (cfg, non_null_bb);
6802
6803                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
6804                 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
6805                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
6806
6807                 /* Slow path */
6808                 // FIXME: Make the wrapper use the preserveall cconv
6809                 // FIXME: Use one icall per slot for small slot numbers ?
6810                 icall_args [0] = vtable_ins;
6811                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
6812                 /* Make the icall return the vtable slot value to save some code space */
6813                 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
6814                 ins->dreg = slot_reg;
6815                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
6816
6817                 /* Fastpath */
6818                 MONO_START_BB (cfg, non_null_bb);
6819                 /* Load the address + arg from the vtable slot */
6820                 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
6821                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
6822
6823                 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
6824         }
6825
6826         if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt && !special_array_interface) {
6827                 /*
6828                  * A simple interface call
6829                  *
6830                  * We make a call through an imt slot to obtain the function descriptor we need to call.
6831                  * The imt slot contains a function descriptor for a runtime function + arg.
6832                  */
6833                 int slot_reg = alloc_preg (cfg);
6834                 int addr_reg = alloc_preg (cfg);
6835                 int arg_reg = alloc_preg (cfg);
6836                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
6837
6838                 vtable_reg = alloc_preg (cfg);
6839                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6840                 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
6841
6842                 /*
6843                  * The slot is already initialized when the vtable is created so there is no need
6844                  * to check it here.
6845                  */
6846
6847                 /* Load the imt slot, which contains a function descriptor. */
6848                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
6849
6850                 /* Load the address + arg of the imt thunk from the imt slot */
6851                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
6852                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
6853                 /*
6854                  * IMT thunks in llvm-only mode are C functions which take an info argument
6855                  * plus the imt method and return the ftndesc to call.
6856                  */
6857                 icall_args [0] = thunk_arg_ins;
6858                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
6859                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
6860                 ftndesc_ins = mini_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
6861
6862                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
6863         }
6864
6865         if ((fsig->generic_param_count || variant_iface || special_array_interface) && !is_gsharedvt) {
6866                 /*
6867                  * This is similar to the interface case, the vtable slot points to an imt thunk which is
6868                  * dynamically extended as more instantiations are discovered.
6869                  * This handles generic virtual methods both on classes and interfaces.
6870                  */
6871                 int slot_reg = alloc_preg (cfg);
6872                 int addr_reg = alloc_preg (cfg);
6873                 int arg_reg = alloc_preg (cfg);
6874                 int ftndesc_reg = alloc_preg (cfg);
6875                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
6876                 MonoBasicBlock *slowpath_bb, *end_bb;
6877
6878                 NEW_BBLOCK (cfg, slowpath_bb);
6879                 NEW_BBLOCK (cfg, end_bb);
6880
6881                 vtable_reg = alloc_preg (cfg);
6882                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6883                 if (is_iface)
6884                         offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
6885                 else
6886                         offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
6887
6888                 /* Load the slot, which contains a function descriptor. */
6889                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
6890
6891                 /* These slots are not initialized, so fall back to the slow path until they are initialized */
6892                 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
6893                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
6894                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
6895
6896                 /* Fastpath */
6897                 /* Same as with iface calls */
6898                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
6899                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
6900                 icall_args [0] = thunk_arg_ins;
6901                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
6902                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
6903                 ftndesc_ins = mini_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
6904                 ftndesc_ins->dreg = ftndesc_reg;
6905                 /*
6906                  * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
6907                  * they don't know about yet. Fall back to the slowpath in that case.
6908                  */
6909                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
6910                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
6911
6912                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6913
6914                 /* Slowpath */
6915                 MONO_START_BB (cfg, slowpath_bb);
6916                 icall_args [0] = vtable_ins;
6917                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
6918                 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
6919                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
6920                 if (is_iface)
6921                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
6922                 else
6923                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
6924                 ftndesc_ins->dreg = ftndesc_reg;
6925                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6926
6927                 /* Common case */
6928                 MONO_START_BB (cfg, end_bb);
6929                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
6930         }
6931
6932         /*
6933          * Non-optimized cases
6934          */
6935         icall_args [0] = sp [0];
6936         EMIT_NEW_ICONST (cfg, icall_args [1], slot);
6937
6938         icall_args [2] = emit_get_rgctx_method (cfg, context_used,
6939                                                                                         cmethod, MONO_RGCTX_INFO_METHOD);
6940
6941         arg_reg = alloc_preg (cfg);
6942         MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
6943         EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
6944
6945         g_assert (is_gsharedvt);
6946         if (is_iface)
6947                 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
6948         else
6949                 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
6950
6951         /*
6952          * Pass the extra argument even if the callee doesn't receive it, most
6953          * calling conventions allow this.
6954          */
6955         return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
6956 }
6957
6958 static gboolean
6959 is_exception_class (MonoClass *klass)
6960 {
6961         while (klass) {
6962                 if (klass == mono_defaults.exception_class)
6963                         return TRUE;
6964                 klass = klass->parent;
6965         }
6966         return FALSE;
6967 }
6968
6969 /*
6970  * is_jit_optimizer_disabled:
6971  *
6972  *   Determine whenever M's assembly has a DebuggableAttribute with the
6973  * IsJITOptimizerDisabled flag set.
6974  */
6975 static gboolean
6976 is_jit_optimizer_disabled (MonoMethod *m)
6977 {
6978         MonoError error;
6979         MonoAssembly *ass = m->klass->image->assembly;
6980         MonoCustomAttrInfo* attrs;
6981         MonoClass *klass;
6982         int i;
6983         gboolean val = FALSE;
6984
6985         g_assert (ass);
6986         if (ass->jit_optimizer_disabled_inited)
6987                 return ass->jit_optimizer_disabled;
6988
6989         klass = mono_class_try_get_debuggable_attribute_class ();
6990
6991         if (!klass) {
6992                 /* Linked away */
6993                 ass->jit_optimizer_disabled = FALSE;
6994                 mono_memory_barrier ();
6995                 ass->jit_optimizer_disabled_inited = TRUE;
6996                 return FALSE;
6997         }
6998
6999         attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
7000         mono_error_cleanup (&error); /* FIXME don't swallow the error */
7001         if (attrs) {
7002                 for (i = 0; i < attrs->num_attrs; ++i) {
7003                         MonoCustomAttrEntry *attr = &attrs->attrs [i];
7004                         const gchar *p;
7005                         MonoMethodSignature *sig;
7006
7007                         if (!attr->ctor || attr->ctor->klass != klass)
7008                                 continue;
7009                         /* Decode the attribute. See reflection.c */
7010                         p = (const char*)attr->data;
7011                         g_assert (read16 (p) == 0x0001);
7012                         p += 2;
7013
7014                         // FIXME: Support named parameters
7015                         sig = mono_method_signature (attr->ctor);
7016                         if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7017                                 continue;
7018                         /* Two boolean arguments */
7019                         p ++;
7020                         val = *p;
7021                 }
7022                 mono_custom_attrs_free (attrs);
7023         }
7024
7025         ass->jit_optimizer_disabled = val;
7026         mono_memory_barrier ();
7027         ass->jit_optimizer_disabled_inited = TRUE;
7028
7029         return val;
7030 }
7031
7032 static gboolean
7033 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7034 {
7035         gboolean supported_tail_call;
7036         int i;
7037
7038         supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7039
7040         for (i = 0; i < fsig->param_count; ++i) {
7041                 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7042                         /* These can point to the current method's stack */
7043                         supported_tail_call = FALSE;
7044         }
7045         if (fsig->hasthis && cmethod->klass->valuetype)
7046                 /* this might point to the current method's stack */
7047                 supported_tail_call = FALSE;
7048         if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7049                 supported_tail_call = FALSE;
7050         if (cfg->method->save_lmf)
7051                 supported_tail_call = FALSE;
7052         if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7053                 supported_tail_call = FALSE;
7054         if (call_opcode != CEE_CALL)
7055                 supported_tail_call = FALSE;
7056
7057         /* Debugging support */
7058 #if 0
7059         if (supported_tail_call) {
7060                 if (!mono_debug_count ())
7061                         supported_tail_call = FALSE;
7062         }
7063 #endif
7064
7065         return supported_tail_call;
7066 }
7067
7068 /*
7069  * handle_ctor_call:
7070  *
7071  *   Handle calls made to ctors from NEWOBJ opcodes.
7072  */
7073 static void
7074 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7075                                   MonoInst **sp, guint8 *ip, int *inline_costs)
7076 {
7077         MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7078
7079         if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7080                                         mono_method_is_generic_sharable (cmethod, TRUE)) {
7081                 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7082                         mono_class_vtable (cfg->domain, cmethod->klass);
7083                         CHECK_TYPELOAD (cmethod->klass);
7084
7085                         vtable_arg = emit_get_rgctx_method (cfg, context_used,
7086                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7087                 } else {
7088                         if (context_used) {
7089                                 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
7090                                                                                                    cmethod->klass, MONO_RGCTX_INFO_VTABLE);
7091                         } else {
7092                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7093
7094                                 CHECK_TYPELOAD (cmethod->klass);
7095                                 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
7096                         }
7097                 }
7098         }
7099
7100         /* Avoid virtual calls to ctors if possible */
7101         if (mono_class_is_marshalbyref (cmethod->klass))
7102                 callvirt_this_arg = sp [0];
7103
7104         if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
7105                 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
7106                 CHECK_CFG_EXCEPTION;
7107         } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7108                            mono_method_check_inlining (cfg, cmethod) &&
7109                            !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
7110                 int costs;
7111
7112                 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
7113                         cfg->real_offset += 5;
7114
7115                         *inline_costs += costs - 5;
7116                 } else {
7117                         INLINE_FAILURE ("inline failure");
7118                         // FIXME-VT: Clean this up
7119                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
7120                                 GSHAREDVT_FAILURE(*ip);
7121                         mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
7122                 }
7123         } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
7124                 MonoInst *addr;
7125
7126                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
7127
7128                 if (cfg->llvm_only) {
7129                         // FIXME: Avoid initializing vtable_arg
7130                         emit_llvmonly_calli (cfg, fsig, sp, addr);
7131                 } else {
7132                         mini_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
7133                 }
7134         } else if (context_used &&
7135                            ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
7136                                  !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
7137                 MonoInst *cmethod_addr;
7138
7139                 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
7140
7141                 if (cfg->llvm_only) {
7142                         MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
7143                                                                                                         MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7144                         emit_llvmonly_calli (cfg, fsig, sp, addr);
7145                 } else {
7146                         cmethod_addr = emit_get_rgctx_method (cfg, context_used,
7147                                                                                                   cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7148
7149                         mini_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
7150                 }
7151         } else {
7152                 INLINE_FAILURE ("ctor call");
7153                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
7154                                                                                   callvirt_this_arg, NULL, vtable_arg);
7155         }
7156  exception_exit:
7157         return;
7158 }
7159
7160 static void
7161 emit_setret (MonoCompile *cfg, MonoInst *val)
7162 {
7163         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
7164         MonoInst *ins;
7165
7166         if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
7167                 MonoInst *ret_addr;
7168
7169                 if (!cfg->vret_addr) {
7170                         EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
7171                 } else {
7172                         EMIT_NEW_RETLOADA (cfg, ret_addr);
7173
7174                         EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
7175                         ins->klass = mono_class_from_mono_type (ret_type);
7176                 }
7177         } else {
7178 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
7179                 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
7180                         MonoInst *iargs [1];
7181                         MonoInst *conv;
7182
7183                         iargs [0] = val;
7184                         conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
7185                         mono_arch_emit_setret (cfg, cfg->method, conv);
7186                 } else {
7187                         mono_arch_emit_setret (cfg, cfg->method, val);
7188                 }
7189 #else
7190                 mono_arch_emit_setret (cfg, cfg->method, val);
7191 #endif
7192         }
7193 }
7194
7195 /*
7196  * mono_method_to_ir:
7197  *
7198  * Translate the .net IL into linear IR.
7199  *
7200  * @start_bblock: if not NULL, the starting basic block, used during inlining.
7201  * @end_bblock: if not NULL, the ending basic block, used during inlining.
7202  * @return_var: if not NULL, the place where the return value is stored, used during inlining.   
7203  * @inline_args: if not NULL, contains the arguments to the inline call
7204  * @inline_offset: if not zero, the real offset from the inline call, or zero otherwise.
7205  * @is_virtual_call: whether this method is being called as a result of a call to callvirt
7206  *
7207  * This method is used to turn ECMA IL into Mono's internal Linear IR
7208  * reprensetation.  It is used both for entire methods, as well as
7209  * inlining existing methods.  In the former case, the @start_bblock,
7210  * @end_bblock, @return_var, @inline_args are all set to NULL, and the
7211  * inline_offset is set to zero.
7212  * 
7213  * Returns: the inline cost, or -1 if there was an error processing this method.
7214  */
7215 int
7216 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
7217                    MonoInst *return_var, MonoInst **inline_args, 
7218                    guint inline_offset, gboolean is_virtual_call)
7219 {
7220         MonoError error;
7221         MonoInst *ins, **sp, **stack_start;
7222         MonoBasicBlock *tblock = NULL;
7223         MonoBasicBlock *init_localsbb = NULL, *init_localsbb2 = NULL;
7224         MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
7225         MonoMethod *cmethod, *method_definition;
7226         MonoInst **arg_array;
7227         MonoMethodHeader *header;
7228         MonoImage *image;
7229         guint32 token, ins_flag;
7230         MonoClass *klass;
7231         MonoClass *constrained_class = NULL;
7232         unsigned char *ip, *end, *target, *err_pos;
7233         MonoMethodSignature *sig;
7234         MonoGenericContext *generic_context = NULL;
7235         MonoGenericContainer *generic_container = NULL;
7236         MonoType **param_types;
7237         int i, n, start_new_bblock, dreg;
7238         int num_calls = 0, inline_costs = 0;
7239         int breakpoint_id = 0;
7240         guint num_args;
7241         GSList *class_inits = NULL;
7242         gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
7243         int context_used;
7244         gboolean init_locals, seq_points, skip_dead_blocks;
7245         gboolean sym_seq_points = FALSE;
7246         MonoDebugMethodInfo *minfo;
7247         MonoBitSet *seq_point_locs = NULL;
7248         MonoBitSet *seq_point_set_locs = NULL;
7249
7250         cfg->disable_inline = is_jit_optimizer_disabled (method);
7251
7252         /* serialization and xdomain stuff may need access to private fields and methods */
7253         dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
7254         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
7255         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
7256         dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
7257         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
7258         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
7259
7260         /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
7261         dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
7262         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
7263         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
7264         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
7265
7266         image = method->klass->image;
7267         header = mono_method_get_header_checked (method, &cfg->error);
7268         if (!header) {
7269                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7270                 goto exception_exit;
7271         } else {
7272                 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7273         }
7274
7275         generic_container = mono_method_get_generic_container (method);
7276         sig = mono_method_signature (method);
7277         num_args = sig->hasthis + sig->param_count;
7278         ip = (unsigned char*)header->code;
7279         cfg->cil_start = ip;
7280         end = ip + header->code_size;
7281         cfg->stat_cil_code_size += header->code_size;
7282
7283         seq_points = cfg->gen_seq_points && cfg->method == method;
7284
7285         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
7286                 /* We could hit a seq point before attaching to the JIT (#8338) */
7287                 seq_points = FALSE;
7288         }
7289
7290         if (cfg->method == method)
7291                 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
7292
7293         if ((cfg->gen_sdb_seq_points && cfg->method == method) || cfg->coverage_info) {
7294                 minfo = mono_debug_lookup_method (method);
7295                 if (minfo) {
7296                         MonoSymSeqPoint *sps;
7297                         int i, n_il_offsets;
7298
7299                         mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
7300                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7301                         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);
7302                         sym_seq_points = TRUE;
7303                         for (i = 0; i < n_il_offsets; ++i) {
7304                                 if (sps [i].il_offset < header->code_size)
7305                                         mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
7306                         }
7307                         g_free (sps);
7308
7309                         MonoDebugMethodAsyncInfo* asyncMethod = mono_debug_lookup_method_async_debug_info (method);
7310                         if (asyncMethod) {
7311                                 for (i = 0; asyncMethod != NULL && i < asyncMethod->num_awaits; i++)
7312                                 {
7313                                         mono_bitset_set_fast (seq_point_locs, asyncMethod->resume_offsets[i]);
7314                                         mono_bitset_set_fast (seq_point_locs, asyncMethod->yield_offsets[i]);
7315                                 }
7316                                 mono_debug_free_method_async_debug_info (asyncMethod);
7317                         }
7318                 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
7319                         /* Methods without line number info like auto-generated property accessors */
7320                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7321                         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);
7322                         sym_seq_points = TRUE;
7323                 }
7324         }
7325
7326         /* 
7327          * Methods without init_locals set could cause asserts in various passes
7328          * (#497220). To work around this, we emit dummy initialization opcodes
7329          * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
7330          * on some platforms.
7331          */
7332         if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
7333                 init_locals = header->init_locals;
7334         else
7335                 init_locals = TRUE;
7336
7337         method_definition = method;
7338         while (method_definition->is_inflated) {
7339                 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
7340                 method_definition = imethod->declaring;
7341         }
7342
7343         /* SkipVerification is not allowed if core-clr is enabled */
7344         if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
7345                 dont_verify = TRUE;
7346                 dont_verify_stloc = TRUE;
7347         }
7348
7349         if (sig->is_inflated)
7350                 generic_context = mono_method_get_context (method);
7351         else if (generic_container)
7352                 generic_context = &generic_container->context;
7353         cfg->generic_context = generic_context;
7354
7355         if (!cfg->gshared)
7356                 g_assert (!sig->has_type_parameters);
7357
7358         if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7359                 g_assert (method->is_inflated);
7360                 g_assert (mono_method_get_context (method)->method_inst);
7361         }
7362         if (method->is_inflated && mono_method_get_context (method)->method_inst)
7363                 g_assert (sig->generic_param_count);
7364
7365         if (cfg->method == method) {
7366                 cfg->real_offset = 0;
7367         } else {
7368                 cfg->real_offset = inline_offset;
7369         }
7370
7371         cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
7372         cfg->cil_offset_to_bb_len = header->code_size;
7373
7374         cfg->current_method = method;
7375
7376         if (cfg->verbose_level > 2)
7377                 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
7378
7379         param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
7380         if (sig->hasthis)
7381                 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
7382         for (n = 0; n < sig->param_count; ++n)
7383                 param_types [n + sig->hasthis] = sig->params [n];
7384         cfg->arg_types = param_types;
7385
7386         cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
7387         if (cfg->method == method) {
7388                 /* ENTRY BLOCK */
7389                 NEW_BBLOCK (cfg, start_bblock);
7390                 cfg->bb_entry = start_bblock;
7391                 start_bblock->cil_code = NULL;
7392                 start_bblock->cil_length = 0;
7393
7394                 /* EXIT BLOCK */
7395                 NEW_BBLOCK (cfg, end_bblock);
7396                 cfg->bb_exit = end_bblock;
7397                 end_bblock->cil_code = NULL;
7398                 end_bblock->cil_length = 0;
7399                 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
7400                 g_assert (cfg->num_bblocks == 2);
7401
7402                 arg_array = cfg->args;
7403
7404                 if (header->num_clauses) {
7405                         cfg->spvars = g_hash_table_new (NULL, NULL);
7406                         cfg->exvars = g_hash_table_new (NULL, NULL);
7407                 }
7408                 /* handle exception clauses */
7409                 for (i = 0; i < header->num_clauses; ++i) {
7410                         MonoBasicBlock *try_bb;
7411                         MonoExceptionClause *clause = &header->clauses [i];
7412                         GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
7413
7414                         try_bb->real_offset = clause->try_offset;
7415                         try_bb->try_start = TRUE;
7416                         try_bb->region = ((i + 1) << 8) | clause->flags;
7417                         GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
7418                         tblock->real_offset = clause->handler_offset;
7419                         tblock->flags |= BB_EXCEPTION_HANDLER;
7420
7421                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
7422                                 mono_create_exvar_for_offset (cfg, clause->handler_offset);
7423                         /*
7424                          * Linking the try block with the EH block hinders inlining as we won't be able to 
7425                          * merge the bblocks from inlining and produce an artificial hole for no good reason.
7426                          */
7427                         if (COMPILE_LLVM (cfg))
7428                                 link_bblock (cfg, try_bb, tblock);
7429
7430                         if (*(ip + clause->handler_offset) == CEE_POP)
7431                                 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
7432
7433                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
7434                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
7435                             clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
7436                                 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7437                                 MONO_ADD_INS (tblock, ins);
7438
7439                                 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
7440                                         /* finally clauses already have a seq point */
7441                                         /* seq points for filter clauses are emitted below */
7442                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7443                                         MONO_ADD_INS (tblock, ins);
7444                                 }
7445
7446                                 /* todo: is a fault block unsafe to optimize? */
7447                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
7448                                         tblock->flags |= BB_EXCEPTION_UNSAFE;
7449                         }
7450
7451                         /*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);
7452                           while (p < end) {
7453                           printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
7454                           }*/
7455                         /* catch and filter blocks get the exception object on the stack */
7456                         if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
7457                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7458
7459                                 /* mostly like handle_stack_args (), but just sets the input args */
7460                                 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
7461                                 tblock->in_scount = 1;
7462                                 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7463                                 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7464
7465                                 cfg->cbb = tblock;
7466
7467 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
7468                                 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
7469                                 if (!cfg->compile_llvm) {
7470                                         MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
7471                                         ins->dreg = tblock->in_stack [0]->dreg;
7472                                         MONO_ADD_INS (tblock, ins);
7473                                 }
7474 #else
7475                                 MonoInst *dummy_use;
7476
7477                                 /* 
7478                                  * Add a dummy use for the exvar so its liveness info will be
7479                                  * correct.
7480                                  */
7481                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
7482 #endif
7483
7484                                 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7485                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7486                                         MONO_ADD_INS (tblock, ins);
7487                                 }
7488                                 
7489                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7490                                         GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
7491                                         tblock->flags |= BB_EXCEPTION_HANDLER;
7492                                         tblock->real_offset = clause->data.filter_offset;
7493                                         tblock->in_scount = 1;
7494                                         tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7495                                         /* The filter block shares the exvar with the handler block */
7496                                         tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7497                                         MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7498                                         MONO_ADD_INS (tblock, ins);
7499                                 }
7500                         }
7501
7502                         if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
7503                                         clause->data.catch_class &&
7504                                         cfg->gshared &&
7505                                         mono_class_check_context_used (clause->data.catch_class)) {
7506                                 /*
7507                                  * In shared generic code with catch
7508                                  * clauses containing type variables
7509                                  * the exception handling code has to
7510                                  * be able to get to the rgctx.
7511                                  * Therefore we have to make sure that
7512                                  * the vtable/mrgctx argument (for
7513                                  * static or generic methods) or the
7514                                  * "this" argument (for non-static
7515                                  * methods) are live.
7516                                  */
7517                                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7518                                                 mini_method_get_context (method)->method_inst ||
7519                                                 method->klass->valuetype) {
7520                                         mono_get_vtable_var (cfg);
7521                                 } else {
7522                                         MonoInst *dummy_use;
7523
7524                                         EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
7525                                 }
7526                         }
7527                 }
7528         } else {
7529                 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
7530                 cfg->cbb = start_bblock;
7531                 cfg->args = arg_array;
7532                 mono_save_args (cfg, sig, inline_args);
7533         }
7534
7535         /* FIRST CODE BLOCK */
7536         NEW_BBLOCK (cfg, tblock);
7537         tblock->cil_code = ip;
7538         cfg->cbb = tblock;
7539         cfg->ip = ip;
7540
7541         ADD_BBLOCK (cfg, tblock);
7542
7543         if (cfg->method == method) {
7544                 breakpoint_id = mono_debugger_method_has_breakpoint (method);
7545                 if (breakpoint_id) {
7546                         MONO_INST_NEW (cfg, ins, OP_BREAK);
7547                         MONO_ADD_INS (cfg->cbb, ins);
7548                 }
7549         }
7550
7551         /* we use a separate basic block for the initialization code */
7552         NEW_BBLOCK (cfg, init_localsbb);
7553         if (cfg->method == method)
7554                 cfg->bb_init = init_localsbb;
7555         init_localsbb->real_offset = cfg->real_offset;
7556         start_bblock->next_bb = init_localsbb;
7557         init_localsbb->next_bb = cfg->cbb;
7558         link_bblock (cfg, start_bblock, init_localsbb);
7559         link_bblock (cfg, init_localsbb, cfg->cbb);
7560         init_localsbb2 = init_localsbb;
7561         cfg->cbb = init_localsbb;
7562
7563         if (cfg->gsharedvt && cfg->method == method) {
7564                 MonoGSharedVtMethodInfo *info;
7565                 MonoInst *var, *locals_var;
7566                 int dreg;
7567
7568                 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
7569                 info->method = cfg->method;
7570                 info->count_entries = 16;
7571                 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
7572                 cfg->gsharedvt_info = info;
7573
7574                 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7575                 /* prevent it from being register allocated */
7576                 //var->flags |= MONO_INST_VOLATILE;
7577                 cfg->gsharedvt_info_var = var;
7578
7579                 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
7580                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
7581
7582                 /* Allocate locals */
7583                 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7584                 /* prevent it from being register allocated */
7585                 //locals_var->flags |= MONO_INST_VOLATILE;
7586                 cfg->gsharedvt_locals_var = locals_var;
7587
7588                 dreg = alloc_ireg (cfg);
7589                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
7590
7591                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7592                 ins->dreg = locals_var->dreg;
7593                 ins->sreg1 = dreg;
7594                 MONO_ADD_INS (cfg->cbb, ins);
7595                 cfg->gsharedvt_locals_var_ins = ins;
7596                 
7597                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7598                 /*
7599                 if (init_locals)
7600                         ins->flags |= MONO_INST_INIT;
7601                 */
7602         }
7603
7604         if (mono_security_core_clr_enabled ()) {
7605                 /* check if this is native code, e.g. an icall or a p/invoke */
7606                 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
7607                         MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
7608                         if (wrapped) {
7609                                 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
7610                                 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
7611
7612                                 /* if this ia a native call then it can only be JITted from platform code */
7613                                 if ((icall || pinvk) && method->klass && method->klass->image) {
7614                                         if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
7615                                                 MonoException *ex = icall ? mono_get_exception_security () : 
7616                                                         mono_get_exception_method_access ();
7617                                                 emit_throw_exception (cfg, ex);
7618                                         }
7619                                 }
7620                         }
7621                 }
7622         }
7623
7624         CHECK_CFG_EXCEPTION;
7625
7626         if (header->code_size == 0)
7627                 UNVERIFIED;
7628
7629         if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
7630                 ip = err_pos;
7631                 UNVERIFIED;
7632         }
7633
7634         if (cfg->method == method)
7635                 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
7636
7637         for (n = 0; n < header->num_locals; ++n) {
7638                 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
7639                         UNVERIFIED;
7640         }
7641         class_inits = NULL;
7642
7643         /* We force the vtable variable here for all shared methods
7644            for the possibility that they might show up in a stack
7645            trace where their exact instantiation is needed. */
7646         if (cfg->gshared && method == cfg->method) {
7647                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7648                                 mini_method_get_context (method)->method_inst ||
7649                                 method->klass->valuetype) {
7650                         mono_get_vtable_var (cfg);
7651                 } else {
7652                         /* FIXME: Is there a better way to do this?
7653                            We need the variable live for the duration
7654                            of the whole method. */
7655                         cfg->args [0]->flags |= MONO_INST_VOLATILE;
7656                 }
7657         }
7658
7659         /* add a check for this != NULL to inlined methods */
7660         if (is_virtual_call) {
7661                 MonoInst *arg_ins;
7662
7663                 NEW_ARGLOAD (cfg, arg_ins, 0);
7664                 MONO_ADD_INS (cfg->cbb, arg_ins);
7665                 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
7666         }
7667
7668         skip_dead_blocks = !dont_verify;
7669         if (skip_dead_blocks) {
7670                 original_bb = bb = mono_basic_block_split (method, &cfg->error, header);
7671                 CHECK_CFG_ERROR;
7672                 g_assert (bb);
7673         }
7674
7675         /* we use a spare stack slot in SWITCH and NEWOBJ and others */
7676         stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
7677
7678         ins_flag = 0;
7679         start_new_bblock = 0;
7680         while (ip < end) {
7681                 if (cfg->method == method)
7682                         cfg->real_offset = ip - header->code;
7683                 else
7684                         cfg->real_offset = inline_offset;
7685                 cfg->ip = ip;
7686
7687                 context_used = 0;
7688
7689                 if (start_new_bblock) {
7690                         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
7691                         if (start_new_bblock == 2) {
7692                                 g_assert (ip == tblock->cil_code);
7693                         } else {
7694                                 GET_BBLOCK (cfg, tblock, ip);
7695                         }
7696                         cfg->cbb->next_bb = tblock;
7697                         cfg->cbb = tblock;
7698                         start_new_bblock = 0;
7699                         for (i = 0; i < cfg->cbb->in_scount; ++i) {
7700                                 if (cfg->verbose_level > 3)
7701                                         printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7702                                 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7703                                 *sp++ = ins;
7704                         }
7705                         if (class_inits)
7706                                 g_slist_free (class_inits);
7707                         class_inits = NULL;
7708                 } else {
7709                         if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
7710                                 link_bblock (cfg, cfg->cbb, tblock);
7711                                 if (sp != stack_start) {
7712                                         handle_stack_args (cfg, stack_start, sp - stack_start);
7713                                         sp = stack_start;
7714                                         CHECK_UNVERIFIABLE (cfg);
7715                                 }
7716                                 cfg->cbb->next_bb = tblock;
7717                                 cfg->cbb = tblock;
7718                                 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7719                                         if (cfg->verbose_level > 3)
7720                                                 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7721                                         EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7722                                         *sp++ = ins;
7723                                 }
7724                                 g_slist_free (class_inits);
7725                                 class_inits = NULL;
7726                         }
7727                 }
7728
7729                 if (skip_dead_blocks) {
7730                         int ip_offset = ip - header->code;
7731
7732                         if (ip_offset == bb->end)
7733                                 bb = bb->next;
7734
7735                         if (bb->dead) {
7736                                 int op_size = mono_opcode_size (ip, end);
7737                                 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
7738
7739                                 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
7740
7741                                 if (ip_offset + op_size == bb->end) {
7742                                         MONO_INST_NEW (cfg, ins, OP_NOP);
7743                                         MONO_ADD_INS (cfg->cbb, ins);
7744                                         start_new_bblock = 1;
7745                                 }
7746
7747                                 ip += op_size;
7748                                 continue;
7749                         }
7750                 }
7751                 /*
7752                  * Sequence points are points where the debugger can place a breakpoint.
7753                  * Currently, we generate these automatically at points where the IL
7754                  * stack is empty.
7755                  */
7756                 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
7757                         /*
7758                          * Make methods interruptable at the beginning, and at the targets of
7759                          * backward branches.
7760                          * Also, do this at the start of every bblock in methods with clauses too,
7761                          * to be able to handle instructions with inprecise control flow like
7762                          * throw/endfinally.
7763                          * Backward branches are handled at the end of method-to-ir ().
7764                          */
7765                         gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
7766                         gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
7767
7768                         /* Avoid sequence points on empty IL like .volatile */
7769                         // FIXME: Enable this
7770                         //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
7771                         NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
7772                         if ((sp != stack_start) && !sym_seq_point)
7773                                 ins->flags |= MONO_INST_NONEMPTY_STACK;
7774                         MONO_ADD_INS (cfg->cbb, ins);
7775
7776                         if (sym_seq_points)
7777                                 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
7778
7779                         if ((cfg->method == method) && cfg->coverage_info) {
7780                                 guint32 cil_offset = ip - header->code;
7781                                 gpointer counter = &cfg->coverage_info->data [cil_offset].count;
7782                                 cfg->coverage_info->data [cil_offset].cil_code = ip;
7783
7784                                 if (mono_arch_opcode_supported (OP_ATOMIC_ADD_I4)) {
7785                                         MonoInst *one_ins, *load_ins;
7786
7787                                         EMIT_NEW_PCONST (cfg, load_ins, counter);
7788                                         EMIT_NEW_ICONST (cfg, one_ins, 1);
7789                                         MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_I4);
7790                                         ins->dreg = mono_alloc_ireg (cfg);
7791                                         ins->inst_basereg = load_ins->dreg;
7792                                         ins->inst_offset = 0;
7793                                         ins->sreg2 = one_ins->dreg;
7794                                         ins->type = STACK_I4;
7795                                         MONO_ADD_INS (cfg->cbb, ins);
7796                                 } else {
7797                                         EMIT_NEW_PCONST (cfg, ins, counter);
7798                                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
7799                                 }
7800                         }
7801                 }
7802
7803                 cfg->cbb->real_offset = cfg->real_offset;
7804
7805                 if (cfg->verbose_level > 3)
7806                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
7807
7808                 switch (*ip) {
7809                 case CEE_NOP:
7810                         if (seq_points && !sym_seq_points && sp != stack_start) {
7811                                 /*
7812                                  * The C# compiler uses these nops to notify the JIT that it should
7813                                  * insert seq points.
7814                                  */
7815                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
7816                                 MONO_ADD_INS (cfg->cbb, ins);
7817                         }
7818                         if (cfg->keep_cil_nops)
7819                                 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
7820                         else
7821                                 MONO_INST_NEW (cfg, ins, OP_NOP);
7822                         ip++;
7823                         MONO_ADD_INS (cfg->cbb, ins);
7824                         break;
7825                 case CEE_BREAK:
7826                         if (mini_should_insert_breakpoint (cfg->method)) {
7827                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
7828                         } else {
7829                                 MONO_INST_NEW (cfg, ins, OP_NOP);
7830                         }
7831                         ip++;
7832                         MONO_ADD_INS (cfg->cbb, ins);
7833                         break;
7834                 case CEE_LDARG_0:
7835                 case CEE_LDARG_1:
7836                 case CEE_LDARG_2:
7837                 case CEE_LDARG_3:
7838                         CHECK_STACK_OVF (1);
7839                         n = (*ip)-CEE_LDARG_0;
7840                         CHECK_ARG (n);
7841                         EMIT_NEW_ARGLOAD (cfg, ins, n);
7842                         ip++;
7843                         *sp++ = ins;
7844                         break;
7845                 case CEE_LDLOC_0:
7846                 case CEE_LDLOC_1:
7847                 case CEE_LDLOC_2:
7848                 case CEE_LDLOC_3:
7849                         CHECK_STACK_OVF (1);
7850                         n = (*ip)-CEE_LDLOC_0;
7851                         CHECK_LOCAL (n);
7852                         EMIT_NEW_LOCLOAD (cfg, ins, n);
7853                         ip++;
7854                         *sp++ = ins;
7855                         break;
7856                 case CEE_STLOC_0:
7857                 case CEE_STLOC_1:
7858                 case CEE_STLOC_2:
7859                 case CEE_STLOC_3: {
7860                         CHECK_STACK (1);
7861                         n = (*ip)-CEE_STLOC_0;
7862                         CHECK_LOCAL (n);
7863                         --sp;
7864                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
7865                                 UNVERIFIED;
7866                         emit_stloc_ir (cfg, sp, header, n);
7867                         ++ip;
7868                         inline_costs += 1;
7869                         break;
7870                         }
7871                 case CEE_LDARG_S:
7872                         CHECK_OPSIZE (2);
7873                         CHECK_STACK_OVF (1);
7874                         n = ip [1];
7875                         CHECK_ARG (n);
7876                         EMIT_NEW_ARGLOAD (cfg, ins, n);
7877                         *sp++ = ins;
7878                         ip += 2;
7879                         break;
7880                 case CEE_LDARGA_S:
7881                         CHECK_OPSIZE (2);
7882                         CHECK_STACK_OVF (1);
7883                         n = ip [1];
7884                         CHECK_ARG (n);
7885                         NEW_ARGLOADA (cfg, ins, n);
7886                         MONO_ADD_INS (cfg->cbb, ins);
7887                         *sp++ = ins;
7888                         ip += 2;
7889                         break;
7890                 case CEE_STARG_S:
7891                         CHECK_OPSIZE (2);
7892                         CHECK_STACK (1);
7893                         --sp;
7894                         n = ip [1];
7895                         CHECK_ARG (n);
7896                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
7897                                 UNVERIFIED;
7898                         emit_starg_ir (cfg, sp, n);
7899                         ip += 2;
7900                         break;
7901                 case CEE_LDLOC_S:
7902                         CHECK_OPSIZE (2);
7903                         CHECK_STACK_OVF (1);
7904                         n = ip [1];
7905                         CHECK_LOCAL (n);
7906                         if ((ip [2] == CEE_LDFLD) && ip_in_bb (cfg, cfg->cbb, ip + 2) && MONO_TYPE_ISSTRUCT (header->locals [n])) {
7907                                 /* Avoid loading a struct just to load one of its fields */
7908                                 EMIT_NEW_LOCLOADA (cfg, ins, n);
7909                         } else {
7910                                 EMIT_NEW_LOCLOAD (cfg, ins, n);
7911                         }
7912                         *sp++ = ins;
7913                         ip += 2;
7914                         break;
7915                 case CEE_LDLOCA_S: {
7916                         unsigned char *tmp_ip;
7917                         CHECK_OPSIZE (2);
7918                         CHECK_STACK_OVF (1);
7919                         CHECK_LOCAL (ip [1]);
7920
7921                         if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
7922                                 ip = tmp_ip;
7923                                 inline_costs += 1;
7924                                 break;
7925                         }
7926
7927                         EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
7928                         *sp++ = ins;
7929                         ip += 2;
7930                         break;
7931                 }
7932                 case CEE_STLOC_S:
7933                         CHECK_OPSIZE (2);
7934                         CHECK_STACK (1);
7935                         --sp;
7936                         CHECK_LOCAL (ip [1]);
7937                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
7938                                 UNVERIFIED;
7939                         emit_stloc_ir (cfg, sp, header, ip [1]);
7940                         ip += 2;
7941                         inline_costs += 1;
7942                         break;
7943                 case CEE_LDNULL:
7944                         CHECK_STACK_OVF (1);
7945                         EMIT_NEW_PCONST (cfg, ins, NULL);
7946                         ins->type = STACK_OBJ;
7947                         ++ip;
7948                         *sp++ = ins;
7949                         break;
7950                 case CEE_LDC_I4_M1:
7951                         CHECK_STACK_OVF (1);
7952                         EMIT_NEW_ICONST (cfg, ins, -1);
7953                         ++ip;
7954                         *sp++ = ins;
7955                         break;
7956                 case CEE_LDC_I4_0:
7957                 case CEE_LDC_I4_1:
7958                 case CEE_LDC_I4_2:
7959                 case CEE_LDC_I4_3:
7960                 case CEE_LDC_I4_4:
7961                 case CEE_LDC_I4_5:
7962                 case CEE_LDC_I4_6:
7963                 case CEE_LDC_I4_7:
7964                 case CEE_LDC_I4_8:
7965                         CHECK_STACK_OVF (1);
7966                         EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
7967                         ++ip;
7968                         *sp++ = ins;
7969                         break;
7970                 case CEE_LDC_I4_S:
7971                         CHECK_OPSIZE (2);
7972                         CHECK_STACK_OVF (1);
7973                         ++ip;
7974                         EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
7975                         ++ip;
7976                         *sp++ = ins;
7977                         break;
7978                 case CEE_LDC_I4:
7979                         CHECK_OPSIZE (5);
7980                         CHECK_STACK_OVF (1);
7981                         EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
7982                         ip += 5;
7983                         *sp++ = ins;
7984                         break;
7985                 case CEE_LDC_I8:
7986                         CHECK_OPSIZE (9);
7987                         CHECK_STACK_OVF (1);
7988                         MONO_INST_NEW (cfg, ins, OP_I8CONST);
7989                         ins->type = STACK_I8;
7990                         ins->dreg = alloc_dreg (cfg, STACK_I8);
7991                         ++ip;
7992                         ins->inst_l = (gint64)read64 (ip);
7993                         MONO_ADD_INS (cfg->cbb, ins);
7994                         ip += 8;
7995                         *sp++ = ins;
7996                         break;
7997                 case CEE_LDC_R4: {
7998                         float *f;
7999                         gboolean use_aotconst = FALSE;
8000
8001 #ifdef TARGET_POWERPC
8002                         /* FIXME: Clean this up */
8003                         if (cfg->compile_aot)
8004                                 use_aotconst = TRUE;
8005 #endif
8006
8007                         /* FIXME: we should really allocate this only late in the compilation process */
8008                         f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8009                         CHECK_OPSIZE (5);
8010                         CHECK_STACK_OVF (1);
8011
8012                         if (use_aotconst) {
8013                                 MonoInst *cons;
8014                                 int dreg;
8015
8016                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8017
8018                                 dreg = alloc_freg (cfg);
8019                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8020                                 ins->type = cfg->r4_stack_type;
8021                         } else {
8022                                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8023                                 ins->type = cfg->r4_stack_type;
8024                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8025                                 ins->inst_p0 = f;
8026                                 MONO_ADD_INS (cfg->cbb, ins);
8027                         }
8028                         ++ip;
8029                         readr4 (ip, f);
8030                         ip += 4;
8031                         *sp++ = ins;                    
8032                         break;
8033                 }
8034                 case CEE_LDC_R8: {
8035                         double *d;
8036                         gboolean use_aotconst = FALSE;
8037
8038 #ifdef TARGET_POWERPC
8039                         /* FIXME: Clean this up */
8040                         if (cfg->compile_aot)
8041                                 use_aotconst = TRUE;
8042 #endif
8043
8044                         /* FIXME: we should really allocate this only late in the compilation process */
8045                         d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8046                         CHECK_OPSIZE (9);
8047                         CHECK_STACK_OVF (1);
8048
8049                         if (use_aotconst) {
8050                                 MonoInst *cons;
8051                                 int dreg;
8052
8053                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8054
8055                                 dreg = alloc_freg (cfg);
8056                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8057                                 ins->type = STACK_R8;
8058                         } else {
8059                                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8060                                 ins->type = STACK_R8;
8061                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8062                                 ins->inst_p0 = d;
8063                                 MONO_ADD_INS (cfg->cbb, ins);
8064                         }
8065                         ++ip;
8066                         readr8 (ip, d);
8067                         ip += 8;
8068                         *sp++ = ins;
8069                         break;
8070                 }
8071                 case CEE_DUP: {
8072                         MonoInst *temp, *store;
8073                         CHECK_STACK (1);
8074                         CHECK_STACK_OVF (1);
8075                         sp--;
8076                         ins = *sp;
8077
8078                         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8079                         EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8080
8081                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8082                         *sp++ = ins;
8083
8084                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8085                         *sp++ = ins;
8086
8087                         ++ip;
8088                         inline_costs += 2;
8089                         break;
8090                 }
8091                 case CEE_POP:
8092                         CHECK_STACK (1);
8093                         ip++;
8094                         --sp;
8095
8096 #ifdef TARGET_X86
8097                         if (sp [0]->type == STACK_R8)
8098                                 /* we need to pop the value from the x86 FP stack */
8099                                 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8100 #endif
8101                         break;
8102                 case CEE_JMP: {
8103                         MonoCallInst *call;
8104                         MonoMethodSignature *fsig;
8105                         int i, n;
8106
8107                         INLINE_FAILURE ("jmp");
8108                         GSHAREDVT_FAILURE (*ip);
8109
8110                         CHECK_OPSIZE (5);
8111                         if (stack_start != sp)
8112                                 UNVERIFIED;
8113                         token = read32 (ip + 1);
8114                         /* FIXME: check the signature matches */
8115                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8116                         CHECK_CFG_ERROR;
8117  
8118                         if (cfg->gshared && mono_method_check_context_used (cmethod))
8119                                 GENERIC_SHARING_FAILURE (CEE_JMP);
8120
8121                         mini_profiler_emit_tail_call (cfg, cmethod);
8122
8123                         fsig = mono_method_signature (cmethod);
8124                         n = fsig->param_count + fsig->hasthis;
8125                         if (cfg->llvm_only) {
8126                                 MonoInst **args;
8127
8128                                 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8129                                 for (i = 0; i < n; ++i)
8130                                         EMIT_NEW_ARGLOAD (cfg, args [i], i);
8131                                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
8132                                 /*
8133                                  * The code in mono-basic-block.c treats the rest of the code as dead, but we
8134                                  * have to emit a normal return since llvm expects it.
8135                                  */
8136                                 if (cfg->ret)
8137                                         emit_setret (cfg, ins);
8138                                 MONO_INST_NEW (cfg, ins, OP_BR);
8139                                 ins->inst_target_bb = end_bblock;
8140                                 MONO_ADD_INS (cfg->cbb, ins);
8141                                 link_bblock (cfg, cfg->cbb, end_bblock);
8142                                 ip += 5;
8143                                 break;
8144                         } else if (cfg->backend->have_op_tail_call) {
8145                                 /* Handle tail calls similarly to calls */
8146                                 DISABLE_AOT (cfg);
8147
8148                                 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
8149                                 call->method = cmethod;
8150                                 call->tail_call = TRUE;
8151                                 call->signature = mono_method_signature (cmethod);
8152                                 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8153                                 call->inst.inst_p0 = cmethod;
8154                                 for (i = 0; i < n; ++i)
8155                                         EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
8156
8157                                 if (mini_type_is_vtype (mini_get_underlying_type (call->signature->ret)))
8158                                         call->vret_var = cfg->vret_addr;
8159
8160                                 mono_arch_emit_call (cfg, call);
8161                                 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
8162                                 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
8163                         } else {
8164                                 for (i = 0; i < num_args; ++i)
8165                                         /* Prevent arguments from being optimized away */
8166                                         arg_array [i]->flags |= MONO_INST_VOLATILE;
8167
8168                                 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8169                                 ins = (MonoInst*)call;
8170                                 ins->inst_p0 = cmethod;
8171                                 MONO_ADD_INS (cfg->cbb, ins);
8172                         }
8173
8174                         ip += 5;
8175                         start_new_bblock = 1;
8176                         break;
8177                 }
8178                 case CEE_CALLI: {
8179                         MonoInst *addr;
8180                         MonoMethodSignature *fsig;
8181
8182                         CHECK_OPSIZE (5);
8183                         token = read32 (ip + 1);
8184
8185                         ins = NULL;
8186
8187                         //GSHAREDVT_FAILURE (*ip);
8188                         cmethod = NULL;
8189                         CHECK_STACK (1);
8190                         --sp;
8191                         addr = *sp;
8192                         fsig = mini_get_signature (method, token, generic_context, &cfg->error);
8193                         CHECK_CFG_ERROR;
8194
8195                         if (method->dynamic && fsig->pinvoke) {
8196                                 MonoInst *args [3];
8197
8198                                 /*
8199                                  * This is a call through a function pointer using a pinvoke
8200                                  * signature. Have to create a wrapper and call that instead.
8201                                  * FIXME: This is very slow, need to create a wrapper at JIT time
8202                                  * instead based on the signature.
8203                                  */
8204                                 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
8205                                 EMIT_NEW_PCONST (cfg, args [1], fsig);
8206                                 args [2] = addr;
8207                                 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
8208                         }
8209
8210                         n = fsig->param_count + fsig->hasthis;
8211
8212                         CHECK_STACK (n);
8213
8214                         //g_assert (!virtual_ || fsig->hasthis);
8215
8216                         sp -= n;
8217
8218                         inline_costs += 10 * num_calls++;
8219
8220                         /*
8221                          * Making generic calls out of gsharedvt methods.
8222                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8223                          * patching gshared method addresses into a gsharedvt method.
8224                          */
8225                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8226                                 /*
8227                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
8228                                  */
8229                                 MonoInst *callee = addr;
8230
8231                                 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
8232                                         /* Not tested */
8233                                         GSHAREDVT_FAILURE (*ip);
8234
8235                                 if (cfg->llvm_only)
8236                                         // FIXME:
8237                                         GSHAREDVT_FAILURE (*ip);
8238
8239                                 addr = emit_get_rgctx_sig (cfg, context_used,
8240                                                                                           fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
8241                                 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, callee);
8242                                 goto calli_end;
8243                         }
8244
8245                         /* Prevent inlining of methods with indirect calls */
8246                         INLINE_FAILURE ("indirect call");
8247
8248                         if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
8249                                 MonoJumpInfoType info_type;
8250                                 gpointer info_data;
8251
8252                                 /*
8253                                  * Instead of emitting an indirect call, emit a direct call
8254                                  * with the contents of the aotconst as the patch info.
8255                                  */
8256                                 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
8257                                         info_type = (MonoJumpInfoType)addr->inst_c1;
8258                                         info_data = addr->inst_p0;
8259                                 } else {
8260                                         info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
8261                                         info_data = addr->inst_right->inst_left;
8262                                 }
8263
8264                                 if (info_type == MONO_PATCH_INFO_ICALL_ADDR) {
8265                                         ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR_CALL, info_data, fsig, sp);
8266                                         NULLIFY_INS (addr);
8267                                         goto calli_end;
8268                                 } else if (info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8269                                         ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
8270                                         NULLIFY_INS (addr);
8271                                         goto calli_end;
8272                                 }
8273                         }
8274                         ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8275
8276                         calli_end:
8277
8278                         /* End of call, INS should contain the result of the call, if any */
8279
8280                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8281                                 g_assert (ins);
8282                                 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
8283                         }
8284
8285                         CHECK_CFG_EXCEPTION;
8286
8287                         ip += 5;
8288                         ins_flag = 0;
8289                         constrained_class = NULL;
8290                         break;
8291                 }
8292                 case CEE_CALL:
8293                 case CEE_CALLVIRT: {
8294                         MonoInst *addr = NULL;
8295                         MonoMethodSignature *fsig = NULL;
8296                         int array_rank = 0;
8297                         int virtual_ = *ip == CEE_CALLVIRT;
8298                         gboolean pass_imt_from_rgctx = FALSE;
8299                         MonoInst *imt_arg = NULL;
8300                         MonoInst *keep_this_alive = NULL;
8301                         gboolean pass_vtable = FALSE;
8302                         gboolean pass_mrgctx = FALSE;
8303                         MonoInst *vtable_arg = NULL;
8304                         gboolean check_this = FALSE;
8305                         gboolean supported_tail_call = FALSE;
8306                         gboolean tail_call = FALSE;
8307                         gboolean need_seq_point = FALSE;
8308                         guint32 call_opcode = *ip;
8309                         gboolean emit_widen = TRUE;
8310                         gboolean push_res = TRUE;
8311                         gboolean skip_ret = FALSE;
8312                         gboolean delegate_invoke = FALSE;
8313                         gboolean direct_icall = FALSE;
8314                         gboolean constrained_partial_call = FALSE;
8315                         MonoMethod *cil_method;
8316
8317                         CHECK_OPSIZE (5);
8318                         token = read32 (ip + 1);
8319
8320                         ins = NULL;
8321
8322                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8323                         CHECK_CFG_ERROR;
8324
8325                         cil_method = cmethod;
8326                                 
8327                         if (constrained_class) {
8328                                 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8329                                         if (!mini_is_gsharedvt_klass (constrained_class)) {
8330                                                 g_assert (!cmethod->klass->valuetype);
8331                                                 if (!mini_type_is_reference (&constrained_class->byval_arg))
8332                                                         constrained_partial_call = TRUE;
8333                                         }
8334                                 }
8335
8336                                 if (method->wrapper_type != MONO_WRAPPER_NONE) {
8337                                         if (cfg->verbose_level > 2)
8338                                                 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8339                                         if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
8340                                                    constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
8341                                                   cfg->gshared)) {
8342                                                 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
8343                                                 CHECK_CFG_ERROR;
8344                                         }
8345                                 } else {
8346                                         if (cfg->verbose_level > 2)
8347                                                 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8348
8349                                         if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8350                                                 /* 
8351                                                  * This is needed since get_method_constrained can't find 
8352                                                  * the method in klass representing a type var.
8353                                                  * The type var is guaranteed to be a reference type in this
8354                                                  * case.
8355                                                  */
8356                                                 if (!mini_is_gsharedvt_klass (constrained_class))
8357                                                         g_assert (!cmethod->klass->valuetype);
8358                                         } else {
8359                                                 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
8360                                                 CHECK_CFG_ERROR;
8361                                         }
8362                                 }
8363
8364                                 if (constrained_class->enumtype && !strcmp (cmethod->name, "GetHashCode")) {
8365                                         /* Use the corresponding method from the base type to avoid boxing */
8366                                         MonoType *base_type = mono_class_enum_basetype (constrained_class);
8367                                         g_assert (base_type);
8368                                         constrained_class = mono_class_from_mono_type (base_type);
8369                                         cmethod = mono_class_get_method_from_name (constrained_class, cmethod->name, 0);
8370                                         g_assert (cmethod);
8371                                 }
8372                         }
8373                                         
8374                         if (!dont_verify && !cfg->skip_visibility) {
8375                                 MonoMethod *target_method = cil_method;
8376                                 if (method->is_inflated) {
8377                                         target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
8378                                         CHECK_CFG_ERROR;
8379                                 }
8380                                 if (!mono_method_can_access_method (method_definition, target_method) &&
8381                                         !mono_method_can_access_method (method, cil_method))
8382                                         emit_method_access_failure (cfg, method, cil_method);
8383                         }
8384
8385                         if (mono_security_core_clr_enabled ())
8386                                 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
8387
8388                         if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
8389                                 /* MS.NET seems to silently convert this to a callvirt */
8390                                 virtual_ = 1;
8391
8392                         {
8393                                 /*
8394                                  * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
8395                                  * converts to a callvirt.
8396                                  *
8397                                  * tests/bug-515884.il is an example of this behavior
8398                                  */
8399                                 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
8400                                 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
8401                                 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
8402                                         virtual_ = 1;
8403                         }
8404
8405                         if (!cmethod->klass->inited)
8406                                 if (!mono_class_init (cmethod->klass))
8407                                         TYPE_LOAD_ERROR (cmethod->klass);
8408
8409                         fsig = mono_method_signature (cmethod);
8410                         if (!fsig)
8411                                 LOAD_ERROR;
8412                         if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
8413                                 mini_class_is_system_array (cmethod->klass)) {
8414                                 array_rank = cmethod->klass->rank;
8415                         } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
8416                                 direct_icall = TRUE;
8417                         } else if (fsig->pinvoke) {
8418                                 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8419                                 fsig = mono_method_signature (wrapper);
8420                         } else if (constrained_class) {
8421                         } else {
8422                                 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
8423                                 CHECK_CFG_ERROR;
8424                         }
8425
8426                         if (cfg->llvm_only && !cfg->method->wrapper_type && (!cmethod || cmethod->is_inflated))
8427                                 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
8428
8429                         /* See code below */
8430                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8431                                 MonoBasicBlock *tbb;
8432
8433                                 GET_BBLOCK (cfg, tbb, ip + 5);
8434                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8435                                         /*
8436                                          * We want to extend the try block to cover the call, but we can't do it if the
8437                                          * call is made directly since its followed by an exception check.
8438                                          */
8439                                         direct_icall = FALSE;
8440                                 }
8441                         }
8442
8443                         mono_save_token_info (cfg, image, token, cil_method);
8444
8445                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
8446                                 need_seq_point = TRUE;
8447
8448                         /* Don't support calls made using type arguments for now */
8449                         /*
8450                           if (cfg->gsharedvt) {
8451                           if (mini_is_gsharedvt_signature (fsig))
8452                           GSHAREDVT_FAILURE (*ip);
8453                           }
8454                         */
8455
8456                         if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
8457                                 g_assert_not_reached ();
8458
8459                         n = fsig->param_count + fsig->hasthis;
8460
8461                         if (!cfg->gshared && mono_class_is_gtd (cmethod->klass))
8462                                 UNVERIFIED;
8463
8464                         if (!cfg->gshared)
8465                                 g_assert (!mono_method_check_context_used (cmethod));
8466
8467                         CHECK_STACK (n);
8468
8469                         //g_assert (!virtual_ || fsig->hasthis);
8470
8471                         sp -= n;
8472
8473                         if (cmethod && cmethod->klass->image == mono_defaults.corlib && !strcmp (cmethod->klass->name, "ThrowHelper"))
8474                                 cfg->cbb->out_of_line = TRUE;
8475
8476                         /*
8477                          * We have the `constrained.' prefix opcode.
8478                          */
8479                         if (constrained_class) {
8480                                 if (mini_is_gsharedvt_klass (constrained_class)) {
8481                                         if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
8482                                                 /* The 'Own method' case below */
8483                                         } else if (cmethod->klass->image != mono_defaults.corlib && !mono_class_is_interface (cmethod->klass) && !cmethod->klass->valuetype) {
8484                                                 /* 'The type parameter is instantiated as a reference type' case below. */
8485                                         } else {
8486                                                 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
8487                                                 CHECK_CFG_EXCEPTION;
8488                                                 g_assert (ins);
8489                                                 goto call_end;
8490                                         }
8491                                 }
8492
8493                                 if (constrained_partial_call) {
8494                                         gboolean need_box = TRUE;
8495
8496                                         /*
8497                                          * The receiver is a valuetype, but the exact type is not known at compile time. This means the
8498                                          * called method is not known at compile time either. The called method could end up being
8499                                          * one of the methods on the parent classes (object/valuetype/enum), in which case we need
8500                                          * to box the receiver.
8501                                          * A simple solution would be to box always and make a normal virtual call, but that would
8502                                          * be bad performance wise.
8503                                          */
8504                                         if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass)) {
8505                                                 /*
8506                                                  * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
8507                                                  */
8508                                                 need_box = FALSE;
8509                                         }
8510
8511                                         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)) {
8512                                                 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
8513                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8514                                                 ins->klass = constrained_class;
8515                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8516                                                 CHECK_CFG_EXCEPTION;
8517                                         } else if (need_box) {
8518                                                 MonoInst *box_type;
8519                                                 MonoBasicBlock *is_ref_bb, *end_bb;
8520                                                 MonoInst *nonbox_call;
8521
8522                                                 /*
8523                                                  * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
8524                                                  * if needed.
8525                                                  * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
8526                                                  * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
8527                                                  */
8528                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8529
8530                                                 NEW_BBLOCK (cfg, is_ref_bb);
8531                                                 NEW_BBLOCK (cfg, end_bb);
8532
8533                                                 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);
8534                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
8535                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
8536
8537                                                 /* Non-ref case */
8538                                                 nonbox_call = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8539
8540                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8541
8542                                                 /* Ref case */
8543                                                 MONO_START_BB (cfg, is_ref_bb);
8544                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8545                                                 ins->klass = constrained_class;
8546                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8547                                                 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8548
8549                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8550
8551                                                 MONO_START_BB (cfg, end_bb);
8552                                                 cfg->cbb = end_bb;
8553
8554                                                 nonbox_call->dreg = ins->dreg;
8555                                                 goto call_end;
8556                                         } else {
8557                                                 g_assert (mono_class_is_interface (cmethod->klass));
8558                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8559                                                 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8560                                                 goto call_end;
8561                                         }
8562                                 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
8563                                         /*
8564                                          * The type parameter is instantiated as a valuetype,
8565                                          * but that type doesn't override the method we're
8566                                          * calling, so we need to box `this'.
8567                                          */
8568                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8569                                         ins->klass = constrained_class;
8570                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8571                                         CHECK_CFG_EXCEPTION;
8572                                 } else if (!constrained_class->valuetype) {
8573                                         int dreg = alloc_ireg_ref (cfg);
8574
8575                                         /*
8576                                          * The type parameter is instantiated as a reference
8577                                          * type.  We have a managed pointer on the stack, so
8578                                          * we need to dereference it here.
8579                                          */
8580                                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
8581                                         ins->type = STACK_OBJ;
8582                                         sp [0] = ins;
8583                                 } else {
8584                                         if (cmethod->klass->valuetype) {
8585                                                 /* Own method */
8586                                         } else {
8587                                                 /* Interface method */
8588                                                 int ioffset, slot;
8589
8590                                                 mono_class_setup_vtable (constrained_class);
8591                                                 CHECK_TYPELOAD (constrained_class);
8592                                                 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
8593                                                 if (ioffset == -1)
8594                                                         TYPE_LOAD_ERROR (constrained_class);
8595                                                 slot = mono_method_get_vtable_slot (cmethod);
8596                                                 if (slot == -1)
8597                                                         TYPE_LOAD_ERROR (cmethod->klass);
8598                                                 cmethod = constrained_class->vtable [ioffset + slot];
8599
8600                                                 if (cmethod->klass == mono_defaults.enum_class) {
8601                                                         /* Enum implements some interfaces, so treat this as the first case */
8602                                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8603                                                         ins->klass = constrained_class;
8604                                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8605                                                         CHECK_CFG_EXCEPTION;
8606                                                 }
8607                                         }
8608                                         virtual_ = 0;
8609                                 }
8610                                 constrained_class = NULL;
8611                         }
8612
8613                         if (check_call_signature (cfg, fsig, sp))
8614                                 UNVERIFIED;
8615
8616                         if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
8617                                 delegate_invoke = TRUE;
8618
8619                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
8620                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8621                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
8622                                         emit_widen = FALSE;
8623                                 }
8624
8625                                 goto call_end;
8626                         }
8627
8628                         /* 
8629                          * If the callee is a shared method, then its static cctor
8630                          * might not get called after the call was patched.
8631                          */
8632                         if (cfg->gshared && cmethod->klass != method->klass && mono_class_is_ginst (cmethod->klass) && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
8633                                 emit_class_init (cfg, cmethod->klass);
8634                                 CHECK_TYPELOAD (cmethod->klass);
8635                         }
8636
8637                         check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
8638
8639                         if (cfg->gshared) {
8640                                 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
8641
8642                                 context_used = mini_method_check_context_used (cfg, cmethod);
8643
8644                                 if (context_used && mono_class_is_interface (cmethod->klass)) {
8645                                         /* Generic method interface
8646                                            calls are resolved via a
8647                                            helper function and don't
8648                                            need an imt. */
8649                                         if (!cmethod_context || !cmethod_context->method_inst)
8650                                                 pass_imt_from_rgctx = TRUE;
8651                                 }
8652
8653                                 /*
8654                                  * If a shared method calls another
8655                                  * shared method then the caller must
8656                                  * have a generic sharing context
8657                                  * because the magic trampoline
8658                                  * requires it.  FIXME: We shouldn't
8659                                  * have to force the vtable/mrgctx
8660                                  * variable here.  Instead there
8661                                  * should be a flag in the cfg to
8662                                  * request a generic sharing context.
8663                                  */
8664                                 if (context_used &&
8665                                                 ((cfg->method->flags & METHOD_ATTRIBUTE_STATIC) || cfg->method->klass->valuetype))
8666                                         mono_get_vtable_var (cfg);
8667                         }
8668
8669                         if (pass_vtable) {
8670                                 if (context_used) {
8671                                         vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8672                                 } else {
8673                                         MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8674
8675                                         CHECK_TYPELOAD (cmethod->klass);
8676                                         EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8677                                 }
8678                         }
8679
8680                         if (pass_mrgctx) {
8681                                 g_assert (!vtable_arg);
8682
8683                                 if (!cfg->compile_aot) {
8684                                         /* 
8685                                          * emit_get_rgctx_method () calls mono_class_vtable () so check 
8686                                          * for type load errors before.
8687                                          */
8688                                         mono_class_setup_vtable (cmethod->klass);
8689                                         CHECK_TYPELOAD (cmethod->klass);
8690                                 }
8691
8692                                 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8693
8694                                 /* !marshalbyref is needed to properly handle generic methods + remoting */
8695                                 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
8696                                          MONO_METHOD_IS_FINAL (cmethod)) &&
8697                                         !mono_class_is_marshalbyref (cmethod->klass)) {
8698                                         if (virtual_)
8699                                                 check_this = TRUE;
8700                                         virtual_ = 0;
8701                                 }
8702                         }
8703
8704                         if (pass_imt_from_rgctx) {
8705                                 g_assert (!pass_vtable);
8706
8707                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
8708                                         cmethod, MONO_RGCTX_INFO_METHOD);
8709                         }
8710
8711                         if (check_this)
8712                                 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
8713
8714                         /* Calling virtual generic methods */
8715                         if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
8716                             !(MONO_METHOD_IS_FINAL (cmethod) && 
8717                               cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
8718                             fsig->generic_param_count && 
8719                                 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
8720                                 !cfg->llvm_only) {
8721                                 MonoInst *this_temp, *this_arg_temp, *store;
8722                                 MonoInst *iargs [4];
8723
8724                                 g_assert (fsig->is_inflated);
8725
8726                                 /* Prevent inlining of methods that contain indirect calls */
8727                                 INLINE_FAILURE ("virtual generic call");
8728
8729                                 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8730                                         GSHAREDVT_FAILURE (*ip);
8731
8732                                 if (cfg->backend->have_generalized_imt_trampoline && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
8733                                         g_assert (!imt_arg);
8734                                         if (!context_used)
8735                                                 g_assert (cmethod->is_inflated);
8736                                         imt_arg = emit_get_rgctx_method (cfg, context_used,
8737                                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
8738                                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
8739                                 } else {
8740                                         this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
8741                                         NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
8742                                         MONO_ADD_INS (cfg->cbb, store);
8743
8744                                         /* FIXME: This should be a managed pointer */
8745                                         this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8746
8747                                         EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
8748                                         iargs [1] = emit_get_rgctx_method (cfg, context_used,
8749                                                                                                            cmethod, MONO_RGCTX_INFO_METHOD);
8750                                         EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
8751                                         addr = mono_emit_jit_icall (cfg,
8752                                                                                                 mono_helper_compile_generic_method, iargs);
8753
8754                                         EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
8755
8756                                         ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8757                                 }
8758
8759                                 goto call_end;
8760                         }
8761
8762                         /*
8763                          * Implement a workaround for the inherent races involved in locking:
8764                          * Monitor.Enter ()
8765                          * try {
8766                          * } finally {
8767                          *    Monitor.Exit ()
8768                          * }
8769                          * If a thread abort happens between the call to Monitor.Enter () and the start of the
8770                          * try block, the Exit () won't be executed, see:
8771                          * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
8772                          * To work around this, we extend such try blocks to include the last x bytes
8773                          * of the Monitor.Enter () call.
8774                          */
8775                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8776                                 MonoBasicBlock *tbb;
8777
8778                                 GET_BBLOCK (cfg, tbb, ip + 5);
8779                                 /* 
8780                                  * Only extend try blocks with a finally, to avoid catching exceptions thrown
8781                                  * from Monitor.Enter like ArgumentNullException.
8782                                  */
8783                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8784                                         /* Mark this bblock as needing to be extended */
8785                                         tbb->extend_try_block = TRUE;
8786                                 }
8787                         }
8788
8789                         /* Conversion to a JIT intrinsic */
8790                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
8791                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8792                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
8793                                         emit_widen = FALSE;
8794                                 }
8795                                 goto call_end;
8796                         }
8797                         CHECK_CFG_ERROR;
8798                         
8799                         /* Inlining */
8800                         if ((cfg->opt & MONO_OPT_INLINE) &&
8801                                 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
8802                             mono_method_check_inlining (cfg, cmethod)) {
8803                                 int costs;
8804                                 gboolean always = FALSE;
8805
8806                                 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
8807                                         (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
8808                                         /* Prevent inlining of methods that call wrappers */
8809                                         INLINE_FAILURE ("wrapper call");
8810                                         cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
8811                                         always = TRUE;
8812                                 }
8813
8814                                 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
8815                                 if (costs) {
8816                                         cfg->real_offset += 5;
8817
8818                                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8819                                                 /* *sp is already set by inline_method */
8820                                                 sp++;
8821                                                 push_res = FALSE;
8822                                         }
8823
8824                                         inline_costs += costs;
8825
8826                                         goto call_end;
8827                                 }
8828                         }
8829
8830                         /* Tail recursion elimination */
8831                         if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
8832                                 gboolean has_vtargs = FALSE;
8833                                 int i;
8834
8835                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
8836                                 INLINE_FAILURE ("tail call");
8837
8838                                 /* keep it simple */
8839                                 for (i =  fsig->param_count - 1; i >= 0; i--) {
8840                                         if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i])) 
8841                                                 has_vtargs = TRUE;
8842                                 }
8843
8844                                 if (!has_vtargs) {
8845                                         if (need_seq_point) {
8846                                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
8847                                                 need_seq_point = FALSE;
8848                                         }
8849                                         for (i = 0; i < n; ++i)
8850                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
8851
8852                                         mini_profiler_emit_tail_call (cfg, cmethod);
8853
8854                                         MONO_INST_NEW (cfg, ins, OP_BR);
8855                                         MONO_ADD_INS (cfg->cbb, ins);
8856                                         tblock = start_bblock->out_bb [0];
8857                                         link_bblock (cfg, cfg->cbb, tblock);
8858                                         ins->inst_target_bb = tblock;
8859                                         start_new_bblock = 1;
8860
8861                                         /* skip the CEE_RET, too */
8862                                         if (ip_in_bb (cfg, cfg->cbb, ip + 5))
8863                                                 skip_ret = TRUE;
8864                                         push_res = FALSE;
8865                                         goto call_end;
8866                                 }
8867                         }
8868
8869                         inline_costs += 10 * num_calls++;
8870
8871                         /*
8872                          * Synchronized wrappers.
8873                          * Its hard to determine where to replace a method with its synchronized
8874                          * wrapper without causing an infinite recursion. The current solution is
8875                          * to add the synchronized wrapper in the trampolines, and to
8876                          * change the called method to a dummy wrapper, and resolve that wrapper
8877                          * to the real method in mono_jit_compile_method ().
8878                          */
8879                         if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
8880                                 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
8881                                 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
8882                                         cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
8883                         }
8884
8885                         /*
8886                          * Making generic calls out of gsharedvt methods.
8887                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8888                          * patching gshared method addresses into a gsharedvt method.
8889                          */
8890                         if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || mono_class_is_ginst (cmethod->klass)) &&
8891                                 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
8892                                 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
8893                                 MonoRgctxInfoType info_type;
8894
8895                                 if (virtual_) {
8896                                         //if (mono_class_is_interface (cmethod->klass))
8897                                                 //GSHAREDVT_FAILURE (*ip);
8898                                         // disable for possible remoting calls
8899                                         if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
8900                                                 GSHAREDVT_FAILURE (*ip);
8901                                         if (fsig->generic_param_count) {
8902                                                 /* virtual generic call */
8903                                                 g_assert (!imt_arg);
8904                                                 /* Same as the virtual generic case above */
8905                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
8906                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
8907                                                 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
8908                                                 vtable_arg = NULL;
8909                                         } else if (mono_class_is_interface (cmethod->klass) && !imt_arg) {
8910                                                 /* This can happen when we call a fully instantiated iface method */
8911                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
8912                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
8913                                                 vtable_arg = NULL;
8914                                         }
8915                                 }
8916
8917                                 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
8918                                         keep_this_alive = sp [0];
8919
8920                                 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
8921                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
8922                                 else
8923                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
8924                                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
8925
8926                                 if (cfg->llvm_only) {
8927                                         // FIXME: Avoid initializing vtable_arg
8928                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
8929                                 } else {
8930                                         ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
8931                                 }
8932                                 goto call_end;
8933                         }
8934
8935                         /* Generic sharing */
8936
8937                         /*
8938                          * Use this if the callee is gsharedvt sharable too, since
8939                          * at runtime we might find an instantiation so the call cannot
8940                          * be patched (the 'no_patch' code path in mini-trampolines.c).
8941                          */
8942                         if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
8943                                 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
8944                                  !mono_class_generic_sharing_enabled (cmethod->klass)) &&
8945                                 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
8946                                  !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
8947                                 INLINE_FAILURE ("gshared");
8948
8949                                 g_assert (cfg->gshared && cmethod);
8950                                 g_assert (!addr);
8951
8952                                 /*
8953                                  * We are compiling a call to a
8954                                  * generic method from shared code,
8955                                  * which means that we have to look up
8956                                  * the method in the rgctx and do an
8957                                  * indirect call.
8958                                  */
8959                                 if (fsig->hasthis)
8960                                         MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
8961
8962                                 if (cfg->llvm_only) {
8963                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
8964                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
8965                                         else
8966                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8967                                         // FIXME: Avoid initializing imt_arg/vtable_arg
8968                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
8969                                 } else {
8970                                         addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8971                                         ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
8972                                 }
8973                                 goto call_end;
8974                         }
8975
8976                         /* Direct calls to icalls */
8977                         if (direct_icall) {
8978                                 MonoMethod *wrapper;
8979                                 int costs;
8980
8981                                 /* Inline the wrapper */
8982                                 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8983
8984                                 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
8985                                 g_assert (costs > 0);
8986                                 cfg->real_offset += 5;
8987
8988                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8989                                         /* *sp is already set by inline_method */
8990                                         sp++;
8991                                         push_res = FALSE;
8992                                 }
8993
8994                                 inline_costs += costs;
8995
8996                                 goto call_end;
8997                         }
8998                                         
8999                         /* Array methods */
9000                         if (array_rank) {
9001                                 MonoInst *addr;
9002
9003                                 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */ 
9004                                         MonoInst *val = sp [fsig->param_count];
9005
9006                                         if (val->type == STACK_OBJ) {
9007                                                 MonoInst *iargs [2];
9008
9009                                                 iargs [0] = sp [0];
9010                                                 iargs [1] = val;
9011                                                 
9012                                                 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9013                                         }
9014                                         
9015                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9016                                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9017                                         if (cfg->gen_write_barriers && val->type == STACK_OBJ && !MONO_INS_IS_PCONST_NULL (val))
9018                                                 mini_emit_write_barrier (cfg, addr, val);
9019                                         if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9020                                                 GSHAREDVT_FAILURE (*ip);
9021                                 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9022                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9023
9024                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9025                                 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9026                                         if (!cmethod->klass->element_class->valuetype && !readonly)
9027                                                 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9028                                         CHECK_TYPELOAD (cmethod->klass);
9029                                         
9030                                         readonly = FALSE;
9031                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9032                                         ins = addr;
9033                                 } else {
9034                                         g_assert_not_reached ();
9035                                 }
9036
9037                                 emit_widen = FALSE;
9038                                 goto call_end;
9039                         }
9040
9041                         ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9042                         if (ins)
9043                                 goto call_end;
9044
9045                         /* Tail prefix / tail call optimization */
9046
9047                         /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9048                         /* FIXME: runtime generic context pointer for jumps? */
9049                         /* FIXME: handle this for generic sharing eventually */
9050                         if ((ins_flag & MONO_INST_TAILCALL) &&
9051                                 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9052                                 supported_tail_call = TRUE;
9053
9054                         if (supported_tail_call) {
9055                                 MonoCallInst *call;
9056
9057                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9058                                 INLINE_FAILURE ("tail call");
9059
9060                                 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9061
9062                                 if (cfg->backend->have_op_tail_call) {
9063                                         /* Handle tail calls similarly to normal calls */
9064                                         tail_call = TRUE;
9065                                 } else {
9066                                         mini_profiler_emit_tail_call (cfg, cmethod);
9067
9068                                         MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9069                                         call->tail_call = TRUE;
9070                                         call->method = cmethod;
9071                                         call->signature = mono_method_signature (cmethod);
9072
9073                                         /*
9074                                          * We implement tail calls by storing the actual arguments into the 
9075                                          * argument variables, then emitting a CEE_JMP.
9076                                          */
9077                                         for (i = 0; i < n; ++i) {
9078                                                 /* Prevent argument from being register allocated */
9079                                                 arg_array [i]->flags |= MONO_INST_VOLATILE;
9080                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9081                                         }
9082                                         ins = (MonoInst*)call;
9083                                         ins->inst_p0 = cmethod;
9084                                         ins->inst_p1 = arg_array [0];
9085                                         MONO_ADD_INS (cfg->cbb, ins);
9086                                         link_bblock (cfg, cfg->cbb, end_bblock);
9087                                         start_new_bblock = 1;
9088
9089                                         // FIXME: Eliminate unreachable epilogs
9090
9091                                         /*
9092                                          * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9093                                          * only reachable from this call.
9094                                          */
9095                                         GET_BBLOCK (cfg, tblock, ip + 5);
9096                                         if (tblock == cfg->cbb || tblock->in_count == 0)
9097                                                 skip_ret = TRUE;
9098                                         push_res = FALSE;
9099
9100                                         goto call_end;
9101                                 }
9102                         }
9103
9104                         /*
9105                          * Virtual calls in llvm-only mode.
9106                          */
9107                         if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9108                                 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9109                                 goto call_end;
9110                         }
9111
9112                         /* Common call */
9113                         if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) && !(cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
9114                                 INLINE_FAILURE ("call");
9115                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9116                                                                                           imt_arg, vtable_arg);
9117
9118                         if (tail_call && !cfg->llvm_only) {
9119                                 link_bblock (cfg, cfg->cbb, end_bblock);
9120                                 start_new_bblock = 1;
9121
9122                                 // FIXME: Eliminate unreachable epilogs
9123
9124                                 /*
9125                                  * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9126                                  * only reachable from this call.
9127                                  */
9128                                 GET_BBLOCK (cfg, tblock, ip + 5);
9129                                 if (tblock == cfg->cbb || tblock->in_count == 0)
9130                                         skip_ret = TRUE;
9131                                 push_res = FALSE;
9132                         }
9133
9134                         call_end:
9135
9136                         /* End of call, INS should contain the result of the call, if any */
9137
9138                         if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9139                                 g_assert (ins);
9140                                 if (emit_widen)
9141                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9142                                 else
9143                                         *sp++ = ins;
9144                         }
9145
9146                         if (keep_this_alive) {
9147                                 MonoInst *dummy_use;
9148
9149                                 /* See mono_emit_method_call_full () */
9150                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
9151                         }
9152
9153                         if (cfg->llvm_only && cmethod && method_needs_stack_walk (cfg, cmethod)) {
9154                                 /*
9155                                  * Clang can convert these calls to tail calls which screw up the stack
9156                                  * walk. This happens even when the -fno-optimize-sibling-calls
9157                                  * option is passed to clang.
9158                                  * Work around this by emitting a dummy call.
9159                                  */
9160                                 mono_emit_jit_icall (cfg, mono_dummy_jit_icall, NULL);
9161                         }
9162
9163                         CHECK_CFG_EXCEPTION;
9164
9165                         ip += 5;
9166                         if (skip_ret) {
9167                                 g_assert (*ip == CEE_RET);
9168                                 ip += 1;
9169                         }
9170                         ins_flag = 0;
9171                         constrained_class = NULL;
9172                         if (need_seq_point)
9173                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9174                         break;
9175                 }
9176                 case CEE_RET:
9177                         mini_profiler_emit_leave (cfg, sig->ret->type != MONO_TYPE_VOID ? sp [-1] : NULL);
9178
9179                         if (cfg->method != method) {
9180                                 /* return from inlined method */
9181                                 /* 
9182                                  * If in_count == 0, that means the ret is unreachable due to
9183                                  * being preceeded by a throw. In that case, inline_method () will
9184                                  * handle setting the return value 
9185                                  * (test case: test_0_inline_throw ()).
9186                                  */
9187                                 if (return_var && cfg->cbb->in_count) {
9188                                         MonoType *ret_type = mono_method_signature (method)->ret;
9189
9190                                         MonoInst *store;
9191                                         CHECK_STACK (1);
9192                                         --sp;
9193
9194                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9195                                                 UNVERIFIED;
9196
9197                                         //g_assert (returnvar != -1);
9198                                         EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
9199                                         cfg->ret_var_set = TRUE;
9200                                 } 
9201                         } else {
9202                                 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
9203                                         emit_pop_lmf (cfg);
9204
9205                                 if (cfg->ret) {
9206                                         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
9207
9208                                         if (seq_points && !sym_seq_points) {
9209                                                 /* 
9210                                                  * Place a seq point here too even through the IL stack is not
9211                                                  * empty, so a step over on
9212                                                  * call <FOO>
9213                                                  * ret
9214                                                  * will work correctly.
9215                                                  */
9216                                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
9217                                                 MONO_ADD_INS (cfg->cbb, ins);
9218                                         }
9219
9220                                         g_assert (!return_var);
9221                                         CHECK_STACK (1);
9222                                         --sp;
9223
9224                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9225                                                 UNVERIFIED;
9226
9227                                         emit_setret (cfg, *sp);
9228                                 }
9229                         }
9230                         if (sp != stack_start)
9231                                 UNVERIFIED;
9232                         MONO_INST_NEW (cfg, ins, OP_BR);
9233                         ip++;
9234                         ins->inst_target_bb = end_bblock;
9235                         MONO_ADD_INS (cfg->cbb, ins);
9236                         link_bblock (cfg, cfg->cbb, end_bblock);
9237                         start_new_bblock = 1;
9238                         break;
9239                 case CEE_BR_S:
9240                         CHECK_OPSIZE (2);
9241                         MONO_INST_NEW (cfg, ins, OP_BR);
9242                         ip++;
9243                         target = ip + 1 + (signed char)(*ip);
9244                         ++ip;
9245                         GET_BBLOCK (cfg, tblock, target);
9246                         link_bblock (cfg, cfg->cbb, tblock);
9247                         ins->inst_target_bb = tblock;
9248                         if (sp != stack_start) {
9249                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9250                                 sp = stack_start;
9251                                 CHECK_UNVERIFIABLE (cfg);
9252                         }
9253                         MONO_ADD_INS (cfg->cbb, ins);
9254                         start_new_bblock = 1;
9255                         inline_costs += BRANCH_COST;
9256                         break;
9257                 case CEE_BEQ_S:
9258                 case CEE_BGE_S:
9259                 case CEE_BGT_S:
9260                 case CEE_BLE_S:
9261                 case CEE_BLT_S:
9262                 case CEE_BNE_UN_S:
9263                 case CEE_BGE_UN_S:
9264                 case CEE_BGT_UN_S:
9265                 case CEE_BLE_UN_S:
9266                 case CEE_BLT_UN_S:
9267                         CHECK_OPSIZE (2);
9268                         CHECK_STACK (2);
9269                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
9270                         ip++;
9271                         target = ip + 1 + *(signed char*)ip;
9272                         ip++;
9273
9274                         ADD_BINCOND (NULL);
9275
9276                         sp = stack_start;
9277                         inline_costs += BRANCH_COST;
9278                         break;
9279                 case CEE_BR:
9280                         CHECK_OPSIZE (5);
9281                         MONO_INST_NEW (cfg, ins, OP_BR);
9282                         ip++;
9283
9284                         target = ip + 4 + (gint32)read32(ip);
9285                         ip += 4;
9286                         GET_BBLOCK (cfg, tblock, target);
9287                         link_bblock (cfg, cfg->cbb, tblock);
9288                         ins->inst_target_bb = tblock;
9289                         if (sp != stack_start) {
9290                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9291                                 sp = stack_start;
9292                                 CHECK_UNVERIFIABLE (cfg);
9293                         }
9294
9295                         MONO_ADD_INS (cfg->cbb, ins);
9296
9297                         start_new_bblock = 1;
9298                         inline_costs += BRANCH_COST;
9299                         break;
9300                 case CEE_BRFALSE_S:
9301                 case CEE_BRTRUE_S:
9302                 case CEE_BRFALSE:
9303                 case CEE_BRTRUE: {
9304                         MonoInst *cmp;
9305                         gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
9306                         gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
9307                         guint32 opsize = is_short ? 1 : 4;
9308
9309                         CHECK_OPSIZE (opsize);
9310                         CHECK_STACK (1);
9311                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
9312                                 UNVERIFIED;
9313                         ip ++;
9314                         target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
9315                         ip += opsize;
9316
9317                         sp--;
9318
9319                         GET_BBLOCK (cfg, tblock, target);
9320                         link_bblock (cfg, cfg->cbb, tblock);
9321                         GET_BBLOCK (cfg, tblock, ip);
9322                         link_bblock (cfg, cfg->cbb, tblock);
9323
9324                         if (sp != stack_start) {
9325                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9326                                 CHECK_UNVERIFIABLE (cfg);
9327                         }
9328
9329                         MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
9330                         cmp->sreg1 = sp [0]->dreg;
9331                         type_from_op (cfg, cmp, sp [0], NULL);
9332                         CHECK_TYPE (cmp);
9333
9334 #if SIZEOF_REGISTER == 4
9335                         if (cmp->opcode == OP_LCOMPARE_IMM) {
9336                                 /* Convert it to OP_LCOMPARE */
9337                                 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9338                                 ins->type = STACK_I8;
9339                                 ins->dreg = alloc_dreg (cfg, STACK_I8);
9340                                 ins->inst_l = 0;
9341                                 MONO_ADD_INS (cfg->cbb, ins);
9342                                 cmp->opcode = OP_LCOMPARE;
9343                                 cmp->sreg2 = ins->dreg;
9344                         }
9345 #endif
9346                         MONO_ADD_INS (cfg->cbb, cmp);
9347
9348                         MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
9349                         type_from_op (cfg, ins, sp [0], NULL);
9350                         MONO_ADD_INS (cfg->cbb, ins);
9351                         ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
9352                         GET_BBLOCK (cfg, tblock, target);
9353                         ins->inst_true_bb = tblock;
9354                         GET_BBLOCK (cfg, tblock, ip);
9355                         ins->inst_false_bb = tblock;
9356                         start_new_bblock = 2;
9357
9358                         sp = stack_start;
9359                         inline_costs += BRANCH_COST;
9360                         break;
9361                 }
9362                 case CEE_BEQ:
9363                 case CEE_BGE:
9364                 case CEE_BGT:
9365                 case CEE_BLE:
9366                 case CEE_BLT:
9367                 case CEE_BNE_UN:
9368                 case CEE_BGE_UN:
9369                 case CEE_BGT_UN:
9370                 case CEE_BLE_UN:
9371                 case CEE_BLT_UN:
9372                         CHECK_OPSIZE (5);
9373                         CHECK_STACK (2);
9374                         MONO_INST_NEW (cfg, ins, *ip);
9375                         ip++;
9376                         target = ip + 4 + (gint32)read32(ip);
9377                         ip += 4;
9378
9379                         ADD_BINCOND (NULL);
9380
9381                         sp = stack_start;
9382                         inline_costs += BRANCH_COST;
9383                         break;
9384                 case CEE_SWITCH: {
9385                         MonoInst *src1;
9386                         MonoBasicBlock **targets;
9387                         MonoBasicBlock *default_bblock;
9388                         MonoJumpInfoBBTable *table;
9389                         int offset_reg = alloc_preg (cfg);
9390                         int target_reg = alloc_preg (cfg);
9391                         int table_reg = alloc_preg (cfg);
9392                         int sum_reg = alloc_preg (cfg);
9393                         gboolean use_op_switch;
9394
9395                         CHECK_OPSIZE (5);
9396                         CHECK_STACK (1);
9397                         n = read32 (ip + 1);
9398                         --sp;
9399                         src1 = sp [0];
9400                         if ((src1->type != STACK_I4) && (src1->type != STACK_PTR)) 
9401                                 UNVERIFIED;
9402
9403                         ip += 5;
9404                         CHECK_OPSIZE (n * sizeof (guint32));
9405                         target = ip + n * sizeof (guint32);
9406
9407                         GET_BBLOCK (cfg, default_bblock, target);
9408                         default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
9409
9410                         targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
9411                         for (i = 0; i < n; ++i) {
9412                                 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
9413                                 targets [i] = tblock;
9414                                 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
9415                                 ip += 4;
9416                         }
9417
9418                         if (sp != stack_start) {
9419                                 /* 
9420                                  * Link the current bb with the targets as well, so handle_stack_args
9421                                  * will set their in_stack correctly.
9422                                  */
9423                                 link_bblock (cfg, cfg->cbb, default_bblock);
9424                                 for (i = 0; i < n; ++i)
9425                                         link_bblock (cfg, cfg->cbb, targets [i]);
9426
9427                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9428                                 sp = stack_start;
9429                                 CHECK_UNVERIFIABLE (cfg);
9430
9431                                 /* Undo the links */
9432                                 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
9433                                 for (i = 0; i < n; ++i)
9434                                         mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
9435                         }
9436
9437                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
9438                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
9439
9440                         for (i = 0; i < n; ++i)
9441                                 link_bblock (cfg, cfg->cbb, targets [i]);
9442
9443                         table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
9444                         table->table = targets;
9445                         table->table_size = n;
9446
9447                         use_op_switch = FALSE;
9448 #ifdef TARGET_ARM
9449                         /* ARM implements SWITCH statements differently */
9450                         /* FIXME: Make it use the generic implementation */
9451                         if (!cfg->compile_aot)
9452                                 use_op_switch = TRUE;
9453 #endif
9454
9455                         if (COMPILE_LLVM (cfg))
9456                                 use_op_switch = TRUE;
9457
9458                         cfg->cbb->has_jump_table = 1;
9459
9460                         if (use_op_switch) {
9461                                 MONO_INST_NEW (cfg, ins, OP_SWITCH);
9462                                 ins->sreg1 = src1->dreg;
9463                                 ins->inst_p0 = table;
9464                                 ins->inst_many_bb = targets;
9465                                 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
9466                                 MONO_ADD_INS (cfg->cbb, ins);
9467                         } else {
9468                                 if (sizeof (gpointer) == 8)
9469                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
9470                                 else
9471                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
9472
9473 #if SIZEOF_REGISTER == 8
9474                                 /* The upper word might not be zero, and we add it to a 64 bit address later */
9475                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
9476 #endif
9477
9478                                 if (cfg->compile_aot) {
9479                                         MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
9480                                 } else {
9481                                         MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
9482                                         ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
9483                                         ins->inst_p0 = table;
9484                                         ins->dreg = table_reg;
9485                                         MONO_ADD_INS (cfg->cbb, ins);
9486                                 }
9487
9488                                 /* FIXME: Use load_memindex */
9489                                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
9490                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
9491                                 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
9492                         }
9493                         start_new_bblock = 1;
9494                         inline_costs += (BRANCH_COST * 2);
9495                         break;
9496                 }
9497                 case CEE_LDIND_I1:
9498                 case CEE_LDIND_U1:
9499                 case CEE_LDIND_I2:
9500                 case CEE_LDIND_U2:
9501                 case CEE_LDIND_I4:
9502                 case CEE_LDIND_U4:
9503                 case CEE_LDIND_I8:
9504                 case CEE_LDIND_I:
9505                 case CEE_LDIND_R4:
9506                 case CEE_LDIND_R8:
9507                 case CEE_LDIND_REF:
9508                         CHECK_STACK (1);
9509                         --sp;
9510
9511                         ins = mini_emit_memory_load (cfg, &ldind_to_type (*ip)->byval_arg, sp [0], 0, ins_flag);
9512                         *sp++ = ins;
9513                         ins_flag = 0;
9514                         ++ip;
9515                         break;
9516                 case CEE_STIND_REF:
9517                 case CEE_STIND_I1:
9518                 case CEE_STIND_I2:
9519                 case CEE_STIND_I4:
9520                 case CEE_STIND_I8:
9521                 case CEE_STIND_R4:
9522                 case CEE_STIND_R8:
9523                 case CEE_STIND_I:
9524                         CHECK_STACK (2);
9525                         sp -= 2;
9526
9527                         if (ins_flag & MONO_INST_VOLATILE) {
9528                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
9529                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
9530                         }
9531
9532                         NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
9533                         ins->flags |= ins_flag;
9534                         ins_flag = 0;
9535
9536                         MONO_ADD_INS (cfg->cbb, ins);
9537
9538                         if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1]))
9539                                 mini_emit_write_barrier (cfg, sp [0], sp [1]);
9540
9541                         inline_costs += 1;
9542                         ++ip;
9543                         break;
9544
9545                 case CEE_MUL:
9546                         CHECK_STACK (2);
9547
9548                         MONO_INST_NEW (cfg, ins, (*ip));
9549                         sp -= 2;
9550                         ins->sreg1 = sp [0]->dreg;
9551                         ins->sreg2 = sp [1]->dreg;
9552                         type_from_op (cfg, ins, sp [0], sp [1]);
9553                         CHECK_TYPE (ins);
9554                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9555
9556                         /* Use the immediate opcodes if possible */
9557                         if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
9558                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9559                                 if (imm_opcode != -1) {
9560                                         ins->opcode = imm_opcode;
9561                                         ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
9562                                         ins->sreg2 = -1;
9563
9564                                         NULLIFY_INS (sp [1]);
9565                                 }
9566                         }
9567
9568                         MONO_ADD_INS ((cfg)->cbb, (ins));
9569
9570                         *sp++ = mono_decompose_opcode (cfg, ins);
9571                         ip++;
9572                         break;
9573                 case CEE_ADD:
9574                 case CEE_SUB:
9575                 case CEE_DIV:
9576                 case CEE_DIV_UN:
9577                 case CEE_REM:
9578                 case CEE_REM_UN:
9579                 case CEE_AND:
9580                 case CEE_OR:
9581                 case CEE_XOR:
9582                 case CEE_SHL:
9583                 case CEE_SHR:
9584                 case CEE_SHR_UN:
9585                         CHECK_STACK (2);
9586
9587                         MONO_INST_NEW (cfg, ins, (*ip));
9588                         sp -= 2;
9589                         ins->sreg1 = sp [0]->dreg;
9590                         ins->sreg2 = sp [1]->dreg;
9591                         type_from_op (cfg, ins, sp [0], sp [1]);
9592                         CHECK_TYPE (ins);
9593                         add_widen_op (cfg, ins, &sp [0], &sp [1]);
9594                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9595
9596                         /* FIXME: Pass opcode to is_inst_imm */
9597
9598                         /* Use the immediate opcodes if possible */
9599                         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)) {
9600                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9601                                 if (imm_opcode != -1) {
9602                                         ins->opcode = imm_opcode;
9603                                         if (sp [1]->opcode == OP_I8CONST) {
9604 #if SIZEOF_REGISTER == 8
9605                                                 ins->inst_imm = sp [1]->inst_l;
9606 #else
9607                                                 ins->inst_ls_word = sp [1]->inst_ls_word;
9608                                                 ins->inst_ms_word = sp [1]->inst_ms_word;
9609 #endif
9610                                         }
9611                                         else
9612                                                 ins->inst_imm = (gssize)(sp [1]->inst_c0);
9613                                         ins->sreg2 = -1;
9614
9615                                         /* Might be followed by an instruction added by add_widen_op */
9616                                         if (sp [1]->next == NULL)
9617                                                 NULLIFY_INS (sp [1]);
9618                                 }
9619                         }
9620                         MONO_ADD_INS ((cfg)->cbb, (ins));
9621
9622                         *sp++ = mono_decompose_opcode (cfg, ins);
9623                         ip++;
9624                         break;
9625                 case CEE_NEG:
9626                 case CEE_NOT:
9627                 case CEE_CONV_I1:
9628                 case CEE_CONV_I2:
9629                 case CEE_CONV_I4:
9630                 case CEE_CONV_R4:
9631                 case CEE_CONV_R8:
9632                 case CEE_CONV_U4:
9633                 case CEE_CONV_I8:
9634                 case CEE_CONV_U8:
9635                 case CEE_CONV_OVF_I8:
9636                 case CEE_CONV_OVF_U8:
9637                 case CEE_CONV_R_UN:
9638                         CHECK_STACK (1);
9639
9640                         /* Special case this earlier so we have long constants in the IR */
9641                         if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
9642                                 int data = sp [-1]->inst_c0;
9643                                 sp [-1]->opcode = OP_I8CONST;
9644                                 sp [-1]->type = STACK_I8;
9645 #if SIZEOF_REGISTER == 8
9646                                 if ((*ip) == CEE_CONV_U8)
9647                                         sp [-1]->inst_c0 = (guint32)data;
9648                                 else
9649                                         sp [-1]->inst_c0 = data;
9650 #else
9651                                 sp [-1]->inst_ls_word = data;
9652                                 if ((*ip) == CEE_CONV_U8)
9653                                         sp [-1]->inst_ms_word = 0;
9654                                 else
9655                                         sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
9656 #endif
9657                                 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
9658                         }
9659                         else {
9660                                 ADD_UNOP (*ip);
9661                         }
9662                         ip++;
9663                         break;
9664                 case CEE_CONV_OVF_I4:
9665                 case CEE_CONV_OVF_I1:
9666                 case CEE_CONV_OVF_I2:
9667                 case CEE_CONV_OVF_I:
9668                 case CEE_CONV_OVF_U:
9669                         CHECK_STACK (1);
9670
9671                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9672                                 ADD_UNOP (CEE_CONV_OVF_I8);
9673                                 ADD_UNOP (*ip);
9674                         } else {
9675                                 ADD_UNOP (*ip);
9676                         }
9677                         ip++;
9678                         break;
9679                 case CEE_CONV_OVF_U1:
9680                 case CEE_CONV_OVF_U2:
9681                 case CEE_CONV_OVF_U4:
9682                         CHECK_STACK (1);
9683
9684                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9685                                 ADD_UNOP (CEE_CONV_OVF_U8);
9686                                 ADD_UNOP (*ip);
9687                         } else {
9688                                 ADD_UNOP (*ip);
9689                         }
9690                         ip++;
9691                         break;
9692                 case CEE_CONV_OVF_I1_UN:
9693                 case CEE_CONV_OVF_I2_UN:
9694                 case CEE_CONV_OVF_I4_UN:
9695                 case CEE_CONV_OVF_I8_UN:
9696                 case CEE_CONV_OVF_U1_UN:
9697                 case CEE_CONV_OVF_U2_UN:
9698                 case CEE_CONV_OVF_U4_UN:
9699                 case CEE_CONV_OVF_U8_UN:
9700                 case CEE_CONV_OVF_I_UN:
9701                 case CEE_CONV_OVF_U_UN:
9702                 case CEE_CONV_U2:
9703                 case CEE_CONV_U1:
9704                 case CEE_CONV_I:
9705                 case CEE_CONV_U:
9706                         CHECK_STACK (1);
9707                         ADD_UNOP (*ip);
9708                         CHECK_CFG_EXCEPTION;
9709                         ip++;
9710                         break;
9711                 case CEE_ADD_OVF:
9712                 case CEE_ADD_OVF_UN:
9713                 case CEE_MUL_OVF:
9714                 case CEE_MUL_OVF_UN:
9715                 case CEE_SUB_OVF:
9716                 case CEE_SUB_OVF_UN:
9717                         CHECK_STACK (2);
9718                         ADD_BINOP (*ip);
9719                         ip++;
9720                         break;
9721                 case CEE_CPOBJ:
9722                         GSHAREDVT_FAILURE (*ip);
9723                         CHECK_OPSIZE (5);
9724                         CHECK_STACK (2);
9725                         token = read32 (ip + 1);
9726                         klass = mini_get_class (method, token, generic_context);
9727                         CHECK_TYPELOAD (klass);
9728                         sp -= 2;
9729                         mini_emit_memory_copy (cfg, sp [0], sp [1], klass, FALSE, ins_flag);
9730                         ins_flag = 0;
9731                         ip += 5;
9732                         break;
9733                 case CEE_LDOBJ: {
9734                         int loc_index = -1;
9735                         int stloc_len = 0;
9736
9737                         CHECK_OPSIZE (5);
9738                         CHECK_STACK (1);
9739                         --sp;
9740                         token = read32 (ip + 1);
9741                         klass = mini_get_class (method, token, generic_context);
9742                         CHECK_TYPELOAD (klass);
9743
9744                         /* Optimize the common ldobj+stloc combination */
9745                         switch (ip [5]) {
9746                         case CEE_STLOC_S:
9747                                 loc_index = ip [6];
9748                                 stloc_len = 2;
9749                                 break;
9750                         case CEE_STLOC_0:
9751                         case CEE_STLOC_1:
9752                         case CEE_STLOC_2:
9753                         case CEE_STLOC_3:
9754                                 loc_index = ip [5] - CEE_STLOC_0;
9755                                 stloc_len = 1;
9756                                 break;
9757                         default:
9758                                 break;
9759                         }
9760
9761                         if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
9762                                 CHECK_LOCAL (loc_index);
9763
9764                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
9765                                 ins->dreg = cfg->locals [loc_index]->dreg;
9766                                 ins->flags |= ins_flag;
9767                                 ip += 5;
9768                                 ip += stloc_len;
9769                                 if (ins_flag & MONO_INST_VOLATILE) {
9770                                         /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9771                                         mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9772                                 }
9773                                 ins_flag = 0;
9774                                 break;
9775                         }
9776
9777                         /* Optimize the ldobj+stobj combination */
9778                         if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 5) && read32 (ip + 6) == token)) {
9779                                 CHECK_STACK (1);
9780
9781                                 sp --;
9782
9783                                 mini_emit_memory_copy (cfg, sp [0], sp [1], klass, FALSE, ins_flag);
9784
9785                                 ip += 5 + 5;
9786                                 ins_flag = 0;
9787                                 break;
9788                         }
9789
9790                         ins = mini_emit_memory_load (cfg, &klass->byval_arg, sp [0], 0, ins_flag);
9791                         *sp++ = ins;
9792
9793                         ip += 5;
9794                         ins_flag = 0;
9795                         inline_costs += 1;
9796                         break;
9797                 }
9798                 case CEE_LDSTR:
9799                         CHECK_STACK_OVF (1);
9800                         CHECK_OPSIZE (5);
9801                         n = read32 (ip + 1);
9802
9803                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
9804                                 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
9805                                 ins->type = STACK_OBJ;
9806                                 *sp = ins;
9807                         }
9808                         else if (method->wrapper_type != MONO_WRAPPER_NONE) {
9809                                 MonoInst *iargs [1];
9810                                 char *str = (char *)mono_method_get_wrapper_data (method, n);
9811
9812                                 if (cfg->compile_aot)
9813                                         EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
9814                                 else
9815                                         EMIT_NEW_PCONST (cfg, iargs [0], str);
9816                                 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
9817                         } else {
9818                                 if (cfg->opt & MONO_OPT_SHARED) {
9819                                         MonoInst *iargs [3];
9820
9821                                         if (cfg->compile_aot) {
9822                                                 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
9823                                         }
9824                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
9825                                         EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
9826                                         EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
9827                                         *sp = mono_emit_jit_icall (cfg, ves_icall_mono_ldstr, iargs);
9828                                         mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
9829                                         CHECK_CFG_ERROR;
9830                                 } else {
9831                                         if (cfg->cbb->out_of_line) {
9832                                                 MonoInst *iargs [2];
9833
9834                                                 if (image == mono_defaults.corlib) {
9835                                                         /* 
9836                                                          * Avoid relocations in AOT and save some space by using a 
9837                                                          * version of helper_ldstr specialized to mscorlib.
9838                                                          */
9839                                                         EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
9840                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
9841                                                 } else {
9842                                                         /* Avoid creating the string object */
9843                                                         EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
9844                                                         EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
9845                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
9846                                                 }
9847                                         } 
9848                                         else
9849                                         if (cfg->compile_aot) {
9850                                                 NEW_LDSTRCONST (cfg, ins, image, n);
9851                                                 *sp = ins;
9852                                                 MONO_ADD_INS (cfg->cbb, ins);
9853                                         } 
9854                                         else {
9855                                                 NEW_PCONST (cfg, ins, NULL);
9856                                                 ins->type = STACK_OBJ;
9857                                                 ins->inst_p0 = mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
9858                                                 CHECK_CFG_ERROR;
9859                                                 
9860                                                 if (!ins->inst_p0)
9861                                                         OUT_OF_MEMORY_FAILURE;
9862
9863                                                 *sp = ins;
9864                                                 MONO_ADD_INS (cfg->cbb, ins);
9865                                         }
9866                                 }
9867                         }
9868
9869                         sp++;
9870                         ip += 5;
9871                         break;
9872                 case CEE_NEWOBJ: {
9873                         MonoInst *iargs [2];
9874                         MonoMethodSignature *fsig;
9875                         MonoInst this_ins;
9876                         MonoInst *alloc;
9877                         MonoInst *vtable_arg = NULL;
9878
9879                         CHECK_OPSIZE (5);
9880                         token = read32 (ip + 1);
9881                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9882                         CHECK_CFG_ERROR;
9883
9884                         fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
9885                         CHECK_CFG_ERROR;
9886
9887                         mono_save_token_info (cfg, image, token, cmethod);
9888
9889                         if (!mono_class_init (cmethod->klass))
9890                                 TYPE_LOAD_ERROR (cmethod->klass);
9891
9892                         context_used = mini_method_check_context_used (cfg, cmethod);
9893                                         
9894                         if (!dont_verify && !cfg->skip_visibility) {
9895                                 MonoMethod *cil_method = cmethod;
9896                                 MonoMethod *target_method = cil_method;
9897
9898                                 if (method->is_inflated) {
9899                                         target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
9900                                         CHECK_CFG_ERROR;
9901                                 }
9902                                 
9903                                 if (!mono_method_can_access_method (method_definition, target_method) &&
9904                                         !mono_method_can_access_method (method, cil_method))
9905                                         emit_method_access_failure (cfg, method, cil_method);
9906                         }
9907
9908                         if (mono_security_core_clr_enabled ())
9909                                 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
9910
9911                         if (cfg->gshared && cmethod && cmethod->klass != method->klass && mono_class_is_ginst (cmethod->klass) && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
9912                                 emit_class_init (cfg, cmethod->klass);
9913                                 CHECK_TYPELOAD (cmethod->klass);
9914                         }
9915
9916                         /*
9917                         if (cfg->gsharedvt) {
9918                                 if (mini_is_gsharedvt_variable_signature (sig))
9919                                         GSHAREDVT_FAILURE (*ip);
9920                         }
9921                         */
9922
9923                         n = fsig->param_count;
9924                         CHECK_STACK (n);
9925
9926                         /* 
9927                          * Generate smaller code for the common newobj <exception> instruction in
9928                          * argument checking code.
9929                          */
9930                         if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
9931                                 is_exception_class (cmethod->klass) && n <= 2 &&
9932                                 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) && 
9933                                 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
9934                                 MonoInst *iargs [3];
9935
9936                                 sp -= n;
9937
9938                                 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
9939                                 switch (n) {
9940                                 case 0:
9941                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
9942                                         break;
9943                                 case 1:
9944                                         iargs [1] = sp [0];
9945                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
9946                                         break;
9947                                 case 2:
9948                                         iargs [1] = sp [0];
9949                                         iargs [2] = sp [1];
9950                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
9951                                         break;
9952                                 default:
9953                                         g_assert_not_reached ();
9954                                 }
9955
9956                                 ip += 5;
9957                                 inline_costs += 5;
9958                                 break;
9959                         }
9960
9961                         /* move the args to allow room for 'this' in the first position */
9962                         while (n--) {
9963                                 --sp;
9964                                 sp [1] = sp [0];
9965                         }
9966
9967                         /* check_call_signature () requires sp[0] to be set */
9968                         this_ins.type = STACK_OBJ;
9969                         sp [0] = &this_ins;
9970                         if (check_call_signature (cfg, fsig, sp))
9971                                 UNVERIFIED;
9972
9973                         iargs [0] = NULL;
9974
9975                         if (mini_class_is_system_array (cmethod->klass)) {
9976                                 *sp = emit_get_rgctx_method (cfg, context_used,
9977                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
9978
9979                                 /* Avoid varargs in the common case */
9980                                 if (fsig->param_count == 1)
9981                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
9982                                 else if (fsig->param_count == 2)
9983                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
9984                                 else if (fsig->param_count == 3)
9985                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
9986                                 else if (fsig->param_count == 4)
9987                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
9988                                 else
9989                                         alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
9990                         } else if (cmethod->string_ctor) {
9991                                 g_assert (!context_used);
9992                                 g_assert (!vtable_arg);
9993                                 /* we simply pass a null pointer */
9994                                 EMIT_NEW_PCONST (cfg, *sp, NULL); 
9995                                 /* now call the string ctor */
9996                                 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
9997                         } else {
9998                                 if (cmethod->klass->valuetype) {
9999                                         iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10000                                         emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10001                                         EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10002
10003                                         alloc = NULL;
10004
10005                                         /* 
10006                                          * The code generated by mini_emit_virtual_call () expects
10007                                          * iargs [0] to be a boxed instance, but luckily the vcall
10008                                          * will be transformed into a normal call there.
10009                                          */
10010                                 } else if (context_used) {
10011                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10012                                         *sp = alloc;
10013                                 } else {
10014                                         MonoVTable *vtable = NULL;
10015
10016                                         if (!cfg->compile_aot)
10017                                                 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10018                                         CHECK_TYPELOAD (cmethod->klass);
10019
10020                                         /*
10021                                          * TypeInitializationExceptions thrown from the mono_runtime_class_init
10022                                          * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10023                                          * As a workaround, we call class cctors before allocating objects.
10024                                          */
10025                                         if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10026                                                 emit_class_init (cfg, cmethod->klass);
10027                                                 if (cfg->verbose_level > 2)
10028                                                         printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10029                                                 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10030                                         }
10031
10032                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10033                                         *sp = alloc;
10034                                 }
10035                                 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10036
10037                                 if (alloc)
10038                                         MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10039
10040                                 /* Now call the actual ctor */
10041                                 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10042                                 CHECK_CFG_EXCEPTION;
10043                         }
10044
10045                         if (alloc == NULL) {
10046                                 /* Valuetype */
10047                                 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10048                                 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10049                                 *sp++= ins;
10050                         } else {
10051                                 *sp++ = alloc;
10052                         }
10053                         
10054                         ip += 5;
10055                         inline_costs += 5;
10056                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10057                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10058                         break;
10059                 }
10060                 case CEE_CASTCLASS:
10061                 case CEE_ISINST: {
10062                         CHECK_STACK (1);
10063                         --sp;
10064                         CHECK_OPSIZE (5);
10065                         token = read32 (ip + 1);
10066                         klass = mini_get_class (method, token, generic_context);
10067                         CHECK_TYPELOAD (klass);
10068                         if (sp [0]->type != STACK_OBJ)
10069                                 UNVERIFIED;
10070
10071                         MONO_INST_NEW (cfg, ins, *ip == CEE_ISINST ? OP_ISINST : OP_CASTCLASS);
10072                         ins->dreg = alloc_preg (cfg);
10073                         ins->sreg1 = (*sp)->dreg;
10074                         ins->klass = klass;
10075                         ins->type = STACK_OBJ;
10076                         MONO_ADD_INS (cfg->cbb, ins);
10077
10078                         CHECK_CFG_EXCEPTION;
10079                         *sp++ = ins;
10080                         ip += 5;
10081
10082                         cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10083                         break;
10084                 }
10085                 case CEE_UNBOX_ANY: {
10086                         MonoInst *res, *addr;
10087
10088                         CHECK_STACK (1);
10089                         --sp;
10090                         CHECK_OPSIZE (5);
10091                         token = read32 (ip + 1);
10092                         klass = mini_get_class (method, token, generic_context);
10093                         CHECK_TYPELOAD (klass);
10094
10095                         mono_save_token_info (cfg, image, token, klass);
10096
10097                         context_used = mini_class_check_context_used (cfg, klass);
10098
10099                         if (mini_is_gsharedvt_klass (klass)) {
10100                                 res = handle_unbox_gsharedvt (cfg, klass, *sp);
10101                                 inline_costs += 2;
10102                         } else if (generic_class_is_reference_type (cfg, klass)) {
10103                                 if (MONO_INS_IS_PCONST_NULL (*sp)) {
10104                                         EMIT_NEW_PCONST (cfg, res, NULL);
10105                                         res->type = STACK_OBJ;
10106                                 } else {
10107                                         MONO_INST_NEW (cfg, res, OP_CASTCLASS);
10108                                         res->dreg = alloc_preg (cfg);
10109                                         res->sreg1 = (*sp)->dreg;
10110                                         res->klass = klass;
10111                                         res->type = STACK_OBJ;
10112                                         MONO_ADD_INS (cfg->cbb, res);
10113                                         cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10114                                 }
10115                         } else if (mono_class_is_nullable (klass)) {
10116                                 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
10117                         } else {
10118                                 addr = handle_unbox (cfg, klass, sp, context_used);
10119                                 /* LDOBJ */
10120                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10121                                 res = ins;
10122                                 inline_costs += 2;
10123                         }
10124
10125                         *sp ++ = res;
10126                         ip += 5;
10127                         break;
10128                 }
10129                 case CEE_BOX: {
10130                         MonoInst *val;
10131                         MonoClass *enum_class;
10132                         MonoMethod *has_flag;
10133
10134                         CHECK_STACK (1);
10135                         --sp;
10136                         val = *sp;
10137                         CHECK_OPSIZE (5);
10138                         token = read32 (ip + 1);
10139                         klass = mini_get_class (method, token, generic_context);
10140                         CHECK_TYPELOAD (klass);
10141
10142                         mono_save_token_info (cfg, image, token, klass);
10143
10144                         context_used = mini_class_check_context_used (cfg, klass);
10145
10146                         if (generic_class_is_reference_type (cfg, klass)) {
10147                                 *sp++ = val;
10148                                 ip += 5;
10149                                 break;
10150                         }
10151
10152                         if (klass == mono_defaults.void_class)
10153                                 UNVERIFIED;
10154                         if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
10155                                 UNVERIFIED;
10156                         /* frequent check in generic code: box (struct), brtrue */
10157
10158                         /*
10159                          * Look for:
10160                          *
10161                          *   <push int/long ptr>
10162                          *   <push int/long>
10163                          *   box MyFlags
10164                          *   constrained. MyFlags
10165                          *   callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
10166                          *
10167                          * If we find this sequence and the operand types on box and constrained
10168                          * are equal, we can emit a specialized instruction sequence instead of
10169                          * the very slow HasFlag () call.
10170                          */
10171                         if ((cfg->opt & MONO_OPT_INTRINS) &&
10172                             /* Cheap checks first. */
10173                             ip + 5 + 6 + 5 < end &&
10174                             ip [5] == CEE_PREFIX1 &&
10175                             ip [6] == CEE_CONSTRAINED_ &&
10176                             ip [11] == CEE_CALLVIRT &&
10177                             ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
10178                             mono_class_is_enum (klass) &&
10179                             (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
10180                             (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
10181                             has_flag->klass == mono_defaults.enum_class &&
10182                             !strcmp (has_flag->name, "HasFlag") &&
10183                             has_flag->signature->hasthis &&
10184                             has_flag->signature->param_count == 1) {
10185                                 CHECK_TYPELOAD (enum_class);
10186
10187                                 if (enum_class == klass) {
10188                                         MonoInst *enum_this, *enum_flag;
10189
10190                                         ip += 5 + 6 + 5;
10191                                         --sp;
10192
10193                                         enum_this = sp [0];
10194                                         enum_flag = sp [1];
10195
10196                                         *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
10197                                         break;
10198                                 }
10199                         }
10200
10201                         // FIXME: LLVM can't handle the inconsistent bb linking
10202                         if (!mono_class_is_nullable (klass) &&
10203                                 !mini_is_gsharedvt_klass (klass) &&
10204                                 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
10205                                 (ip [5] == CEE_BRTRUE || 
10206                                  ip [5] == CEE_BRTRUE_S ||
10207                                  ip [5] == CEE_BRFALSE ||
10208                                  ip [5] == CEE_BRFALSE_S)) {
10209                                 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
10210                                 int dreg;
10211                                 MonoBasicBlock *true_bb, *false_bb;
10212
10213                                 ip += 5;
10214
10215                                 if (cfg->verbose_level > 3) {
10216                                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
10217                                         printf ("<box+brtrue opt>\n");
10218                                 }
10219
10220                                 switch (*ip) {
10221                                 case CEE_BRTRUE_S:
10222                                 case CEE_BRFALSE_S:
10223                                         CHECK_OPSIZE (2);
10224                                         ip++;
10225                                         target = ip + 1 + (signed char)(*ip);
10226                                         ip++;
10227                                         break;
10228                                 case CEE_BRTRUE:
10229                                 case CEE_BRFALSE:
10230                                         CHECK_OPSIZE (5);
10231                                         ip++;
10232                                         target = ip + 4 + (gint)(read32 (ip));
10233                                         ip += 4;
10234                                         break;
10235                                 default:
10236                                         g_assert_not_reached ();
10237                                 }
10238
10239                                 /* 
10240                                  * We need to link both bblocks, since it is needed for handling stack
10241                                  * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
10242                                  * Branching to only one of them would lead to inconsistencies, so
10243                                  * generate an ICONST+BRTRUE, the branch opts will get rid of them.
10244                                  */
10245                                 GET_BBLOCK (cfg, true_bb, target);
10246                                 GET_BBLOCK (cfg, false_bb, ip);
10247
10248                                 mono_link_bblock (cfg, cfg->cbb, true_bb);
10249                                 mono_link_bblock (cfg, cfg->cbb, false_bb);
10250
10251                                 if (sp != stack_start) {
10252                                         handle_stack_args (cfg, stack_start, sp - stack_start);
10253                                         sp = stack_start;
10254                                         CHECK_UNVERIFIABLE (cfg);
10255                                 }
10256
10257                                 if (COMPILE_LLVM (cfg)) {
10258                                         dreg = alloc_ireg (cfg);
10259                                         MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
10260                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
10261
10262                                         MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
10263                                 } else {
10264                                         /* The JIT can't eliminate the iconst+compare */
10265                                         MONO_INST_NEW (cfg, ins, OP_BR);
10266                                         ins->inst_target_bb = is_true ? true_bb : false_bb;
10267                                         MONO_ADD_INS (cfg->cbb, ins);
10268                                 }
10269
10270                                 start_new_bblock = 1;
10271                                 break;
10272                         }
10273
10274                         *sp++ = handle_box (cfg, val, klass, context_used);
10275
10276                         CHECK_CFG_EXCEPTION;
10277                         ip += 5;
10278                         inline_costs += 1;
10279                         break;
10280                 }
10281                 case CEE_UNBOX: {
10282                         CHECK_STACK (1);
10283                         --sp;
10284                         CHECK_OPSIZE (5);
10285                         token = read32 (ip + 1);
10286                         klass = mini_get_class (method, token, generic_context);
10287                         CHECK_TYPELOAD (klass);
10288
10289                         mono_save_token_info (cfg, image, token, klass);
10290
10291                         context_used = mini_class_check_context_used (cfg, klass);
10292
10293                         if (mono_class_is_nullable (klass)) {
10294                                 MonoInst *val;
10295
10296                                 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
10297                                 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
10298
10299                                 *sp++= ins;
10300                         } else {
10301                                 ins = handle_unbox (cfg, klass, sp, context_used);
10302                                 *sp++ = ins;
10303                         }
10304                         ip += 5;
10305                         inline_costs += 2;
10306                         break;
10307                 }
10308                 case CEE_LDFLD:
10309                 case CEE_LDFLDA:
10310                 case CEE_STFLD:
10311                 case CEE_LDSFLD:
10312                 case CEE_LDSFLDA:
10313                 case CEE_STSFLD: {
10314                         MonoClassField *field;
10315 #ifndef DISABLE_REMOTING
10316                         int costs;
10317 #endif
10318                         guint foffset;
10319                         gboolean is_instance;
10320                         int op;
10321                         gpointer addr = NULL;
10322                         gboolean is_special_static;
10323                         MonoType *ftype;
10324                         MonoInst *store_val = NULL;
10325                         MonoInst *thread_ins;
10326
10327                         op = *ip;
10328                         is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
10329                         if (is_instance) {
10330                                 if (op == CEE_STFLD) {
10331                                         CHECK_STACK (2);
10332                                         sp -= 2;
10333                                         store_val = sp [1];
10334                                 } else {
10335                                         CHECK_STACK (1);
10336                                         --sp;
10337                                 }
10338                                 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
10339                                         UNVERIFIED;
10340                                 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
10341                                         UNVERIFIED;
10342                         } else {
10343                                 if (op == CEE_STSFLD) {
10344                                         CHECK_STACK (1);
10345                                         sp--;
10346                                         store_val = sp [0];
10347                                 }
10348                         }
10349
10350                         CHECK_OPSIZE (5);
10351                         token = read32 (ip + 1);
10352                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
10353                                 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
10354                                 klass = field->parent;
10355                         }
10356                         else {
10357                                 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
10358                                 CHECK_CFG_ERROR;
10359                         }
10360                         if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
10361                                 FIELD_ACCESS_FAILURE (method, field);
10362                         mono_class_init (klass);
10363
10364                         /* if the class is Critical then transparent code cannot access it's fields */
10365                         if (!is_instance && mono_security_core_clr_enabled ())
10366                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
10367
10368                         /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
10369                            any visible *instance* field  (in fact there's a single case for a static field in Marshal) XXX
10370                         if (mono_security_core_clr_enabled ())
10371                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
10372                         */
10373
10374                         ftype = mono_field_get_type (field);
10375
10376                         /*
10377                          * LDFLD etc. is usable on static fields as well, so convert those cases to
10378                          * the static case.
10379                          */
10380                         if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
10381                                 switch (op) {
10382                                 case CEE_LDFLD:
10383                                         op = CEE_LDSFLD;
10384                                         break;
10385                                 case CEE_STFLD:
10386                                         op = CEE_STSFLD;
10387                                         break;
10388                                 case CEE_LDFLDA:
10389                                         op = CEE_LDSFLDA;
10390                                         break;
10391                                 default:
10392                                         g_assert_not_reached ();
10393                                 }
10394                                 is_instance = FALSE;
10395                         }
10396
10397                         context_used = mini_class_check_context_used (cfg, klass);
10398
10399                         /* INSTANCE CASE */
10400
10401                         foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
10402                         if (op == CEE_STFLD) {
10403                                 if (target_type_is_incompatible (cfg, field->type, sp [1]))
10404                                         UNVERIFIED;
10405 #ifndef DISABLE_REMOTING
10406                                 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
10407                                         MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
10408                                         MonoInst *iargs [5];
10409
10410                                         GSHAREDVT_FAILURE (op);
10411
10412                                         iargs [0] = sp [0];
10413                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10414                                         EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10415                                         EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : 
10416                                                     field->offset);
10417                                         iargs [4] = sp [1];
10418
10419                                         if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10420                                                 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), 
10421                                                                                            iargs, ip, cfg->real_offset, TRUE);
10422                                                 CHECK_CFG_EXCEPTION;
10423                                                 g_assert (costs > 0);
10424                                                       
10425                                                 cfg->real_offset += 5;
10426
10427                                                 inline_costs += costs;
10428                                         } else {
10429                                                 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
10430                                         }
10431                                 } else
10432 #endif
10433                                 {
10434                                         MonoInst *store, *wbarrier_ptr_ins = NULL;
10435
10436                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10437
10438                                         if (ins_flag & MONO_INST_VOLATILE) {
10439                                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10440                                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10441                                         }
10442
10443                                         if (mini_is_gsharedvt_klass (klass)) {
10444                                                 MonoInst *offset_ins;
10445
10446                                                 context_used = mini_class_check_context_used (cfg, klass);
10447
10448                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10449                                                 /* The value is offset by 1 */
10450                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10451                                                 dreg = alloc_ireg_mp (cfg);
10452                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10453                                                 wbarrier_ptr_ins = ins;
10454                                                 /* The decomposition will call mini_emit_memory_copy () which will emit a wbarrier if needed */
10455                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
10456                                         } else {
10457                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
10458                                         }
10459                                         if (sp [0]->opcode != OP_LDADDR)
10460                                                 store->flags |= MONO_INST_FAULT;
10461
10462                                         if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !MONO_INS_IS_PCONST_NULL (sp [1])) {
10463                                                 if (mini_is_gsharedvt_klass (klass)) {
10464                                                         g_assert (wbarrier_ptr_ins);
10465                                                         mini_emit_write_barrier (cfg, wbarrier_ptr_ins, sp [1]);
10466                                                 } else {
10467                                                         /* insert call to write barrier */
10468                                                         MonoInst *ptr;
10469                                                         int dreg;
10470
10471                                                         dreg = alloc_ireg_mp (cfg);
10472                                                         EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10473                                                         mini_emit_write_barrier (cfg, ptr, sp [1]);
10474                                                 }
10475                                         }
10476
10477                                         store->flags |= ins_flag;
10478                                 }
10479                                 ins_flag = 0;
10480                                 ip += 5;
10481                                 break;
10482                         }
10483
10484 #ifndef DISABLE_REMOTING
10485                         if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
10486                                 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type); 
10487                                 MonoInst *iargs [4];
10488
10489                                 GSHAREDVT_FAILURE (op);
10490
10491                                 iargs [0] = sp [0];
10492                                 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10493                                 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10494                                 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
10495                                 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10496                                         costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), 
10497                                                                                    iargs, ip, cfg->real_offset, TRUE);
10498                                         CHECK_CFG_EXCEPTION;
10499                                         g_assert (costs > 0);
10500                                                       
10501                                         cfg->real_offset += 5;
10502
10503                                         *sp++ = iargs [0];
10504
10505                                         inline_costs += costs;
10506                                 } else {
10507                                         ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
10508                                         *sp++ = ins;
10509                                 }
10510                         } else 
10511 #endif
10512                         if (is_instance) {
10513                                 if (sp [0]->type == STACK_VTYPE) {
10514                                         MonoInst *var;
10515
10516                                         /* Have to compute the address of the variable */
10517
10518                                         var = get_vreg_to_inst (cfg, sp [0]->dreg);
10519                                         if (!var)
10520                                                 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
10521                                         else
10522                                                 g_assert (var->klass == klass);
10523                                         
10524                                         EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
10525                                         sp [0] = ins;
10526                                 }
10527
10528                                 if (op == CEE_LDFLDA) {
10529                                         if (sp [0]->type == STACK_OBJ) {
10530                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
10531                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
10532                                         }
10533
10534                                         dreg = alloc_ireg_mp (cfg);
10535
10536                                         if (mini_is_gsharedvt_klass (klass)) {
10537                                                 MonoInst *offset_ins;
10538
10539                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10540                                                 /* The value is offset by 1 */
10541                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10542                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10543                                         } else {
10544                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10545                                         }
10546                                         ins->klass = mono_class_from_mono_type (field->type);
10547                                         ins->type = STACK_MP;
10548                                         *sp++ = ins;
10549                                 } else {
10550                                         MonoInst *load;
10551
10552                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10553
10554                                         if (sp [0]->opcode == OP_LDADDR && klass->simd_type && cfg->opt & MONO_OPT_SIMD) {
10555                                                 ins = mono_emit_simd_field_load (cfg, field, sp [0]);
10556                                                 if (ins) {
10557                                                         *sp++ = ins;
10558                                                         ins_flag = 0;
10559                                                         ip += 5;
10560                                                         break;
10561                                                 }
10562                                         }
10563
10564                                         MonoInst *field_add_inst = sp [0];
10565                                         if (mini_is_gsharedvt_klass (klass)) {
10566                                                 MonoInst *offset_ins;
10567
10568                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10569                                                 /* The value is offset by 1 */
10570                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10571                                                 EMIT_NEW_BIALU (cfg, field_add_inst, OP_PADD, alloc_ireg_mp (cfg), sp [0]->dreg, offset_ins->dreg);
10572                                                 foffset = 0;
10573                                         }
10574
10575                                         load = mini_emit_memory_load (cfg, field->type, field_add_inst, foffset, ins_flag);
10576
10577                                         if (sp [0]->opcode != OP_LDADDR)
10578                                                 load->flags |= MONO_INST_FAULT;
10579                                         *sp++ = load;
10580                                 }
10581                         }
10582
10583                         if (is_instance) {
10584                                 ins_flag = 0;
10585                                 ip += 5;
10586                                 break;
10587                         }
10588
10589                         /* STATIC CASE */
10590                         context_used = mini_class_check_context_used (cfg, klass);
10591
10592                         if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL) {
10593                                 mono_error_set_field_load (&cfg->error, field->parent, field->name, "Using static instructions with literal field");
10594                                 CHECK_CFG_ERROR;
10595                         }
10596
10597                         /* The special_static_fields field is init'd in mono_class_vtable, so it needs
10598                          * to be called here.
10599                          */
10600                         if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
10601                                 mono_class_vtable (cfg->domain, klass);
10602                                 CHECK_TYPELOAD (klass);
10603                         }
10604                         mono_domain_lock (cfg->domain);
10605                         if (cfg->domain->special_static_fields)
10606                                 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
10607                         mono_domain_unlock (cfg->domain);
10608
10609                         is_special_static = mono_class_field_is_special_static (field);
10610
10611                         if (is_special_static && ((gsize)addr & 0x80000000) == 0)
10612                                 thread_ins = mono_create_tls_get (cfg, TLS_KEY_THREAD);
10613                         else
10614                                 thread_ins = NULL;
10615
10616                         /* Generate IR to compute the field address */
10617                         if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
10618                                 /*
10619                                  * Fast access to TLS data
10620                                  * Inline version of get_thread_static_data () in
10621                                  * threads.c.
10622                                  */
10623                                 guint32 offset;
10624                                 int idx, static_data_reg, array_reg, dreg;
10625
10626                                 if (context_used && cfg->gsharedvt && mini_is_gsharedvt_klass (klass))
10627                                         GSHAREDVT_FAILURE (op);
10628
10629                                 static_data_reg = alloc_ireg (cfg);
10630                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
10631
10632                                 if (cfg->compile_aot) {
10633                                         int offset_reg, offset2_reg, idx_reg;
10634
10635                                         /* For TLS variables, this will return the TLS offset */
10636                                         EMIT_NEW_SFLDACONST (cfg, ins, field);
10637                                         offset_reg = ins->dreg;
10638                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
10639                                         idx_reg = alloc_ireg (cfg);
10640                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
10641                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
10642                                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
10643                                         array_reg = alloc_ireg (cfg);
10644                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
10645                                         offset2_reg = alloc_ireg (cfg);
10646                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
10647                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
10648                                         dreg = alloc_ireg (cfg);
10649                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
10650                                 } else {
10651                                         offset = (gsize)addr & 0x7fffffff;
10652                                         idx = offset & 0x3f;
10653
10654                                         array_reg = alloc_ireg (cfg);
10655                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
10656                                         dreg = alloc_ireg (cfg);
10657                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
10658                                 }
10659                         } else if ((cfg->opt & MONO_OPT_SHARED) ||
10660                                         (cfg->compile_aot && is_special_static) ||
10661                                         (context_used && is_special_static)) {
10662                                 MonoInst *iargs [2];
10663
10664                                 g_assert (field->parent);
10665                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10666                                 if (context_used) {
10667                                         iargs [1] = emit_get_rgctx_field (cfg, context_used,
10668                                                 field, MONO_RGCTX_INFO_CLASS_FIELD);
10669                                 } else {
10670                                         EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10671                                 }
10672                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10673                         } else if (context_used) {
10674                                 MonoInst *static_data;
10675
10676                                 /*
10677                                 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
10678                                         method->klass->name_space, method->klass->name, method->name,
10679                                         depth, field->offset);
10680                                 */
10681
10682                                 if (mono_class_needs_cctor_run (klass, method))
10683                                         emit_class_init (cfg, klass);
10684
10685                                 /*
10686                                  * The pointer we're computing here is
10687                                  *
10688                                  *   super_info.static_data + field->offset
10689                                  */
10690                                 static_data = mini_emit_get_rgctx_klass (cfg, context_used,
10691                                         klass, MONO_RGCTX_INFO_STATIC_DATA);
10692
10693                                 if (mini_is_gsharedvt_klass (klass)) {
10694                                         MonoInst *offset_ins;
10695
10696                                         offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10697                                         /* The value is offset by 1 */
10698                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10699                                         dreg = alloc_ireg_mp (cfg);
10700                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
10701                                 } else if (field->offset == 0) {
10702                                         ins = static_data;
10703                                 } else {
10704                                         int addr_reg = mono_alloc_preg (cfg);
10705                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
10706                                 }
10707                         } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
10708                                 MonoInst *iargs [2];
10709
10710                                 g_assert (field->parent);
10711                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10712                                 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10713                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10714                         } else {
10715                                 MonoVTable *vtable = NULL;
10716
10717                                 if (!cfg->compile_aot)
10718                                         vtable = mono_class_vtable (cfg->domain, klass);
10719                                 CHECK_TYPELOAD (klass);
10720
10721                                 if (!addr) {
10722                                         if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
10723                                                 if (!(g_slist_find (class_inits, klass))) {
10724                                                         emit_class_init (cfg, klass);
10725                                                         if (cfg->verbose_level > 2)
10726                                                                 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
10727                                                         class_inits = g_slist_prepend (class_inits, klass);
10728                                                 }
10729                                         } else {
10730                                                 if (cfg->run_cctors) {
10731                                                         /* This makes so that inline cannot trigger */
10732                                                         /* .cctors: too many apps depend on them */
10733                                                         /* running with a specific order... */
10734                                                         g_assert (vtable);
10735                                                         if (! vtable->initialized)
10736                                                                 INLINE_FAILURE ("class init");
10737                                                         if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
10738                                                                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
10739                                                                 goto exception_exit;
10740                                                         }
10741                                                 }
10742                                         }
10743                                         if (cfg->compile_aot)
10744                                                 EMIT_NEW_SFLDACONST (cfg, ins, field);
10745                                         else {
10746                                                 g_assert (vtable);
10747                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
10748                                                 g_assert (addr);
10749                                                 EMIT_NEW_PCONST (cfg, ins, addr);
10750                                         }
10751                                 } else {
10752                                         MonoInst *iargs [1];
10753                                         EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
10754                                         ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
10755                                 }
10756                         }
10757
10758                         /* Generate IR to do the actual load/store operation */
10759
10760                         if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
10761                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10762                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10763                         }
10764
10765                         if (op == CEE_LDSFLDA) {
10766                                 ins->klass = mono_class_from_mono_type (ftype);
10767                                 ins->type = STACK_PTR;
10768                                 *sp++ = ins;
10769                         } else if (op == CEE_STSFLD) {
10770                                 MonoInst *store;
10771
10772                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
10773                                 store->flags |= ins_flag;
10774                         } else {
10775                                 gboolean is_const = FALSE;
10776                                 MonoVTable *vtable = NULL;
10777                                 gpointer addr = NULL;
10778
10779                                 if (!context_used) {
10780                                         vtable = mono_class_vtable (cfg->domain, klass);
10781                                         CHECK_TYPELOAD (klass);
10782                                 }
10783                                 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
10784                                                 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
10785                                         int ro_type = ftype->type;
10786                                         if (!addr)
10787                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
10788                                         if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
10789                                                 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
10790                                         }
10791
10792                                         GSHAREDVT_FAILURE (op);
10793
10794                                         /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
10795                                         is_const = TRUE;
10796                                         switch (ro_type) {
10797                                         case MONO_TYPE_BOOLEAN:
10798                                         case MONO_TYPE_U1:
10799                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
10800                                                 sp++;
10801                                                 break;
10802                                         case MONO_TYPE_I1:
10803                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
10804                                                 sp++;
10805                                                 break;                                          
10806                                         case MONO_TYPE_CHAR:
10807                                         case MONO_TYPE_U2:
10808                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
10809                                                 sp++;
10810                                                 break;
10811                                         case MONO_TYPE_I2:
10812                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
10813                                                 sp++;
10814                                                 break;
10815                                                 break;
10816                                         case MONO_TYPE_I4:
10817                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
10818                                                 sp++;
10819                                                 break;                                          
10820                                         case MONO_TYPE_U4:
10821                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
10822                                                 sp++;
10823                                                 break;
10824                                         case MONO_TYPE_I:
10825                                         case MONO_TYPE_U:
10826                                         case MONO_TYPE_PTR:
10827                                         case MONO_TYPE_FNPTR:
10828                                                 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
10829                                                 type_to_eval_stack_type ((cfg), field->type, *sp);
10830                                                 sp++;
10831                                                 break;
10832                                         case MONO_TYPE_STRING:
10833                                         case MONO_TYPE_OBJECT:
10834                                         case MONO_TYPE_CLASS:
10835                                         case MONO_TYPE_SZARRAY:
10836                                         case MONO_TYPE_ARRAY:
10837                                                 if (!mono_gc_is_moving ()) {
10838                                                         EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
10839                                                         type_to_eval_stack_type ((cfg), field->type, *sp);
10840                                                         sp++;
10841                                                 } else {
10842                                                         is_const = FALSE;
10843                                                 }
10844                                                 break;
10845                                         case MONO_TYPE_I8:
10846                                         case MONO_TYPE_U8:
10847                                                 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
10848                                                 sp++;
10849                                                 break;
10850                                         case MONO_TYPE_R4:
10851                                         case MONO_TYPE_R8:
10852                                         case MONO_TYPE_VALUETYPE:
10853                                         default:
10854                                                 is_const = FALSE;
10855                                                 break;
10856                                         }
10857                                 }
10858
10859                                 if (!is_const) {
10860                                         MonoInst *load;
10861
10862                                         CHECK_STACK_OVF (1);
10863
10864                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
10865                                         load->flags |= ins_flag;
10866                                         ins_flag = 0;
10867                                         *sp++ = load;
10868                                 }
10869                         }
10870
10871                         if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
10872                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10873                                 mini_emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10874                         }
10875
10876                         ins_flag = 0;
10877                         ip += 5;
10878                         break;
10879                 }
10880                 case CEE_STOBJ:
10881                         CHECK_STACK (2);
10882                         sp -= 2;
10883                         CHECK_OPSIZE (5);
10884                         token = read32 (ip + 1);
10885                         klass = mini_get_class (method, token, generic_context);
10886                         CHECK_TYPELOAD (klass);
10887
10888                         /* FIXME: should check item at sp [1] is compatible with the type of the store. */
10889                         mini_emit_memory_store (cfg, &klass->byval_arg, sp [0], sp [1], ins_flag);
10890                         ins_flag = 0;
10891                         ip += 5;
10892                         inline_costs += 1;
10893                         break;
10894
10895                         /*
10896                          * Array opcodes
10897                          */
10898                 case CEE_NEWARR: {
10899                         MonoInst *len_ins;
10900                         const char *data_ptr;
10901                         int data_size = 0;
10902                         guint32 field_token;
10903
10904                         CHECK_STACK (1);
10905                         --sp;
10906
10907                         CHECK_OPSIZE (5);
10908                         token = read32 (ip + 1);
10909
10910                         klass = mini_get_class (method, token, generic_context);
10911                         CHECK_TYPELOAD (klass);
10912                         if (klass->byval_arg.type == MONO_TYPE_VOID)
10913                                 UNVERIFIED;
10914
10915                         context_used = mini_class_check_context_used (cfg, klass);
10916
10917                         if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
10918                                 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
10919                                 ins->sreg1 = sp [0]->dreg;
10920                                 ins->type = STACK_I4;
10921                                 ins->dreg = alloc_ireg (cfg);
10922                                 MONO_ADD_INS (cfg->cbb, ins);
10923                                 *sp = mono_decompose_opcode (cfg, ins);
10924                         }
10925
10926                         if (context_used) {
10927                                 MonoInst *args [3];
10928                                 MonoClass *array_class = mono_array_class_get (klass, 1);
10929                                 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
10930
10931                                 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
10932
10933                                 /* vtable */
10934                                 args [0] = mini_emit_get_rgctx_klass (cfg, context_used,
10935                                         array_class, MONO_RGCTX_INFO_VTABLE);
10936                                 /* array len */
10937                                 args [1] = sp [0];
10938
10939                                 if (managed_alloc)
10940                                         ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
10941                                 else
10942                                         ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
10943                         } else {
10944                                 if (cfg->opt & MONO_OPT_SHARED) {
10945                                         /* Decompose now to avoid problems with references to the domainvar */
10946                                         MonoInst *iargs [3];
10947
10948                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10949                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10950                                         iargs [2] = sp [0];
10951
10952                                         ins = mono_emit_jit_icall (cfg, ves_icall_array_new, iargs);
10953                                 } else {
10954                                         /* Decompose later since it is needed by abcrem */
10955                                         MonoClass *array_type = mono_array_class_get (klass, 1);
10956                                         mono_class_vtable (cfg->domain, array_type);
10957                                         CHECK_TYPELOAD (array_type);
10958
10959                                         MONO_INST_NEW (cfg, ins, OP_NEWARR);
10960                                         ins->dreg = alloc_ireg_ref (cfg);
10961                                         ins->sreg1 = sp [0]->dreg;
10962                                         ins->inst_newa_class = klass;
10963                                         ins->type = STACK_OBJ;
10964                                         ins->klass = array_type;
10965                                         MONO_ADD_INS (cfg->cbb, ins);
10966                                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
10967                                         cfg->cbb->has_array_access = TRUE;
10968
10969                                         /* Needed so mono_emit_load_get_addr () gets called */
10970                                         mono_get_got_var (cfg);
10971                                 }
10972                         }
10973
10974                         len_ins = sp [0];
10975                         ip += 5;
10976                         *sp++ = ins;
10977                         inline_costs += 1;
10978
10979                         /* 
10980                          * we inline/optimize the initialization sequence if possible.
10981                          * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
10982                          * for small sizes open code the memcpy
10983                          * ensure the rva field is big enough
10984                          */
10985                         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))) {
10986                                 MonoMethod *memcpy_method = mini_get_memcpy_method ();
10987                                 MonoInst *iargs [3];
10988                                 int add_reg = alloc_ireg_mp (cfg);
10989
10990                                 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
10991                                 if (cfg->compile_aot) {
10992                                         EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
10993                                 } else {
10994                                         EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
10995                                 }
10996                                 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
10997                                 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
10998                                 ip += 11;
10999                         }
11000
11001                         break;
11002                 }
11003                 case CEE_LDLEN:
11004                         CHECK_STACK (1);
11005                         --sp;
11006                         if (sp [0]->type != STACK_OBJ)
11007                                 UNVERIFIED;
11008
11009                         MONO_INST_NEW (cfg, ins, OP_LDLEN);
11010                         ins->dreg = alloc_preg (cfg);
11011                         ins->sreg1 = sp [0]->dreg;
11012                         ins->type = STACK_I4;
11013                         /* This flag will be inherited by the decomposition */
11014                         ins->flags |= MONO_INST_FAULT;
11015                         MONO_ADD_INS (cfg->cbb, ins);
11016                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11017                         cfg->cbb->has_array_access = TRUE;
11018                         ip ++;
11019                         *sp++ = ins;
11020                         break;
11021                 case CEE_LDELEMA:
11022                         CHECK_STACK (2);
11023                         sp -= 2;
11024                         CHECK_OPSIZE (5);
11025                         if (sp [0]->type != STACK_OBJ)
11026                                 UNVERIFIED;
11027
11028                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11029
11030                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11031                         CHECK_TYPELOAD (klass);
11032                         /* we need to make sure that this array is exactly the type it needs
11033                          * to be for correctness. the wrappers are lax with their usage
11034                          * so we need to ignore them here
11035                          */
11036                         if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11037                                 MonoClass *array_class = mono_array_class_get (klass, 1);
11038                                 mini_emit_check_array_type (cfg, sp [0], array_class);
11039                                 CHECK_TYPELOAD (array_class);
11040                         }
11041
11042                         readonly = FALSE;
11043                         ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11044                         *sp++ = ins;
11045                         ip += 5;
11046                         break;
11047                 case CEE_LDELEM:
11048                 case CEE_LDELEM_I1:
11049                 case CEE_LDELEM_U1:
11050                 case CEE_LDELEM_I2:
11051                 case CEE_LDELEM_U2:
11052                 case CEE_LDELEM_I4:
11053                 case CEE_LDELEM_U4:
11054                 case CEE_LDELEM_I8:
11055                 case CEE_LDELEM_I:
11056                 case CEE_LDELEM_R4:
11057                 case CEE_LDELEM_R8:
11058                 case CEE_LDELEM_REF: {
11059                         MonoInst *addr;
11060
11061                         CHECK_STACK (2);
11062                         sp -= 2;
11063
11064                         if (*ip == CEE_LDELEM) {
11065                                 CHECK_OPSIZE (5);
11066                                 token = read32 (ip + 1);
11067                                 klass = mini_get_class (method, token, generic_context);
11068                                 CHECK_TYPELOAD (klass);
11069                                 mono_class_init (klass);
11070                         }
11071                         else
11072                                 klass = array_access_to_klass (*ip);
11073
11074                         if (sp [0]->type != STACK_OBJ)
11075                                 UNVERIFIED;
11076
11077                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11078
11079                         if (mini_is_gsharedvt_variable_klass (klass)) {
11080                                 // FIXME-VT: OP_ICONST optimization
11081                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11082                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11083                                 ins->opcode = OP_LOADV_MEMBASE;
11084                         } else if (sp [1]->opcode == OP_ICONST) {
11085                                 int array_reg = sp [0]->dreg;
11086                                 int index_reg = sp [1]->dreg;
11087                                 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11088
11089                                 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
11090                                         MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
11091
11092                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
11093                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
11094                         } else {
11095                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11096                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11097                         }
11098                         *sp++ = ins;
11099                         if (*ip == CEE_LDELEM)
11100                                 ip += 5;
11101                         else
11102                                 ++ip;
11103                         break;
11104                 }
11105                 case CEE_STELEM_I:
11106                 case CEE_STELEM_I1:
11107                 case CEE_STELEM_I2:
11108                 case CEE_STELEM_I4:
11109                 case CEE_STELEM_I8:
11110                 case CEE_STELEM_R4:
11111                 case CEE_STELEM_R8:
11112                 case CEE_STELEM_REF:
11113                 case CEE_STELEM: {
11114                         CHECK_STACK (3);
11115                         sp -= 3;
11116
11117                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11118
11119                         if (*ip == CEE_STELEM) {
11120                                 CHECK_OPSIZE (5);
11121                                 token = read32 (ip + 1);
11122                                 klass = mini_get_class (method, token, generic_context);
11123                                 CHECK_TYPELOAD (klass);
11124                                 mono_class_init (klass);
11125                         }
11126                         else
11127                                 klass = array_access_to_klass (*ip);
11128
11129                         if (sp [0]->type != STACK_OBJ)
11130                                 UNVERIFIED;
11131
11132                         emit_array_store (cfg, klass, sp, TRUE);
11133
11134                         if (*ip == CEE_STELEM)
11135                                 ip += 5;
11136                         else
11137                                 ++ip;
11138                         inline_costs += 1;
11139                         break;
11140                 }
11141                 case CEE_CKFINITE: {
11142                         CHECK_STACK (1);
11143                         --sp;
11144
11145                         if (cfg->llvm_only) {
11146                                 MonoInst *iargs [1];
11147
11148                                 iargs [0] = sp [0];
11149                                 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
11150                         } else  {
11151                                 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
11152                                 ins->sreg1 = sp [0]->dreg;
11153                                 ins->dreg = alloc_freg (cfg);
11154                                 ins->type = STACK_R8;
11155                                 MONO_ADD_INS (cfg->cbb, ins);
11156
11157                                 *sp++ = mono_decompose_opcode (cfg, ins);
11158                         }
11159
11160                         ++ip;
11161                         break;
11162                 }
11163                 case CEE_REFANYVAL: {
11164                         MonoInst *src_var, *src;
11165
11166                         int klass_reg = alloc_preg (cfg);
11167                         int dreg = alloc_preg (cfg);
11168
11169                         GSHAREDVT_FAILURE (*ip);
11170
11171                         CHECK_STACK (1);
11172                         MONO_INST_NEW (cfg, ins, *ip);
11173                         --sp;
11174                         CHECK_OPSIZE (5);
11175                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11176                         CHECK_TYPELOAD (klass);
11177
11178                         context_used = mini_class_check_context_used (cfg, klass);
11179
11180                         // FIXME:
11181                         src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11182                         if (!src_var)
11183                                 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
11184                         EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
11185                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
11186
11187                         if (context_used) {
11188                                 MonoInst *klass_ins;
11189
11190                                 klass_ins = mini_emit_get_rgctx_klass (cfg, context_used,
11191                                                 klass, MONO_RGCTX_INFO_KLASS);
11192
11193                                 // FIXME:
11194                                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
11195                                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
11196                         } else {
11197                                 mini_emit_class_check (cfg, klass_reg, klass);
11198                         }
11199                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
11200                         ins->type = STACK_MP;
11201                         ins->klass = klass;
11202                         *sp++ = ins;
11203                         ip += 5;
11204                         break;
11205                 }
11206                 case CEE_MKREFANY: {
11207                         MonoInst *loc, *addr;
11208
11209                         GSHAREDVT_FAILURE (*ip);
11210
11211                         CHECK_STACK (1);
11212                         MONO_INST_NEW (cfg, ins, *ip);
11213                         --sp;
11214                         CHECK_OPSIZE (5);
11215                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11216                         CHECK_TYPELOAD (klass);
11217
11218                         context_used = mini_class_check_context_used (cfg, klass);
11219
11220                         loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
11221                         EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
11222
11223                         if (context_used) {
11224                                 MonoInst *const_ins;
11225                                 int type_reg = alloc_preg (cfg);
11226
11227                                 const_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
11228                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
11229                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11230                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11231                         } else {
11232                                 int const_reg = alloc_preg (cfg);
11233                                 int type_reg = alloc_preg (cfg);
11234
11235                                 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
11236                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
11237                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11238                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11239                         }
11240                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
11241
11242                         EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
11243                         ins->type = STACK_VTYPE;
11244                         ins->klass = mono_defaults.typed_reference_class;
11245                         *sp++ = ins;
11246                         ip += 5;
11247                         break;
11248                 }
11249                 case CEE_LDTOKEN: {
11250                         gpointer handle;
11251                         MonoClass *handle_class;
11252
11253                         CHECK_STACK_OVF (1);
11254
11255                         CHECK_OPSIZE (5);
11256                         n = read32 (ip + 1);
11257
11258                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
11259                                         method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
11260                                 handle = mono_method_get_wrapper_data (method, n);
11261                                 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
11262                                 if (handle_class == mono_defaults.typehandle_class)
11263                                         handle = &((MonoClass*)handle)->byval_arg;
11264                         }
11265                         else {
11266                                 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
11267                                 CHECK_CFG_ERROR;
11268                         }
11269                         if (!handle)
11270                                 LOAD_ERROR;
11271                         mono_class_init (handle_class);
11272                         if (cfg->gshared) {
11273                                 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
11274                                                 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
11275                                         /* This case handles ldtoken
11276                                            of an open type, like for
11277                                            typeof(Gen<>). */
11278                                         context_used = 0;
11279                                 } else if (handle_class == mono_defaults.typehandle_class) {
11280                                         context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
11281                                 } else if (handle_class == mono_defaults.fieldhandle_class)
11282                                         context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
11283                                 else if (handle_class == mono_defaults.methodhandle_class)
11284                                         context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
11285                                 else
11286                                         g_assert_not_reached ();
11287                         }
11288
11289                         if ((cfg->opt & MONO_OPT_SHARED) &&
11290                                         method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
11291                                         method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
11292                                 MonoInst *addr, *vtvar, *iargs [3];
11293                                 int method_context_used;
11294
11295                                 method_context_used = mini_method_check_context_used (cfg, method);
11296
11297                                 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL); 
11298
11299                                 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
11300                                 EMIT_NEW_ICONST (cfg, iargs [1], n);
11301                                 if (method_context_used) {
11302                                         iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
11303                                                 method, MONO_RGCTX_INFO_METHOD);
11304                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
11305                                 } else {
11306                                         EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
11307                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
11308                                 }
11309                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11310
11311                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11312
11313                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11314                         } else {
11315                                 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) && 
11316                                         ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) && 
11317                                         (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
11318                                         (cmethod->klass == mono_defaults.systemtype_class) &&
11319                                         (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
11320                                         MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
11321
11322                                         mono_class_init (tclass);
11323                                         if (context_used) {
11324                                                 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11325                                                         tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
11326                                         } else if (cfg->compile_aot) {
11327                                                 if (method->wrapper_type) {
11328                                                         error_init (&error); //got to do it since there are multiple conditionals below
11329                                                         if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
11330                                                                 /* Special case for static synchronized wrappers */
11331                                                                 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
11332                                                         } else {
11333                                                                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
11334                                                                 /* FIXME: n is not a normal token */
11335                                                                 DISABLE_AOT (cfg);
11336                                                                 EMIT_NEW_PCONST (cfg, ins, NULL);
11337                                                         }
11338                                                 } else {
11339                                                         EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
11340                                                 }
11341                                         } else {
11342                                                 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
11343                                                 CHECK_CFG_ERROR;
11344                                                 EMIT_NEW_PCONST (cfg, ins, rt);
11345                                         }
11346                                         ins->type = STACK_OBJ;
11347                                         ins->klass = cmethod->klass;
11348                                         ip += 5;
11349                                 } else {
11350                                         MonoInst *addr, *vtvar;
11351
11352                                         vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11353
11354                                         if (context_used) {
11355                                                 if (handle_class == mono_defaults.typehandle_class) {
11356                                                         ins = mini_emit_get_rgctx_klass (cfg, context_used,
11357                                                                         mono_class_from_mono_type ((MonoType *)handle),
11358                                                                         MONO_RGCTX_INFO_TYPE);
11359                                                 } else if (handle_class == mono_defaults.methodhandle_class) {
11360                                                         ins = emit_get_rgctx_method (cfg, context_used,
11361                                                                         (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
11362                                                 } else if (handle_class == mono_defaults.fieldhandle_class) {
11363                                                         ins = emit_get_rgctx_field (cfg, context_used,
11364                                                                         (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
11365                                                 } else {
11366                                                         g_assert_not_reached ();
11367                                                 }
11368                                         } else if (cfg->compile_aot) {
11369                                                 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
11370                                         } else {
11371                                                 EMIT_NEW_PCONST (cfg, ins, handle);
11372                                         }
11373                                         EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11374                                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11375                                         EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11376                                 }
11377                         }
11378
11379                         *sp++ = ins;
11380                         ip += 5;
11381                         break;
11382                 }
11383                 case CEE_THROW:
11384                         CHECK_STACK (1);
11385                         if (sp [-1]->type != STACK_OBJ)
11386                                 UNVERIFIED;
11387
11388                         MONO_INST_NEW (cfg, ins, OP_THROW);
11389                         --sp;
11390                         ins->sreg1 = sp [0]->dreg;
11391                         ip++;
11392                         cfg->cbb->out_of_line = TRUE;
11393                         MONO_ADD_INS (cfg->cbb, ins);
11394                         MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
11395                         MONO_ADD_INS (cfg->cbb, ins);
11396                         sp = stack_start;
11397                         
11398                         link_bblock (cfg, cfg->cbb, end_bblock);
11399                         start_new_bblock = 1;
11400                         /* This can complicate code generation for llvm since the return value might not be defined */
11401                         if (COMPILE_LLVM (cfg))
11402                                 INLINE_FAILURE ("throw");
11403                         break;
11404                 case CEE_ENDFINALLY:
11405                         if (!ip_in_finally_clause (cfg, ip - header->code))
11406                                 UNVERIFIED;
11407                         /* mono_save_seq_point_info () depends on this */
11408                         if (sp != stack_start)
11409                                 emit_seq_point (cfg, method, ip, FALSE, FALSE);
11410                         MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
11411                         MONO_ADD_INS (cfg->cbb, ins);
11412                         ip++;
11413                         start_new_bblock = 1;
11414
11415                         /*
11416                          * Control will leave the method so empty the stack, otherwise
11417                          * the next basic block will start with a nonempty stack.
11418                          */
11419                         while (sp != stack_start) {
11420                                 sp--;
11421                         }
11422                         break;
11423                 case CEE_LEAVE:
11424                 case CEE_LEAVE_S: {
11425                         GList *handlers;
11426
11427                         if (*ip == CEE_LEAVE) {
11428                                 CHECK_OPSIZE (5);
11429                                 target = ip + 5 + (gint32)read32(ip + 1);
11430                         } else {
11431                                 CHECK_OPSIZE (2);
11432                                 target = ip + 2 + (signed char)(ip [1]);
11433                         }
11434
11435                         /* empty the stack */
11436                         while (sp != stack_start) {
11437                                 sp--;
11438                         }
11439
11440                         /* 
11441                          * If this leave statement is in a catch block, check for a
11442                          * pending exception, and rethrow it if necessary.
11443                          * We avoid doing this in runtime invoke wrappers, since those are called
11444                          * by native code which excepts the wrapper to catch all exceptions.
11445                          */
11446                         for (i = 0; i < header->num_clauses; ++i) {
11447                                 MonoExceptionClause *clause = &header->clauses [i];
11448
11449                                 /* 
11450                                  * Use <= in the final comparison to handle clauses with multiple
11451                                  * leave statements, like in bug #78024.
11452                                  * The ordering of the exception clauses guarantees that we find the
11453                                  * innermost clause.
11454                                  */
11455                                 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) {
11456                                         MonoInst *exc_ins;
11457                                         MonoBasicBlock *dont_throw;
11458
11459                                         /*
11460                                           MonoInst *load;
11461
11462                                           NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
11463                                         */
11464
11465                                         exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
11466
11467                                         NEW_BBLOCK (cfg, dont_throw);
11468
11469                                         /*
11470                                          * Currently, we always rethrow the abort exception, despite the 
11471                                          * fact that this is not correct. See thread6.cs for an example. 
11472                                          * But propagating the abort exception is more important than 
11473                                          * getting the sematics right.
11474                                          */
11475                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
11476                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11477                                         MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
11478
11479                                         MONO_START_BB (cfg, dont_throw);
11480                                 }
11481                         }
11482
11483 #ifdef ENABLE_LLVM
11484                         cfg->cbb->try_end = (intptr_t)(ip - header->code);
11485 #endif
11486
11487                         if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
11488                                 GList *tmp;
11489
11490                                 for (tmp = handlers; tmp; tmp = tmp->next) {
11491                                         MonoExceptionClause *clause = (MonoExceptionClause *)tmp->data;
11492                                         MonoInst *abort_exc = (MonoInst *)mono_find_exvar_for_offset (cfg, clause->handler_offset);
11493                                         MonoBasicBlock *dont_throw;
11494
11495                                         tblock = cfg->cil_offset_to_bb [clause->handler_offset];
11496                                         g_assert (tblock);
11497                                         link_bblock (cfg, cfg->cbb, tblock);
11498
11499                                         MONO_EMIT_NEW_PCONST (cfg, abort_exc->dreg, 0);
11500
11501                                         MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
11502                                         ins->inst_target_bb = tblock;
11503                                         ins->inst_eh_block = clause;
11504                                         MONO_ADD_INS (cfg->cbb, ins);
11505                                         cfg->cbb->has_call_handler = 1;
11506
11507                                         /* Throw exception if exvar is set */
11508                                         /* FIXME Do we need this for calls from catch/filter ? */
11509                                         NEW_BBLOCK (cfg, dont_throw);
11510                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, abort_exc->dreg, 0);
11511                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11512                                         mono_emit_jit_icall (cfg, mono_thread_self_abort, NULL);
11513                                         cfg->cbb->clause_hole = clause;
11514
11515                                         MONO_START_BB (cfg, dont_throw);
11516                                         cfg->cbb->clause_hole = clause;
11517
11518                                         if (COMPILE_LLVM (cfg)) {
11519                                                 MonoBasicBlock *target_bb;
11520
11521                                                 /* 
11522                                                  * Link the finally bblock with the target, since it will
11523                                                  * conceptually branch there.
11524                                                  */
11525                                                 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
11526                                                 GET_BBLOCK (cfg, target_bb, target);
11527                                                 link_bblock (cfg, tblock, target_bb);
11528                                         }
11529                                 }
11530                                 g_list_free (handlers);
11531                         } 
11532
11533                         MONO_INST_NEW (cfg, ins, OP_BR);
11534                         MONO_ADD_INS (cfg->cbb, ins);
11535                         GET_BBLOCK (cfg, tblock, target);
11536                         link_bblock (cfg, cfg->cbb, tblock);
11537                         ins->inst_target_bb = tblock;
11538
11539                         start_new_bblock = 1;
11540
11541                         if (*ip == CEE_LEAVE)
11542                                 ip += 5;
11543                         else
11544                                 ip += 2;
11545
11546                         break;
11547                 }
11548
11549                         /*
11550                          * Mono specific opcodes
11551                          */
11552                 case MONO_CUSTOM_PREFIX: {
11553
11554                         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
11555
11556                         CHECK_OPSIZE (2);
11557                         switch (ip [1]) {
11558                         case CEE_MONO_ICALL: {
11559                                 gpointer func;
11560                                 MonoJitICallInfo *info;
11561
11562                                 token = read32 (ip + 2);
11563                                 func = mono_method_get_wrapper_data (method, token);
11564                                 info = mono_find_jit_icall_by_addr (func);
11565                                 if (!info)
11566                                         g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
11567                                 g_assert (info);
11568
11569                                 CHECK_STACK (info->sig->param_count);
11570                                 sp -= info->sig->param_count;
11571
11572                                 if (cfg->compile_aot && !strcmp (info->name, "mono_threads_attach_coop")) {
11573                                         MonoInst *addr;
11574
11575                                         /*
11576                                          * This is called on unattached threads, so it cannot go through the trampoline
11577                                          * infrastructure. Use an indirect call through a got slot initialized at load time
11578                                          * instead.
11579                                          */
11580                                         EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL, (char*)info->name);
11581                                         ins = mini_emit_calli (cfg, info->sig, sp, addr, NULL, NULL);
11582                                 } else {
11583                                         ins = mono_emit_jit_icall (cfg, info->func, sp);
11584                                 }
11585
11586                                 if (!MONO_TYPE_IS_VOID (info->sig->ret))
11587                                         *sp++ = ins;
11588
11589                                 ip += 6;
11590                                 inline_costs += 10 * num_calls++;
11591
11592                                 break;
11593                         }
11594                         case CEE_MONO_LDPTR_CARD_TABLE:
11595                         case CEE_MONO_LDPTR_NURSERY_START:
11596                         case CEE_MONO_LDPTR_NURSERY_BITS:
11597                         case CEE_MONO_LDPTR_INT_REQ_FLAG:
11598                         case CEE_MONO_LDPTR_PROFILER_ALLOCATION_COUNT: {
11599                                 CHECK_STACK_OVF (1);
11600
11601                                 switch (ip [1]) {
11602                                 case CEE_MONO_LDPTR_CARD_TABLE:
11603                                         ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
11604                                         break;
11605                                 case CEE_MONO_LDPTR_NURSERY_START:
11606                                         ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
11607                                         break;
11608                                 case CEE_MONO_LDPTR_NURSERY_BITS:
11609                                         ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
11610                                         break;
11611                                 case CEE_MONO_LDPTR_INT_REQ_FLAG:
11612                                         ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
11613                                         break;
11614                                 case CEE_MONO_LDPTR_PROFILER_ALLOCATION_COUNT:
11615                                         ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_PROFILER_ALLOCATION_COUNT, NULL);
11616                                         break;
11617                                 default:
11618                                         g_assert_not_reached ();
11619                                         break;
11620                                 }
11621
11622                                 *sp++ = ins;
11623                                 ip += 2;
11624                                 inline_costs += 10 * num_calls++;
11625                                 break;
11626                         }
11627                         case CEE_MONO_LDPTR: {
11628                                 gpointer ptr;
11629
11630                                 CHECK_STACK_OVF (1);
11631                                 CHECK_OPSIZE (6);
11632                                 token = read32 (ip + 2);
11633
11634                                 ptr = mono_method_get_wrapper_data (method, token);
11635                                 EMIT_NEW_PCONST (cfg, ins, ptr);
11636                                 *sp++ = ins;
11637                                 ip += 6;
11638                                 inline_costs += 10 * num_calls++;
11639                                 /* Can't embed random pointers into AOT code */
11640                                 DISABLE_AOT (cfg);
11641                                 break;
11642                         }
11643                         case CEE_MONO_JIT_ICALL_ADDR: {
11644                                 MonoJitICallInfo *callinfo;
11645                                 gpointer ptr;
11646
11647                                 CHECK_STACK_OVF (1);
11648                                 CHECK_OPSIZE (6);
11649                                 token = read32 (ip + 2);
11650
11651                                 ptr = mono_method_get_wrapper_data (method, token);
11652                                 callinfo = mono_find_jit_icall_by_addr (ptr);
11653                                 g_assert (callinfo);
11654                                 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
11655                                 *sp++ = ins;
11656                                 ip += 6;
11657                                 inline_costs += 10 * num_calls++;
11658                                 break;
11659                         }
11660                         case CEE_MONO_ICALL_ADDR: {
11661                                 MonoMethod *cmethod;
11662                                 gpointer ptr;
11663
11664                                 CHECK_STACK_OVF (1);
11665                                 CHECK_OPSIZE (6);
11666                                 token = read32 (ip + 2);
11667
11668                                 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
11669
11670                                 if (cfg->compile_aot) {
11671                                         if (cfg->direct_pinvoke && ip + 6 < end && (ip [6] == CEE_POP)) {
11672                                                 /*
11673                                                  * This is generated by emit_native_wrapper () to resolve the pinvoke address
11674                                                  * before the call, its not needed when using direct pinvoke.
11675                                                  * This is not an optimization, but its used to avoid looking up pinvokes
11676                                                  * on platforms which don't support dlopen ().
11677                                                  */
11678                                                 EMIT_NEW_PCONST (cfg, ins, NULL);
11679                                         } else {
11680                                                 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
11681                                         }
11682                                 } else {
11683                                         ptr = mono_lookup_internal_call (cmethod);
11684                                         g_assert (ptr);
11685                                         EMIT_NEW_PCONST (cfg, ins, ptr);
11686                                 }
11687                                 *sp++ = ins;
11688                                 ip += 6;
11689                                 break;
11690                         }
11691                         case CEE_MONO_VTADDR: {
11692                                 MonoInst *src_var, *src;
11693
11694                                 CHECK_STACK (1);
11695                                 --sp;
11696
11697                                 // FIXME:
11698                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11699                                 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
11700                                 *sp++ = src;
11701                                 ip += 2;
11702                                 break;
11703                         }
11704                         case CEE_MONO_NEWOBJ: {
11705                                 MonoInst *iargs [2];
11706
11707                                 CHECK_STACK_OVF (1);
11708                                 CHECK_OPSIZE (6);
11709                                 token = read32 (ip + 2);
11710                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11711                                 mono_class_init (klass);
11712                                 NEW_DOMAINCONST (cfg, iargs [0]);
11713                                 MONO_ADD_INS (cfg->cbb, iargs [0]);
11714                                 NEW_CLASSCONST (cfg, iargs [1], klass);
11715                                 MONO_ADD_INS (cfg->cbb, iargs [1]);
11716                                 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
11717                                 ip += 6;
11718                                 inline_costs += 10 * num_calls++;
11719                                 break;
11720                         }
11721                         case CEE_MONO_OBJADDR:
11722                                 CHECK_STACK (1);
11723                                 --sp;
11724                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
11725                                 ins->dreg = alloc_ireg_mp (cfg);
11726                                 ins->sreg1 = sp [0]->dreg;
11727                                 ins->type = STACK_MP;
11728                                 MONO_ADD_INS (cfg->cbb, ins);
11729                                 *sp++ = ins;
11730                                 ip += 2;
11731                                 break;
11732                         case CEE_MONO_LDNATIVEOBJ:
11733                                 /*
11734                                  * Similar to LDOBJ, but instead load the unmanaged 
11735                                  * representation of the vtype to the stack.
11736                                  */
11737                                 CHECK_STACK (1);
11738                                 CHECK_OPSIZE (6);
11739                                 --sp;
11740                                 token = read32 (ip + 2);
11741                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11742                                 g_assert (klass->valuetype);
11743                                 mono_class_init (klass);
11744
11745                                 {
11746                                         MonoInst *src, *dest, *temp;
11747
11748                                         src = sp [0];
11749                                         temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
11750                                         temp->backend.is_pinvoke = 1;
11751                                         EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
11752                                         mini_emit_memory_copy (cfg, dest, src, klass, TRUE, 0);
11753
11754                                         EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
11755                                         dest->type = STACK_VTYPE;
11756                                         dest->klass = klass;
11757
11758                                         *sp ++ = dest;
11759                                         ip += 6;
11760                                 }
11761                                 break;
11762                         case CEE_MONO_RETOBJ: {
11763                                 /*
11764                                  * Same as RET, but return the native representation of a vtype
11765                                  * to the caller.
11766                                  */
11767                                 g_assert (cfg->ret);
11768                                 g_assert (mono_method_signature (method)->pinvoke); 
11769                                 CHECK_STACK (1);
11770                                 --sp;
11771                                 
11772                                 CHECK_OPSIZE (6);
11773                                 token = read32 (ip + 2);    
11774                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11775
11776                                 if (!cfg->vret_addr) {
11777                                         g_assert (cfg->ret_var_is_local);
11778
11779                                         EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
11780                                 } else {
11781                                         EMIT_NEW_RETLOADA (cfg, ins);
11782                                 }
11783                                 mini_emit_memory_copy (cfg, ins, sp [0], klass, TRUE, 0);
11784                                 
11785                                 if (sp != stack_start)
11786                                         UNVERIFIED;
11787                                 
11788                                 mini_profiler_emit_leave (cfg, sp [0]);
11789
11790                                 MONO_INST_NEW (cfg, ins, OP_BR);
11791                                 ins->inst_target_bb = end_bblock;
11792                                 MONO_ADD_INS (cfg->cbb, ins);
11793                                 link_bblock (cfg, cfg->cbb, end_bblock);
11794                                 start_new_bblock = 1;
11795                                 ip += 6;
11796                                 break;
11797                         }
11798                         case CEE_MONO_SAVE_LMF:
11799                         case CEE_MONO_RESTORE_LMF:
11800                                 ip += 2;
11801                                 break;
11802                         case CEE_MONO_CLASSCONST:
11803                                 CHECK_STACK_OVF (1);
11804                                 CHECK_OPSIZE (6);
11805                                 token = read32 (ip + 2);
11806                                 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
11807                                 *sp++ = ins;
11808                                 ip += 6;
11809                                 inline_costs += 10 * num_calls++;
11810                                 break;
11811                         case CEE_MONO_NOT_TAKEN:
11812                                 cfg->cbb->out_of_line = TRUE;
11813                                 ip += 2;
11814                                 break;
11815                         case CEE_MONO_TLS: {
11816                                 MonoTlsKey key;
11817
11818                                 CHECK_STACK_OVF (1);
11819                                 CHECK_OPSIZE (6);
11820                                 key = (MonoTlsKey)read32 (ip + 2);
11821                                 g_assert (key < TLS_KEY_NUM);
11822
11823                                 ins = mono_create_tls_get (cfg, key);
11824                                 g_assert (ins);
11825                                 ins->type = STACK_PTR;
11826                                 *sp++ = ins;
11827                                 ip += 6;
11828                                 break;
11829                         }
11830                         case CEE_MONO_DYN_CALL: {
11831                                 MonoCallInst *call;
11832
11833                                 /* It would be easier to call a trampoline, but that would put an
11834                                  * extra frame on the stack, confusing exception handling. So
11835                                  * implement it inline using an opcode for now.
11836                                  */
11837
11838                                 if (!cfg->dyn_call_var) {
11839                                         cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
11840                                         /* prevent it from being register allocated */
11841                                         cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
11842                                 }
11843
11844                                 /* Has to use a call inst since local regalloc expects it */
11845                                 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
11846                                 ins = (MonoInst*)call;
11847                                 sp -= 2;
11848                                 ins->sreg1 = sp [0]->dreg;
11849                                 ins->sreg2 = sp [1]->dreg;
11850                                 MONO_ADD_INS (cfg->cbb, ins);
11851
11852                                 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
11853                                 /* OP_DYN_CALL might need to allocate a dynamically sized param area */
11854                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
11855
11856                                 ip += 2;
11857                                 inline_costs += 10 * num_calls++;
11858
11859                                 break;
11860                         }
11861                         case CEE_MONO_MEMORY_BARRIER: {
11862                                 CHECK_OPSIZE (6);
11863                                 mini_emit_memory_barrier (cfg, (int)read32 (ip + 2));
11864                                 ip += 6;
11865                                 break;
11866                         }
11867                         case CEE_MONO_ATOMIC_STORE_I4: {
11868                                 g_assert (mono_arch_opcode_supported (OP_ATOMIC_STORE_I4));
11869
11870                                 CHECK_OPSIZE (6);
11871                                 CHECK_STACK (2);
11872                                 sp -= 2;
11873
11874                                 MONO_INST_NEW (cfg, ins, OP_ATOMIC_STORE_I4);
11875                                 ins->dreg = sp [0]->dreg;
11876                                 ins->sreg1 = sp [1]->dreg;
11877                                 ins->backend.memory_barrier_kind = (int) read32 (ip + 2);
11878                                 MONO_ADD_INS (cfg->cbb, ins);
11879
11880                                 ip += 6;
11881                                 break;
11882                         }
11883                         case CEE_MONO_JIT_ATTACH: {
11884                                 MonoInst *args [16], *domain_ins;
11885                                 MonoInst *ad_ins, *jit_tls_ins;
11886                                 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
11887
11888                                 g_assert (!mono_threads_is_blocking_transition_enabled ());
11889
11890                                 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
11891
11892                                 EMIT_NEW_PCONST (cfg, ins, NULL);
11893                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
11894
11895                                 ad_ins = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
11896                                 jit_tls_ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
11897
11898                                 if (ad_ins && jit_tls_ins) {
11899                                         NEW_BBLOCK (cfg, next_bb);
11900                                         NEW_BBLOCK (cfg, call_bb);
11901
11902                                         if (cfg->compile_aot) {
11903                                                 /* AOT code is only used in the root domain */
11904                                                 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
11905                                         } else {
11906                                                 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
11907                                         }
11908                                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
11909                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
11910
11911                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
11912                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
11913
11914                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
11915                                         MONO_START_BB (cfg, call_bb);
11916                                 }
11917
11918                                 /* AOT code is only used in the root domain */
11919                                 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
11920                                 if (cfg->compile_aot) {
11921                                         MonoInst *addr;
11922
11923                                         /*
11924                                          * This is called on unattached threads, so it cannot go through the trampoline
11925                                          * infrastructure. Use an indirect call through a got slot initialized at load time
11926                                          * instead.
11927                                          */
11928                                         EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_THREAD_ATTACH, NULL);
11929                                         ins = mini_emit_calli (cfg, helper_sig_jit_thread_attach, args, addr, NULL, NULL);
11930                                 } else {
11931                                         ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
11932                                 }
11933                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
11934
11935                                 if (next_bb)
11936                                         MONO_START_BB (cfg, next_bb);
11937
11938                                 /*
11939                                  * Parts of the initlocals code needs to come after this, since it might call methods like memset.
11940                                  */
11941                                 init_localsbb2 = cfg->cbb;
11942                                 NEW_BBLOCK (cfg, next_bb);
11943                                 MONO_START_BB (cfg, next_bb);
11944
11945                                 ip += 2;
11946                                 break;
11947                         }
11948                         case CEE_MONO_JIT_DETACH: {
11949                                 MonoInst *args [16];
11950
11951                                 /* Restore the original domain */
11952                                 dreg = alloc_ireg (cfg);
11953                                 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
11954                                 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
11955                                 ip += 2;
11956                                 break;
11957                         }
11958                         case CEE_MONO_CALLI_EXTRA_ARG: {
11959                                 MonoInst *addr;
11960                                 MonoMethodSignature *fsig;
11961                                 MonoInst *arg;
11962
11963                                 /*
11964                                  * This is the same as CEE_CALLI, but passes an additional argument
11965                                  * to the called method in llvmonly mode.
11966                                  * This is only used by delegate invoke wrappers to call the
11967                                  * actual delegate method.
11968                                  */
11969                                 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
11970
11971                                 CHECK_OPSIZE (6);
11972                                 token = read32 (ip + 2);
11973
11974                                 ins = NULL;
11975
11976                                 cmethod = NULL;
11977                                 CHECK_STACK (1);
11978                                 --sp;
11979                                 addr = *sp;
11980                                 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
11981                                 CHECK_CFG_ERROR;
11982
11983                                 if (cfg->llvm_only)
11984                                         cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
11985
11986                                 n = fsig->param_count + fsig->hasthis + 1;
11987
11988                                 CHECK_STACK (n);
11989
11990                                 sp -= n;
11991                                 arg = sp [n - 1];
11992
11993                                 if (cfg->llvm_only) {
11994                                         /*
11995                                          * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
11996                                          * cconv. This is set by mono_init_delegate ().
11997                                          */
11998                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
11999                                                 MonoInst *callee = addr;
12000                                                 MonoInst *call, *localloc_ins;
12001                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12002                                                 int low_bit_reg = alloc_preg (cfg);
12003
12004                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12005                                                 NEW_BBLOCK (cfg, end_bb);
12006
12007                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12008                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12009                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12010
12011                                                 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12012                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12013                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12014                                                 /*
12015                                                  * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12016                                                  */
12017                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12018                                                 ins->dreg = alloc_preg (cfg);
12019                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12020                                                 MONO_ADD_INS (cfg->cbb, ins);
12021                                                 localloc_ins = ins;
12022                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12023                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12024                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12025
12026                                                 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12027                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12028
12029                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12030                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12031                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12032                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12033                                                 ins->dreg = call->dreg;
12034
12035                                                 MONO_START_BB (cfg, end_bb);
12036                                         } else {
12037                                                 /* Caller uses a normal calling conv */
12038
12039                                                 MonoInst *callee = addr;
12040                                                 MonoInst *call, *localloc_ins;
12041                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12042                                                 int low_bit_reg = alloc_preg (cfg);
12043
12044                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12045                                                 NEW_BBLOCK (cfg, end_bb);
12046
12047                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12048                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12049                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12050
12051                                                 /* Normal case: callee uses a normal cconv, no conversion is needed */
12052                                                 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12053                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12054                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12055                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12056                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12057                                                 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12058                                                 MONO_ADD_INS (cfg->cbb, addr);
12059                                                 /*
12060                                                  * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12061                                                  */
12062                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12063                                                 ins->dreg = alloc_preg (cfg);
12064                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12065                                                 MONO_ADD_INS (cfg->cbb, ins);
12066                                                 localloc_ins = ins;
12067                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12068                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12069                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12070
12071                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12072                                                 ins->dreg = call->dreg;
12073                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12074
12075                                                 MONO_START_BB (cfg, end_bb);
12076                                         }
12077                                 } else {
12078                                         /* Same as CEE_CALLI */
12079                                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12080                                                 /*
12081                                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
12082                                                  */
12083                                                 MonoInst *callee = addr;
12084
12085                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12086                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12087                                                 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12088                                         } else {
12089                                                 ins = (MonoInst*)mini_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12090                                         }
12091                                 }
12092
12093                                 if (!MONO_TYPE_IS_VOID (fsig->ret))
12094                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12095
12096                                 CHECK_CFG_EXCEPTION;
12097
12098                                 ip += 6;
12099                                 ins_flag = 0;
12100                                 constrained_class = NULL;
12101                                 break;
12102                         }
12103                         case CEE_MONO_LDDOMAIN:
12104                                 CHECK_STACK_OVF (1);
12105                                 EMIT_NEW_PCONST (cfg, ins, cfg->compile_aot ? NULL : cfg->domain);
12106                                 ip += 2;
12107                                 *sp++ = ins;
12108                                 break;
12109                         case CEE_MONO_GET_LAST_ERROR:
12110                                 CHECK_OPSIZE (2);
12111                                 CHECK_STACK_OVF (1);
12112
12113                                 MONO_INST_NEW (cfg, ins, OP_GET_LAST_ERROR);
12114                                 ins->dreg = alloc_dreg (cfg, STACK_I4);
12115                                 ins->type = STACK_I4;
12116                                 MONO_ADD_INS (cfg->cbb, ins);
12117
12118                                 ip += 2;
12119                                 *sp++ = ins;
12120                                 break;
12121                         case CEE_MONO_GET_RGCTX_ARG:
12122                                 CHECK_OPSIZE (2);
12123                                 CHECK_STACK_OVF (1);
12124
12125                                 mono_create_rgctx_var (cfg);
12126
12127                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
12128                                 ins->dreg = alloc_dreg (cfg, STACK_PTR);
12129                                 ins->sreg1 = cfg->rgctx_var->dreg;
12130                                 ins->type = STACK_PTR;
12131                                 MONO_ADD_INS (cfg->cbb, ins);
12132
12133                                 ip += 2;
12134                                 *sp++ = ins;
12135                                 break;
12136                         default:
12137                                 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12138                                 break;
12139                         }
12140                         break;
12141                 }
12142
12143                 case CEE_PREFIX1: {
12144                         CHECK_OPSIZE (2);
12145                         switch (ip [1]) {
12146                         case CEE_ARGLIST: {
12147                                 /* somewhat similar to LDTOKEN */
12148                                 MonoInst *addr, *vtvar;
12149                                 CHECK_STACK_OVF (1);
12150                                 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL); 
12151
12152                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12153                                 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12154
12155                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12156                                 ins->type = STACK_VTYPE;
12157                                 ins->klass = mono_defaults.argumenthandle_class;
12158                                 *sp++ = ins;
12159                                 ip += 2;
12160                                 break;
12161                         }
12162                         case CEE_CEQ:
12163                         case CEE_CGT:
12164                         case CEE_CGT_UN:
12165                         case CEE_CLT:
12166                         case CEE_CLT_UN: {
12167                                 MonoInst *cmp, *arg1, *arg2;
12168
12169                                 CHECK_STACK (2);
12170                                 sp -= 2;
12171                                 arg1 = sp [0];
12172                                 arg2 = sp [1];
12173
12174                                 /*
12175                                  * The following transforms:
12176                                  *    CEE_CEQ    into OP_CEQ
12177                                  *    CEE_CGT    into OP_CGT
12178                                  *    CEE_CGT_UN into OP_CGT_UN
12179                                  *    CEE_CLT    into OP_CLT
12180                                  *    CEE_CLT_UN into OP_CLT_UN
12181                                  */
12182                                 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12183
12184                                 MONO_INST_NEW (cfg, ins, cmp->opcode);
12185                                 cmp->sreg1 = arg1->dreg;
12186                                 cmp->sreg2 = arg2->dreg;
12187                                 type_from_op (cfg, cmp, arg1, arg2);
12188                                 CHECK_TYPE (cmp);
12189                                 add_widen_op (cfg, cmp, &arg1, &arg2);
12190                                 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
12191                                         cmp->opcode = OP_LCOMPARE;
12192                                 else if (arg1->type == STACK_R4)
12193                                         cmp->opcode = OP_RCOMPARE;
12194                                 else if (arg1->type == STACK_R8)
12195                                         cmp->opcode = OP_FCOMPARE;
12196                                 else
12197                                         cmp->opcode = OP_ICOMPARE;
12198                                 MONO_ADD_INS (cfg->cbb, cmp);
12199                                 ins->type = STACK_I4;
12200                                 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
12201                                 type_from_op (cfg, ins, arg1, arg2);
12202
12203                                 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
12204                                         /*
12205                                          * The backends expect the fceq opcodes to do the
12206                                          * comparison too.
12207                                          */
12208                                         ins->sreg1 = cmp->sreg1;
12209                                         ins->sreg2 = cmp->sreg2;
12210                                         NULLIFY_INS (cmp);
12211                                 }
12212                                 MONO_ADD_INS (cfg->cbb, ins);
12213                                 *sp++ = ins;
12214                                 ip += 2;
12215                                 break;
12216                         }
12217                         case CEE_LDFTN: {
12218                                 MonoInst *argconst;
12219                                 MonoMethod *cil_method;
12220
12221                                 CHECK_STACK_OVF (1);
12222                                 CHECK_OPSIZE (6);
12223                                 n = read32 (ip + 2);
12224                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12225                                 CHECK_CFG_ERROR;
12226
12227                                 mono_class_init (cmethod->klass);
12228
12229                                 mono_save_token_info (cfg, image, n, cmethod);
12230
12231                                 context_used = mini_method_check_context_used (cfg, cmethod);
12232
12233                                 cil_method = cmethod;
12234                                 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
12235                                         emit_method_access_failure (cfg, method, cil_method);
12236
12237                                 if (mono_security_core_clr_enabled ())
12238                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12239
12240                                 /* 
12241                                  * Optimize the common case of ldftn+delegate creation
12242                                  */
12243                                 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
12244                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12245                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12246                                                 MonoInst *target_ins, *handle_ins;
12247                                                 MonoMethod *invoke;
12248                                                 int invoke_context_used;
12249
12250                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
12251                                                 if (!invoke || !mono_method_signature (invoke))
12252                                                         LOAD_ERROR;
12253
12254                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12255
12256                                                 target_ins = sp [-1];
12257
12258                                                 if (mono_security_core_clr_enabled ())
12259                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12260
12261                                                 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
12262                                                         /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
12263                                                         if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
12264                                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
12265                                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
12266                                                         }
12267                                                 }
12268
12269                                                 /* FIXME: SGEN support */
12270                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
12271                                                         ip += 6;
12272                                                         if (cfg->verbose_level > 3)
12273                                                                 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));
12274                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
12275                                                                 sp --;
12276                                                                 *sp = handle_ins;
12277                                                                 CHECK_CFG_EXCEPTION;
12278                                                                 ip += 5;
12279                                                                 sp ++;
12280                                                                 break;
12281                                                         }
12282                                                         ip -= 6;
12283                                                 }
12284                                         }
12285                                 }
12286
12287                                 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
12288                                 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
12289                                 *sp++ = ins;
12290                                 
12291                                 ip += 6;
12292                                 inline_costs += 10 * num_calls++;
12293                                 break;
12294                         }
12295                         case CEE_LDVIRTFTN: {
12296                                 MonoInst *args [2];
12297
12298                                 CHECK_STACK (1);
12299                                 CHECK_OPSIZE (6);
12300                                 n = read32 (ip + 2);
12301                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12302                                 CHECK_CFG_ERROR;
12303
12304                                 mono_class_init (cmethod->klass);
12305  
12306                                 context_used = mini_method_check_context_used (cfg, cmethod);
12307
12308                                 if (mono_security_core_clr_enabled ())
12309                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12310
12311                                 /*
12312                                  * Optimize the common case of ldvirtftn+delegate creation
12313                                  */
12314                                 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)) {
12315                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12316                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12317                                                 MonoInst *target_ins, *handle_ins;
12318                                                 MonoMethod *invoke;
12319                                                 int invoke_context_used;
12320                                                 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
12321
12322                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
12323                                                 if (!invoke || !mono_method_signature (invoke))
12324                                                         LOAD_ERROR;
12325
12326                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12327
12328                                                 target_ins = sp [-1];
12329
12330                                                 if (mono_security_core_clr_enabled ())
12331                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12332
12333                                                 /* FIXME: SGEN support */
12334                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
12335                                                         ip += 6;
12336                                                         if (cfg->verbose_level > 3)
12337                                                                 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));
12338                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
12339                                                                 sp -= 2;
12340                                                                 *sp = handle_ins;
12341                                                                 CHECK_CFG_EXCEPTION;
12342                                                                 ip += 5;
12343                                                                 sp ++;
12344                                                                 break;
12345                                                         }
12346                                                         ip -= 6;
12347                                                 }
12348                                         }
12349                                 }
12350
12351                                 --sp;
12352                                 args [0] = *sp;
12353
12354                                 args [1] = emit_get_rgctx_method (cfg, context_used,
12355                                                                                                   cmethod, MONO_RGCTX_INFO_METHOD);
12356
12357                                 if (context_used)
12358                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
12359                                 else
12360                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
12361
12362                                 ip += 6;
12363                                 inline_costs += 10 * num_calls++;
12364                                 break;
12365                         }
12366                         case CEE_LDARG:
12367                                 CHECK_STACK_OVF (1);
12368                                 CHECK_OPSIZE (4);
12369                                 n = read16 (ip + 2);
12370                                 CHECK_ARG (n);
12371                                 EMIT_NEW_ARGLOAD (cfg, ins, n);
12372                                 *sp++ = ins;
12373                                 ip += 4;
12374                                 break;
12375                         case CEE_LDARGA:
12376                                 CHECK_STACK_OVF (1);
12377                                 CHECK_OPSIZE (4);
12378                                 n = read16 (ip + 2);
12379                                 CHECK_ARG (n);
12380                                 NEW_ARGLOADA (cfg, ins, n);
12381                                 MONO_ADD_INS (cfg->cbb, ins);
12382                                 *sp++ = ins;
12383                                 ip += 4;
12384                                 break;
12385                         case CEE_STARG:
12386                                 CHECK_STACK (1);
12387                                 --sp;
12388                                 CHECK_OPSIZE (4);
12389                                 n = read16 (ip + 2);
12390                                 CHECK_ARG (n);
12391                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
12392                                         UNVERIFIED;
12393                                 emit_starg_ir (cfg, sp, n);
12394                                 ip += 4;
12395                                 break;
12396                         case CEE_LDLOC:
12397                                 CHECK_STACK_OVF (1);
12398                                 CHECK_OPSIZE (4);
12399                                 n = read16 (ip + 2);
12400                                 CHECK_LOCAL (n);
12401                                 if ((ip [4] == CEE_LDFLD) && ip_in_bb (cfg, cfg->cbb, ip + 4) && header->locals [n]->type == MONO_TYPE_VALUETYPE) {
12402                                         /* Avoid loading a struct just to load one of its fields */
12403                                         EMIT_NEW_LOCLOADA (cfg, ins, n);
12404                                 } else {
12405                                         EMIT_NEW_LOCLOAD (cfg, ins, n);
12406                                 }
12407                                 *sp++ = ins;
12408                                 ip += 4;
12409                                 break;
12410                         case CEE_LDLOCA: {
12411                                 unsigned char *tmp_ip;
12412                                 CHECK_STACK_OVF (1);
12413                                 CHECK_OPSIZE (4);
12414                                 n = read16 (ip + 2);
12415                                 CHECK_LOCAL (n);
12416
12417                                 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
12418                                         ip = tmp_ip;
12419                                         inline_costs += 1;
12420                                         break;
12421                                 }                       
12422                                 
12423                                 EMIT_NEW_LOCLOADA (cfg, ins, n);
12424                                 *sp++ = ins;
12425                                 ip += 4;
12426                                 break;
12427                         }
12428                         case CEE_STLOC:
12429                                 CHECK_STACK (1);
12430                                 --sp;
12431                                 CHECK_OPSIZE (4);
12432                                 n = read16 (ip + 2);
12433                                 CHECK_LOCAL (n);
12434                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
12435                                         UNVERIFIED;
12436                                 emit_stloc_ir (cfg, sp, header, n);
12437                                 ip += 4;
12438                                 inline_costs += 1;
12439                                 break;
12440                         case CEE_LOCALLOC: {
12441                                 CHECK_STACK (1);
12442                                 MonoBasicBlock *non_zero_bb, *end_bb;
12443                                 int alloc_ptr = alloc_preg (cfg);
12444                                 --sp;
12445                                 if (sp != stack_start) 
12446                                         UNVERIFIED;
12447                                 if (cfg->method != method) 
12448                                         /* 
12449                                          * Inlining this into a loop in a parent could lead to 
12450                                          * stack overflows which is different behavior than the
12451                                          * non-inlined case, thus disable inlining in this case.
12452                                          */
12453                                         INLINE_FAILURE("localloc");
12454
12455                                 NEW_BBLOCK (cfg, non_zero_bb);
12456                                 NEW_BBLOCK (cfg, end_bb);
12457
12458                                 /* if size != zero */
12459                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
12460                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_zero_bb);
12461
12462                                 //size is zero, so result is NULL
12463                                 MONO_EMIT_NEW_PCONST (cfg, alloc_ptr, NULL);
12464                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12465
12466                                 MONO_START_BB (cfg, non_zero_bb);
12467                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
12468                                 ins->dreg = alloc_ptr;
12469                                 ins->sreg1 = sp [0]->dreg;
12470                                 ins->type = STACK_PTR;
12471                                 MONO_ADD_INS (cfg->cbb, ins);
12472
12473                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12474                                 if (init_locals)
12475                                         ins->flags |= MONO_INST_INIT;
12476
12477                                 MONO_START_BB (cfg, end_bb);
12478                                 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, alloc_preg (cfg), alloc_ptr);
12479                                 ins->type = STACK_PTR;
12480
12481                                 *sp++ = ins;
12482                                 ip += 2;
12483                                 break;
12484                         }
12485                         case CEE_ENDFILTER: {
12486                                 MonoExceptionClause *clause, *nearest;
12487                                 int cc;
12488
12489                                 CHECK_STACK (1);
12490                                 --sp;
12491                                 if ((sp != stack_start) || (sp [0]->type != STACK_I4)) 
12492                                         UNVERIFIED;
12493                                 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
12494                                 ins->sreg1 = (*sp)->dreg;
12495                                 MONO_ADD_INS (cfg->cbb, ins);
12496                                 start_new_bblock = 1;
12497                                 ip += 2;
12498
12499                                 nearest = NULL;
12500                                 for (cc = 0; cc < header->num_clauses; ++cc) {
12501                                         clause = &header->clauses [cc];
12502                                         if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
12503                                                 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
12504                                             (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
12505                                                 nearest = clause;
12506                                 }
12507                                 g_assert (nearest);
12508                                 if ((ip - header->code) != nearest->handler_offset)
12509                                         UNVERIFIED;
12510
12511                                 break;
12512                         }
12513                         case CEE_UNALIGNED_:
12514                                 ins_flag |= MONO_INST_UNALIGNED;
12515                                 /* FIXME: record alignment? we can assume 1 for now */
12516                                 CHECK_OPSIZE (3);
12517                                 ip += 3;
12518                                 break;
12519                         case CEE_VOLATILE_:
12520                                 ins_flag |= MONO_INST_VOLATILE;
12521                                 ip += 2;
12522                                 break;
12523                         case CEE_TAIL_:
12524                                 ins_flag   |= MONO_INST_TAILCALL;
12525                                 cfg->flags |= MONO_CFG_HAS_TAIL;
12526                                 /* Can't inline tail calls at this time */
12527                                 inline_costs += 100000;
12528                                 ip += 2;
12529                                 break;
12530                         case CEE_INITOBJ:
12531                                 CHECK_STACK (1);
12532                                 --sp;
12533                                 CHECK_OPSIZE (6);
12534                                 token = read32 (ip + 2);
12535                                 klass = mini_get_class (method, token, generic_context);
12536                                 CHECK_TYPELOAD (klass);
12537                                 if (generic_class_is_reference_type (cfg, klass))
12538                                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
12539                                 else
12540                                         mini_emit_initobj (cfg, *sp, NULL, klass);
12541                                 ip += 6;
12542                                 inline_costs += 1;
12543                                 break;
12544                         case CEE_CONSTRAINED_:
12545                                 CHECK_OPSIZE (6);
12546                                 token = read32 (ip + 2);
12547                                 constrained_class = mini_get_class (method, token, generic_context);
12548                                 CHECK_TYPELOAD (constrained_class);
12549                                 ip += 6;
12550                                 break;
12551                         case CEE_CPBLK:
12552                                 CHECK_STACK (3);
12553                                 sp -= 3;
12554                                 mini_emit_memory_copy_bytes (cfg, sp [0], sp [1], sp [2], ins_flag);
12555                                 ip += 2;
12556                                 ins_flag = 0;
12557                                 inline_costs += 1;
12558                                 break;
12559                         case CEE_INITBLK:
12560                                 CHECK_STACK (3);
12561                                 sp -= 3;
12562                                 mini_emit_memory_init_bytes (cfg, sp [0], sp [1], sp [2], ins_flag);
12563                                 ip += 2;
12564                                 ins_flag = 0;
12565                                 inline_costs += 1;
12566                                 break;
12567                         case CEE_NO_:
12568                                 CHECK_OPSIZE (3);
12569                                 if (ip [2] & 0x1)
12570                                         ins_flag |= MONO_INST_NOTYPECHECK;
12571                                 if (ip [2] & 0x2)
12572                                         ins_flag |= MONO_INST_NORANGECHECK;
12573                                 /* we ignore the no-nullcheck for now since we
12574                                  * really do it explicitly only when doing callvirt->call
12575                                  */
12576                                 ip += 3;
12577                                 break;
12578                         case CEE_RETHROW: {
12579                                 MonoInst *load;
12580                                 int handler_offset = -1;
12581
12582                                 for (i = 0; i < header->num_clauses; ++i) {
12583                                         MonoExceptionClause *clause = &header->clauses [i];
12584                                         if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
12585                                                 handler_offset = clause->handler_offset;
12586                                                 break;
12587                                         }
12588                                 }
12589
12590                                 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
12591
12592                                 if (handler_offset == -1)
12593                                         UNVERIFIED;
12594
12595                                 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
12596                                 MONO_INST_NEW (cfg, ins, OP_RETHROW);
12597                                 ins->sreg1 = load->dreg;
12598                                 MONO_ADD_INS (cfg->cbb, ins);
12599
12600                                 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12601                                 MONO_ADD_INS (cfg->cbb, ins);
12602
12603                                 sp = stack_start;
12604                                 link_bblock (cfg, cfg->cbb, end_bblock);
12605                                 start_new_bblock = 1;
12606                                 ip += 2;
12607                                 break;
12608                         }
12609                         case CEE_SIZEOF: {
12610                                 guint32 val;
12611                                 int ialign;
12612
12613                                 CHECK_STACK_OVF (1);
12614                                 CHECK_OPSIZE (6);
12615                                 token = read32 (ip + 2);
12616                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
12617                                         MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
12618                                         CHECK_CFG_ERROR;
12619
12620                                         val = mono_type_size (type, &ialign);
12621                                 } else {
12622                                         MonoClass *klass = mini_get_class (method, token, generic_context);
12623                                         CHECK_TYPELOAD (klass);
12624
12625                                         val = mono_type_size (&klass->byval_arg, &ialign);
12626
12627                                         if (mini_is_gsharedvt_klass (klass))
12628                                                 GSHAREDVT_FAILURE (*ip);
12629                                 }
12630                                 EMIT_NEW_ICONST (cfg, ins, val);
12631                                 *sp++= ins;
12632                                 ip += 6;
12633                                 break;
12634                         }
12635                         case CEE_REFANYTYPE: {
12636                                 MonoInst *src_var, *src;
12637
12638                                 GSHAREDVT_FAILURE (*ip);
12639
12640                                 CHECK_STACK (1);
12641                                 --sp;
12642
12643                                 // FIXME:
12644                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12645                                 if (!src_var)
12646                                         src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12647                                 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12648                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
12649                                 *sp++ = ins;
12650                                 ip += 2;
12651                                 break;
12652                         }
12653                         case CEE_READONLY_:
12654                                 readonly = TRUE;
12655                                 ip += 2;
12656                                 break;
12657
12658                         case CEE_UNUSED56:
12659                         case CEE_UNUSED57:
12660                         case CEE_UNUSED70:
12661                         case CEE_UNUSED:
12662                         case CEE_UNUSED99:
12663                                 UNVERIFIED;
12664                                 
12665                         default:
12666                                 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
12667                                 UNVERIFIED;
12668                         }
12669                         break;
12670                 }
12671                 case CEE_UNUSED58:
12672                 case CEE_UNUSED1:
12673                         UNVERIFIED;
12674
12675                 default:
12676                         g_warning ("opcode 0x%02x not handled", *ip);
12677                         UNVERIFIED;
12678                 }
12679         }
12680         if (start_new_bblock != 1)
12681                 UNVERIFIED;
12682
12683         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
12684         if (cfg->cbb->next_bb) {
12685                 /* This could already be set because of inlining, #693905 */
12686                 MonoBasicBlock *bb = cfg->cbb;
12687
12688                 while (bb->next_bb)
12689                         bb = bb->next_bb;
12690                 bb->next_bb = end_bblock;
12691         } else {
12692                 cfg->cbb->next_bb = end_bblock;
12693         }
12694
12695         if (cfg->method == method && cfg->domainvar) {
12696                 MonoInst *store;
12697                 MonoInst *get_domain;
12698
12699                 cfg->cbb = init_localsbb;
12700
12701                 get_domain = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12702                 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
12703                 MONO_ADD_INS (cfg->cbb, store);
12704         }
12705
12706 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
12707         if (cfg->compile_aot)
12708                 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
12709                 mono_get_got_var (cfg);
12710 #endif
12711
12712         if (cfg->method == method && cfg->got_var)
12713                 mono_emit_load_got_addr (cfg);
12714
12715         if (init_localsbb) {
12716                 cfg->cbb = init_localsbb;
12717                 cfg->ip = NULL;
12718                 for (i = 0; i < header->num_locals; ++i) {
12719                         /*
12720                          * Vtype initialization might need to be done after CEE_JIT_ATTACH, since it can make calls to memset (),
12721                          * which need the trampoline code to work.
12722                          */
12723                         if (MONO_TYPE_ISSTRUCT (header->locals [i]))
12724                                 cfg->cbb = init_localsbb2;
12725                         else
12726                                 cfg->cbb = init_localsbb;
12727                         emit_init_local (cfg, i, header->locals [i], init_locals);
12728                 }
12729         }
12730
12731         if (cfg->init_ref_vars && cfg->method == method) {
12732                 /* Emit initialization for ref vars */
12733                 // FIXME: Avoid duplication initialization for IL locals.
12734                 for (i = 0; i < cfg->num_varinfo; ++i) {
12735                         MonoInst *ins = cfg->varinfo [i];
12736
12737                         if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
12738                                 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
12739                 }
12740         }
12741
12742         if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
12743                 cfg->cbb = init_localsbb;
12744                 emit_push_lmf (cfg);
12745         }
12746
12747         cfg->cbb = init_localsbb;
12748         mini_profiler_emit_enter (cfg);
12749
12750         if (seq_points) {
12751                 MonoBasicBlock *bb;
12752
12753                 /*
12754                  * Make seq points at backward branch targets interruptable.
12755                  */
12756                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12757                         if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
12758                                 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
12759         }
12760
12761         /* Add a sequence point for method entry/exit events */
12762         if (seq_points && cfg->gen_sdb_seq_points) {
12763                 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
12764                 MONO_ADD_INS (init_localsbb, ins);
12765                 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
12766                 MONO_ADD_INS (cfg->bb_exit, ins);
12767         }
12768
12769         /*
12770          * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
12771          * the code they refer to was dead (#11880).
12772          */
12773         if (sym_seq_points) {
12774                 for (i = 0; i < header->code_size; ++i) {
12775                         if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
12776                                 MonoInst *ins;
12777
12778                                 NEW_SEQ_POINT (cfg, ins, i, FALSE);
12779                                 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
12780                         }
12781                 }
12782         }
12783
12784         cfg->ip = NULL;
12785
12786         if (cfg->method == method) {
12787                 MonoBasicBlock *bb;
12788                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12789                         if (bb == cfg->bb_init)
12790                                 bb->region = -1;
12791                         else
12792                                 bb->region = mono_find_block_region (cfg, bb->real_offset);
12793                         if (cfg->spvars)
12794                                 mono_create_spvar_for_region (cfg, bb->region);
12795                         if (cfg->verbose_level > 2)
12796                                 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
12797                 }
12798         } else {
12799                 MonoBasicBlock *bb;
12800                 /* get_most_deep_clause () in mini-llvm.c depends on this for inlined bblocks */
12801                 for (bb = start_bblock; bb != end_bblock; bb  = bb->next_bb) {
12802                         bb->real_offset = inline_offset;
12803                 }
12804         }
12805
12806         if (inline_costs < 0) {
12807                 char *mname;
12808
12809                 /* Method is too large */
12810                 mname = mono_method_full_name (method, TRUE);
12811                 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
12812                 g_free (mname);
12813         }
12814
12815         if ((cfg->verbose_level > 2) && (cfg->method == method)) 
12816                 mono_print_code (cfg, "AFTER METHOD-TO-IR");
12817
12818         goto cleanup;
12819
12820 mono_error_exit:
12821         g_assert (!mono_error_ok (&cfg->error));
12822         goto cleanup;
12823  
12824  exception_exit:
12825         g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
12826         goto cleanup;
12827
12828  unverified:
12829         set_exception_type_from_invalid_il (cfg, method, ip);
12830         goto cleanup;
12831
12832  cleanup:
12833         g_slist_free (class_inits);
12834         mono_basic_block_free (original_bb);
12835         cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
12836         if (cfg->exception_type)
12837                 return -1;
12838         else
12839                 return inline_costs;
12840 }
12841
12842 static int
12843 store_membase_reg_to_store_membase_imm (int opcode)
12844 {
12845         switch (opcode) {
12846         case OP_STORE_MEMBASE_REG:
12847                 return OP_STORE_MEMBASE_IMM;
12848         case OP_STOREI1_MEMBASE_REG:
12849                 return OP_STOREI1_MEMBASE_IMM;
12850         case OP_STOREI2_MEMBASE_REG:
12851                 return OP_STOREI2_MEMBASE_IMM;
12852         case OP_STOREI4_MEMBASE_REG:
12853                 return OP_STOREI4_MEMBASE_IMM;
12854         case OP_STOREI8_MEMBASE_REG:
12855                 return OP_STOREI8_MEMBASE_IMM;
12856         default:
12857                 g_assert_not_reached ();
12858         }
12859
12860         return -1;
12861 }               
12862
12863 int
12864 mono_op_to_op_imm (int opcode)
12865 {
12866         switch (opcode) {
12867         case OP_IADD:
12868                 return OP_IADD_IMM;
12869         case OP_ISUB:
12870                 return OP_ISUB_IMM;
12871         case OP_IDIV:
12872                 return OP_IDIV_IMM;
12873         case OP_IDIV_UN:
12874                 return OP_IDIV_UN_IMM;
12875         case OP_IREM:
12876                 return OP_IREM_IMM;
12877         case OP_IREM_UN:
12878                 return OP_IREM_UN_IMM;
12879         case OP_IMUL:
12880                 return OP_IMUL_IMM;
12881         case OP_IAND:
12882                 return OP_IAND_IMM;
12883         case OP_IOR:
12884                 return OP_IOR_IMM;
12885         case OP_IXOR:
12886                 return OP_IXOR_IMM;
12887         case OP_ISHL:
12888                 return OP_ISHL_IMM;
12889         case OP_ISHR:
12890                 return OP_ISHR_IMM;
12891         case OP_ISHR_UN:
12892                 return OP_ISHR_UN_IMM;
12893
12894         case OP_LADD:
12895                 return OP_LADD_IMM;
12896         case OP_LSUB:
12897                 return OP_LSUB_IMM;
12898         case OP_LAND:
12899                 return OP_LAND_IMM;
12900         case OP_LOR:
12901                 return OP_LOR_IMM;
12902         case OP_LXOR:
12903                 return OP_LXOR_IMM;
12904         case OP_LSHL:
12905                 return OP_LSHL_IMM;
12906         case OP_LSHR:
12907                 return OP_LSHR_IMM;
12908         case OP_LSHR_UN:
12909                 return OP_LSHR_UN_IMM;
12910 #if SIZEOF_REGISTER == 8
12911         case OP_LREM:
12912                 return OP_LREM_IMM;
12913 #endif
12914
12915         case OP_COMPARE:
12916                 return OP_COMPARE_IMM;
12917         case OP_ICOMPARE:
12918                 return OP_ICOMPARE_IMM;
12919         case OP_LCOMPARE:
12920                 return OP_LCOMPARE_IMM;
12921
12922         case OP_STORE_MEMBASE_REG:
12923                 return OP_STORE_MEMBASE_IMM;
12924         case OP_STOREI1_MEMBASE_REG:
12925                 return OP_STOREI1_MEMBASE_IMM;
12926         case OP_STOREI2_MEMBASE_REG:
12927                 return OP_STOREI2_MEMBASE_IMM;
12928         case OP_STOREI4_MEMBASE_REG:
12929                 return OP_STOREI4_MEMBASE_IMM;
12930
12931 #if defined(TARGET_X86) || defined (TARGET_AMD64)
12932         case OP_X86_PUSH:
12933                 return OP_X86_PUSH_IMM;
12934         case OP_X86_COMPARE_MEMBASE_REG:
12935                 return OP_X86_COMPARE_MEMBASE_IMM;
12936 #endif
12937 #if defined(TARGET_AMD64)
12938         case OP_AMD64_ICOMPARE_MEMBASE_REG:
12939                 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
12940 #endif
12941         case OP_VOIDCALL_REG:
12942                 return OP_VOIDCALL;
12943         case OP_CALL_REG:
12944                 return OP_CALL;
12945         case OP_LCALL_REG:
12946                 return OP_LCALL;
12947         case OP_FCALL_REG:
12948                 return OP_FCALL;
12949         case OP_LOCALLOC:
12950                 return OP_LOCALLOC_IMM;
12951         }
12952
12953         return -1;
12954 }
12955
12956 static int
12957 ldind_to_load_membase (int opcode)
12958 {
12959         switch (opcode) {
12960         case CEE_LDIND_I1:
12961                 return OP_LOADI1_MEMBASE;
12962         case CEE_LDIND_U1:
12963                 return OP_LOADU1_MEMBASE;
12964         case CEE_LDIND_I2:
12965                 return OP_LOADI2_MEMBASE;
12966         case CEE_LDIND_U2:
12967                 return OP_LOADU2_MEMBASE;
12968         case CEE_LDIND_I4:
12969                 return OP_LOADI4_MEMBASE;
12970         case CEE_LDIND_U4:
12971                 return OP_LOADU4_MEMBASE;
12972         case CEE_LDIND_I:
12973                 return OP_LOAD_MEMBASE;
12974         case CEE_LDIND_REF:
12975                 return OP_LOAD_MEMBASE;
12976         case CEE_LDIND_I8:
12977                 return OP_LOADI8_MEMBASE;
12978         case CEE_LDIND_R4:
12979                 return OP_LOADR4_MEMBASE;
12980         case CEE_LDIND_R8:
12981                 return OP_LOADR8_MEMBASE;
12982         default:
12983                 g_assert_not_reached ();
12984         }
12985
12986         return -1;
12987 }
12988
12989 static int
12990 stind_to_store_membase (int opcode)
12991 {
12992         switch (opcode) {
12993         case CEE_STIND_I1:
12994                 return OP_STOREI1_MEMBASE_REG;
12995         case CEE_STIND_I2:
12996                 return OP_STOREI2_MEMBASE_REG;
12997         case CEE_STIND_I4:
12998                 return OP_STOREI4_MEMBASE_REG;
12999         case CEE_STIND_I:
13000         case CEE_STIND_REF:
13001                 return OP_STORE_MEMBASE_REG;
13002         case CEE_STIND_I8:
13003                 return OP_STOREI8_MEMBASE_REG;
13004         case CEE_STIND_R4:
13005                 return OP_STORER4_MEMBASE_REG;
13006         case CEE_STIND_R8:
13007                 return OP_STORER8_MEMBASE_REG;
13008         default:
13009                 g_assert_not_reached ();
13010         }
13011
13012         return -1;
13013 }
13014
13015 int
13016 mono_load_membase_to_load_mem (int opcode)
13017 {
13018         // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13019 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13020         switch (opcode) {
13021         case OP_LOAD_MEMBASE:
13022                 return OP_LOAD_MEM;
13023         case OP_LOADU1_MEMBASE:
13024                 return OP_LOADU1_MEM;
13025         case OP_LOADU2_MEMBASE:
13026                 return OP_LOADU2_MEM;
13027         case OP_LOADI4_MEMBASE:
13028                 return OP_LOADI4_MEM;
13029         case OP_LOADU4_MEMBASE:
13030                 return OP_LOADU4_MEM;
13031 #if SIZEOF_REGISTER == 8
13032         case OP_LOADI8_MEMBASE:
13033                 return OP_LOADI8_MEM;
13034 #endif
13035         }
13036 #endif
13037
13038         return -1;
13039 }
13040
13041 static inline int
13042 op_to_op_dest_membase (int store_opcode, int opcode)
13043 {
13044 #if defined(TARGET_X86)
13045         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13046                 return -1;
13047
13048         switch (opcode) {
13049         case OP_IADD:
13050                 return OP_X86_ADD_MEMBASE_REG;
13051         case OP_ISUB:
13052                 return OP_X86_SUB_MEMBASE_REG;
13053         case OP_IAND:
13054                 return OP_X86_AND_MEMBASE_REG;
13055         case OP_IOR:
13056                 return OP_X86_OR_MEMBASE_REG;
13057         case OP_IXOR:
13058                 return OP_X86_XOR_MEMBASE_REG;
13059         case OP_ADD_IMM:
13060         case OP_IADD_IMM:
13061                 return OP_X86_ADD_MEMBASE_IMM;
13062         case OP_SUB_IMM:
13063         case OP_ISUB_IMM:
13064                 return OP_X86_SUB_MEMBASE_IMM;
13065         case OP_AND_IMM:
13066         case OP_IAND_IMM:
13067                 return OP_X86_AND_MEMBASE_IMM;
13068         case OP_OR_IMM:
13069         case OP_IOR_IMM:
13070                 return OP_X86_OR_MEMBASE_IMM;
13071         case OP_XOR_IMM:
13072         case OP_IXOR_IMM:
13073                 return OP_X86_XOR_MEMBASE_IMM;
13074         case OP_MOVE:
13075                 return OP_NOP;
13076         }
13077 #endif
13078
13079 #if defined(TARGET_AMD64)
13080         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13081                 return -1;
13082
13083         switch (opcode) {
13084         case OP_IADD:
13085                 return OP_X86_ADD_MEMBASE_REG;
13086         case OP_ISUB:
13087                 return OP_X86_SUB_MEMBASE_REG;
13088         case OP_IAND:
13089                 return OP_X86_AND_MEMBASE_REG;
13090         case OP_IOR:
13091                 return OP_X86_OR_MEMBASE_REG;
13092         case OP_IXOR:
13093                 return OP_X86_XOR_MEMBASE_REG;
13094         case OP_IADD_IMM:
13095                 return OP_X86_ADD_MEMBASE_IMM;
13096         case OP_ISUB_IMM:
13097                 return OP_X86_SUB_MEMBASE_IMM;
13098         case OP_IAND_IMM:
13099                 return OP_X86_AND_MEMBASE_IMM;
13100         case OP_IOR_IMM:
13101                 return OP_X86_OR_MEMBASE_IMM;
13102         case OP_IXOR_IMM:
13103                 return OP_X86_XOR_MEMBASE_IMM;
13104         case OP_LADD:
13105                 return OP_AMD64_ADD_MEMBASE_REG;
13106         case OP_LSUB:
13107                 return OP_AMD64_SUB_MEMBASE_REG;
13108         case OP_LAND:
13109                 return OP_AMD64_AND_MEMBASE_REG;
13110         case OP_LOR:
13111                 return OP_AMD64_OR_MEMBASE_REG;
13112         case OP_LXOR:
13113                 return OP_AMD64_XOR_MEMBASE_REG;
13114         case OP_ADD_IMM:
13115         case OP_LADD_IMM:
13116                 return OP_AMD64_ADD_MEMBASE_IMM;
13117         case OP_SUB_IMM:
13118         case OP_LSUB_IMM:
13119                 return OP_AMD64_SUB_MEMBASE_IMM;
13120         case OP_AND_IMM:
13121         case OP_LAND_IMM:
13122                 return OP_AMD64_AND_MEMBASE_IMM;
13123         case OP_OR_IMM:
13124         case OP_LOR_IMM:
13125                 return OP_AMD64_OR_MEMBASE_IMM;
13126         case OP_XOR_IMM:
13127         case OP_LXOR_IMM:
13128                 return OP_AMD64_XOR_MEMBASE_IMM;
13129         case OP_MOVE:
13130                 return OP_NOP;
13131         }
13132 #endif
13133
13134         return -1;
13135 }
13136
13137 static inline int
13138 op_to_op_store_membase (int store_opcode, int opcode)
13139 {
13140 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13141         switch (opcode) {
13142         case OP_ICEQ:
13143                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13144                         return OP_X86_SETEQ_MEMBASE;
13145         case OP_CNE:
13146                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13147                         return OP_X86_SETNE_MEMBASE;
13148         }
13149 #endif
13150
13151         return -1;
13152 }
13153
13154 static inline int
13155 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13156 {
13157 #ifdef TARGET_X86
13158         /* FIXME: This has sign extension issues */
13159         /*
13160         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13161                 return OP_X86_COMPARE_MEMBASE8_IMM;
13162         */
13163
13164         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13165                 return -1;
13166
13167         switch (opcode) {
13168         case OP_X86_PUSH:
13169                 return OP_X86_PUSH_MEMBASE;
13170         case OP_COMPARE_IMM:
13171         case OP_ICOMPARE_IMM:
13172                 return OP_X86_COMPARE_MEMBASE_IMM;
13173         case OP_COMPARE:
13174         case OP_ICOMPARE:
13175                 return OP_X86_COMPARE_MEMBASE_REG;
13176         }
13177 #endif
13178
13179 #ifdef TARGET_AMD64
13180         /* FIXME: This has sign extension issues */
13181         /*
13182         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13183                 return OP_X86_COMPARE_MEMBASE8_IMM;
13184         */
13185
13186         switch (opcode) {
13187         case OP_X86_PUSH:
13188                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13189                         return OP_X86_PUSH_MEMBASE;
13190                 break;
13191                 /* FIXME: This only works for 32 bit immediates
13192         case OP_COMPARE_IMM:
13193         case OP_LCOMPARE_IMM:
13194                 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13195                         return OP_AMD64_COMPARE_MEMBASE_IMM;
13196                 */
13197         case OP_ICOMPARE_IMM:
13198                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13199                         return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13200                 break;
13201         case OP_COMPARE:
13202         case OP_LCOMPARE:
13203                 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
13204                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
13205                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13206                         return OP_AMD64_COMPARE_MEMBASE_REG;
13207                 break;
13208         case OP_ICOMPARE:
13209                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13210                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
13211                 break;
13212         }
13213 #endif
13214
13215         return -1;
13216 }
13217
13218 static inline int
13219 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
13220 {
13221 #ifdef TARGET_X86
13222         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13223                 return -1;
13224         
13225         switch (opcode) {
13226         case OP_COMPARE:
13227         case OP_ICOMPARE:
13228                 return OP_X86_COMPARE_REG_MEMBASE;
13229         case OP_IADD:
13230                 return OP_X86_ADD_REG_MEMBASE;
13231         case OP_ISUB:
13232                 return OP_X86_SUB_REG_MEMBASE;
13233         case OP_IAND:
13234                 return OP_X86_AND_REG_MEMBASE;
13235         case OP_IOR:
13236                 return OP_X86_OR_REG_MEMBASE;
13237         case OP_IXOR:
13238                 return OP_X86_XOR_REG_MEMBASE;
13239         }
13240 #endif
13241
13242 #ifdef TARGET_AMD64
13243         if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
13244                 switch (opcode) {
13245                 case OP_ICOMPARE:
13246                         return OP_AMD64_ICOMPARE_REG_MEMBASE;
13247                 case OP_IADD:
13248                         return OP_X86_ADD_REG_MEMBASE;
13249                 case OP_ISUB:
13250                         return OP_X86_SUB_REG_MEMBASE;
13251                 case OP_IAND:
13252                         return OP_X86_AND_REG_MEMBASE;
13253                 case OP_IOR:
13254                         return OP_X86_OR_REG_MEMBASE;
13255                 case OP_IXOR:
13256                         return OP_X86_XOR_REG_MEMBASE;
13257                 }
13258         } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
13259                 switch (opcode) {
13260                 case OP_COMPARE:
13261                 case OP_LCOMPARE:
13262                         return OP_AMD64_COMPARE_REG_MEMBASE;
13263                 case OP_LADD:
13264                         return OP_AMD64_ADD_REG_MEMBASE;
13265                 case OP_LSUB:
13266                         return OP_AMD64_SUB_REG_MEMBASE;
13267                 case OP_LAND:
13268                         return OP_AMD64_AND_REG_MEMBASE;
13269                 case OP_LOR:
13270                         return OP_AMD64_OR_REG_MEMBASE;
13271                 case OP_LXOR:
13272                         return OP_AMD64_XOR_REG_MEMBASE;
13273                 }
13274         }
13275 #endif
13276
13277         return -1;
13278 }
13279
13280 int
13281 mono_op_to_op_imm_noemul (int opcode)
13282 {
13283         switch (opcode) {
13284 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
13285         case OP_LSHR:
13286         case OP_LSHL:
13287         case OP_LSHR_UN:
13288                 return -1;
13289 #endif
13290 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
13291         case OP_IDIV:
13292         case OP_IDIV_UN:
13293         case OP_IREM:
13294         case OP_IREM_UN:
13295                 return -1;
13296 #endif
13297 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
13298         case OP_IMUL:
13299                 return -1;
13300 #endif
13301         default:
13302                 return mono_op_to_op_imm (opcode);
13303         }
13304 }
13305
13306 /**
13307  * mono_handle_global_vregs:
13308  *
13309  *   Make vregs used in more than one bblock 'global', i.e. allocate a variable
13310  * for them.
13311  */
13312 void
13313 mono_handle_global_vregs (MonoCompile *cfg)
13314 {
13315         gint32 *vreg_to_bb;
13316         MonoBasicBlock *bb;
13317         int i, pos;
13318
13319         vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
13320
13321 #ifdef MONO_ARCH_SIMD_INTRINSICS
13322         if (cfg->uses_simd_intrinsics)
13323                 mono_simd_simplify_indirection (cfg);
13324 #endif
13325
13326         /* Find local vregs used in more than one bb */
13327         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13328                 MonoInst *ins = bb->code;       
13329                 int block_num = bb->block_num;
13330
13331                 if (cfg->verbose_level > 2)
13332                         printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
13333
13334                 cfg->cbb = bb;
13335                 for (; ins; ins = ins->next) {
13336                         const char *spec = INS_INFO (ins->opcode);
13337                         int regtype = 0, regindex;
13338                         gint32 prev_bb;
13339
13340                         if (G_UNLIKELY (cfg->verbose_level > 2))
13341                                 mono_print_ins (ins);
13342
13343                         g_assert (ins->opcode >= MONO_CEE_LAST);
13344
13345                         for (regindex = 0; regindex < 4; regindex ++) {
13346                                 int vreg = 0;
13347
13348                                 if (regindex == 0) {
13349                                         regtype = spec [MONO_INST_DEST];
13350                                         if (regtype == ' ')
13351                                                 continue;
13352                                         vreg = ins->dreg;
13353                                 } else if (regindex == 1) {
13354                                         regtype = spec [MONO_INST_SRC1];
13355                                         if (regtype == ' ')
13356                                                 continue;
13357                                         vreg = ins->sreg1;
13358                                 } else if (regindex == 2) {
13359                                         regtype = spec [MONO_INST_SRC2];
13360                                         if (regtype == ' ')
13361                                                 continue;
13362                                         vreg = ins->sreg2;
13363                                 } else if (regindex == 3) {
13364                                         regtype = spec [MONO_INST_SRC3];
13365                                         if (regtype == ' ')
13366                                                 continue;
13367                                         vreg = ins->sreg3;
13368                                 }
13369
13370 #if SIZEOF_REGISTER == 4
13371                                 /* In the LLVM case, the long opcodes are not decomposed */
13372                                 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
13373                                         /*
13374                                          * Since some instructions reference the original long vreg,
13375                                          * and some reference the two component vregs, it is quite hard
13376                                          * to determine when it needs to be global. So be conservative.
13377                                          */
13378                                         if (!get_vreg_to_inst (cfg, vreg)) {
13379                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13380
13381                                                 if (cfg->verbose_level > 2)
13382                                                         printf ("LONG VREG R%d made global.\n", vreg);
13383                                         }
13384
13385                                         /*
13386                                          * Make the component vregs volatile since the optimizations can
13387                                          * get confused otherwise.
13388                                          */
13389                                         get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
13390                                         get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
13391                                 }
13392 #endif
13393
13394                                 g_assert (vreg != -1);
13395
13396                                 prev_bb = vreg_to_bb [vreg];
13397                                 if (prev_bb == 0) {
13398                                         /* 0 is a valid block num */
13399                                         vreg_to_bb [vreg] = block_num + 1;
13400                                 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
13401                                         if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
13402                                                 continue;
13403
13404                                         if (!get_vreg_to_inst (cfg, vreg)) {
13405                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
13406                                                         printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
13407
13408                                                 switch (regtype) {
13409                                                 case 'i':
13410                                                         if (vreg_is_ref (cfg, vreg))
13411                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
13412                                                         else
13413                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
13414                                                         break;
13415                                                 case 'l':
13416                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13417                                                         break;
13418                                                 case 'f':
13419                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
13420                                                         break;
13421                                                 case 'v':
13422                                                 case 'x':
13423                                                         mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
13424                                                         break;
13425                                                 default:
13426                                                         g_assert_not_reached ();
13427                                                 }
13428                                         }
13429
13430                                         /* Flag as having been used in more than one bb */
13431                                         vreg_to_bb [vreg] = -1;
13432                                 }
13433                         }
13434                 }
13435         }
13436
13437         /* If a variable is used in only one bblock, convert it into a local vreg */
13438         for (i = 0; i < cfg->num_varinfo; i++) {
13439                 MonoInst *var = cfg->varinfo [i];
13440                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
13441
13442                 switch (var->type) {
13443                 case STACK_I4:
13444                 case STACK_OBJ:
13445                 case STACK_PTR:
13446                 case STACK_MP:
13447                 case STACK_VTYPE:
13448 #if SIZEOF_REGISTER == 8
13449                 case STACK_I8:
13450 #endif
13451 #if !defined(TARGET_X86)
13452                 /* Enabling this screws up the fp stack on x86 */
13453                 case STACK_R8:
13454 #endif
13455                         if (mono_arch_is_soft_float ())
13456                                 break;
13457
13458                         /*
13459                         if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
13460                                 break;
13461                         */
13462
13463                         /* Arguments are implicitly global */
13464                         /* Putting R4 vars into registers doesn't work currently */
13465                         /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
13466                         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) {
13467                                 /* 
13468                                  * Make that the variable's liveness interval doesn't contain a call, since
13469                                  * that would cause the lvreg to be spilled, making the whole optimization
13470                                  * useless.
13471                                  */
13472                                 /* This is too slow for JIT compilation */
13473 #if 0
13474                                 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
13475                                         MonoInst *ins;
13476                                         int def_index, call_index, ins_index;
13477                                         gboolean spilled = FALSE;
13478
13479                                         def_index = -1;
13480                                         call_index = -1;
13481                                         ins_index = 0;
13482                                         for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
13483                                                 const char *spec = INS_INFO (ins->opcode);
13484
13485                                                 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
13486                                                         def_index = ins_index;
13487
13488                                                 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
13489                                                         ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
13490                                                         if (call_index > def_index) {
13491                                                                 spilled = TRUE;
13492                                                                 break;
13493                                                         }
13494                                                 }
13495
13496                                                 if (MONO_IS_CALL (ins))
13497                                                         call_index = ins_index;
13498
13499                                                 ins_index ++;
13500                                         }
13501
13502                                         if (spilled)
13503                                                 break;
13504                                 }
13505 #endif
13506
13507                                 if (G_UNLIKELY (cfg->verbose_level > 2))
13508                                         printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
13509                                 var->flags |= MONO_INST_IS_DEAD;
13510                                 cfg->vreg_to_inst [var->dreg] = NULL;
13511                         }
13512                         break;
13513                 }
13514         }
13515
13516         /* 
13517          * Compress the varinfo and vars tables so the liveness computation is faster and
13518          * takes up less space.
13519          */
13520         pos = 0;
13521         for (i = 0; i < cfg->num_varinfo; ++i) {
13522                 MonoInst *var = cfg->varinfo [i];
13523                 if (pos < i && cfg->locals_start == i)
13524                         cfg->locals_start = pos;
13525                 if (!(var->flags & MONO_INST_IS_DEAD)) {
13526                         if (pos < i) {
13527                                 cfg->varinfo [pos] = cfg->varinfo [i];
13528                                 cfg->varinfo [pos]->inst_c0 = pos;
13529                                 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
13530                                 cfg->vars [pos].idx = pos;
13531 #if SIZEOF_REGISTER == 4
13532                                 if (cfg->varinfo [pos]->type == STACK_I8) {
13533                                         /* Modify the two component vars too */
13534                                         MonoInst *var1;
13535
13536                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
13537                                         var1->inst_c0 = pos;
13538                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
13539                                         var1->inst_c0 = pos;
13540                                 }
13541 #endif
13542                         }
13543                         pos ++;
13544                 }
13545         }
13546         cfg->num_varinfo = pos;
13547         if (cfg->locals_start > cfg->num_varinfo)
13548                 cfg->locals_start = cfg->num_varinfo;
13549 }
13550
13551 /*
13552  * mono_allocate_gsharedvt_vars:
13553  *
13554  *   Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
13555  * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
13556  */
13557 void
13558 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
13559 {
13560         int i;
13561
13562         cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
13563
13564         for (i = 0; i < cfg->num_varinfo; ++i) {
13565                 MonoInst *ins = cfg->varinfo [i];
13566                 int idx;
13567
13568                 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
13569                         if (i >= cfg->locals_start) {
13570                                 /* Local */
13571                                 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
13572                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
13573                                 ins->opcode = OP_GSHAREDVT_LOCAL;
13574                                 ins->inst_imm = idx;
13575                         } else {
13576                                 /* Arg */
13577                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
13578                                 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
13579                         }
13580                 }
13581         }
13582 }
13583
13584 /**
13585  * mono_spill_global_vars:
13586  *
13587  *   Generate spill code for variables which are not allocated to registers, 
13588  * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
13589  * code is generated which could be optimized by the local optimization passes.
13590  */
13591 void
13592 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
13593 {
13594         MonoBasicBlock *bb;
13595         char spec2 [16];
13596         int orig_next_vreg;
13597         guint32 *vreg_to_lvreg;
13598         guint32 *lvregs;
13599         guint32 i, lvregs_len, lvregs_size;
13600         gboolean dest_has_lvreg = FALSE;
13601         MonoStackType stacktypes [128];
13602         MonoInst **live_range_start, **live_range_end;
13603         MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
13604
13605         *need_local_opts = FALSE;
13606
13607         memset (spec2, 0, sizeof (spec2));
13608
13609         /* FIXME: Move this function to mini.c */
13610         stacktypes ['i'] = STACK_PTR;
13611         stacktypes ['l'] = STACK_I8;
13612         stacktypes ['f'] = STACK_R8;
13613 #ifdef MONO_ARCH_SIMD_INTRINSICS
13614         stacktypes ['x'] = STACK_VTYPE;
13615 #endif
13616
13617 #if SIZEOF_REGISTER == 4
13618         /* Create MonoInsts for longs */
13619         for (i = 0; i < cfg->num_varinfo; i++) {
13620                 MonoInst *ins = cfg->varinfo [i];
13621
13622                 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
13623                         switch (ins->type) {
13624                         case STACK_R8:
13625                         case STACK_I8: {
13626                                 MonoInst *tree;
13627
13628                                 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
13629                                         break;
13630
13631                                 g_assert (ins->opcode == OP_REGOFFSET);
13632
13633                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
13634                                 g_assert (tree);
13635                                 tree->opcode = OP_REGOFFSET;
13636                                 tree->inst_basereg = ins->inst_basereg;
13637                                 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
13638
13639                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
13640                                 g_assert (tree);
13641                                 tree->opcode = OP_REGOFFSET;
13642                                 tree->inst_basereg = ins->inst_basereg;
13643                                 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
13644                                 break;
13645                         }
13646                         default:
13647                                 break;
13648                         }
13649                 }
13650         }
13651 #endif
13652
13653         if (cfg->compute_gc_maps) {
13654                 /* registers need liveness info even for !non refs */
13655                 for (i = 0; i < cfg->num_varinfo; i++) {
13656                         MonoInst *ins = cfg->varinfo [i];
13657
13658                         if (ins->opcode == OP_REGVAR)
13659                                 ins->flags |= MONO_INST_GC_TRACK;
13660                 }
13661         }
13662                 
13663         /* FIXME: widening and truncation */
13664
13665         /*
13666          * As an optimization, when a variable allocated to the stack is first loaded into 
13667          * an lvreg, we will remember the lvreg and use it the next time instead of loading
13668          * the variable again.
13669          */
13670         orig_next_vreg = cfg->next_vreg;
13671         vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
13672         lvregs_size = 1024;
13673         lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * lvregs_size);
13674         lvregs_len = 0;
13675
13676         /* 
13677          * These arrays contain the first and last instructions accessing a given
13678          * variable.
13679          * Since we emit bblocks in the same order we process them here, and we
13680          * don't split live ranges, these will precisely describe the live range of
13681          * the variable, i.e. the instruction range where a valid value can be found
13682          * in the variables location.
13683          * The live range is computed using the liveness info computed by the liveness pass.
13684          * We can't use vmv->range, since that is an abstract live range, and we need
13685          * one which is instruction precise.
13686          * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
13687          */
13688         /* FIXME: Only do this if debugging info is requested */
13689         live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
13690         live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
13691         live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13692         live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13693         
13694         /* Add spill loads/stores */
13695         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13696                 MonoInst *ins;
13697
13698                 if (cfg->verbose_level > 2)
13699                         printf ("\nSPILL BLOCK %d:\n", bb->block_num);
13700
13701                 /* Clear vreg_to_lvreg array */
13702                 for (i = 0; i < lvregs_len; i++)
13703                         vreg_to_lvreg [lvregs [i]] = 0;
13704                 lvregs_len = 0;
13705
13706                 cfg->cbb = bb;
13707                 MONO_BB_FOR_EACH_INS (bb, ins) {
13708                         const char *spec = INS_INFO (ins->opcode);
13709                         int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
13710                         gboolean store, no_lvreg;
13711                         int sregs [MONO_MAX_SRC_REGS];
13712
13713                         if (G_UNLIKELY (cfg->verbose_level > 2))
13714                                 mono_print_ins (ins);
13715
13716                         if (ins->opcode == OP_NOP)
13717                                 continue;
13718
13719                         /* 
13720                          * We handle LDADDR here as well, since it can only be decomposed
13721                          * when variable addresses are known.
13722                          */
13723                         if (ins->opcode == OP_LDADDR) {
13724                                 MonoInst *var = (MonoInst *)ins->inst_p0;
13725
13726                                 if (var->opcode == OP_VTARG_ADDR) {
13727                                         /* Happens on SPARC/S390 where vtypes are passed by reference */
13728                                         MonoInst *vtaddr = var->inst_left;
13729                                         if (vtaddr->opcode == OP_REGVAR) {
13730                                                 ins->opcode = OP_MOVE;
13731                                                 ins->sreg1 = vtaddr->dreg;
13732                                         }
13733                                         else if (var->inst_left->opcode == OP_REGOFFSET) {
13734                                                 ins->opcode = OP_LOAD_MEMBASE;
13735                                                 ins->inst_basereg = vtaddr->inst_basereg;
13736                                                 ins->inst_offset = vtaddr->inst_offset;
13737                                         } else
13738                                                 NOT_IMPLEMENTED;
13739                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
13740                                         /* gsharedvt arg passed by ref */
13741                                         g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
13742
13743                                         ins->opcode = OP_LOAD_MEMBASE;
13744                                         ins->inst_basereg = var->inst_basereg;
13745                                         ins->inst_offset = var->inst_offset;
13746                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
13747                                         MonoInst *load, *load2, *load3;
13748                                         int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
13749                                         int reg1, reg2, reg3;
13750                                         MonoInst *info_var = cfg->gsharedvt_info_var;
13751                                         MonoInst *locals_var = cfg->gsharedvt_locals_var;
13752
13753                                         /*
13754                                          * gsharedvt local.
13755                                          * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
13756                                          */
13757
13758                                         g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
13759
13760                                         g_assert (info_var);
13761                                         g_assert (locals_var);
13762
13763                                         /* Mark the instruction used to compute the locals var as used */
13764                                         cfg->gsharedvt_locals_var_ins = NULL;
13765
13766                                         /* Load the offset */
13767                                         if (info_var->opcode == OP_REGOFFSET) {
13768                                                 reg1 = alloc_ireg (cfg);
13769                                                 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
13770                                         } else if (info_var->opcode == OP_REGVAR) {
13771                                                 load = NULL;
13772                                                 reg1 = info_var->dreg;
13773                                         } else {
13774                                                 g_assert_not_reached ();
13775                                         }
13776                                         reg2 = alloc_ireg (cfg);
13777                                         NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
13778                                         /* Load the locals area address */
13779                                         reg3 = alloc_ireg (cfg);
13780                                         if (locals_var->opcode == OP_REGOFFSET) {
13781                                                 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
13782                                         } else if (locals_var->opcode == OP_REGVAR) {
13783                                                 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
13784                                         } else {
13785                                                 g_assert_not_reached ();
13786                                         }
13787                                         /* Compute the address */
13788                                         ins->opcode = OP_PADD;
13789                                         ins->sreg1 = reg3;
13790                                         ins->sreg2 = reg2;
13791
13792                                         mono_bblock_insert_before_ins (bb, ins, load3);
13793                                         mono_bblock_insert_before_ins (bb, load3, load2);
13794                                         if (load)
13795                                                 mono_bblock_insert_before_ins (bb, load2, load);
13796                                 } else {
13797                                         g_assert (var->opcode == OP_REGOFFSET);
13798
13799                                         ins->opcode = OP_ADD_IMM;
13800                                         ins->sreg1 = var->inst_basereg;
13801                                         ins->inst_imm = var->inst_offset;
13802                                 }
13803
13804                                 *need_local_opts = TRUE;
13805                                 spec = INS_INFO (ins->opcode);
13806                         }
13807
13808                         if (ins->opcode < MONO_CEE_LAST) {
13809                                 mono_print_ins (ins);
13810                                 g_assert_not_reached ();
13811                         }
13812
13813                         /*
13814                          * Store opcodes have destbasereg in the dreg, but in reality, it is an
13815                          * src register.
13816                          * FIXME:
13817                          */
13818                         if (MONO_IS_STORE_MEMBASE (ins)) {
13819                                 tmp_reg = ins->dreg;
13820                                 ins->dreg = ins->sreg2;
13821                                 ins->sreg2 = tmp_reg;
13822                                 store = TRUE;
13823
13824                                 spec2 [MONO_INST_DEST] = ' ';
13825                                 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
13826                                 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
13827                                 spec2 [MONO_INST_SRC3] = ' ';
13828                                 spec = spec2;
13829                         } else if (MONO_IS_STORE_MEMINDEX (ins))
13830                                 g_assert_not_reached ();
13831                         else
13832                                 store = FALSE;
13833                         no_lvreg = FALSE;
13834
13835                         if (G_UNLIKELY (cfg->verbose_level > 2)) {
13836                                 printf ("\t %.3s %d", spec, ins->dreg);
13837                                 num_sregs = mono_inst_get_src_registers (ins, sregs);
13838                                 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
13839                                         printf (" %d", sregs [srcindex]);
13840                                 printf ("\n");
13841                         }
13842
13843                         /***************/
13844                         /*    DREG     */
13845                         /***************/
13846                         regtype = spec [MONO_INST_DEST];
13847                         g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
13848                         prev_dreg = -1;
13849
13850                         if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
13851                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
13852                                 MonoInst *store_ins;
13853                                 int store_opcode;
13854                                 MonoInst *def_ins = ins;
13855                                 int dreg = ins->dreg; /* The original vreg */
13856
13857                                 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
13858
13859                                 if (var->opcode == OP_REGVAR) {
13860                                         ins->dreg = var->dreg;
13861                                 } 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)) {
13862                                         /* 
13863                                          * Instead of emitting a load+store, use a _membase opcode.
13864                                          */
13865                                         g_assert (var->opcode == OP_REGOFFSET);
13866                                         if (ins->opcode == OP_MOVE) {
13867                                                 NULLIFY_INS (ins);
13868                                                 def_ins = NULL;
13869                                         } else {
13870                                                 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
13871                                                 ins->inst_basereg = var->inst_basereg;
13872                                                 ins->inst_offset = var->inst_offset;
13873                                                 ins->dreg = -1;
13874                                         }
13875                                         spec = INS_INFO (ins->opcode);
13876                                 } else {
13877                                         guint32 lvreg;
13878
13879                                         g_assert (var->opcode == OP_REGOFFSET);
13880
13881                                         prev_dreg = ins->dreg;
13882
13883                                         /* Invalidate any previous lvreg for this vreg */
13884                                         vreg_to_lvreg [ins->dreg] = 0;
13885
13886                                         lvreg = 0;
13887
13888                                         if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
13889                                                 regtype = 'l';
13890                                                 store_opcode = OP_STOREI8_MEMBASE_REG;
13891                                         }
13892
13893                                         ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
13894
13895 #if SIZEOF_REGISTER != 8
13896                                         if (regtype == 'l') {
13897                                                 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));
13898                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
13899                                                 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));
13900                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
13901                                                 def_ins = store_ins;
13902                                         }
13903                                         else
13904 #endif
13905                                         {
13906                                                 g_assert (store_opcode != OP_STOREV_MEMBASE);
13907
13908                                                 /* Try to fuse the store into the instruction itself */
13909                                                 /* FIXME: Add more instructions */
13910                                                 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
13911                                                         ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
13912                                                         ins->inst_imm = ins->inst_c0;
13913                                                         ins->inst_destbasereg = var->inst_basereg;
13914                                                         ins->inst_offset = var->inst_offset;
13915                                                         spec = INS_INFO (ins->opcode);
13916                                                 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
13917                                                         ins->opcode = store_opcode;
13918                                                         ins->inst_destbasereg = var->inst_basereg;
13919                                                         ins->inst_offset = var->inst_offset;
13920
13921                                                         no_lvreg = TRUE;
13922
13923                                                         tmp_reg = ins->dreg;
13924                                                         ins->dreg = ins->sreg2;
13925                                                         ins->sreg2 = tmp_reg;
13926                                                         store = TRUE;
13927
13928                                                         spec2 [MONO_INST_DEST] = ' ';
13929                                                         spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
13930                                                         spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
13931                                                         spec2 [MONO_INST_SRC3] = ' ';
13932                                                         spec = spec2;
13933                                                 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
13934                                                         // FIXME: The backends expect the base reg to be in inst_basereg
13935                                                         ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
13936                                                         ins->dreg = -1;
13937                                                         ins->inst_basereg = var->inst_basereg;
13938                                                         ins->inst_offset = var->inst_offset;
13939                                                         spec = INS_INFO (ins->opcode);
13940                                                 } else {
13941                                                         /* printf ("INS: "); mono_print_ins (ins); */
13942                                                         /* Create a store instruction */
13943                                                         NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
13944
13945                                                         /* Insert it after the instruction */
13946                                                         mono_bblock_insert_after_ins (bb, ins, store_ins);
13947
13948                                                         def_ins = store_ins;
13949
13950                                                         /* 
13951                                                          * We can't assign ins->dreg to var->dreg here, since the
13952                                                          * sregs could use it. So set a flag, and do it after
13953                                                          * the sregs.
13954                                                          */
13955                                                         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)))
13956                                                                 dest_has_lvreg = TRUE;
13957                                                 }
13958                                         }
13959                                 }
13960
13961                                 if (def_ins && !live_range_start [dreg]) {
13962                                         live_range_start [dreg] = def_ins;
13963                                         live_range_start_bb [dreg] = bb;
13964                                 }
13965
13966                                 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
13967                                         MonoInst *tmp;
13968
13969                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
13970                                         tmp->inst_c1 = dreg;
13971                                         mono_bblock_insert_after_ins (bb, def_ins, tmp);
13972                                 }
13973                         }
13974
13975                         /************/
13976                         /*  SREGS   */
13977                         /************/
13978                         num_sregs = mono_inst_get_src_registers (ins, sregs);
13979                         for (srcindex = 0; srcindex < 3; ++srcindex) {
13980                                 regtype = spec [MONO_INST_SRC1 + srcindex];
13981                                 sreg = sregs [srcindex];
13982
13983                                 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
13984                                 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
13985                                         MonoInst *var = get_vreg_to_inst (cfg, sreg);
13986                                         MonoInst *use_ins = ins;
13987                                         MonoInst *load_ins;
13988                                         guint32 load_opcode;
13989
13990                                         if (var->opcode == OP_REGVAR) {
13991                                                 sregs [srcindex] = var->dreg;
13992                                                 //mono_inst_set_src_registers (ins, sregs);
13993                                                 live_range_end [sreg] = use_ins;
13994                                                 live_range_end_bb [sreg] = bb;
13995
13996                                                 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
13997                                                         MonoInst *tmp;
13998
13999                                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14000                                                         /* var->dreg is a hreg */
14001                                                         tmp->inst_c1 = sreg;
14002                                                         mono_bblock_insert_after_ins (bb, ins, tmp);
14003                                                 }
14004
14005                                                 continue;
14006                                         }
14007
14008                                         g_assert (var->opcode == OP_REGOFFSET);
14009                                                 
14010                                         load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14011
14012                                         g_assert (load_opcode != OP_LOADV_MEMBASE);
14013
14014                                         if (vreg_to_lvreg [sreg]) {
14015                                                 g_assert (vreg_to_lvreg [sreg] != -1);
14016
14017                                                 /* The variable is already loaded to an lvreg */
14018                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14019                                                         printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14020                                                 sregs [srcindex] = vreg_to_lvreg [sreg];
14021                                                 //mono_inst_set_src_registers (ins, sregs);
14022                                                 continue;
14023                                         }
14024
14025                                         /* Try to fuse the load into the instruction */
14026                                         if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14027                                                 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14028                                                 sregs [0] = var->inst_basereg;
14029                                                 //mono_inst_set_src_registers (ins, sregs);
14030                                                 ins->inst_offset = var->inst_offset;
14031                                         } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14032                                                 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14033                                                 sregs [1] = var->inst_basereg;
14034                                                 //mono_inst_set_src_registers (ins, sregs);
14035                                                 ins->inst_offset = var->inst_offset;
14036                                         } else {
14037                                                 if (MONO_IS_REAL_MOVE (ins)) {
14038                                                         ins->opcode = OP_NOP;
14039                                                         sreg = ins->dreg;
14040                                                 } else {
14041                                                         //printf ("%d ", srcindex); mono_print_ins (ins);
14042
14043                                                         sreg = alloc_dreg (cfg, stacktypes [regtype]);
14044
14045                                                         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) {
14046                                                                 if (var->dreg == prev_dreg) {
14047                                                                         /*
14048                                                                          * sreg refers to the value loaded by the load
14049                                                                          * emitted below, but we need to use ins->dreg
14050                                                                          * since it refers to the store emitted earlier.
14051                                                                          */
14052                                                                         sreg = ins->dreg;
14053                                                                 }
14054                                                                 g_assert (sreg != -1);
14055                                                                 vreg_to_lvreg [var->dreg] = sreg;
14056                                                                 if (lvregs_len >= lvregs_size) {
14057                                                                         guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
14058                                                                         memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
14059                                                                         lvregs = new_lvregs;
14060                                                                         lvregs_size *= 2;
14061                                                                 }
14062                                                                 lvregs [lvregs_len ++] = var->dreg;
14063                                                         }
14064                                                 }
14065
14066                                                 sregs [srcindex] = sreg;
14067                                                 //mono_inst_set_src_registers (ins, sregs);
14068
14069 #if SIZEOF_REGISTER != 8
14070                                                 if (regtype == 'l') {
14071                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14072                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14073                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14074                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14075                                                         use_ins = load_ins;
14076                                                 }
14077                                                 else
14078 #endif
14079                                                 {
14080 #if SIZEOF_REGISTER == 4
14081                                                         g_assert (load_opcode != OP_LOADI8_MEMBASE);
14082 #endif
14083                                                         NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14084                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14085                                                         use_ins = load_ins;
14086                                                 }
14087                                         }
14088
14089                                         if (var->dreg < orig_next_vreg) {
14090                                                 live_range_end [var->dreg] = use_ins;
14091                                                 live_range_end_bb [var->dreg] = bb;
14092                                         }
14093
14094                                         if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14095                                                 MonoInst *tmp;
14096
14097                                                 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14098                                                 tmp->inst_c1 = var->dreg;
14099                                                 mono_bblock_insert_after_ins (bb, ins, tmp);
14100                                         }
14101                                 }
14102                         }
14103                         mono_inst_set_src_registers (ins, sregs);
14104
14105                         if (dest_has_lvreg) {
14106                                 g_assert (ins->dreg != -1);
14107                                 vreg_to_lvreg [prev_dreg] = ins->dreg;
14108                                 if (lvregs_len >= lvregs_size) {
14109                                         guint32 *new_lvregs = mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * lvregs_size * 2);
14110                                         memcpy (new_lvregs, lvregs, sizeof (guint32) * lvregs_size);
14111                                         lvregs = new_lvregs;
14112                                         lvregs_size *= 2;
14113                                 }
14114                                 lvregs [lvregs_len ++] = prev_dreg;
14115                                 dest_has_lvreg = FALSE;
14116                         }
14117
14118                         if (store) {
14119                                 tmp_reg = ins->dreg;
14120                                 ins->dreg = ins->sreg2;
14121                                 ins->sreg2 = tmp_reg;
14122                         }
14123
14124                         if (MONO_IS_CALL (ins)) {
14125                                 /* Clear vreg_to_lvreg array */
14126                                 for (i = 0; i < lvregs_len; i++)
14127                                         vreg_to_lvreg [lvregs [i]] = 0;
14128                                 lvregs_len = 0;
14129                         } else if (ins->opcode == OP_NOP) {
14130                                 ins->dreg = -1;
14131                                 MONO_INST_NULLIFY_SREGS (ins);
14132                         }
14133
14134                         if (cfg->verbose_level > 2)
14135                                 mono_print_ins_index (1, ins);
14136                 }
14137
14138                 /* Extend the live range based on the liveness info */
14139                 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14140                         for (i = 0; i < cfg->num_varinfo; i ++) {
14141                                 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14142
14143                                 if (vreg_is_volatile (cfg, vi->vreg))
14144                                         /* The liveness info is incomplete */
14145                                         continue;
14146
14147                                 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14148                                         /* Live from at least the first ins of this bb */
14149                                         live_range_start [vi->vreg] = bb->code;
14150                                         live_range_start_bb [vi->vreg] = bb;
14151                                 }
14152
14153                                 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14154                                         /* Live at least until the last ins of this bb */
14155                                         live_range_end [vi->vreg] = bb->last_ins;
14156                                         live_range_end_bb [vi->vreg] = bb;
14157                                 }
14158                         }
14159                 }
14160         }
14161         
14162         /*
14163          * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14164          * by storing the current native offset into MonoMethodVar->live_range_start/end.
14165          */
14166         if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14167                 for (i = 0; i < cfg->num_varinfo; ++i) {
14168                         int vreg = MONO_VARINFO (cfg, i)->vreg;
14169                         MonoInst *ins;
14170
14171                         if (live_range_start [vreg]) {
14172                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14173                                 ins->inst_c0 = i;
14174                                 ins->inst_c1 = vreg;
14175                                 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14176                         }
14177                         if (live_range_end [vreg]) {
14178                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14179                                 ins->inst_c0 = i;
14180                                 ins->inst_c1 = vreg;
14181                                 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14182                                         mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14183                                 else
14184                                         mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14185                         }
14186                 }
14187         }
14188
14189         if (cfg->gsharedvt_locals_var_ins) {
14190                 /* Nullify if unused */
14191                 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14192                 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14193         }
14194
14195         g_free (live_range_start);
14196         g_free (live_range_end);
14197         g_free (live_range_start_bb);
14198         g_free (live_range_end_bb);
14199 }
14200
14201
14202 /**
14203  * FIXME:
14204  * - use 'iadd' instead of 'int_add'
14205  * - handling ovf opcodes: decompose in method_to_ir.
14206  * - unify iregs/fregs
14207  *   -> partly done, the missing parts are:
14208  *   - a more complete unification would involve unifying the hregs as well, so
14209  *     code wouldn't need if (fp) all over the place. but that would mean the hregs
14210  *     would no longer map to the machine hregs, so the code generators would need to
14211  *     be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
14212  *     wouldn't work any more. Duplicating the code in mono_local_regalloc () into
14213  *     fp/non-fp branches speeds it up by about 15%.
14214  * - use sext/zext opcodes instead of shifts
14215  * - add OP_ICALL
14216  * - get rid of TEMPLOADs if possible and use vregs instead
14217  * - clean up usage of OP_P/OP_ opcodes
14218  * - cleanup usage of DUMMY_USE
14219  * - cleanup the setting of ins->type for MonoInst's which are pushed on the 
14220  *   stack
14221  * - set the stack type and allocate a dreg in the EMIT_NEW macros
14222  * - get rid of all the <foo>2 stuff when the new JIT is ready.
14223  * - make sure handle_stack_args () is called before the branch is emitted
14224  * - when the new IR is done, get rid of all unused stuff
14225  * - COMPARE/BEQ as separate instructions or unify them ?
14226  *   - keeping them separate allows specialized compare instructions like
14227  *     compare_imm, compare_membase
14228  *   - most back ends unify fp compare+branch, fp compare+ceq
14229  * - integrate mono_save_args into inline_method
14230  * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
14231  * - handle long shift opts on 32 bit platforms somehow: they require 
14232  *   3 sregs (2 for arg1 and 1 for arg2)
14233  * - make byref a 'normal' type.
14234  * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
14235  *   variable if needed.
14236  * - do not start a new IL level bblock when cfg->cbb is changed by a function call
14237  *   like inline_method.
14238  * - remove inlining restrictions
14239  * - fix LNEG and enable cfold of INEG
14240  * - generalize x86 optimizations like ldelema as a peephole optimization
14241  * - add store_mem_imm for amd64
14242  * - optimize the loading of the interruption flag in the managed->native wrappers
14243  * - avoid special handling of OP_NOP in passes
14244  * - move code inserting instructions into one function/macro.
14245  * - try a coalescing phase after liveness analysis
14246  * - add float -> vreg conversion + local optimizations on !x86
14247  * - figure out how to handle decomposed branches during optimizations, ie.
14248  *   compare+branch, op_jump_table+op_br etc.
14249  * - promote RuntimeXHandles to vregs
14250  * - vtype cleanups:
14251  *   - add a NEW_VARLOADA_VREG macro
14252  * - the vtype optimizations are blocked by the LDADDR opcodes generated for 
14253  *   accessing vtype fields.
14254  * - get rid of I8CONST on 64 bit platforms
14255  * - dealing with the increase in code size due to branches created during opcode
14256  *   decomposition:
14257  *   - use extended basic blocks
14258  *     - all parts of the JIT
14259  *     - handle_global_vregs () && local regalloc
14260  *   - avoid introducing global vregs during decomposition, like 'vtable' in isinst
14261  * - sources of increase in code size:
14262  *   - vtypes
14263  *   - long compares
14264  *   - isinst and castclass
14265  *   - lvregs not allocated to global registers even if used multiple times
14266  * - call cctors outside the JIT, to make -v output more readable and JIT timings more
14267  *   meaningful.
14268  * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
14269  * - add all micro optimizations from the old JIT
14270  * - put tree optimizations into the deadce pass
14271  * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
14272  *   specific function.
14273  * - unify the float comparison opcodes with the other comparison opcodes, i.e.
14274  *   fcompare + branchCC.
14275  * - create a helper function for allocating a stack slot, taking into account 
14276  *   MONO_CFG_HAS_SPILLUP.
14277  * - merge r68207.
14278  * - optimize mono_regstate2_alloc_int/float.
14279  * - fix the pessimistic handling of variables accessed in exception handler blocks.
14280  * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
14281  *   parts of the tree could be separated by other instructions, killing the tree
14282  *   arguments, or stores killing loads etc. Also, should we fold loads into other
14283  *   instructions if the result of the load is used multiple times ?
14284  * - make the REM_IMM optimization in mini-x86.c arch-independent.
14285  * - LAST MERGE: 108395.
14286  * - when returning vtypes in registers, generate IR and append it to the end of the
14287  *   last bb instead of doing it in the epilog.
14288  * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
14289  */
14290
14291 /*
14292
14293 NOTES
14294 -----
14295
14296 - When to decompose opcodes:
14297   - earlier: this makes some optimizations hard to implement, since the low level IR
14298   no longer contains the neccessary information. But it is easier to do.
14299   - later: harder to implement, enables more optimizations.
14300 - Branches inside bblocks:
14301   - created when decomposing complex opcodes. 
14302     - branches to another bblock: harmless, but not tracked by the branch 
14303       optimizations, so need to branch to a label at the start of the bblock.
14304     - branches to inside the same bblock: very problematic, trips up the local
14305       reg allocator. Can be fixed by spitting the current bblock, but that is a
14306       complex operation, since some local vregs can become global vregs etc.
14307 - Local/global vregs:
14308   - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
14309     local register allocator.
14310   - global vregs: used in more than one bblock. Have an associated MonoMethodVar
14311     structure, created by mono_create_var (). Assigned to hregs or the stack by
14312     the global register allocator.
14313 - When to do optimizations like alu->alu_imm:
14314   - earlier -> saves work later on since the IR will be smaller/simpler
14315   - later -> can work on more instructions
14316 - Handling of valuetypes:
14317   - When a vtype is pushed on the stack, a new temporary is created, an 
14318     instruction computing its address (LDADDR) is emitted and pushed on
14319     the stack. Need to optimize cases when the vtype is used immediately as in
14320     argument passing, stloc etc.
14321 - Instead of the to_end stuff in the old JIT, simply call the function handling
14322   the values on the stack before emitting the last instruction of the bb.
14323 */
14324
14325 #endif /* !DISABLE_JIT */