[runtime] Refactor and unify tls access
[mono.git] / mono / mini / method-to-ir.c
1 /*
2  * method-to-ir.c: Convert CIL to the JIT internal representation
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
8  * (C) 2002 Ximian, Inc.
9  * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
10  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12  */
13
14 #include <config.h>
15 #include <mono/utils/mono-compiler.h>
16
17 #ifndef DISABLE_JIT
18
19 #include <signal.h>
20
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #include <math.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32
33 #ifdef HAVE_ALLOCA_H
34 #include <alloca.h>
35 #endif
36
37 #include <mono/utils/memcheck.h>
38 #include "mini.h"
39 #include <mono/metadata/abi-details.h>
40 #include <mono/metadata/assembly.h>
41 #include <mono/metadata/attrdefs.h>
42 #include <mono/metadata/loader.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/class.h>
45 #include <mono/metadata/object.h>
46 #include <mono/metadata/exception.h>
47 #include <mono/metadata/opcodes.h>
48 #include <mono/metadata/mono-endian.h>
49 #include <mono/metadata/tokentype.h>
50 #include <mono/metadata/tabledefs.h>
51 #include <mono/metadata/marshal.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/metadata/mono-debug.h>
54 #include <mono/metadata/mono-debug-debugger.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/metadata/debug-mono-symfile.h>
63 #include <mono/utils/mono-compiler.h>
64 #include <mono/utils/mono-memory-model.h>
65 #include <mono/utils/mono-error-internals.h>
66 #include <mono/metadata/mono-basic-block.h>
67 #include <mono/metadata/reflection-internals.h>
68 #include <mono/utils/mono-threads-coop.h>
69
70 #include "trace.h"
71
72 #include "ir-emit.h"
73
74 #include "jit-icalls.h"
75 #include "jit.h"
76 #include "debugger-agent.h"
77 #include "seq-points.h"
78 #include "aot-compiler.h"
79 #include "mini-llvm.h"
80
81 #define BRANCH_COST 10
82 #define INLINE_LENGTH_LIMIT 20
83
84 /* These have 'cfg' as an implicit argument */
85 #define INLINE_FAILURE(msg) do {                                                                        \
86         if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
87                 inline_failure (cfg, msg);                                                                              \
88                 goto exception_exit;                                                                                    \
89         } \
90         } while (0)
91 #define CHECK_CFG_EXCEPTION do {\
92                 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
93                         goto exception_exit;                                            \
94         } while (0)
95 #define FIELD_ACCESS_FAILURE(method, field) do {                                        \
96                 field_access_failure ((cfg), (method), (field));                        \
97                 goto exception_exit;    \
98         } while (0)
99 #define GENERIC_SHARING_FAILURE(opcode) do {            \
100                 if (cfg->gshared) {                                                                     \
101                         gshared_failure (cfg, opcode, __FILE__, __LINE__);      \
102                         goto exception_exit;    \
103                 }                       \
104         } while (0)
105 #define GSHAREDVT_FAILURE(opcode) do {          \
106         if (cfg->gsharedvt) {                                                                                           \
107                 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__);                    \
108                 goto exception_exit;                                                                                    \
109         }                                                                                                                                       \
110         } while (0)
111 #define OUT_OF_MEMORY_FAILURE do {      \
112                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);                \
113                 mono_error_set_out_of_memory (&cfg->error, "");                                 \
114                 goto exception_exit;    \
115         } while (0)
116 #define DISABLE_AOT(cfg) do { \
117                 if ((cfg)->verbose_level >= 2)                                            \
118                         printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__);   \
119                 (cfg)->disable_aot = TRUE;                                                        \
120         } while (0)
121 #define LOAD_ERROR do { \
122                 break_on_unverified ();                                                         \
123                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
124                 goto exception_exit;                                                                    \
125         } while (0)
126
127 #define TYPE_LOAD_ERROR(klass) do { \
128                 cfg->exception_ptr = klass; \
129                 LOAD_ERROR;                                     \
130         } while (0)
131
132 #define CHECK_CFG_ERROR do {\
133                 if (!mono_error_ok (&cfg->error)) { \
134                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);        \
135                         goto mono_error_exit; \
136                 } \
137         } while (0)
138
139 /* Determine whenever 'ins' represents a load of the 'this' argument */
140 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
141
142 static int ldind_to_load_membase (int opcode);
143 static int stind_to_store_membase (int opcode);
144
145 int mono_op_to_op_imm (int opcode);
146 int mono_op_to_op_imm_noemul (int opcode);
147
148 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
149
150 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
151                                                   guchar *ip, guint real_offset, gboolean inline_always);
152 static MonoInst*
153 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
154
155 inline static MonoInst*
156 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg);
157
158 /* helper methods signatures */
159 static MonoMethodSignature *helper_sig_domain_get;
160 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
161 static MonoMethodSignature *helper_sig_llvmonly_imt_trampoline;
162 static MonoMethodSignature *helper_sig_jit_thread_attach;
163 static MonoMethodSignature *helper_sig_get_tls_tramp;
164 static MonoMethodSignature *helper_sig_set_tls_tramp;
165
166 /* type loading helpers */
167 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, System.Runtime.CompilerServices, RuntimeHelpers)
168 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, System.Diagnostics, DebuggableAttribute)
169
170 /*
171  * Instruction metadata
172  */
173 #ifdef MINI_OP
174 #undef MINI_OP
175 #endif
176 #ifdef MINI_OP3
177 #undef MINI_OP3
178 #endif
179 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
180 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
181 #define NONE ' '
182 #define IREG 'i'
183 #define FREG 'f'
184 #define VREG 'v'
185 #define XREG 'x'
186 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
187 #define LREG IREG
188 #else
189 #define LREG 'l'
190 #endif
191 /* keep in sync with the enum in mini.h */
192 const char
193 ins_info[] = {
194 #include "mini-ops.h"
195 };
196 #undef MINI_OP
197 #undef MINI_OP3
198
199 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
200 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
201 /* 
202  * This should contain the index of the last sreg + 1. This is not the same
203  * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
204  */
205 const gint8 ins_sreg_counts[] = {
206 #include "mini-ops.h"
207 };
208 #undef MINI_OP
209 #undef MINI_OP3
210
211 #define MONO_INIT_VARINFO(vi,id) do { \
212         (vi)->range.first_use.pos.bid = 0xffff; \
213         (vi)->reg = -1; \
214         (vi)->idx = (id); \
215 } while (0)
216
217 guint32
218 mono_alloc_ireg (MonoCompile *cfg)
219 {
220         return alloc_ireg (cfg);
221 }
222
223 guint32
224 mono_alloc_lreg (MonoCompile *cfg)
225 {
226         return alloc_lreg (cfg);
227 }
228
229 guint32
230 mono_alloc_freg (MonoCompile *cfg)
231 {
232         return alloc_freg (cfg);
233 }
234
235 guint32
236 mono_alloc_preg (MonoCompile *cfg)
237 {
238         return alloc_preg (cfg);
239 }
240
241 guint32
242 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
243 {
244         return alloc_dreg (cfg, stack_type);
245 }
246
247 /*
248  * mono_alloc_ireg_ref:
249  *
250  *   Allocate an IREG, and mark it as holding a GC ref.
251  */
252 guint32
253 mono_alloc_ireg_ref (MonoCompile *cfg)
254 {
255         return alloc_ireg_ref (cfg);
256 }
257
258 /*
259  * mono_alloc_ireg_mp:
260  *
261  *   Allocate an IREG, and mark it as holding a managed pointer.
262  */
263 guint32
264 mono_alloc_ireg_mp (MonoCompile *cfg)
265 {
266         return alloc_ireg_mp (cfg);
267 }
268
269 /*
270  * mono_alloc_ireg_copy:
271  *
272  *   Allocate an IREG with the same GC type as VREG.
273  */
274 guint32
275 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
276 {
277         if (vreg_is_ref (cfg, vreg))
278                 return alloc_ireg_ref (cfg);
279         else if (vreg_is_mp (cfg, vreg))
280                 return alloc_ireg_mp (cfg);
281         else
282                 return alloc_ireg (cfg);
283 }
284
285 guint
286 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
287 {
288         if (type->byref)
289                 return OP_MOVE;
290
291         type = mini_get_underlying_type (type);
292 handle_enum:
293         switch (type->type) {
294         case MONO_TYPE_I1:
295         case MONO_TYPE_U1:
296                 return OP_MOVE;
297         case MONO_TYPE_I2:
298         case MONO_TYPE_U2:
299                 return OP_MOVE;
300         case MONO_TYPE_I4:
301         case MONO_TYPE_U4:
302                 return OP_MOVE;
303         case MONO_TYPE_I:
304         case MONO_TYPE_U:
305         case MONO_TYPE_PTR:
306         case MONO_TYPE_FNPTR:
307                 return OP_MOVE;
308         case MONO_TYPE_CLASS:
309         case MONO_TYPE_STRING:
310         case MONO_TYPE_OBJECT:
311         case MONO_TYPE_SZARRAY:
312         case MONO_TYPE_ARRAY:    
313                 return OP_MOVE;
314         case MONO_TYPE_I8:
315         case MONO_TYPE_U8:
316 #if SIZEOF_REGISTER == 8
317                 return OP_MOVE;
318 #else
319                 return OP_LMOVE;
320 #endif
321         case MONO_TYPE_R4:
322                 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
323         case MONO_TYPE_R8:
324                 return OP_FMOVE;
325         case MONO_TYPE_VALUETYPE:
326                 if (type->data.klass->enumtype) {
327                         type = mono_class_enum_basetype (type->data.klass);
328                         goto handle_enum;
329                 }
330                 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
331                         return OP_XMOVE;
332                 return OP_VMOVE;
333         case MONO_TYPE_TYPEDBYREF:
334                 return OP_VMOVE;
335         case MONO_TYPE_GENERICINST:
336                 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
337                         return OP_XMOVE;
338                 type = &type->data.generic_class->container_class->byval_arg;
339                 goto handle_enum;
340         case MONO_TYPE_VAR:
341         case MONO_TYPE_MVAR:
342                 g_assert (cfg->gshared);
343                 if (mini_type_var_is_vt (type))
344                         return OP_VMOVE;
345                 else
346                         return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
347         default:
348                 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
349         }
350         return -1;
351 }
352
353 void
354 mono_print_bb (MonoBasicBlock *bb, const char *msg)
355 {
356         int i;
357         MonoInst *tree;
358
359         printf ("\n%s %d: [IN: ", msg, bb->block_num);
360         for (i = 0; i < bb->in_count; ++i)
361                 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
362         printf (", OUT: ");
363         for (i = 0; i < bb->out_count; ++i)
364                 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
365         printf (" ]\n");
366         for (tree = bb->code; tree; tree = tree->next)
367                 mono_print_ins_index (-1, tree);
368 }
369
370 void
371 mono_create_helper_signatures (void)
372 {
373         helper_sig_domain_get = mono_create_icall_signature ("ptr");
374         helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
375         helper_sig_llvmonly_imt_trampoline = mono_create_icall_signature ("ptr ptr ptr");
376         helper_sig_jit_thread_attach = mono_create_icall_signature ("ptr ptr");
377         helper_sig_get_tls_tramp = mono_create_icall_signature ("ptr");
378         helper_sig_set_tls_tramp = mono_create_icall_signature ("void ptr");
379 }
380
381 static MONO_NEVER_INLINE void
382 break_on_unverified (void)
383 {
384         if (mini_get_debug_options ()->break_on_unverified)
385                 G_BREAKPOINT ();
386 }
387
388 static MONO_NEVER_INLINE void
389 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
390 {
391         char *method_fname = mono_method_full_name (method, TRUE);
392         char *field_fname = mono_field_full_name (field);
393         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
394         mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
395         g_free (method_fname);
396         g_free (field_fname);
397 }
398
399 static MONO_NEVER_INLINE void
400 inline_failure (MonoCompile *cfg, const char *msg)
401 {
402         if (cfg->verbose_level >= 2)
403                 printf ("inline failed: %s\n", msg);
404         mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
405 }
406
407 static MONO_NEVER_INLINE void
408 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
409 {
410         if (cfg->verbose_level > 2)                                                                                     \
411                 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);
412         mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
413 }
414
415 static MONO_NEVER_INLINE void
416 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
417 {
418         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);
419         if (cfg->verbose_level >= 2)
420                 printf ("%s\n", cfg->exception_message);
421         mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
422 }
423
424 /*
425  * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e. 
426  * foo<T> (int i) { ldarg.0; box T; }
427  */
428 #define UNVERIFIED do { \
429         if (cfg->gsharedvt) { \
430                 if (cfg->verbose_level > 2)                                                                     \
431                         printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
432                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
433                 goto exception_exit;                                                                                    \
434         }                                                                                                                                       \
435         break_on_unverified ();                                                                                         \
436         goto unverified;                                                                                                        \
437 } while (0)
438
439 #define GET_BBLOCK(cfg,tblock,ip) do {  \
440                 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
441                 if (!(tblock)) {        \
442                         if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
443             NEW_BBLOCK (cfg, (tblock)); \
444                         (tblock)->cil_code = (ip);      \
445                         ADD_BBLOCK (cfg, (tblock));     \
446                 } \
447         } while (0)
448
449 #if defined(TARGET_X86) || defined(TARGET_AMD64)
450 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
451                 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
452                 (dest)->dreg = alloc_ireg_mp ((cfg)); \
453                 (dest)->sreg1 = (sr1); \
454                 (dest)->sreg2 = (sr2); \
455                 (dest)->inst_imm = (imm); \
456                 (dest)->backend.shift_amount = (shift); \
457                 MONO_ADD_INS ((cfg)->cbb, (dest)); \
458         } while (0)
459 #endif
460
461 /* Emit conversions so both operands of a binary opcode are of the same type */
462 static void
463 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
464 {
465         MonoInst *arg1 = *arg1_ref;
466         MonoInst *arg2 = *arg2_ref;
467
468         if (cfg->r4fp &&
469                 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
470                  (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
471                 MonoInst *conv;
472
473                 /* Mixing r4/r8 is allowed by the spec */
474                 if (arg1->type == STACK_R4) {
475                         int dreg = alloc_freg (cfg);
476
477                         EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
478                         conv->type = STACK_R8;
479                         ins->sreg1 = dreg;
480                         *arg1_ref = conv;
481                 }
482                 if (arg2->type == STACK_R4) {
483                         int dreg = alloc_freg (cfg);
484
485                         EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
486                         conv->type = STACK_R8;
487                         ins->sreg2 = dreg;
488                         *arg2_ref = conv;
489                 }
490         }
491
492 #if SIZEOF_REGISTER == 8
493         /* FIXME: Need to add many more cases */
494         if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
495                 MonoInst *widen;
496
497                 int dr = alloc_preg (cfg);
498                 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
499                 (ins)->sreg2 = widen->dreg;
500         }
501 #endif
502 }
503
504 #define ADD_BINOP(op) do {      \
505                 MONO_INST_NEW (cfg, ins, (op)); \
506                 sp -= 2;        \
507                 ins->sreg1 = sp [0]->dreg;      \
508                 ins->sreg2 = sp [1]->dreg;      \
509                 type_from_op (cfg, ins, sp [0], sp [1]);        \
510                 CHECK_TYPE (ins);       \
511                 /* Have to insert a widening op */               \
512         add_widen_op (cfg, ins, &sp [0], &sp [1]);               \
513         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
514         MONO_ADD_INS ((cfg)->cbb, (ins)); \
515         *sp++ = mono_decompose_opcode ((cfg), (ins));   \
516         } while (0)
517
518 #define ADD_UNOP(op) do {       \
519                 MONO_INST_NEW (cfg, ins, (op)); \
520                 sp--;   \
521                 ins->sreg1 = sp [0]->dreg;      \
522                 type_from_op (cfg, ins, sp [0], NULL);  \
523                 CHECK_TYPE (ins);       \
524         (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
525         MONO_ADD_INS ((cfg)->cbb, (ins)); \
526                 *sp++ = mono_decompose_opcode (cfg, ins);       \
527         } while (0)
528
529 #define ADD_BINCOND(next_block) do {    \
530                 MonoInst *cmp;  \
531                 sp -= 2; \
532                 MONO_INST_NEW(cfg, cmp, OP_COMPARE);    \
533                 cmp->sreg1 = sp [0]->dreg;      \
534                 cmp->sreg2 = sp [1]->dreg;      \
535                 type_from_op (cfg, cmp, sp [0], sp [1]);        \
536                 CHECK_TYPE (cmp);       \
537                 add_widen_op (cfg, cmp, &sp [0], &sp [1]);                                              \
538                 type_from_op (cfg, ins, sp [0], sp [1]);                                                        \
539                 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);   \
540                 GET_BBLOCK (cfg, tblock, target);               \
541                 link_bblock (cfg, cfg->cbb, tblock);    \
542                 ins->inst_true_bb = tblock;     \
543                 if ((next_block)) {     \
544                         link_bblock (cfg, cfg->cbb, (next_block));      \
545                         ins->inst_false_bb = (next_block);      \
546                         start_new_bblock = 1;   \
547                 } else {        \
548                         GET_BBLOCK (cfg, tblock, ip);           \
549                         link_bblock (cfg, cfg->cbb, tblock);    \
550                         ins->inst_false_bb = tblock;    \
551                         start_new_bblock = 2;   \
552                 }       \
553                 if (sp != stack_start) {                                                                        \
554                     handle_stack_args (cfg, stack_start, sp - stack_start); \
555                         CHECK_UNVERIFIABLE (cfg); \
556                 } \
557         MONO_ADD_INS (cfg->cbb, cmp); \
558                 MONO_ADD_INS (cfg->cbb, ins);   \
559         } while (0)
560
561 /* *
562  * link_bblock: Links two basic blocks
563  *
564  * links two basic blocks in the control flow graph, the 'from'
565  * argument is the starting block and the 'to' argument is the block
566  * the control flow ends to after 'from'.
567  */
568 static void
569 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
570 {
571         MonoBasicBlock **newa;
572         int i, found;
573
574 #if 0
575         if (from->cil_code) {
576                 if (to->cil_code)
577                         printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
578                 else
579                         printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
580         } else {
581                 if (to->cil_code)
582                         printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
583                 else
584                         printf ("edge from entry to exit\n");
585         }
586 #endif
587
588         found = FALSE;
589         for (i = 0; i < from->out_count; ++i) {
590                 if (to == from->out_bb [i]) {
591                         found = TRUE;
592                         break;
593                 }
594         }
595         if (!found) {
596                 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
597                 for (i = 0; i < from->out_count; ++i) {
598                         newa [i] = from->out_bb [i];
599                 }
600                 newa [i] = to;
601                 from->out_count++;
602                 from->out_bb = newa;
603         }
604
605         found = FALSE;
606         for (i = 0; i < to->in_count; ++i) {
607                 if (from == to->in_bb [i]) {
608                         found = TRUE;
609                         break;
610                 }
611         }
612         if (!found) {
613                 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
614                 for (i = 0; i < to->in_count; ++i) {
615                         newa [i] = to->in_bb [i];
616                 }
617                 newa [i] = from;
618                 to->in_count++;
619                 to->in_bb = newa;
620         }
621 }
622
623 void
624 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
625 {
626         link_bblock (cfg, from, to);
627 }
628
629 /**
630  * mono_find_block_region:
631  *
632  *   We mark each basic block with a region ID. We use that to avoid BB
633  *   optimizations when blocks are in different regions.
634  *
635  * Returns:
636  *   A region token that encodes where this region is, and information
637  *   about the clause owner for this block.
638  *
639  *   The region encodes the try/catch/filter clause that owns this block
640  *   as well as the type.  -1 is a special value that represents a block
641  *   that is in none of try/catch/filter.
642  */
643 static int
644 mono_find_block_region (MonoCompile *cfg, int offset)
645 {
646         MonoMethodHeader *header = cfg->header;
647         MonoExceptionClause *clause;
648         int i;
649
650         for (i = 0; i < header->num_clauses; ++i) {
651                 clause = &header->clauses [i];
652                 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
653                     (offset < (clause->handler_offset)))
654                         return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
655                            
656                 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
657                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
658                                 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
659                         else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
660                                 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
661                         else
662                                 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
663                 }
664         }
665         for (i = 0; i < header->num_clauses; ++i) {
666                 clause = &header->clauses [i];
667
668                 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
669                         return ((i + 1) << 8) | clause->flags;
670         }
671
672         return -1;
673 }
674
675 static gboolean
676 ip_in_finally_clause (MonoCompile *cfg, int offset)
677 {
678         MonoMethodHeader *header = cfg->header;
679         MonoExceptionClause *clause;
680         int i;
681
682         for (i = 0; i < header->num_clauses; ++i) {
683                 clause = &header->clauses [i];
684                 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FAULT)
685                         continue;
686
687                 if (MONO_OFFSET_IN_HANDLER (clause, offset))
688                         return TRUE;
689         }
690         return FALSE;
691 }
692
693 static GList*
694 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
695 {
696         MonoMethodHeader *header = cfg->header;
697         MonoExceptionClause *clause;
698         int i;
699         GList *res = NULL;
700
701         for (i = 0; i < header->num_clauses; ++i) {
702                 clause = &header->clauses [i];
703                 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) && 
704                     (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
705                         if (clause->flags == type)
706                                 res = g_list_append (res, clause);
707                 }
708         }
709         return res;
710 }
711
712 static void
713 mono_create_spvar_for_region (MonoCompile *cfg, int region)
714 {
715         MonoInst *var;
716
717         var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
718         if (var)
719                 return;
720
721         var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
722         /* prevent it from being register allocated */
723         var->flags |= MONO_INST_VOLATILE;
724
725         g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
726 }
727
728 MonoInst *
729 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
730 {
731         return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
732 }
733
734 static MonoInst*
735 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
736 {
737         MonoInst *var;
738
739         var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
740         if (var)
741                 return var;
742
743         var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
744         /* prevent it from being register allocated */
745         var->flags |= MONO_INST_VOLATILE;
746
747         g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
748
749         return var;
750 }
751
752 /*
753  * Returns the type used in the eval stack when @type is loaded.
754  * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
755  */
756 void
757 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
758 {
759         MonoClass *klass;
760
761         type = mini_get_underlying_type (type);
762         inst->klass = klass = mono_class_from_mono_type (type);
763         if (type->byref) {
764                 inst->type = STACK_MP;
765                 return;
766         }
767
768 handle_enum:
769         switch (type->type) {
770         case MONO_TYPE_VOID:
771                 inst->type = STACK_INV;
772                 return;
773         case MONO_TYPE_I1:
774         case MONO_TYPE_U1:
775         case MONO_TYPE_I2:
776         case MONO_TYPE_U2:
777         case MONO_TYPE_I4:
778         case MONO_TYPE_U4:
779                 inst->type = STACK_I4;
780                 return;
781         case MONO_TYPE_I:
782         case MONO_TYPE_U:
783         case MONO_TYPE_PTR:
784         case MONO_TYPE_FNPTR:
785                 inst->type = STACK_PTR;
786                 return;
787         case MONO_TYPE_CLASS:
788         case MONO_TYPE_STRING:
789         case MONO_TYPE_OBJECT:
790         case MONO_TYPE_SZARRAY:
791         case MONO_TYPE_ARRAY:    
792                 inst->type = STACK_OBJ;
793                 return;
794         case MONO_TYPE_I8:
795         case MONO_TYPE_U8:
796                 inst->type = STACK_I8;
797                 return;
798         case MONO_TYPE_R4:
799                 inst->type = cfg->r4_stack_type;
800                 break;
801         case MONO_TYPE_R8:
802                 inst->type = STACK_R8;
803                 return;
804         case MONO_TYPE_VALUETYPE:
805                 if (type->data.klass->enumtype) {
806                         type = mono_class_enum_basetype (type->data.klass);
807                         goto handle_enum;
808                 } else {
809                         inst->klass = klass;
810                         inst->type = STACK_VTYPE;
811                         return;
812                 }
813         case MONO_TYPE_TYPEDBYREF:
814                 inst->klass = mono_defaults.typed_reference_class;
815                 inst->type = STACK_VTYPE;
816                 return;
817         case MONO_TYPE_GENERICINST:
818                 type = &type->data.generic_class->container_class->byval_arg;
819                 goto handle_enum;
820         case MONO_TYPE_VAR:
821         case MONO_TYPE_MVAR:
822                 g_assert (cfg->gshared);
823                 if (mini_is_gsharedvt_type (type)) {
824                         g_assert (cfg->gsharedvt);
825                         inst->type = STACK_VTYPE;
826                 } else {
827                         type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
828                 }
829                 return;
830         default:
831                 g_error ("unknown type 0x%02x in eval stack type", type->type);
832         }
833 }
834
835 /*
836  * The following tables are used to quickly validate the IL code in type_from_op ().
837  */
838 static const char
839 bin_num_table [STACK_MAX] [STACK_MAX] = {
840         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
841         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
842         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
843         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
844         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8,  STACK_INV, STACK_INV, STACK_INV, STACK_R8},
845         {STACK_INV, STACK_MP,  STACK_INV, STACK_MP,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
846         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
847         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
848         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
849 };
850
851 static const char 
852 neg_table [] = {
853         STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
854 };
855
856 /* reduce the size of this table */
857 static const char
858 bin_int_table [STACK_MAX] [STACK_MAX] = {
859         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
860         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
861         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
862         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
863         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
864         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
865         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
866         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
867 };
868
869 static const char
870 bin_comp_table [STACK_MAX] [STACK_MAX] = {
871 /*      Inv i  L  p  F  &  O  vt r4 */
872         {0},
873         {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
874         {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
875         {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
876         {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
877         {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
878         {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
879         {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
880         {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
881 };
882
883 /* reduce the size of this table */
884 static const char
885 shift_table [STACK_MAX] [STACK_MAX] = {
886         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
887         {STACK_INV, STACK_I4,  STACK_INV, STACK_I4,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
888         {STACK_INV, STACK_I8,  STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
889         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
890         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
891         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
892         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
893         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
894 };
895
896 /*
897  * Tables to map from the non-specific opcode to the matching
898  * type-specific opcode.
899  */
900 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
901 static const guint16
902 binops_op_map [STACK_MAX] = {
903         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
904 };
905
906 /* handles from CEE_NEG to CEE_CONV_U8 */
907 static const guint16
908 unops_op_map [STACK_MAX] = {
909         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
910 };
911
912 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
913 static const guint16
914 ovfops_op_map [STACK_MAX] = {
915         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
916 };
917
918 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
919 static const guint16
920 ovf2ops_op_map [STACK_MAX] = {
921         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
922 };
923
924 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
925 static const guint16
926 ovf3ops_op_map [STACK_MAX] = {
927         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
928 };
929
930 /* handles from CEE_BEQ to CEE_BLT_UN */
931 static const guint16
932 beqops_op_map [STACK_MAX] = {
933         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
934 };
935
936 /* handles from CEE_CEQ to CEE_CLT_UN */
937 static const guint16
938 ceqops_op_map [STACK_MAX] = {
939         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
940 };
941
942 /*
943  * Sets ins->type (the type on the eval stack) according to the
944  * type of the opcode and the arguments to it.
945  * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
946  *
947  * FIXME: this function sets ins->type unconditionally in some cases, but
948  * it should set it to invalid for some types (a conv.x on an object)
949  */
950 static void
951 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
952 {
953         switch (ins->opcode) {
954         /* binops */
955         case CEE_ADD:
956         case CEE_SUB:
957         case CEE_MUL:
958         case CEE_DIV:
959         case CEE_REM:
960                 /* FIXME: check unverifiable args for STACK_MP */
961                 ins->type = bin_num_table [src1->type] [src2->type];
962                 ins->opcode += binops_op_map [ins->type];
963                 break;
964         case CEE_DIV_UN:
965         case CEE_REM_UN:
966         case CEE_AND:
967         case CEE_OR:
968         case CEE_XOR:
969                 ins->type = bin_int_table [src1->type] [src2->type];
970                 ins->opcode += binops_op_map [ins->type];
971                 break;
972         case CEE_SHL:
973         case CEE_SHR:
974         case CEE_SHR_UN:
975                 ins->type = shift_table [src1->type] [src2->type];
976                 ins->opcode += binops_op_map [ins->type];
977                 break;
978         case OP_COMPARE:
979         case OP_LCOMPARE:
980         case OP_ICOMPARE:
981                 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
982                 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
983                         ins->opcode = OP_LCOMPARE;
984                 else if (src1->type == STACK_R4)
985                         ins->opcode = OP_RCOMPARE;
986                 else if (src1->type == STACK_R8)
987                         ins->opcode = OP_FCOMPARE;
988                 else
989                         ins->opcode = OP_ICOMPARE;
990                 break;
991         case OP_ICOMPARE_IMM:
992                 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
993                 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
994                         ins->opcode = OP_LCOMPARE_IMM;          
995                 break;
996         case CEE_BEQ:
997         case CEE_BGE:
998         case CEE_BGT:
999         case CEE_BLE:
1000         case CEE_BLT:
1001         case CEE_BNE_UN:
1002         case CEE_BGE_UN:
1003         case CEE_BGT_UN:
1004         case CEE_BLE_UN:
1005         case CEE_BLT_UN:
1006                 ins->opcode += beqops_op_map [src1->type];
1007                 break;
1008         case OP_CEQ:
1009                 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
1010                 ins->opcode += ceqops_op_map [src1->type];
1011                 break;
1012         case OP_CGT:
1013         case OP_CGT_UN:
1014         case OP_CLT:
1015         case OP_CLT_UN:
1016                 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1017                 ins->opcode += ceqops_op_map [src1->type];
1018                 break;
1019         /* unops */
1020         case CEE_NEG:
1021                 ins->type = neg_table [src1->type];
1022                 ins->opcode += unops_op_map [ins->type];
1023                 break;
1024         case CEE_NOT:
1025                 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1026                         ins->type = src1->type;
1027                 else
1028                         ins->type = STACK_INV;
1029                 ins->opcode += unops_op_map [ins->type];
1030                 break;
1031         case CEE_CONV_I1:
1032         case CEE_CONV_I2:
1033         case CEE_CONV_I4:
1034         case CEE_CONV_U4:
1035                 ins->type = STACK_I4;
1036                 ins->opcode += unops_op_map [src1->type];
1037                 break;
1038         case CEE_CONV_R_UN:
1039                 ins->type = STACK_R8;
1040                 switch (src1->type) {
1041                 case STACK_I4:
1042                 case STACK_PTR:
1043                         ins->opcode = OP_ICONV_TO_R_UN;
1044                         break;
1045                 case STACK_I8:
1046                         ins->opcode = OP_LCONV_TO_R_UN; 
1047                         break;
1048                 }
1049                 break;
1050         case CEE_CONV_OVF_I1:
1051         case CEE_CONV_OVF_U1:
1052         case CEE_CONV_OVF_I2:
1053         case CEE_CONV_OVF_U2:
1054         case CEE_CONV_OVF_I4:
1055         case CEE_CONV_OVF_U4:
1056                 ins->type = STACK_I4;
1057                 ins->opcode += ovf3ops_op_map [src1->type];
1058                 break;
1059         case CEE_CONV_OVF_I_UN:
1060         case CEE_CONV_OVF_U_UN:
1061                 ins->type = STACK_PTR;
1062                 ins->opcode += ovf2ops_op_map [src1->type];
1063                 break;
1064         case CEE_CONV_OVF_I1_UN:
1065         case CEE_CONV_OVF_I2_UN:
1066         case CEE_CONV_OVF_I4_UN:
1067         case CEE_CONV_OVF_U1_UN:
1068         case CEE_CONV_OVF_U2_UN:
1069         case CEE_CONV_OVF_U4_UN:
1070                 ins->type = STACK_I4;
1071                 ins->opcode += ovf2ops_op_map [src1->type];
1072                 break;
1073         case CEE_CONV_U:
1074                 ins->type = STACK_PTR;
1075                 switch (src1->type) {
1076                 case STACK_I4:
1077                         ins->opcode = OP_ICONV_TO_U;
1078                         break;
1079                 case STACK_PTR:
1080                 case STACK_MP:
1081 #if SIZEOF_VOID_P == 8
1082                         ins->opcode = OP_LCONV_TO_U;
1083 #else
1084                         ins->opcode = OP_MOVE;
1085 #endif
1086                         break;
1087                 case STACK_I8:
1088                         ins->opcode = OP_LCONV_TO_U;
1089                         break;
1090                 case STACK_R8:
1091                         ins->opcode = OP_FCONV_TO_U;
1092                         break;
1093                 }
1094                 break;
1095         case CEE_CONV_I8:
1096         case CEE_CONV_U8:
1097                 ins->type = STACK_I8;
1098                 ins->opcode += unops_op_map [src1->type];
1099                 break;
1100         case CEE_CONV_OVF_I8:
1101         case CEE_CONV_OVF_U8:
1102                 ins->type = STACK_I8;
1103                 ins->opcode += ovf3ops_op_map [src1->type];
1104                 break;
1105         case CEE_CONV_OVF_U8_UN:
1106         case CEE_CONV_OVF_I8_UN:
1107                 ins->type = STACK_I8;
1108                 ins->opcode += ovf2ops_op_map [src1->type];
1109                 break;
1110         case CEE_CONV_R4:
1111                 ins->type = cfg->r4_stack_type;
1112                 ins->opcode += unops_op_map [src1->type];
1113                 break;
1114         case CEE_CONV_R8:
1115                 ins->type = STACK_R8;
1116                 ins->opcode += unops_op_map [src1->type];
1117                 break;
1118         case OP_CKFINITE:
1119                 ins->type = STACK_R8;           
1120                 break;
1121         case CEE_CONV_U2:
1122         case CEE_CONV_U1:
1123                 ins->type = STACK_I4;
1124                 ins->opcode += ovfops_op_map [src1->type];
1125                 break;
1126         case CEE_CONV_I:
1127         case CEE_CONV_OVF_I:
1128         case CEE_CONV_OVF_U:
1129                 ins->type = STACK_PTR;
1130                 ins->opcode += ovfops_op_map [src1->type];
1131                 break;
1132         case CEE_ADD_OVF:
1133         case CEE_ADD_OVF_UN:
1134         case CEE_MUL_OVF:
1135         case CEE_MUL_OVF_UN:
1136         case CEE_SUB_OVF:
1137         case CEE_SUB_OVF_UN:
1138                 ins->type = bin_num_table [src1->type] [src2->type];
1139                 ins->opcode += ovfops_op_map [src1->type];
1140                 if (ins->type == STACK_R8)
1141                         ins->type = STACK_INV;
1142                 break;
1143         case OP_LOAD_MEMBASE:
1144                 ins->type = STACK_PTR;
1145                 break;
1146         case OP_LOADI1_MEMBASE:
1147         case OP_LOADU1_MEMBASE:
1148         case OP_LOADI2_MEMBASE:
1149         case OP_LOADU2_MEMBASE:
1150         case OP_LOADI4_MEMBASE:
1151         case OP_LOADU4_MEMBASE:
1152                 ins->type = STACK_PTR;
1153                 break;
1154         case OP_LOADI8_MEMBASE:
1155                 ins->type = STACK_I8;
1156                 break;
1157         case OP_LOADR4_MEMBASE:
1158                 ins->type = cfg->r4_stack_type;
1159                 break;
1160         case OP_LOADR8_MEMBASE:
1161                 ins->type = STACK_R8;
1162                 break;
1163         default:
1164                 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1165                 break;
1166         }
1167
1168         if (ins->type == STACK_MP)
1169                 ins->klass = mono_defaults.object_class;
1170 }
1171
1172 static const char 
1173 ldind_type [] = {
1174         STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1175 };
1176
1177 #if 0
1178
1179 static const char
1180 param_table [STACK_MAX] [STACK_MAX] = {
1181         {0},
1182 };
1183
1184 static int
1185 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1186 {
1187         int i;
1188
1189         if (sig->hasthis) {
1190                 switch (args->type) {
1191                 case STACK_I4:
1192                 case STACK_I8:
1193                 case STACK_R8:
1194                 case STACK_VTYPE:
1195                 case STACK_INV:
1196                         return 0;
1197                 }
1198                 args++;
1199         }
1200         for (i = 0; i < sig->param_count; ++i) {
1201                 switch (args [i].type) {
1202                 case STACK_INV:
1203                         return 0;
1204                 case STACK_MP:
1205                         if (!sig->params [i]->byref)
1206                                 return 0;
1207                         continue;
1208                 case STACK_OBJ:
1209                         if (sig->params [i]->byref)
1210                                 return 0;
1211                         switch (sig->params [i]->type) {
1212                         case MONO_TYPE_CLASS:
1213                         case MONO_TYPE_STRING:
1214                         case MONO_TYPE_OBJECT:
1215                         case MONO_TYPE_SZARRAY:
1216                         case MONO_TYPE_ARRAY:
1217                                 break;
1218                         default:
1219                                 return 0;
1220                         }
1221                         continue;
1222                 case STACK_R8:
1223                         if (sig->params [i]->byref)
1224                                 return 0;
1225                         if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1226                                 return 0;
1227                         continue;
1228                 case STACK_PTR:
1229                 case STACK_I4:
1230                 case STACK_I8:
1231                 case STACK_VTYPE:
1232                         break;
1233                 }
1234                 /*if (!param_table [args [i].type] [sig->params [i]->type])
1235                         return 0;*/
1236         }
1237         return 1;
1238 }
1239 #endif
1240
1241 /*
1242  * When we need a pointer to the current domain many times in a method, we
1243  * call mono_domain_get() once and we store the result in a local variable.
1244  * This function returns the variable that represents the MonoDomain*.
1245  */
1246 inline static MonoInst *
1247 mono_get_domainvar (MonoCompile *cfg)
1248 {
1249         if (!cfg->domainvar)
1250                 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1251         return cfg->domainvar;
1252 }
1253
1254 /*
1255  * The got_var contains the address of the Global Offset Table when AOT 
1256  * compiling.
1257  */
1258 MonoInst *
1259 mono_get_got_var (MonoCompile *cfg)
1260 {
1261         if (!cfg->compile_aot || !cfg->backend->need_got_var)
1262                 return NULL;
1263         if (!cfg->got_var) {
1264                 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1265         }
1266         return cfg->got_var;
1267 }
1268
1269 static MonoInst *
1270 mono_get_vtable_var (MonoCompile *cfg)
1271 {
1272         g_assert (cfg->gshared);
1273
1274         if (!cfg->rgctx_var) {
1275                 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1276                 /* force the var to be stack allocated */
1277                 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1278         }
1279
1280         return cfg->rgctx_var;
1281 }
1282
1283 static MonoType*
1284 type_from_stack_type (MonoInst *ins) {
1285         switch (ins->type) {
1286         case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1287         case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1288         case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1289         case STACK_R4: return &mono_defaults.single_class->byval_arg;
1290         case STACK_R8: return &mono_defaults.double_class->byval_arg;
1291         case STACK_MP:
1292                 return &ins->klass->this_arg;
1293         case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1294         case STACK_VTYPE: return &ins->klass->byval_arg;
1295         default:
1296                 g_error ("stack type %d to monotype not handled\n", ins->type);
1297         }
1298         return NULL;
1299 }
1300
1301 static G_GNUC_UNUSED int
1302 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1303 {
1304         t = mono_type_get_underlying_type (t);
1305         switch (t->type) {
1306         case MONO_TYPE_I1:
1307         case MONO_TYPE_U1:
1308         case MONO_TYPE_I2:
1309         case MONO_TYPE_U2:
1310         case MONO_TYPE_I4:
1311         case MONO_TYPE_U4:
1312                 return STACK_I4;
1313         case MONO_TYPE_I:
1314         case MONO_TYPE_U:
1315         case MONO_TYPE_PTR:
1316         case MONO_TYPE_FNPTR:
1317                 return STACK_PTR;
1318         case MONO_TYPE_CLASS:
1319         case MONO_TYPE_STRING:
1320         case MONO_TYPE_OBJECT:
1321         case MONO_TYPE_SZARRAY:
1322         case MONO_TYPE_ARRAY:    
1323                 return STACK_OBJ;
1324         case MONO_TYPE_I8:
1325         case MONO_TYPE_U8:
1326                 return STACK_I8;
1327         case MONO_TYPE_R4:
1328                 return cfg->r4_stack_type;
1329         case MONO_TYPE_R8:
1330                 return STACK_R8;
1331         case MONO_TYPE_VALUETYPE:
1332         case MONO_TYPE_TYPEDBYREF:
1333                 return STACK_VTYPE;
1334         case MONO_TYPE_GENERICINST:
1335                 if (mono_type_generic_inst_is_valuetype (t))
1336                         return STACK_VTYPE;
1337                 else
1338                         return STACK_OBJ;
1339                 break;
1340         default:
1341                 g_assert_not_reached ();
1342         }
1343
1344         return -1;
1345 }
1346
1347 static MonoClass*
1348 array_access_to_klass (int opcode)
1349 {
1350         switch (opcode) {
1351         case CEE_LDELEM_U1:
1352                 return mono_defaults.byte_class;
1353         case CEE_LDELEM_U2:
1354                 return mono_defaults.uint16_class;
1355         case CEE_LDELEM_I:
1356         case CEE_STELEM_I:
1357                 return mono_defaults.int_class;
1358         case CEE_LDELEM_I1:
1359         case CEE_STELEM_I1:
1360                 return mono_defaults.sbyte_class;
1361         case CEE_LDELEM_I2:
1362         case CEE_STELEM_I2:
1363                 return mono_defaults.int16_class;
1364         case CEE_LDELEM_I4:
1365         case CEE_STELEM_I4:
1366                 return mono_defaults.int32_class;
1367         case CEE_LDELEM_U4:
1368                 return mono_defaults.uint32_class;
1369         case CEE_LDELEM_I8:
1370         case CEE_STELEM_I8:
1371                 return mono_defaults.int64_class;
1372         case CEE_LDELEM_R4:
1373         case CEE_STELEM_R4:
1374                 return mono_defaults.single_class;
1375         case CEE_LDELEM_R8:
1376         case CEE_STELEM_R8:
1377                 return mono_defaults.double_class;
1378         case CEE_LDELEM_REF:
1379         case CEE_STELEM_REF:
1380                 return mono_defaults.object_class;
1381         default:
1382                 g_assert_not_reached ();
1383         }
1384         return NULL;
1385 }
1386
1387 /*
1388  * We try to share variables when possible
1389  */
1390 static MonoInst *
1391 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1392 {
1393         MonoInst *res;
1394         int pos, vnum;
1395
1396         /* inlining can result in deeper stacks */ 
1397         if (slot >= cfg->header->max_stack)
1398                 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1399
1400         pos = ins->type - 1 + slot * STACK_MAX;
1401
1402         switch (ins->type) {
1403         case STACK_I4:
1404         case STACK_I8:
1405         case STACK_R8:
1406         case STACK_PTR:
1407         case STACK_MP:
1408         case STACK_OBJ:
1409                 if ((vnum = cfg->intvars [pos]))
1410                         return cfg->varinfo [vnum];
1411                 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1412                 cfg->intvars [pos] = res->inst_c0;
1413                 break;
1414         default:
1415                 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1416         }
1417         return res;
1418 }
1419
1420 static void
1421 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1422 {
1423         /* 
1424          * Don't use this if a generic_context is set, since that means AOT can't
1425          * look up the method using just the image+token.
1426          * table == 0 means this is a reference made from a wrapper.
1427          */
1428         if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1429                 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1430                 jump_info_token->image = image;
1431                 jump_info_token->token = token;
1432                 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1433         }
1434 }
1435
1436 /*
1437  * This function is called to handle items that are left on the evaluation stack
1438  * at basic block boundaries. What happens is that we save the values to local variables
1439  * and we reload them later when first entering the target basic block (with the
1440  * handle_loaded_temps () function).
1441  * A single joint point will use the same variables (stored in the array bb->out_stack or
1442  * bb->in_stack, if the basic block is before or after the joint point).
1443  *
1444  * This function needs to be called _before_ emitting the last instruction of
1445  * the bb (i.e. before emitting a branch).
1446  * If the stack merge fails at a join point, cfg->unverifiable is set.
1447  */
1448 static void
1449 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1450 {
1451         int i, bindex;
1452         MonoBasicBlock *bb = cfg->cbb;
1453         MonoBasicBlock *outb;
1454         MonoInst *inst, **locals;
1455         gboolean found;
1456
1457         if (!count)
1458                 return;
1459         if (cfg->verbose_level > 3)
1460                 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1461         if (!bb->out_scount) {
1462                 bb->out_scount = count;
1463                 //printf ("bblock %d has out:", bb->block_num);
1464                 found = FALSE;
1465                 for (i = 0; i < bb->out_count; ++i) {
1466                         outb = bb->out_bb [i];
1467                         /* exception handlers are linked, but they should not be considered for stack args */
1468                         if (outb->flags & BB_EXCEPTION_HANDLER)
1469                                 continue;
1470                         //printf (" %d", outb->block_num);
1471                         if (outb->in_stack) {
1472                                 found = TRUE;
1473                                 bb->out_stack = outb->in_stack;
1474                                 break;
1475                         }
1476                 }
1477                 //printf ("\n");
1478                 if (!found) {
1479                         bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1480                         for (i = 0; i < count; ++i) {
1481                                 /* 
1482                                  * try to reuse temps already allocated for this purpouse, if they occupy the same
1483                                  * stack slot and if they are of the same type.
1484                                  * This won't cause conflicts since if 'local' is used to 
1485                                  * store one of the values in the in_stack of a bblock, then
1486                                  * the same variable will be used for the same outgoing stack 
1487                                  * slot as well. 
1488                                  * This doesn't work when inlining methods, since the bblocks
1489                                  * in the inlined methods do not inherit their in_stack from
1490                                  * the bblock they are inlined to. See bug #58863 for an
1491                                  * example.
1492                                  */
1493                                 if (cfg->inlined_method)
1494                                         bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1495                                 else
1496                                         bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1497                         }
1498                 }
1499         }
1500
1501         for (i = 0; i < bb->out_count; ++i) {
1502                 outb = bb->out_bb [i];
1503                 /* exception handlers are linked, but they should not be considered for stack args */
1504                 if (outb->flags & BB_EXCEPTION_HANDLER)
1505                         continue;
1506                 if (outb->in_scount) {
1507                         if (outb->in_scount != bb->out_scount) {
1508                                 cfg->unverifiable = TRUE;
1509                                 return;
1510                         }
1511                         continue; /* check they are the same locals */
1512                 }
1513                 outb->in_scount = count;
1514                 outb->in_stack = bb->out_stack;
1515         }
1516
1517         locals = bb->out_stack;
1518         cfg->cbb = bb;
1519         for (i = 0; i < count; ++i) {
1520                 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1521                 inst->cil_code = sp [i]->cil_code;
1522                 sp [i] = locals [i];
1523                 if (cfg->verbose_level > 3)
1524                         printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1525         }
1526
1527         /*
1528          * It is possible that the out bblocks already have in_stack assigned, and
1529          * the in_stacks differ. In this case, we will store to all the different 
1530          * in_stacks.
1531          */
1532
1533         found = TRUE;
1534         bindex = 0;
1535         while (found) {
1536                 /* Find a bblock which has a different in_stack */
1537                 found = FALSE;
1538                 while (bindex < bb->out_count) {
1539                         outb = bb->out_bb [bindex];
1540                         /* exception handlers are linked, but they should not be considered for stack args */
1541                         if (outb->flags & BB_EXCEPTION_HANDLER) {
1542                                 bindex++;
1543                                 continue;
1544                         }
1545                         if (outb->in_stack != locals) {
1546                                 for (i = 0; i < count; ++i) {
1547                                         EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1548                                         inst->cil_code = sp [i]->cil_code;
1549                                         sp [i] = locals [i];
1550                                         if (cfg->verbose_level > 3)
1551                                                 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1552                                 }
1553                                 locals = outb->in_stack;
1554                                 found = TRUE;
1555                                 break;
1556                         }
1557                         bindex ++;
1558                 }
1559         }
1560 }
1561
1562 static MonoInst*
1563 emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1564 {
1565         MonoInst *ins;
1566
1567         if (cfg->compile_aot) {
1568                 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1569         } else {
1570                 MonoJumpInfo ji;
1571                 gpointer target;
1572                 MonoError error;
1573
1574                 ji.type = patch_type;
1575                 ji.data.target = data;
1576                 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1577                 mono_error_assert_ok (&error);
1578
1579                 EMIT_NEW_PCONST (cfg, ins, target);
1580         }
1581         return ins;
1582 }
1583
1584 MonoInst*
1585 mini_emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1586 {
1587         return emit_runtime_constant (cfg, patch_type, data);
1588 }
1589
1590 static void 
1591 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1592 {
1593         int val_reg;
1594
1595         g_assert (val == 0);
1596
1597         if (align == 0)
1598                 align = 4;
1599
1600         if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1601                 switch (size) {
1602                 case 1:
1603                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1604                         return;
1605                 case 2:
1606                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1607                         return;
1608                 case 4:
1609                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1610                         return;
1611 #if SIZEOF_REGISTER == 8
1612                 case 8:
1613                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1614                         return;
1615 #endif
1616                 }
1617         }
1618
1619         val_reg = alloc_preg (cfg);
1620
1621         if (SIZEOF_REGISTER == 8)
1622                 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1623         else
1624                 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1625
1626         if (align < 4) {
1627                 /* This could be optimized further if neccesary */
1628                 while (size >= 1) {
1629                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1630                         offset += 1;
1631                         size -= 1;
1632                 }
1633                 return;
1634         }       
1635
1636         if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1637                 if (offset % 8) {
1638                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1639                         offset += 4;
1640                         size -= 4;
1641                 }
1642                 while (size >= 8) {
1643                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1644                         offset += 8;
1645                         size -= 8;
1646                 }
1647         }       
1648
1649         while (size >= 4) {
1650                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1651                 offset += 4;
1652                 size -= 4;
1653         }
1654         while (size >= 2) {
1655                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1656                 offset += 2;
1657                 size -= 2;
1658         }
1659         while (size >= 1) {
1660                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1661                 offset += 1;
1662                 size -= 1;
1663         }
1664 }
1665
1666 void 
1667 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1668 {
1669         int cur_reg;
1670
1671         if (align == 0)
1672                 align = 4;
1673
1674         /*FIXME arbitrary hack to avoid unbound code expansion.*/
1675         g_assert (size < 10000);
1676
1677         if (align < 4) {
1678                 /* This could be optimized further if neccesary */
1679                 while (size >= 1) {
1680                         cur_reg = alloc_preg (cfg);
1681                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1682                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1683                         doffset += 1;
1684                         soffset += 1;
1685                         size -= 1;
1686                 }
1687         }
1688
1689         if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1690                 while (size >= 8) {
1691                         cur_reg = alloc_preg (cfg);
1692                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1693                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1694                         doffset += 8;
1695                         soffset += 8;
1696                         size -= 8;
1697                 }
1698         }       
1699
1700         while (size >= 4) {
1701                 cur_reg = alloc_preg (cfg);
1702                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1703                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1704                 doffset += 4;
1705                 soffset += 4;
1706                 size -= 4;
1707         }
1708         while (size >= 2) {
1709                 cur_reg = alloc_preg (cfg);
1710                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1711                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1712                 doffset += 2;
1713                 soffset += 2;
1714                 size -= 2;
1715         }
1716         while (size >= 1) {
1717                 cur_reg = alloc_preg (cfg);
1718                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1719                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1720                 doffset += 1;
1721                 soffset += 1;
1722                 size -= 1;
1723         }
1724 }
1725
1726 MonoInst*
1727 mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
1728 {
1729         if (cfg->compile_aot) {
1730                 MonoInst *addr;
1731                 /*
1732                  * tls getters are critical pieces of code and we don't want to resolve them
1733                  * through the standard plt/tramp mechanism since we might expose ourselves
1734                  * to crashes and infinite recursions.
1735                  */
1736                 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GET_TLS_TRAMP, (void*)key);
1737                 return mono_emit_calli (cfg, helper_sig_get_tls_tramp, NULL, addr, NULL, NULL);
1738         } else {
1739                 gpointer getter = mono_tls_get_tls_getter (key, FALSE);
1740                 return mono_emit_jit_icall (cfg, getter, NULL);
1741         }
1742 }
1743
1744 static MonoInst*
1745 mono_create_tls_set (MonoCompile *cfg, MonoInst *value, MonoTlsKey key)
1746 {
1747         if (cfg->compile_aot) {
1748                 MonoInst *addr;
1749                 EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_SET_TLS_TRAMP, (void*)key);
1750                 return mono_emit_calli (cfg, helper_sig_set_tls_tramp, &value, addr, NULL, NULL);
1751         } else {
1752                 gpointer setter = mono_tls_get_tls_setter (key, FALSE);
1753                 return mono_emit_jit_icall (cfg, setter, &value);
1754         }
1755 }
1756
1757 /*
1758  * emit_push_lmf:
1759  *
1760  *   Emit IR to push the current LMF onto the LMF stack.
1761  */
1762 static void
1763 emit_push_lmf (MonoCompile *cfg)
1764 {
1765         /*
1766          * Emit IR to push the LMF:
1767          * lmf_addr = <lmf_addr from tls>
1768          * lmf->lmf_addr = lmf_addr
1769          * lmf->prev_lmf = *lmf_addr
1770          * *lmf_addr = lmf
1771          */
1772         MonoInst *ins, *lmf_ins;
1773
1774         if (!cfg->lmf_ir)
1775                 return;
1776
1777         if (cfg->lmf_ir_mono_lmf) {
1778                 MonoInst *lmf_vara_ins, *lmf_ins;
1779                 /* Load current lmf */
1780                 lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF);
1781                 g_assert (lmf_ins);
1782                 EMIT_NEW_VARLOADA (cfg, lmf_vara_ins, cfg->lmf_var, NULL);
1783                 /* Save previous_lmf */
1784                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_vara_ins->dreg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
1785                 /* Set new LMF */
1786                 mono_create_tls_set (cfg, lmf_vara_ins, TLS_KEY_LMF);
1787         } else {
1788                 int lmf_reg, prev_lmf_reg;
1789                 /*
1790                  * Store lmf_addr in a variable, so it can be allocated to a global register.
1791                  */
1792                 if (!cfg->lmf_addr_var)
1793                         cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1794
1795 #ifdef HOST_WIN32
1796                 ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
1797                 g_assert (ins);
1798                 int jit_tls_dreg = ins->dreg;
1799
1800                 lmf_reg = alloc_preg (cfg);
1801                 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
1802 #else
1803                 lmf_ins = mono_create_tls_get (cfg, TLS_KEY_LMF_ADDR);
1804                 g_assert (lmf_ins);
1805 #endif
1806                 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
1807
1808                 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1809                 lmf_reg = ins->dreg;
1810
1811                 prev_lmf_reg = alloc_preg (cfg);
1812                 /* Save previous_lmf */
1813                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
1814                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
1815                 /* Set new lmf */
1816                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
1817         }
1818 }
1819
1820 /*
1821  * emit_pop_lmf:
1822  *
1823  *   Emit IR to pop the current LMF from the LMF stack.
1824  */
1825 static void
1826 emit_pop_lmf (MonoCompile *cfg)
1827 {
1828         int lmf_reg, lmf_addr_reg;
1829         MonoInst *ins;
1830
1831         if (!cfg->lmf_ir)
1832                 return;
1833
1834         EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
1835         lmf_reg = ins->dreg;
1836
1837         if (cfg->lmf_ir_mono_lmf) {
1838                 /* Load previous_lmf */
1839                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, alloc_preg (cfg), lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1840                 /* Set new LMF */
1841                 mono_create_tls_set (cfg, ins, TLS_KEY_LMF);
1842         } else {
1843                 int prev_lmf_reg;
1844                 /*
1845                  * Emit IR to pop the LMF:
1846                  * *(lmf->lmf_addr) = lmf->prev_lmf
1847                  */
1848                 /* This could be called before emit_push_lmf () */
1849                 if (!cfg->lmf_addr_var)
1850                         cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1851                 lmf_addr_reg = cfg->lmf_addr_var->dreg;
1852
1853                 prev_lmf_reg = alloc_preg (cfg);
1854                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
1855                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
1856         }
1857 }
1858
1859 static void
1860 emit_instrumentation_call (MonoCompile *cfg, void *func)
1861 {
1862         MonoInst *iargs [1];
1863
1864         /*
1865          * Avoid instrumenting inlined methods since it can
1866          * distort profiling results.
1867          */
1868         if (cfg->method != cfg->current_method)
1869                 return;
1870
1871         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
1872                 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
1873                 mono_emit_jit_icall (cfg, func, iargs);
1874         }
1875 }
1876
1877 static int
1878 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
1879 {
1880 handle_enum:
1881         type = mini_get_underlying_type (type);
1882         switch (type->type) {
1883         case MONO_TYPE_VOID:
1884                 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
1885         case MONO_TYPE_I1:
1886         case MONO_TYPE_U1:
1887         case MONO_TYPE_I2:
1888         case MONO_TYPE_U2:
1889         case MONO_TYPE_I4:
1890         case MONO_TYPE_U4:
1891                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1892         case MONO_TYPE_I:
1893         case MONO_TYPE_U:
1894         case MONO_TYPE_PTR:
1895         case MONO_TYPE_FNPTR:
1896                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1897         case MONO_TYPE_CLASS:
1898         case MONO_TYPE_STRING:
1899         case MONO_TYPE_OBJECT:
1900         case MONO_TYPE_SZARRAY:
1901         case MONO_TYPE_ARRAY:    
1902                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
1903         case MONO_TYPE_I8:
1904         case MONO_TYPE_U8:
1905                 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
1906         case MONO_TYPE_R4:
1907                 if (cfg->r4fp)
1908                         return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
1909                 else
1910                         return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1911         case MONO_TYPE_R8:
1912                 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
1913         case MONO_TYPE_VALUETYPE:
1914                 if (type->data.klass->enumtype) {
1915                         type = mono_class_enum_basetype (type->data.klass);
1916                         goto handle_enum;
1917                 } else
1918                         return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1919         case MONO_TYPE_TYPEDBYREF:
1920                 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1921         case MONO_TYPE_GENERICINST:
1922                 type = &type->data.generic_class->container_class->byval_arg;
1923                 goto handle_enum;
1924         case MONO_TYPE_VAR:
1925         case MONO_TYPE_MVAR:
1926                 /* gsharedvt */
1927                 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
1928         default:
1929                 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1930         }
1931         return -1;
1932 }
1933
1934 //XXX this ignores if t is byref
1935 #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)))))
1936
1937 /*
1938  * target_type_is_incompatible:
1939  * @cfg: MonoCompile context
1940  *
1941  * Check that the item @arg on the evaluation stack can be stored
1942  * in the target type (can be a local, or field, etc).
1943  * The cfg arg can be used to check if we need verification or just
1944  * validity checks.
1945  *
1946  * Returns: non-0 value if arg can't be stored on a target.
1947  */
1948 static int
1949 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
1950 {
1951         MonoType *simple_type;
1952         MonoClass *klass;
1953
1954         if (target->byref) {
1955                 /* FIXME: check that the pointed to types match */
1956                 if (arg->type == STACK_MP) {
1957                         /* This is needed to handle gshared types + ldaddr. We lower the types so we can handle enums and other typedef-like types. */
1958                         MonoClass *target_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&mono_class_from_mono_type (target)->byval_arg));
1959                         MonoClass *source_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg));
1960
1961                         /* if the target is native int& or same type */
1962                         if (target->type == MONO_TYPE_I || target_class_lowered == source_class_lowered)
1963                                 return 0;
1964
1965                         /* Both are primitive type byrefs and the source points to a larger type that the destination */
1966                         if (MONO_TYPE_IS_PRIMITIVE_SCALAR (&target_class_lowered->byval_arg) && MONO_TYPE_IS_PRIMITIVE_SCALAR (&source_class_lowered->byval_arg) &&
1967                                 mono_class_instance_size (target_class_lowered) <= mono_class_instance_size (source_class_lowered))
1968                                 return 0;
1969                         return 1;
1970                 }
1971                 if (arg->type == STACK_PTR)
1972                         return 0;
1973                 return 1;
1974         }
1975
1976         simple_type = mini_get_underlying_type (target);
1977         switch (simple_type->type) {
1978         case MONO_TYPE_VOID:
1979                 return 1;
1980         case MONO_TYPE_I1:
1981         case MONO_TYPE_U1:
1982         case MONO_TYPE_I2:
1983         case MONO_TYPE_U2:
1984         case MONO_TYPE_I4:
1985         case MONO_TYPE_U4:
1986                 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
1987                         return 1;
1988                 return 0;
1989         case MONO_TYPE_PTR:
1990                 /* STACK_MP is needed when setting pinned locals */
1991                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
1992                         return 1;
1993                 return 0;
1994         case MONO_TYPE_I:
1995         case MONO_TYPE_U:
1996         case MONO_TYPE_FNPTR:
1997                 /* 
1998                  * Some opcodes like ldloca returns 'transient pointers' which can be stored in
1999                  * in native int. (#688008).
2000                  */
2001                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2002                         return 1;
2003                 return 0;
2004         case MONO_TYPE_CLASS:
2005         case MONO_TYPE_STRING:
2006         case MONO_TYPE_OBJECT:
2007         case MONO_TYPE_SZARRAY:
2008         case MONO_TYPE_ARRAY:    
2009                 if (arg->type != STACK_OBJ)
2010                         return 1;
2011                 /* FIXME: check type compatibility */
2012                 return 0;
2013         case MONO_TYPE_I8:
2014         case MONO_TYPE_U8:
2015                 if (arg->type != STACK_I8)
2016                         return 1;
2017                 return 0;
2018         case MONO_TYPE_R4:
2019                 if (arg->type != cfg->r4_stack_type)
2020                         return 1;
2021                 return 0;
2022         case MONO_TYPE_R8:
2023                 if (arg->type != STACK_R8)
2024                         return 1;
2025                 return 0;
2026         case MONO_TYPE_VALUETYPE:
2027                 if (arg->type != STACK_VTYPE)
2028                         return 1;
2029                 klass = mono_class_from_mono_type (simple_type);
2030                 if (klass != arg->klass)
2031                         return 1;
2032                 return 0;
2033         case MONO_TYPE_TYPEDBYREF:
2034                 if (arg->type != STACK_VTYPE)
2035                         return 1;
2036                 klass = mono_class_from_mono_type (simple_type);
2037                 if (klass != arg->klass)
2038                         return 1;
2039                 return 0;
2040         case MONO_TYPE_GENERICINST:
2041                 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2042                         MonoClass *target_class;
2043                         if (arg->type != STACK_VTYPE)
2044                                 return 1;
2045                         klass = mono_class_from_mono_type (simple_type);
2046                         target_class = mono_class_from_mono_type (target);
2047                         /* The second cases is needed when doing partial sharing */
2048                         if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
2049                                 return 1;
2050                         return 0;
2051                 } else {
2052                         if (arg->type != STACK_OBJ)
2053                                 return 1;
2054                         /* FIXME: check type compatibility */
2055                         return 0;
2056                 }
2057         case MONO_TYPE_VAR:
2058         case MONO_TYPE_MVAR:
2059                 g_assert (cfg->gshared);
2060                 if (mini_type_var_is_vt (simple_type)) {
2061                         if (arg->type != STACK_VTYPE)
2062                                 return 1;
2063                 } else {
2064                         if (arg->type != STACK_OBJ)
2065                                 return 1;
2066                 }
2067                 return 0;
2068         default:
2069                 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2070         }
2071         return 1;
2072 }
2073
2074 /*
2075  * Prepare arguments for passing to a function call.
2076  * Return a non-zero value if the arguments can't be passed to the given
2077  * signature.
2078  * The type checks are not yet complete and some conversions may need
2079  * casts on 32 or 64 bit architectures.
2080  *
2081  * FIXME: implement this using target_type_is_incompatible ()
2082  */
2083 static int
2084 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2085 {
2086         MonoType *simple_type;
2087         int i;
2088
2089         if (sig->hasthis) {
2090                 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2091                         return 1;
2092                 args++;
2093         }
2094         for (i = 0; i < sig->param_count; ++i) {
2095                 if (sig->params [i]->byref) {
2096                         if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2097                                 return 1;
2098                         continue;
2099                 }
2100                 simple_type = mini_get_underlying_type (sig->params [i]);
2101 handle_enum:
2102                 switch (simple_type->type) {
2103                 case MONO_TYPE_VOID:
2104                         return 1;
2105                         continue;
2106                 case MONO_TYPE_I1:
2107                 case MONO_TYPE_U1:
2108                 case MONO_TYPE_I2:
2109                 case MONO_TYPE_U2:
2110                 case MONO_TYPE_I4:
2111                 case MONO_TYPE_U4:
2112                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2113                                 return 1;
2114                         continue;
2115                 case MONO_TYPE_I:
2116                 case MONO_TYPE_U:
2117                 case MONO_TYPE_PTR:
2118                 case MONO_TYPE_FNPTR:
2119                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2120                                 return 1;
2121                         continue;
2122                 case MONO_TYPE_CLASS:
2123                 case MONO_TYPE_STRING:
2124                 case MONO_TYPE_OBJECT:
2125                 case MONO_TYPE_SZARRAY:
2126                 case MONO_TYPE_ARRAY:    
2127                         if (args [i]->type != STACK_OBJ)
2128                                 return 1;
2129                         continue;
2130                 case MONO_TYPE_I8:
2131                 case MONO_TYPE_U8:
2132                         if (args [i]->type != STACK_I8)
2133                                 return 1;
2134                         continue;
2135                 case MONO_TYPE_R4:
2136                         if (args [i]->type != cfg->r4_stack_type)
2137                                 return 1;
2138                         continue;
2139                 case MONO_TYPE_R8:
2140                         if (args [i]->type != STACK_R8)
2141                                 return 1;
2142                         continue;
2143                 case MONO_TYPE_VALUETYPE:
2144                         if (simple_type->data.klass->enumtype) {
2145                                 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2146                                 goto handle_enum;
2147                         }
2148                         if (args [i]->type != STACK_VTYPE)
2149                                 return 1;
2150                         continue;
2151                 case MONO_TYPE_TYPEDBYREF:
2152                         if (args [i]->type != STACK_VTYPE)
2153                                 return 1;
2154                         continue;
2155                 case MONO_TYPE_GENERICINST:
2156                         simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2157                         goto handle_enum;
2158                 case MONO_TYPE_VAR:
2159                 case MONO_TYPE_MVAR:
2160                         /* gsharedvt */
2161                         if (args [i]->type != STACK_VTYPE)
2162                                 return 1;
2163                         continue;
2164                 default:
2165                         g_error ("unknown type 0x%02x in check_call_signature",
2166                                  simple_type->type);
2167                 }
2168         }
2169         return 0;
2170 }
2171
2172 static int
2173 callvirt_to_call (int opcode)
2174 {
2175         switch (opcode) {
2176         case OP_CALL_MEMBASE:
2177                 return OP_CALL;
2178         case OP_VOIDCALL_MEMBASE:
2179                 return OP_VOIDCALL;
2180         case OP_FCALL_MEMBASE:
2181                 return OP_FCALL;
2182         case OP_RCALL_MEMBASE:
2183                 return OP_RCALL;
2184         case OP_VCALL_MEMBASE:
2185                 return OP_VCALL;
2186         case OP_LCALL_MEMBASE:
2187                 return OP_LCALL;
2188         default:
2189                 g_assert_not_reached ();
2190         }
2191
2192         return -1;
2193 }
2194
2195 static int
2196 callvirt_to_call_reg (int opcode)
2197 {
2198         switch (opcode) {
2199         case OP_CALL_MEMBASE:
2200                 return OP_CALL_REG;
2201         case OP_VOIDCALL_MEMBASE:
2202                 return OP_VOIDCALL_REG;
2203         case OP_FCALL_MEMBASE:
2204                 return OP_FCALL_REG;
2205         case OP_RCALL_MEMBASE:
2206                 return OP_RCALL_REG;
2207         case OP_VCALL_MEMBASE:
2208                 return OP_VCALL_REG;
2209         case OP_LCALL_MEMBASE:
2210                 return OP_LCALL_REG;
2211         default:
2212                 g_assert_not_reached ();
2213         }
2214
2215         return -1;
2216 }
2217
2218 /* Either METHOD or IMT_ARG needs to be set */
2219 static void
2220 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2221 {
2222         int method_reg;
2223
2224         if (COMPILE_LLVM (cfg)) {
2225                 if (imt_arg) {
2226                         method_reg = alloc_preg (cfg);
2227                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2228                 } else {
2229                         MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2230                         method_reg = ins->dreg;
2231                 }
2232
2233 #ifdef ENABLE_LLVM
2234                 call->imt_arg_reg = method_reg;
2235 #endif
2236                 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2237                 return;
2238         }
2239
2240         if (imt_arg) {
2241                 method_reg = alloc_preg (cfg);
2242                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2243         } else {
2244                 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2245                 method_reg = ins->dreg;
2246         }
2247
2248         mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2249 }
2250
2251 static MonoJumpInfo *
2252 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2253 {
2254         MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2255
2256         ji->ip.i = ip;
2257         ji->type = type;
2258         ji->data.target = target;
2259
2260         return ji;
2261 }
2262
2263 static int
2264 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2265 {
2266         if (cfg->gshared)
2267                 return mono_class_check_context_used (klass);
2268         else
2269                 return 0;
2270 }
2271
2272 static int
2273 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2274 {
2275         if (cfg->gshared)
2276                 return mono_method_check_context_used (method);
2277         else
2278                 return 0;
2279 }
2280
2281 /*
2282  * check_method_sharing:
2283  *
2284  *   Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2285  */
2286 static void
2287 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2288 {
2289         gboolean pass_vtable = FALSE;
2290         gboolean pass_mrgctx = FALSE;
2291
2292         if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2293                 (mono_class_is_ginst (cmethod->klass) || mono_class_is_gtd (cmethod->klass))) {
2294                 gboolean sharable = FALSE;
2295
2296                 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2297                         sharable = TRUE;
2298
2299                 /*
2300                  * Pass vtable iff target method might
2301                  * be shared, which means that sharing
2302                  * is enabled for its class and its
2303                  * context is sharable (and it's not a
2304                  * generic method).
2305                  */
2306                 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2307                         pass_vtable = TRUE;
2308         }
2309
2310         if (mini_method_get_context (cmethod) &&
2311                 mini_method_get_context (cmethod)->method_inst) {
2312                 g_assert (!pass_vtable);
2313
2314                 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2315                         pass_mrgctx = TRUE;
2316                 } else {
2317                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2318                                 pass_mrgctx = TRUE;
2319                 }
2320         }
2321
2322         if (out_pass_vtable)
2323                 *out_pass_vtable = pass_vtable;
2324         if (out_pass_mrgctx)
2325                 *out_pass_mrgctx = pass_mrgctx;
2326 }
2327
2328 inline static MonoCallInst *
2329 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig, 
2330                                          MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2331 {
2332         MonoType *sig_ret;
2333         MonoCallInst *call;
2334 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2335         int i;
2336 #endif
2337
2338         if (cfg->llvm_only)
2339                 tail = FALSE;
2340
2341         if (tail) {
2342                 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2343
2344                 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2345         } else
2346                 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2347
2348         call->args = args;
2349         call->signature = sig;
2350         call->rgctx_reg = rgctx;
2351         sig_ret = mini_get_underlying_type (sig->ret);
2352
2353         type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2354
2355         if (tail) {
2356                 if (mini_type_is_vtype (sig_ret)) {
2357                         call->vret_var = cfg->vret_addr;
2358                         //g_assert_not_reached ();
2359                 }
2360         } else if (mini_type_is_vtype (sig_ret)) {
2361                 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2362                 MonoInst *loada;
2363
2364                 temp->backend.is_pinvoke = sig->pinvoke;
2365
2366                 /*
2367                  * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2368                  * address of return value to increase optimization opportunities.
2369                  * Before vtype decomposition, the dreg of the call ins itself represents the
2370                  * fact the call modifies the return value. After decomposition, the call will
2371                  * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2372                  * will be transformed into an LDADDR.
2373                  */
2374                 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2375                 loada->dreg = alloc_preg (cfg);
2376                 loada->inst_p0 = temp;
2377                 /* We reference the call too since call->dreg could change during optimization */
2378                 loada->inst_p1 = call;
2379                 MONO_ADD_INS (cfg->cbb, loada);
2380
2381                 call->inst.dreg = temp->dreg;
2382
2383                 call->vret_var = loada;
2384         } else if (!MONO_TYPE_IS_VOID (sig_ret))
2385                 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2386
2387 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2388         if (COMPILE_SOFT_FLOAT (cfg)) {
2389                 /* 
2390                  * If the call has a float argument, we would need to do an r8->r4 conversion using 
2391                  * an icall, but that cannot be done during the call sequence since it would clobber
2392                  * the call registers + the stack. So we do it before emitting the call.
2393                  */
2394                 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2395                         MonoType *t;
2396                         MonoInst *in = call->args [i];
2397
2398                         if (i >= sig->hasthis)
2399                                 t = sig->params [i - sig->hasthis];
2400                         else
2401                                 t = &mono_defaults.int_class->byval_arg;
2402                         t = mono_type_get_underlying_type (t);
2403
2404                         if (!t->byref && t->type == MONO_TYPE_R4) {
2405                                 MonoInst *iargs [1];
2406                                 MonoInst *conv;
2407
2408                                 iargs [0] = in;
2409                                 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2410
2411                                 /* The result will be in an int vreg */
2412                                 call->args [i] = conv;
2413                         }
2414                 }
2415         }
2416 #endif
2417
2418         call->need_unbox_trampoline = unbox_trampoline;
2419
2420 #ifdef ENABLE_LLVM
2421         if (COMPILE_LLVM (cfg))
2422                 mono_llvm_emit_call (cfg, call);
2423         else
2424                 mono_arch_emit_call (cfg, call);
2425 #else
2426         mono_arch_emit_call (cfg, call);
2427 #endif
2428
2429         cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2430         cfg->flags |= MONO_CFG_HAS_CALLS;
2431         
2432         return call;
2433 }
2434
2435 static void
2436 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2437 {
2438         mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2439         cfg->uses_rgctx_reg = TRUE;
2440         call->rgctx_reg = TRUE;
2441 #ifdef ENABLE_LLVM
2442         call->rgctx_arg_reg = rgctx_reg;
2443 #endif
2444 }       
2445
2446 inline static MonoInst*
2447 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2448 {
2449         MonoCallInst *call;
2450         MonoInst *ins;
2451         int rgctx_reg = -1;
2452         gboolean check_sp = FALSE;
2453
2454         if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2455                 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2456
2457                 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2458                         check_sp = TRUE;
2459         }
2460
2461         if (rgctx_arg) {
2462                 rgctx_reg = mono_alloc_preg (cfg);
2463                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2464         }
2465
2466         if (check_sp) {
2467                 if (!cfg->stack_inbalance_var)
2468                         cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2469
2470                 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2471                 ins->dreg = cfg->stack_inbalance_var->dreg;
2472                 MONO_ADD_INS (cfg->cbb, ins);
2473         }
2474
2475         call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2476
2477         call->inst.sreg1 = addr->dreg;
2478
2479         if (imt_arg)
2480                 emit_imt_argument (cfg, call, NULL, imt_arg);
2481
2482         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2483
2484         if (check_sp) {
2485                 int sp_reg;
2486
2487                 sp_reg = mono_alloc_preg (cfg);
2488
2489                 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2490                 ins->dreg = sp_reg;
2491                 MONO_ADD_INS (cfg->cbb, ins);
2492
2493                 /* Restore the stack so we don't crash when throwing the exception */
2494                 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2495                 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2496                 MONO_ADD_INS (cfg->cbb, ins);
2497
2498                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2499                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2500         }
2501
2502         if (rgctx_arg)
2503                 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2504
2505         return (MonoInst*)call;
2506 }
2507
2508 static MonoInst*
2509 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2510
2511 static MonoInst*
2512 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2513
2514 static MonoInst*
2515 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2516                                                         MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2517 {
2518 #ifndef DISABLE_REMOTING
2519         gboolean might_be_remote = FALSE;
2520 #endif
2521         gboolean virtual_ = this_ins != NULL;
2522         gboolean enable_for_aot = TRUE;
2523         int context_used;
2524         MonoCallInst *call;
2525         MonoInst *call_target = NULL;
2526         int rgctx_reg = 0;
2527         gboolean need_unbox_trampoline;
2528
2529         if (!sig)
2530                 sig = mono_method_signature (method);
2531
2532         if (cfg->llvm_only && (mono_class_is_interface (method->klass)))
2533                 g_assert_not_reached ();
2534
2535         if (rgctx_arg) {
2536                 rgctx_reg = mono_alloc_preg (cfg);
2537                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2538         }
2539
2540         if (method->string_ctor) {
2541                 /* Create the real signature */
2542                 /* FIXME: Cache these */
2543                 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2544                 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2545
2546                 sig = ctor_sig;
2547         }
2548
2549         context_used = mini_method_check_context_used (cfg, method);
2550
2551 #ifndef DISABLE_REMOTING
2552         might_be_remote = this_ins && sig->hasthis &&
2553                 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2554                 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2555
2556         if (might_be_remote && context_used) {
2557                 MonoInst *addr;
2558
2559                 g_assert (cfg->gshared);
2560
2561                 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2562
2563                 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2564         }
2565 #endif
2566
2567         if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2568                 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2569
2570         need_unbox_trampoline = method->klass == mono_defaults.object_class || mono_class_is_interface (method->klass);
2571
2572         call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2573
2574 #ifndef DISABLE_REMOTING
2575         if (might_be_remote)
2576                 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2577         else
2578 #endif
2579                 call->method = method;
2580         call->inst.flags |= MONO_INST_HAS_METHOD;
2581         call->inst.inst_left = this_ins;
2582         call->tail_call = tail;
2583
2584         if (virtual_) {
2585                 int vtable_reg, slot_reg, this_reg;
2586                 int offset;
2587
2588                 this_reg = this_ins->dreg;
2589
2590                 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2591                         MonoInst *dummy_use;
2592
2593                         MONO_EMIT_NULL_CHECK (cfg, this_reg);
2594
2595                         /* Make a call to delegate->invoke_impl */
2596                         call->inst.inst_basereg = this_reg;
2597                         call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2598                         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2599
2600                         /* We must emit a dummy use here because the delegate trampoline will
2601                         replace the 'this' argument with the delegate target making this activation
2602                         no longer a root for the delegate.
2603                         This is an issue for delegates that target collectible code such as dynamic
2604                         methods of GC'able assemblies.
2605
2606                         For a test case look into #667921.
2607
2608                         FIXME: a dummy use is not the best way to do it as the local register allocator
2609                         will put it on a caller save register and spil it around the call. 
2610                         Ideally, we would either put it on a callee save register or only do the store part.  
2611                          */
2612                         EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2613
2614                         return (MonoInst*)call;
2615                 }
2616
2617                 if ((!cfg->compile_aot || enable_for_aot) && 
2618                         (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) || 
2619                          (MONO_METHOD_IS_FINAL (method) &&
2620                           method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2621                         !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2622                         /* 
2623                          * the method is not virtual, we just need to ensure this is not null
2624                          * and then we can call the method directly.
2625                          */
2626 #ifndef DISABLE_REMOTING
2627                         if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2628                                 /* 
2629                                  * The check above ensures method is not gshared, this is needed since
2630                                  * gshared methods can't have wrappers.
2631                                  */
2632                                 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2633                         }
2634 #endif
2635
2636                         if (!method->string_ctor)
2637                                 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2638
2639                         call->inst.opcode = callvirt_to_call (call->inst.opcode);
2640                 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2641                         /*
2642                          * the method is virtual, but we can statically dispatch since either
2643                          * it's class or the method itself are sealed.
2644                          * But first we need to ensure it's not a null reference.
2645                          */
2646                         MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2647
2648                         call->inst.opcode = callvirt_to_call (call->inst.opcode);
2649                 } else if (call_target) {
2650                         vtable_reg = alloc_preg (cfg);
2651                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2652
2653                         call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2654                         call->inst.sreg1 = call_target->dreg;
2655                         call->inst.flags &= !MONO_INST_HAS_METHOD;
2656                 } else {
2657                         vtable_reg = alloc_preg (cfg);
2658                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2659                         if (mono_class_is_interface (method->klass)) {
2660                                 guint32 imt_slot = mono_method_get_imt_slot (method);
2661                                 emit_imt_argument (cfg, call, call->method, imt_arg);
2662                                 slot_reg = vtable_reg;
2663                                 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2664                         } else {
2665                                 slot_reg = vtable_reg;
2666                                 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2667                                         ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2668                                 if (imt_arg) {
2669                                         g_assert (mono_method_signature (method)->generic_param_count);
2670                                         emit_imt_argument (cfg, call, call->method, imt_arg);
2671                                 }
2672                         }
2673
2674                         call->inst.sreg1 = slot_reg;
2675                         call->inst.inst_offset = offset;
2676                         call->is_virtual = TRUE;
2677                 }
2678         }
2679
2680         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2681
2682         if (rgctx_arg)
2683                 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2684
2685         return (MonoInst*)call;
2686 }
2687
2688 MonoInst*
2689 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2690 {
2691         return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2692 }
2693
2694 MonoInst*
2695 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2696                                            MonoInst **args)
2697 {
2698         MonoCallInst *call;
2699
2700         g_assert (sig);
2701
2702         call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2703         call->fptr = func;
2704
2705         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2706
2707         return (MonoInst*)call;
2708 }
2709
2710 MonoInst*
2711 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2712 {
2713         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2714
2715         g_assert (info);
2716
2717         return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2718 }
2719
2720 /*
2721  * mono_emit_abs_call:
2722  *
2723  *   Emit a call to the runtime function described by PATCH_TYPE and DATA.
2724  */
2725 inline static MonoInst*
2726 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data, 
2727                                         MonoMethodSignature *sig, MonoInst **args)
2728 {
2729         MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2730         MonoInst *ins;
2731
2732         /* 
2733          * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2734          * handle it.
2735          */
2736         if (cfg->abs_patches == NULL)
2737                 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2738         g_hash_table_insert (cfg->abs_patches, ji, ji);
2739         ins = mono_emit_native_call (cfg, ji, sig, args);
2740         ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2741         return ins;
2742 }
2743
2744 static MonoMethodSignature*
2745 sig_to_rgctx_sig (MonoMethodSignature *sig)
2746 {
2747         // FIXME: memory allocation
2748         MonoMethodSignature *res;
2749         int i;
2750
2751         res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
2752         memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
2753         res->param_count = sig->param_count + 1;
2754         for (i = 0; i < sig->param_count; ++i)
2755                 res->params [i] = sig->params [i];
2756         res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
2757         return res;
2758 }
2759
2760 /* Make an indirect call to FSIG passing an additional argument */
2761 static MonoInst*
2762 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
2763 {
2764         MonoMethodSignature *csig;
2765         MonoInst *args_buf [16];
2766         MonoInst **args;
2767         int i, pindex, tmp_reg;
2768
2769         /* Make a call with an rgctx/extra arg */
2770         if (fsig->param_count + 2 < 16)
2771                 args = args_buf;
2772         else
2773                 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
2774         pindex = 0;
2775         if (fsig->hasthis)
2776                 args [pindex ++] = orig_args [0];
2777         for (i = 0; i < fsig->param_count; ++i)
2778                 args [pindex ++] = orig_args [fsig->hasthis + i];
2779         tmp_reg = alloc_preg (cfg);
2780         EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
2781         csig = sig_to_rgctx_sig (fsig);
2782         return mono_emit_calli (cfg, csig, args, call_target, NULL, NULL);
2783 }
2784
2785 /* Emit an indirect call to the function descriptor ADDR */
2786 static MonoInst*
2787 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
2788 {
2789         int addr_reg, arg_reg;
2790         MonoInst *call_target;
2791
2792         g_assert (cfg->llvm_only);
2793
2794         /*
2795          * addr points to a <addr, arg> pair, load both of them, and
2796          * make a call to addr, passing arg as an extra arg.
2797          */
2798         addr_reg = alloc_preg (cfg);
2799         EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
2800         arg_reg = alloc_preg (cfg);
2801         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
2802
2803         return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
2804 }
2805
2806 static gboolean
2807 direct_icalls_enabled (MonoCompile *cfg)
2808 {
2809         /* LLVM on amd64 can't handle calls to non-32 bit addresses */
2810 #ifdef TARGET_AMD64
2811         if (cfg->compile_llvm && !cfg->llvm_only)
2812                 return FALSE;
2813 #endif
2814         if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
2815                 return FALSE;
2816         return TRUE;
2817 }
2818
2819 MonoInst*
2820 mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args)
2821 {
2822         /*
2823          * Call the jit icall without a wrapper if possible.
2824          * The wrapper is needed for the following reasons:
2825          * - to handle exceptions thrown using mono_raise_exceptions () from the
2826          *   icall function. The EH code needs the lmf frame pushed by the
2827          *   wrapper to be able to unwind back to managed code.
2828          * - to be able to do stack walks for asynchronously suspended
2829          *   threads when debugging.
2830          */
2831         if (info->no_raise && direct_icalls_enabled (cfg)) {
2832                 char *name;
2833                 int costs;
2834
2835                 if (!info->wrapper_method) {
2836                         name = g_strdup_printf ("__icall_wrapper_%s", info->name);
2837                         info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
2838                         g_free (name);
2839                         mono_memory_barrier ();
2840                 }
2841
2842                 /*
2843                  * Inline the wrapper method, which is basically a call to the C icall, and
2844                  * an exception check.
2845                  */
2846                 costs = inline_method (cfg, info->wrapper_method, NULL,
2847                                                            args, NULL, il_offset, TRUE);
2848                 g_assert (costs > 0);
2849                 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
2850
2851                 return args [0];
2852         } else {
2853                 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2854         }
2855 }
2856  
2857 static MonoInst*
2858 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
2859 {
2860         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2861                 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
2862                         int widen_op = -1;
2863
2864                         /* 
2865                          * Native code might return non register sized integers 
2866                          * without initializing the upper bits.
2867                          */
2868                         switch (mono_type_to_load_membase (cfg, fsig->ret)) {
2869                         case OP_LOADI1_MEMBASE:
2870                                 widen_op = OP_ICONV_TO_I1;
2871                                 break;
2872                         case OP_LOADU1_MEMBASE:
2873                                 widen_op = OP_ICONV_TO_U1;
2874                                 break;
2875                         case OP_LOADI2_MEMBASE:
2876                                 widen_op = OP_ICONV_TO_I2;
2877                                 break;
2878                         case OP_LOADU2_MEMBASE:
2879                                 widen_op = OP_ICONV_TO_U2;
2880                                 break;
2881                         default:
2882                                 break;
2883                         }
2884
2885                         if (widen_op != -1) {
2886                                 int dreg = alloc_preg (cfg);
2887                                 MonoInst *widen;
2888
2889                                 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
2890                                 widen->type = ins->type;
2891                                 ins = widen;
2892                         }
2893                 }
2894         }
2895
2896         return ins;
2897 }
2898
2899
2900 static void
2901 emit_method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
2902 {
2903         MonoInst *args [16];
2904
2905         args [0] = emit_get_rgctx_method (cfg, mono_method_check_context_used (method), method, MONO_RGCTX_INFO_METHOD);
2906         args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cil_method), cil_method, MONO_RGCTX_INFO_METHOD);
2907
2908         mono_emit_jit_icall (cfg, mono_throw_method_access, args);
2909 }
2910
2911 static MonoMethod*
2912 get_memcpy_method (void)
2913 {
2914         static MonoMethod *memcpy_method = NULL;
2915         if (!memcpy_method) {
2916                 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2917                 if (!memcpy_method)
2918                         g_error ("Old corlib found. Install a new one");
2919         }
2920         return memcpy_method;
2921 }
2922
2923 static void
2924 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
2925 {
2926         MonoClassField *field;
2927         gpointer iter = NULL;
2928
2929         while ((field = mono_class_get_fields (klass, &iter))) {
2930                 int foffset;
2931
2932                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
2933                         continue;
2934                 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
2935                 if (mini_type_is_reference (mono_field_get_type (field))) {
2936                         g_assert ((foffset % SIZEOF_VOID_P) == 0);
2937                         *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
2938                 } else {
2939                         MonoClass *field_class = mono_class_from_mono_type (field->type);
2940                         if (field_class->has_references)
2941                                 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
2942                 }
2943         }
2944 }
2945
2946 static void
2947 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
2948 {
2949         int card_table_shift_bits;
2950         gpointer card_table_mask;
2951         guint8 *card_table;
2952         MonoInst *dummy_use;
2953         int nursery_shift_bits;
2954         size_t nursery_size;
2955
2956         if (!cfg->gen_write_barriers)
2957                 return;
2958
2959         card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
2960
2961         mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
2962
2963         if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
2964                 MonoInst *wbarrier;
2965
2966                 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
2967                 wbarrier->sreg1 = ptr->dreg;
2968                 wbarrier->sreg2 = value->dreg;
2969                 MONO_ADD_INS (cfg->cbb, wbarrier);
2970         } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
2971                 int offset_reg = alloc_preg (cfg);
2972                 int card_reg;
2973                 MonoInst *ins;
2974
2975                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
2976                 if (card_table_mask)
2977                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
2978
2979                 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
2980                  * IMM's larger than 32bits.
2981                  */
2982                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
2983                 card_reg = ins->dreg;
2984
2985                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
2986                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
2987         } else {
2988                 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
2989                 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
2990         }
2991
2992         EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
2993 }
2994
2995 static gboolean
2996 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
2997 {
2998         int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
2999         unsigned need_wb = 0;
3000
3001         if (align == 0)
3002                 align = 4;
3003
3004         /*types with references can't have alignment smaller than sizeof(void*) */
3005         if (align < SIZEOF_VOID_P)
3006                 return FALSE;
3007
3008         /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3009         if (size > 32 * SIZEOF_VOID_P)
3010                 return FALSE;
3011
3012         create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3013
3014         /* We don't unroll more than 5 stores to avoid code bloat. */
3015         if (size > 5 * SIZEOF_VOID_P) {
3016                 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3017                 size += (SIZEOF_VOID_P - 1);
3018                 size &= ~(SIZEOF_VOID_P - 1);
3019
3020                 EMIT_NEW_ICONST (cfg, iargs [2], size);
3021                 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3022                 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3023                 return TRUE;
3024         }
3025
3026         destreg = iargs [0]->dreg;
3027         srcreg = iargs [1]->dreg;
3028         offset = 0;
3029
3030         dest_ptr_reg = alloc_preg (cfg);
3031         tmp_reg = alloc_preg (cfg);
3032
3033         /*tmp = dreg*/
3034         EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3035
3036         while (size >= SIZEOF_VOID_P) {
3037                 MonoInst *load_inst;
3038                 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3039                 load_inst->dreg = tmp_reg;
3040                 load_inst->inst_basereg = srcreg;
3041                 load_inst->inst_offset = offset;
3042                 MONO_ADD_INS (cfg->cbb, load_inst);
3043
3044                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3045
3046                 if (need_wb & 0x1)
3047                         emit_write_barrier (cfg, iargs [0], load_inst);
3048
3049                 offset += SIZEOF_VOID_P;
3050                 size -= SIZEOF_VOID_P;
3051                 need_wb >>= 1;
3052
3053                 /*tmp += sizeof (void*)*/
3054                 if (size >= SIZEOF_VOID_P) {
3055                         NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3056                         MONO_ADD_INS (cfg->cbb, iargs [0]);
3057                 }
3058         }
3059
3060         /* Those cannot be references since size < sizeof (void*) */
3061         while (size >= 4) {
3062                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3063                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3064                 offset += 4;
3065                 size -= 4;
3066         }
3067
3068         while (size >= 2) {
3069                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3070                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3071                 offset += 2;
3072                 size -= 2;
3073         }
3074
3075         while (size >= 1) {
3076                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3077                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3078                 offset += 1;
3079                 size -= 1;
3080         }
3081
3082         return TRUE;
3083 }
3084
3085 /*
3086  * Emit code to copy a valuetype of type @klass whose address is stored in
3087  * @src->dreg to memory whose address is stored at @dest->dreg.
3088  */
3089 void
3090 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3091 {
3092         MonoInst *iargs [4];
3093         int n;
3094         guint32 align = 0;
3095         MonoMethod *memcpy_method;
3096         MonoInst *size_ins = NULL;
3097         MonoInst *memcpy_ins = NULL;
3098
3099         g_assert (klass);
3100         if (cfg->gshared)
3101                 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3102
3103         /*
3104          * This check breaks with spilled vars... need to handle it during verification anyway.
3105          * g_assert (klass && klass == src->klass && klass == dest->klass);
3106          */
3107
3108         if (mini_is_gsharedvt_klass (klass)) {
3109                 g_assert (!native);
3110                 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3111                 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3112         }
3113
3114         if (native)
3115                 n = mono_class_native_size (klass, &align);
3116         else
3117                 n = mono_class_value_size (klass, &align);
3118
3119         /* if native is true there should be no references in the struct */
3120         if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3121                 /* Avoid barriers when storing to the stack */
3122                 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3123                           (dest->opcode == OP_LDADDR))) {
3124                         int context_used;
3125
3126                         iargs [0] = dest;
3127                         iargs [1] = src;
3128
3129                         context_used = mini_class_check_context_used (cfg, klass);
3130
3131                         /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3132                         if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3133                                 return;
3134                         } else if (context_used) {
3135                                 iargs [2] = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3136                         }  else {
3137                                 iargs [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
3138                                 if (!cfg->compile_aot)
3139                                         mono_class_compute_gc_descriptor (klass);
3140                         }
3141
3142                         if (size_ins)
3143                                 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3144                         else
3145                                 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3146                         return;
3147                 }
3148         }
3149
3150         if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3151                 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3152                 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3153         } else {
3154                 iargs [0] = dest;
3155                 iargs [1] = src;
3156                 if (size_ins)
3157                         iargs [2] = size_ins;
3158                 else
3159                         EMIT_NEW_ICONST (cfg, iargs [2], n);
3160                 
3161                 memcpy_method = get_memcpy_method ();
3162                 if (memcpy_ins)
3163                         mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3164                 else
3165                         mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3166         }
3167 }
3168
3169 static MonoMethod*
3170 get_memset_method (void)
3171 {
3172         static MonoMethod *memset_method = NULL;
3173         if (!memset_method) {
3174                 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3175                 if (!memset_method)
3176                         g_error ("Old corlib found. Install a new one");
3177         }
3178         return memset_method;
3179 }
3180
3181 void
3182 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3183 {
3184         MonoInst *iargs [3];
3185         int n;
3186         guint32 align;
3187         MonoMethod *memset_method;
3188         MonoInst *size_ins = NULL;
3189         MonoInst *bzero_ins = NULL;
3190         static MonoMethod *bzero_method;
3191
3192         /* FIXME: Optimize this for the case when dest is an LDADDR */
3193         mono_class_init (klass);
3194         if (mini_is_gsharedvt_klass (klass)) {
3195                 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3196                 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3197                 if (!bzero_method)
3198                         bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3199                 g_assert (bzero_method);
3200                 iargs [0] = dest;
3201                 iargs [1] = size_ins;
3202                 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3203                 return;
3204         }
3205
3206         klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3207
3208         n = mono_class_value_size (klass, &align);
3209
3210         if (n <= sizeof (gpointer) * 8) {
3211                 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3212         }
3213         else {
3214                 memset_method = get_memset_method ();
3215                 iargs [0] = dest;
3216                 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3217                 EMIT_NEW_ICONST (cfg, iargs [2], n);
3218                 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3219         }
3220 }
3221
3222 /*
3223  * emit_get_rgctx:
3224  *
3225  *   Emit IR to return either the this pointer for instance method,
3226  * or the mrgctx for static methods.
3227  */
3228 static MonoInst*
3229 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3230 {
3231         MonoInst *this_ins = NULL;
3232
3233         g_assert (cfg->gshared);
3234
3235         if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3236                         !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3237                 !method->klass->valuetype)
3238                 EMIT_NEW_VARLOAD (cfg, this_ins, cfg->this_arg, &mono_defaults.object_class->byval_arg);
3239
3240         if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3241                 MonoInst *mrgctx_loc, *mrgctx_var;
3242
3243                 g_assert (!this_ins);
3244                 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3245
3246                 mrgctx_loc = mono_get_vtable_var (cfg);
3247                 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3248
3249                 return mrgctx_var;
3250         } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3251                 MonoInst *vtable_loc, *vtable_var;
3252
3253                 g_assert (!this_ins);
3254
3255                 vtable_loc = mono_get_vtable_var (cfg);
3256                 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3257
3258                 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3259                         MonoInst *mrgctx_var = vtable_var;
3260                         int vtable_reg;
3261
3262                         vtable_reg = alloc_preg (cfg);
3263                         EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3264                         vtable_var->type = STACK_PTR;
3265                 }
3266
3267                 return vtable_var;
3268         } else {
3269                 MonoInst *ins;
3270                 int vtable_reg;
3271         
3272                 vtable_reg = alloc_preg (cfg);
3273                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3274                 return ins;
3275         }
3276 }
3277
3278 static MonoJumpInfoRgctxEntry *
3279 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3280 {
3281         MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3282         res->method = method;
3283         res->in_mrgctx = in_mrgctx;
3284         res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3285         res->data->type = patch_type;
3286         res->data->data.target = patch_data;
3287         res->info_type = info_type;
3288
3289         return res;
3290 }
3291
3292 static inline MonoInst*
3293 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3294 {
3295         MonoInst *args [16];
3296         MonoInst *call;
3297
3298         // FIXME: No fastpath since the slot is not a compile time constant
3299         args [0] = rgctx;
3300         EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3301         if (entry->in_mrgctx)
3302                 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3303         else
3304                 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3305         return call;
3306 #if 0
3307         /*
3308          * FIXME: This can be called during decompose, which is a problem since it creates
3309          * new bblocks.
3310          * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3311          */
3312         int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3313         gboolean mrgctx;
3314         MonoBasicBlock *is_null_bb, *end_bb;
3315         MonoInst *res, *ins, *call;
3316         MonoInst *args[16];
3317
3318         slot = mini_get_rgctx_entry_slot (entry);
3319
3320         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3321         index = MONO_RGCTX_SLOT_INDEX (slot);
3322         if (mrgctx)
3323                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3324         for (depth = 0; ; ++depth) {
3325                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3326
3327                 if (index < size - 1)
3328                         break;
3329                 index -= size - 1;
3330         }
3331
3332         NEW_BBLOCK (cfg, end_bb);
3333         NEW_BBLOCK (cfg, is_null_bb);
3334
3335         if (mrgctx) {
3336                 rgctx_reg = rgctx->dreg;
3337         } else {
3338                 rgctx_reg = alloc_preg (cfg);
3339
3340                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3341                 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3342                 NEW_BBLOCK (cfg, is_null_bb);
3343
3344                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3345                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3346         }
3347
3348         for (i = 0; i < depth; ++i) {
3349                 int array_reg = alloc_preg (cfg);
3350
3351                 /* load ptr to next array */
3352                 if (mrgctx && i == 0)
3353                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3354                 else
3355                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3356                 rgctx_reg = array_reg;
3357                 /* is the ptr null? */
3358                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3359                 /* if yes, jump to actual trampoline */
3360                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3361         }
3362
3363         /* fetch slot */
3364         val_reg = alloc_preg (cfg);
3365         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3366         /* is the slot null? */
3367         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3368         /* if yes, jump to actual trampoline */
3369         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3370
3371         /* Fastpath */
3372         res_reg = alloc_preg (cfg);
3373         MONO_INST_NEW (cfg, ins, OP_MOVE);
3374         ins->dreg = res_reg;
3375         ins->sreg1 = val_reg;
3376         MONO_ADD_INS (cfg->cbb, ins);
3377         res = ins;
3378         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3379
3380         /* Slowpath */
3381         MONO_START_BB (cfg, is_null_bb);
3382         args [0] = rgctx;
3383         EMIT_NEW_ICONST (cfg, args [1], index);
3384         if (mrgctx)
3385                 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3386         else
3387                 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3388         MONO_INST_NEW (cfg, ins, OP_MOVE);
3389         ins->dreg = res_reg;
3390         ins->sreg1 = call->dreg;
3391         MONO_ADD_INS (cfg->cbb, ins);
3392         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3393
3394         MONO_START_BB (cfg, end_bb);
3395
3396         return res;
3397 #endif
3398 }
3399
3400 /*
3401  * emit_rgctx_fetch:
3402  *
3403  *   Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3404  * given by RGCTX.
3405  */
3406 static inline MonoInst*
3407 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3408 {
3409         if (cfg->llvm_only)
3410                 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3411         else
3412                 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3413 }
3414
3415 MonoInst*
3416 mini_emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3417                                           MonoClass *klass, MonoRgctxInfoType rgctx_type)
3418 {
3419         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);
3420         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3421
3422         return emit_rgctx_fetch (cfg, rgctx, entry);
3423 }
3424
3425 static MonoInst*
3426 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3427                                         MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3428 {
3429         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);
3430         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3431
3432         return emit_rgctx_fetch (cfg, rgctx, entry);
3433 }
3434
3435 static MonoInst*
3436 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3437                                                            MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3438 {
3439         MonoJumpInfoGSharedVtCall *call_info;
3440         MonoJumpInfoRgctxEntry *entry;
3441         MonoInst *rgctx;
3442
3443         call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3444         call_info->sig = sig;
3445         call_info->method = cmethod;
3446
3447         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);
3448         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3449
3450         return emit_rgctx_fetch (cfg, rgctx, entry);
3451 }
3452
3453 /*
3454  * emit_get_rgctx_virt_method:
3455  *
3456  *   Return data for method VIRT_METHOD for a receiver of type KLASS.
3457  */
3458 static MonoInst*
3459 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3460                                                         MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3461 {
3462         MonoJumpInfoVirtMethod *info;
3463         MonoJumpInfoRgctxEntry *entry;
3464         MonoInst *rgctx;
3465
3466         info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3467         info->klass = klass;
3468         info->method = virt_method;
3469
3470         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);
3471         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3472
3473         return emit_rgctx_fetch (cfg, rgctx, entry);
3474 }
3475
3476 static MonoInst*
3477 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3478                                                                  MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3479 {
3480         MonoJumpInfoRgctxEntry *entry;
3481         MonoInst *rgctx;
3482
3483         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);
3484         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3485
3486         return emit_rgctx_fetch (cfg, rgctx, entry);
3487 }
3488
3489 /*
3490  * emit_get_rgctx_method:
3491  *
3492  *   Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3493  * normal constants, else emit a load from the rgctx.
3494  */
3495 static MonoInst*
3496 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3497                                            MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3498 {
3499         if (!context_used) {
3500                 MonoInst *ins;
3501
3502                 switch (rgctx_type) {
3503                 case MONO_RGCTX_INFO_METHOD:
3504                         EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3505                         return ins;
3506                 case MONO_RGCTX_INFO_METHOD_RGCTX:
3507                         EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3508                         return ins;
3509                 default:
3510                         g_assert_not_reached ();
3511                 }
3512         } else {
3513                 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);
3514                 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3515
3516                 return emit_rgctx_fetch (cfg, rgctx, entry);
3517         }
3518 }
3519
3520 static MonoInst*
3521 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3522                                           MonoClassField *field, MonoRgctxInfoType rgctx_type)
3523 {
3524         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);
3525         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3526
3527         return emit_rgctx_fetch (cfg, rgctx, entry);
3528 }
3529
3530 static int
3531 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3532 {
3533         MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3534         MonoRuntimeGenericContextInfoTemplate *template_;
3535         int i, idx;
3536
3537         g_assert (info);
3538
3539         for (i = 0; i < info->num_entries; ++i) {
3540                 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3541
3542                 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3543                         return i;
3544         }
3545
3546         if (info->num_entries == info->count_entries) {
3547                 MonoRuntimeGenericContextInfoTemplate *new_entries;
3548                 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3549
3550                 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3551
3552                 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3553                 info->entries = new_entries;
3554                 info->count_entries = new_count_entries;
3555         }
3556
3557         idx = info->num_entries;
3558         template_ = &info->entries [idx];
3559         template_->info_type = rgctx_type;
3560         template_->data = data;
3561
3562         info->num_entries ++;
3563
3564         return idx;
3565 }
3566
3567 /*
3568  * emit_get_gsharedvt_info:
3569  *
3570  *   This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3571  */
3572 static MonoInst*
3573 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3574 {
3575         MonoInst *ins;
3576         int idx, dreg;
3577
3578         idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3579         /* Load info->entries [idx] */
3580         dreg = alloc_preg (cfg);
3581         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3582
3583         return ins;
3584 }
3585
3586 static MonoInst*
3587 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3588 {
3589         return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3590 }
3591
3592 /*
3593  * On return the caller must check @klass for load errors.
3594  */
3595 static void
3596 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3597 {
3598         MonoInst *vtable_arg;
3599         int context_used;
3600
3601         context_used = mini_class_check_context_used (cfg, klass);
3602
3603         if (context_used) {
3604                 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
3605                                                                                    klass, MONO_RGCTX_INFO_VTABLE);
3606         } else {
3607                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3608
3609                 if (!vtable)
3610                         return;
3611                 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3612         }
3613
3614         if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3615                 MonoInst *ins;
3616
3617                 /*
3618                  * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3619                  * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3620                  */
3621                 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3622                 ins->sreg1 = vtable_arg->dreg;
3623                 MONO_ADD_INS (cfg->cbb, ins);
3624         } else {
3625                 int inited_reg;
3626                 MonoBasicBlock *inited_bb;
3627                 MonoInst *args [16];
3628
3629                 inited_reg = alloc_ireg (cfg);
3630
3631                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, inited_reg, vtable_arg->dreg, MONO_STRUCT_OFFSET (MonoVTable, initialized));
3632
3633                 NEW_BBLOCK (cfg, inited_bb);
3634
3635                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3636                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3637
3638                 args [0] = vtable_arg;
3639                 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3640
3641                 MONO_START_BB (cfg, inited_bb);
3642         }
3643 }
3644
3645 static void
3646 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3647 {
3648         MonoInst *ins;
3649
3650         if (cfg->gen_seq_points && cfg->method == method) {
3651                 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3652                 if (nonempty_stack)
3653                         ins->flags |= MONO_INST_NONEMPTY_STACK;
3654                 MONO_ADD_INS (cfg->cbb, ins);
3655         }
3656 }
3657
3658 void
3659 mini_save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3660 {
3661         if (mini_get_debug_options ()->better_cast_details) {
3662                 int vtable_reg = alloc_preg (cfg);
3663                 int klass_reg = alloc_preg (cfg);
3664                 MonoBasicBlock *is_null_bb = NULL;
3665                 MonoInst *tls_get;
3666                 int to_klass_reg, context_used;
3667
3668                 if (null_check) {
3669                         NEW_BBLOCK (cfg, is_null_bb);
3670
3671                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3672                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3673                 }
3674
3675                 tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3676                 if (!tls_get) {
3677                         fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3678                         exit (1);
3679                 }
3680
3681                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3682                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3683
3684                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3685
3686                 context_used = mini_class_check_context_used (cfg, klass);
3687                 if (context_used) {
3688                         MonoInst *class_ins;
3689
3690                         class_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3691                         to_klass_reg = class_ins->dreg;
3692                 } else {
3693                         to_klass_reg = alloc_preg (cfg);
3694                         MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3695                 }
3696                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3697
3698                 if (null_check)
3699                         MONO_START_BB (cfg, is_null_bb);
3700         }
3701 }
3702
3703 void
3704 mini_reset_cast_details (MonoCompile *cfg)
3705 {
3706         /* Reset the variables holding the cast details */
3707         if (mini_get_debug_options ()->better_cast_details) {
3708                 MonoInst *tls_get = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
3709                 /* It is enough to reset the from field */
3710                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3711         }
3712 }
3713
3714 /*
3715  * On return the caller must check @array_class for load errors
3716  */
3717 static void
3718 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3719 {
3720         int vtable_reg = alloc_preg (cfg);
3721         int context_used;
3722
3723         context_used = mini_class_check_context_used (cfg, array_class);
3724
3725         mini_save_cast_details (cfg, array_class, obj->dreg, FALSE);
3726
3727         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3728
3729         if (cfg->opt & MONO_OPT_SHARED) {
3730                 int class_reg = alloc_preg (cfg);
3731                 MonoInst *ins;
3732
3733                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3734                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3735                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3736         } else if (context_used) {
3737                 MonoInst *vtable_ins;
3738
3739                 vtable_ins = mini_emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3740                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3741         } else {
3742                 if (cfg->compile_aot) {
3743                         int vt_reg;
3744                         MonoVTable *vtable;
3745
3746                         if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3747                                 return;
3748                         vt_reg = alloc_preg (cfg);
3749                         MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3750                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3751                 } else {
3752                         MonoVTable *vtable;
3753                         if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3754                                 return;
3755                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3756                 }
3757         }
3758         
3759         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
3760
3761         mini_reset_cast_details (cfg);
3762 }
3763
3764 /**
3765  * Handles unbox of a Nullable<T>. If context_used is non zero, then shared 
3766  * generic code is generated.
3767  */
3768 static MonoInst*
3769 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
3770 {
3771         MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
3772
3773         if (context_used) {
3774                 MonoInst *rgctx, *addr;
3775
3776                 /* FIXME: What if the class is shared?  We might not
3777                    have to get the address of the method from the
3778                    RGCTX. */
3779                 addr = emit_get_rgctx_method (cfg, context_used, method,
3780                                                                           MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
3781                 if (cfg->llvm_only) {
3782                         cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, mono_method_signature (method));
3783                         return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
3784                 } else {
3785                         rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
3786
3787                         return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
3788                 }
3789         } else {
3790                 gboolean pass_vtable, pass_mrgctx;
3791                 MonoInst *rgctx_arg = NULL;
3792
3793                 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
3794                 g_assert (!pass_mrgctx);
3795
3796                 if (pass_vtable) {
3797                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3798
3799                         g_assert (vtable);
3800                         EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
3801                 }
3802
3803                 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
3804         }
3805 }
3806
3807 static MonoInst*
3808 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
3809 {
3810         MonoInst *add;
3811         int obj_reg;
3812         int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
3813         int klass_reg = alloc_dreg (cfg ,STACK_PTR);
3814         int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
3815         int rank_reg = alloc_dreg (cfg ,STACK_I4);
3816
3817         obj_reg = sp [0]->dreg;
3818         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3819         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
3820
3821         /* FIXME: generics */
3822         g_assert (klass->rank == 0);
3823                         
3824         // Check rank == 0
3825         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
3826         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3827
3828         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3829         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
3830
3831         if (context_used) {
3832                 MonoInst *element_class;
3833
3834                 /* This assertion is from the unboxcast insn */
3835                 g_assert (klass->rank == 0);
3836
3837                 element_class = mini_emit_get_rgctx_klass (cfg, context_used,
3838                                 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
3839
3840                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
3841                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
3842         } else {
3843                 mini_save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
3844                 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
3845                 mini_reset_cast_details (cfg);
3846         }
3847
3848         NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
3849         MONO_ADD_INS (cfg->cbb, add);
3850         add->type = STACK_MP;
3851         add->klass = klass;
3852
3853         return add;
3854 }
3855
3856 static MonoInst*
3857 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
3858 {
3859         MonoInst *addr, *klass_inst, *is_ref, *args[16];
3860         MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
3861         MonoInst *ins;
3862         int dreg, addr_reg;
3863
3864         klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
3865
3866         /* obj */
3867         args [0] = obj;
3868
3869         /* klass */
3870         args [1] = klass_inst;
3871
3872         /* CASTCLASS */
3873         obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
3874
3875         NEW_BBLOCK (cfg, is_ref_bb);
3876         NEW_BBLOCK (cfg, is_nullable_bb);
3877         NEW_BBLOCK (cfg, end_bb);
3878         is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3879         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
3880         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
3881
3882         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
3883         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
3884
3885         /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
3886         addr_reg = alloc_dreg (cfg, STACK_MP);
3887
3888         /* Non-ref case */
3889         /* UNBOX */
3890         NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
3891         MONO_ADD_INS (cfg->cbb, addr);
3892
3893         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3894
3895         /* Ref case */
3896         MONO_START_BB (cfg, is_ref_bb);
3897
3898         /* Save the ref to a temporary */
3899         dreg = alloc_ireg (cfg);
3900         EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
3901         addr->dreg = addr_reg;
3902         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
3903         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3904
3905         /* Nullable case */
3906         MONO_START_BB (cfg, is_nullable_bb);
3907
3908         {
3909                 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
3910                 MonoInst *unbox_call;
3911                 MonoMethodSignature *unbox_sig;
3912
3913                 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
3914                 unbox_sig->ret = &klass->byval_arg;
3915                 unbox_sig->param_count = 1;
3916                 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
3917
3918                 if (cfg->llvm_only)
3919                         unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
3920                 else
3921                         unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
3922
3923                 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
3924                 addr->dreg = addr_reg;
3925         }
3926
3927         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3928
3929         /* End */
3930         MONO_START_BB (cfg, end_bb);
3931
3932         /* LDOBJ */
3933         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
3934
3935         return ins;
3936 }
3937
3938 /*
3939  * Returns NULL and set the cfg exception on error.
3940  */
3941 static MonoInst*
3942 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
3943 {
3944         MonoInst *iargs [2];
3945         void *alloc_ftn;
3946
3947         if (context_used) {
3948                 MonoInst *data;
3949                 MonoRgctxInfoType rgctx_info;
3950                 MonoInst *iargs [2];
3951                 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
3952
3953                 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
3954
3955                 if (cfg->opt & MONO_OPT_SHARED)
3956                         rgctx_info = MONO_RGCTX_INFO_KLASS;
3957                 else
3958                         rgctx_info = MONO_RGCTX_INFO_VTABLE;
3959                 data = mini_emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
3960
3961                 if (cfg->opt & MONO_OPT_SHARED) {
3962                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
3963                         iargs [1] = data;
3964                         alloc_ftn = ves_icall_object_new;
3965                 } else {
3966                         iargs [0] = data;
3967                         alloc_ftn = ves_icall_object_new_specific;
3968                 }
3969
3970                 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
3971                         if (known_instance_size) {
3972                                 int size = mono_class_instance_size (klass);
3973                                 if (size < sizeof (MonoObject))
3974                                         g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
3975
3976                                 EMIT_NEW_ICONST (cfg, iargs [1], size);
3977                         }
3978                         return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
3979                 }
3980
3981                 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
3982         }
3983
3984         if (cfg->opt & MONO_OPT_SHARED) {
3985                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
3986                 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
3987
3988                 alloc_ftn = ves_icall_object_new;
3989         } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !mono_class_is_ginst (klass)) {
3990                 /* This happens often in argument checking code, eg. throw new FooException... */
3991                 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
3992                 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
3993                 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
3994         } else {
3995                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3996                 MonoMethod *managed_alloc = NULL;
3997                 gboolean pass_lw;
3998
3999                 if (!vtable) {
4000                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4001                         cfg->exception_ptr = klass;
4002                         return NULL;
4003                 }
4004
4005                 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4006
4007                 if (managed_alloc) {
4008                         int size = mono_class_instance_size (klass);
4009                         if (size < sizeof (MonoObject))
4010                                 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4011
4012                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4013                         EMIT_NEW_ICONST (cfg, iargs [1], size);
4014                         return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4015                 }
4016                 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4017                 if (pass_lw) {
4018                         guint32 lw = vtable->klass->instance_size;
4019                         lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4020                         EMIT_NEW_ICONST (cfg, iargs [0], lw);
4021                         EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4022                 }
4023                 else {
4024                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4025                 }
4026         }
4027
4028         return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4029 }
4030         
4031 /*
4032  * Returns NULL and set the cfg exception on error.
4033  */     
4034 static MonoInst*
4035 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4036 {
4037         MonoInst *alloc, *ins;
4038
4039         if (mono_class_is_nullable (klass)) {
4040                 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4041
4042                 if (context_used) {
4043                         if (cfg->llvm_only && cfg->gsharedvt) {
4044                                 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4045                                                                                                                 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4046                                 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4047                         } else {
4048                                 /* FIXME: What if the class is shared?  We might not
4049                                    have to get the method address from the RGCTX. */
4050                                 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4051                                                                                                                 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4052                                 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->method, context_used);
4053
4054                                 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4055                         }
4056                 } else {
4057                         gboolean pass_vtable, pass_mrgctx;
4058                         MonoInst *rgctx_arg = NULL;
4059
4060                         check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4061                         g_assert (!pass_mrgctx);
4062
4063                         if (pass_vtable) {
4064                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4065
4066                                 g_assert (vtable);
4067                                 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4068                         }
4069
4070                         return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4071                 }
4072         }
4073
4074         if (mini_is_gsharedvt_klass (klass)) {
4075                 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4076                 MonoInst *res, *is_ref, *src_var, *addr;
4077                 int dreg;
4078
4079                 dreg = alloc_ireg (cfg);
4080
4081                 NEW_BBLOCK (cfg, is_ref_bb);
4082                 NEW_BBLOCK (cfg, is_nullable_bb);
4083                 NEW_BBLOCK (cfg, end_bb);
4084                 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4085                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4086                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4087
4088                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4089                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4090
4091                 /* Non-ref case */
4092                 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4093                 if (!alloc)
4094                         return NULL;
4095                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4096                 ins->opcode = OP_STOREV_MEMBASE;
4097
4098                 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4099                 res->type = STACK_OBJ;
4100                 res->klass = klass;
4101                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4102                 
4103                 /* Ref case */
4104                 MONO_START_BB (cfg, is_ref_bb);
4105
4106                 /* val is a vtype, so has to load the value manually */
4107                 src_var = get_vreg_to_inst (cfg, val->dreg);
4108                 if (!src_var)
4109                         src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4110                 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4111                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4112                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4113
4114                 /* Nullable case */
4115                 MONO_START_BB (cfg, is_nullable_bb);
4116
4117                 {
4118                         MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4119                                                                                                         MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4120                         MonoInst *box_call;
4121                         MonoMethodSignature *box_sig;
4122
4123                         /*
4124                          * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4125                          * construct that method at JIT time, so have to do things by hand.
4126                          */
4127                         box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4128                         box_sig->ret = &mono_defaults.object_class->byval_arg;
4129                         box_sig->param_count = 1;
4130                         box_sig->params [0] = &klass->byval_arg;
4131
4132                         if (cfg->llvm_only)
4133                                 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
4134                         else
4135                                 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4136                         EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4137                         res->type = STACK_OBJ;
4138                         res->klass = klass;
4139                 }
4140
4141                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4142
4143                 MONO_START_BB (cfg, end_bb);
4144
4145                 return res;
4146         } else {
4147                 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4148                 if (!alloc)
4149                         return NULL;
4150
4151                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4152                 return alloc;
4153         }
4154 }
4155
4156 static GHashTable* direct_icall_type_hash;
4157
4158 static gboolean
4159 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4160 {
4161         /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4162         if (!direct_icalls_enabled (cfg))
4163                 return FALSE;
4164
4165         /*
4166          * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4167          * Whitelist a few icalls for now.
4168          */
4169         if (!direct_icall_type_hash) {
4170                 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4171
4172                 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4173                 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4174                 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4175                 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4176                 mono_memory_barrier ();
4177                 direct_icall_type_hash = h;
4178         }
4179
4180         if (cmethod->klass == mono_defaults.math_class)
4181                 return TRUE;
4182         /* No locking needed */
4183         if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4184                 return TRUE;
4185         return FALSE;
4186 }
4187
4188 static gboolean
4189 method_needs_stack_walk (MonoCompile *cfg, MonoMethod *cmethod)
4190 {
4191         if (cmethod->klass == mono_defaults.systemtype_class) {
4192                 if (!strcmp (cmethod->name, "GetType"))
4193                         return TRUE;
4194         }
4195         return FALSE;
4196 }
4197
4198 static G_GNUC_UNUSED MonoInst*
4199 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4200 {
4201         MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4202         guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4203         gboolean is_i4;
4204
4205         switch (enum_type->type) {
4206         case MONO_TYPE_I8:
4207         case MONO_TYPE_U8:
4208 #if SIZEOF_REGISTER == 8
4209         case MONO_TYPE_I:
4210         case MONO_TYPE_U:
4211 #endif
4212                 is_i4 = FALSE;
4213                 break;
4214         default:
4215                 is_i4 = TRUE;
4216                 break;
4217         }
4218
4219         {
4220                 MonoInst *load, *and_, *cmp, *ceq;
4221                 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4222                 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4223                 int dest_reg = alloc_ireg (cfg);
4224
4225                 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
4226                 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
4227                 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
4228                 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
4229
4230                 ceq->type = STACK_I4;
4231
4232                 if (!is_i4) {
4233                         load = mono_decompose_opcode (cfg, load);
4234                         and_ = mono_decompose_opcode (cfg, and_);
4235                         cmp = mono_decompose_opcode (cfg, cmp);
4236                         ceq = mono_decompose_opcode (cfg, ceq);
4237                 }
4238
4239                 return ceq;
4240         }
4241 }
4242
4243 /*
4244  * Returns NULL and set the cfg exception on error.
4245  */
4246 static G_GNUC_UNUSED MonoInst*
4247 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
4248 {
4249         MonoInst *ptr;
4250         int dreg;
4251         gpointer trampoline;
4252         MonoInst *obj, *method_ins, *tramp_ins;
4253         MonoDomain *domain;
4254         guint8 **code_slot;
4255
4256         if (virtual_ && !cfg->llvm_only) {
4257                 MonoMethod *invoke = mono_get_delegate_invoke (klass);
4258                 g_assert (invoke);
4259
4260                 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
4261                         return NULL;
4262         }
4263
4264         obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
4265         if (!obj)
4266                 return NULL;
4267
4268         /* Inline the contents of mono_delegate_ctor */
4269
4270         /* Set target field */
4271         /* Optimize away setting of NULL target */
4272         if (!MONO_INS_IS_PCONST_NULL (target)) {
4273                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
4274                 if (cfg->gen_write_barriers) {
4275                         dreg = alloc_preg (cfg);
4276                         EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
4277                         emit_write_barrier (cfg, ptr, target);
4278                 }
4279         }
4280
4281         /* Set method field */
4282         method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4283         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
4284
4285         /* 
4286          * To avoid looking up the compiled code belonging to the target method
4287          * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
4288          * store it, and we fill it after the method has been compiled.
4289          */
4290         if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
4291                 MonoInst *code_slot_ins;
4292
4293                 if (context_used) {
4294                         code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
4295                 } else {
4296                         domain = mono_domain_get ();
4297                         mono_domain_lock (domain);
4298                         if (!domain_jit_info (domain)->method_code_hash)
4299                                 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
4300                         code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
4301                         if (!code_slot) {
4302                                 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
4303                                 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
4304                         }
4305                         mono_domain_unlock (domain);
4306
4307                         code_slot_ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
4308                 }
4309                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);                
4310         }
4311
4312         if (cfg->llvm_only) {
4313                 MonoInst *args [16];
4314
4315                 if (virtual_) {
4316                         args [0] = obj;
4317                         args [1] = target;
4318                         args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
4319                         mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
4320                 } else {
4321                         args [0] = obj;
4322                         mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
4323                 }
4324
4325                 return obj;
4326         }
4327
4328         if (cfg->compile_aot) {
4329                 MonoDelegateClassMethodPair *del_tramp;
4330
4331                 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
4332                 del_tramp->klass = klass;
4333                 del_tramp->method = context_used ? NULL : method;
4334                 del_tramp->is_virtual = virtual_;
4335                 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
4336         } else {
4337                 if (virtual_)
4338                         trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
4339                 else
4340                         trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
4341                 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
4342         }
4343
4344         /* Set invoke_impl field */
4345         if (virtual_) {
4346                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
4347         } else {
4348                 dreg = alloc_preg (cfg);
4349                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
4350                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
4351
4352                 dreg = alloc_preg (cfg);
4353                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
4354                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
4355         }
4356
4357         dreg = alloc_preg (cfg);
4358         MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
4359         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
4360
4361         /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
4362
4363         return obj;
4364 }
4365
4366 static MonoInst*
4367 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
4368 {
4369         MonoJitICallInfo *info;
4370
4371         /* Need to register the icall so it gets an icall wrapper */
4372         info = mono_get_array_new_va_icall (rank);
4373
4374         cfg->flags |= MONO_CFG_HAS_VARARGS;
4375
4376         /* mono_array_new_va () needs a vararg calling convention */
4377         cfg->exception_message = g_strdup ("array-new");
4378         cfg->disable_llvm = TRUE;
4379
4380         /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
4381         return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
4382 }
4383
4384 /*
4385  * handle_constrained_gsharedvt_call:
4386  *
4387  *   Handle constrained calls where the receiver is a gsharedvt type.
4388  * Return the instruction representing the call. Set the cfg exception on failure.
4389  */
4390 static MonoInst*
4391 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
4392                                                                    gboolean *ref_emit_widen)
4393 {
4394         MonoInst *ins = NULL;
4395         gboolean emit_widen = *ref_emit_widen;
4396
4397         /*
4398          * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
4399          * 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
4400          * pack the arguments into an array, and do the rest of the work in in an icall.
4401          */
4402         if (((cmethod->klass == mono_defaults.object_class) || mono_class_is_interface (cmethod->klass) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
4403                 (MONO_TYPE_IS_VOID (fsig->ret) || MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_IS_REFERENCE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mini_is_gsharedvt_type (fsig->ret)) &&
4404                 (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]))))) {
4405                 MonoInst *args [16];
4406
4407                 /*
4408                  * This case handles calls to
4409                  * - object:ToString()/Equals()/GetHashCode(),
4410                  * - System.IComparable<T>:CompareTo()
4411                  * - System.IEquatable<T>:Equals ()
4412                  * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
4413                  */
4414
4415                 args [0] = sp [0];
4416                 if (mono_method_check_context_used (cmethod))
4417                         args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
4418                 else
4419                         EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
4420                 args [2] = mini_emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
4421
4422                 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
4423                 if (fsig->hasthis && fsig->param_count) {
4424                         /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
4425                         MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
4426                         ins->dreg = alloc_preg (cfg);
4427                         ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
4428                         MONO_ADD_INS (cfg->cbb, ins);
4429                         args [4] = ins;
4430
4431                         if (mini_is_gsharedvt_type (fsig->params [0])) {
4432                                 int addr_reg, deref_arg_reg;
4433
4434                                 ins = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4435                                 deref_arg_reg = alloc_preg (cfg);
4436                                 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
4437                                 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
4438
4439                                 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
4440                                 addr_reg = ins->dreg;
4441                                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
4442                         } else {
4443                                 EMIT_NEW_ICONST (cfg, args [3], 0);
4444                                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
4445                         }
4446                 } else {
4447                         EMIT_NEW_ICONST (cfg, args [3], 0);
4448                         EMIT_NEW_ICONST (cfg, args [4], 0);
4449                 }
4450                 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
4451                 emit_widen = FALSE;
4452
4453                 if (mini_is_gsharedvt_type (fsig->ret)) {
4454                         ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
4455                 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret)) {
4456                         MonoInst *add;
4457
4458                         /* Unbox */
4459                         NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
4460                         MONO_ADD_INS (cfg->cbb, add);
4461                         /* Load value */
4462                         NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
4463                         MONO_ADD_INS (cfg->cbb, ins);
4464                         /* ins represents the call result */
4465                 }
4466         } else {
4467                 GSHAREDVT_FAILURE (CEE_CALLVIRT);
4468         }
4469
4470         *ref_emit_widen = emit_widen;
4471
4472         return ins;
4473
4474  exception_exit:
4475         return NULL;
4476 }
4477
4478 static void
4479 mono_emit_load_got_addr (MonoCompile *cfg)
4480 {
4481         MonoInst *getaddr, *dummy_use;
4482
4483         if (!cfg->got_var || cfg->got_var_allocated)
4484                 return;
4485
4486         MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
4487         getaddr->cil_code = cfg->header->code;
4488         getaddr->dreg = cfg->got_var->dreg;
4489
4490         /* Add it to the start of the first bblock */
4491         if (cfg->bb_entry->code) {
4492                 getaddr->next = cfg->bb_entry->code;
4493                 cfg->bb_entry->code = getaddr;
4494         }
4495         else
4496                 MONO_ADD_INS (cfg->bb_entry, getaddr);
4497
4498         cfg->got_var_allocated = TRUE;
4499
4500         /* 
4501          * Add a dummy use to keep the got_var alive, since real uses might
4502          * only be generated by the back ends.
4503          * Add it to end_bblock, so the variable's lifetime covers the whole
4504          * method.
4505          * It would be better to make the usage of the got var explicit in all
4506          * cases when the backend needs it (i.e. calls, throw etc.), so this
4507          * wouldn't be needed.
4508          */
4509         NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
4510         MONO_ADD_INS (cfg->bb_exit, dummy_use);
4511 }
4512
4513 static int inline_limit;
4514 static gboolean inline_limit_inited;
4515
4516 static gboolean
4517 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
4518 {
4519         MonoMethodHeaderSummary header;
4520         MonoVTable *vtable;
4521 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4522         MonoMethodSignature *sig = mono_method_signature (method);
4523         int i;
4524 #endif
4525
4526         if (cfg->disable_inline)
4527                 return FALSE;
4528         if (cfg->gsharedvt)
4529                 return FALSE;
4530
4531         if (cfg->inline_depth > 10)
4532                 return FALSE;
4533
4534         if (!mono_method_get_header_summary (method, &header))
4535                 return FALSE;
4536
4537         /*runtime, icall and pinvoke are checked by summary call*/
4538         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
4539             (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
4540             (mono_class_is_marshalbyref (method->klass)) ||
4541             header.has_clauses)
4542                 return FALSE;
4543
4544         /* also consider num_locals? */
4545         /* Do the size check early to avoid creating vtables */
4546         if (!inline_limit_inited) {
4547                 if (g_getenv ("MONO_INLINELIMIT"))
4548                         inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
4549                 else
4550                         inline_limit = INLINE_LENGTH_LIMIT;
4551                 inline_limit_inited = TRUE;
4552         }
4553         if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
4554                 return FALSE;
4555
4556         /*
4557          * if we can initialize the class of the method right away, we do,
4558          * otherwise we don't allow inlining if the class needs initialization,
4559          * since it would mean inserting a call to mono_runtime_class_init()
4560          * inside the inlined code
4561          */
4562         if (cfg->gshared && method->klass->has_cctor && mini_class_check_context_used (cfg, method->klass))
4563                 return FALSE;
4564
4565         if (!(cfg->opt & MONO_OPT_SHARED)) {
4566                 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
4567                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
4568                         if (method->klass->has_cctor) {
4569                                 vtable = mono_class_vtable (cfg->domain, method->klass);
4570                                 if (!vtable)
4571                                         return FALSE;
4572                                 if (!cfg->compile_aot) {
4573                                         MonoError error;
4574                                         if (!mono_runtime_class_init_full (vtable, &error)) {
4575                                                 mono_error_cleanup (&error);
4576                                                 return FALSE;
4577                                         }
4578                                 }
4579                         }
4580                 } else if (mono_class_is_before_field_init (method->klass)) {
4581                         if (cfg->run_cctors && method->klass->has_cctor) {
4582                                 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
4583                                 if (!method->klass->runtime_info)
4584                                         /* No vtable created yet */
4585                                         return FALSE;
4586                                 vtable = mono_class_vtable (cfg->domain, method->klass);
4587                                 if (!vtable)
4588                                         return FALSE;
4589                                 /* This makes so that inline cannot trigger */
4590                                 /* .cctors: too many apps depend on them */
4591                                 /* running with a specific order... */
4592                                 if (! vtable->initialized)
4593                                         return FALSE;
4594                                 MonoError error;
4595                                 if (!mono_runtime_class_init_full (vtable, &error)) {
4596                                         mono_error_cleanup (&error);
4597                                         return FALSE;
4598                                 }
4599                         }
4600                 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
4601                         if (!method->klass->runtime_info)
4602                                 /* No vtable created yet */
4603                                 return FALSE;
4604                         vtable = mono_class_vtable (cfg->domain, method->klass);
4605                         if (!vtable)
4606                                 return FALSE;
4607                         if (!vtable->initialized)
4608                                 return FALSE;
4609                 }
4610         } else {
4611                 /* 
4612                  * If we're compiling for shared code
4613                  * the cctor will need to be run at aot method load time, for example,
4614                  * or at the end of the compilation of the inlining method.
4615                  */
4616                 if (mono_class_needs_cctor_run (method->klass, NULL) && !mono_class_is_before_field_init (method->klass))
4617                         return FALSE;
4618         }
4619
4620 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
4621         if (mono_arch_is_soft_float ()) {
4622                 /* FIXME: */
4623                 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
4624                         return FALSE;
4625                 for (i = 0; i < sig->param_count; ++i)
4626                         if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
4627                                 return FALSE;
4628         }
4629 #endif
4630
4631         if (g_list_find (cfg->dont_inline, method))
4632                 return FALSE;
4633
4634         return TRUE;
4635 }
4636
4637 static gboolean
4638 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
4639 {
4640         if (!cfg->compile_aot) {
4641                 g_assert (vtable);
4642                 if (vtable->initialized)
4643                         return FALSE;
4644         }
4645
4646         if (mono_class_is_before_field_init (klass)) {
4647                 if (cfg->method == method)
4648                         return FALSE;
4649         }
4650
4651         if (!mono_class_needs_cctor_run (klass, method))
4652                 return FALSE;
4653
4654         if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
4655                 /* The initialization is already done before the method is called */
4656                 return FALSE;
4657
4658         return TRUE;
4659 }
4660
4661 MonoInst*
4662 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
4663 {
4664         MonoInst *ins;
4665         guint32 size;
4666         int mult_reg, add_reg, array_reg, index_reg, index2_reg;
4667         int context_used;
4668
4669         if (mini_is_gsharedvt_variable_klass (klass)) {
4670                 size = -1;
4671         } else {
4672                 mono_class_init (klass);
4673                 size = mono_class_array_element_size (klass);
4674         }
4675
4676         mult_reg = alloc_preg (cfg);
4677         array_reg = arr->dreg;
4678         index_reg = index->dreg;
4679
4680 #if SIZEOF_REGISTER == 8
4681         /* The array reg is 64 bits but the index reg is only 32 */
4682         if (COMPILE_LLVM (cfg)) {
4683                 /* Not needed */
4684                 index2_reg = index_reg;
4685         } else {
4686                 index2_reg = alloc_preg (cfg);
4687                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
4688         }
4689 #else
4690         if (index->type == STACK_I8) {
4691                 index2_reg = alloc_preg (cfg);
4692                 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
4693         } else {
4694                 index2_reg = index_reg;
4695         }
4696 #endif
4697
4698         if (bcheck)
4699                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
4700
4701 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4702         if (size == 1 || size == 2 || size == 4 || size == 8) {
4703                 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
4704
4705                 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
4706                 ins->klass = mono_class_get_element_class (klass);
4707                 ins->type = STACK_MP;
4708
4709                 return ins;
4710         }
4711 #endif          
4712
4713         add_reg = alloc_ireg_mp (cfg);
4714
4715         if (size == -1) {
4716                 MonoInst *rgctx_ins;
4717
4718                 /* gsharedvt */
4719                 g_assert (cfg->gshared);
4720                 context_used = mini_class_check_context_used (cfg, klass);
4721                 g_assert (context_used);
4722                 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
4723                 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
4724         } else {
4725                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
4726         }
4727         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
4728         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4729         ins->klass = mono_class_get_element_class (klass);
4730         ins->type = STACK_MP;
4731         MONO_ADD_INS (cfg->cbb, ins);
4732
4733         return ins;
4734 }
4735
4736 static MonoInst*
4737 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
4738 {
4739         int bounds_reg = alloc_preg (cfg);
4740         int add_reg = alloc_ireg_mp (cfg);
4741         int mult_reg = alloc_preg (cfg);
4742         int mult2_reg = alloc_preg (cfg);
4743         int low1_reg = alloc_preg (cfg);
4744         int low2_reg = alloc_preg (cfg);
4745         int high1_reg = alloc_preg (cfg);
4746         int high2_reg = alloc_preg (cfg);
4747         int realidx1_reg = alloc_preg (cfg);
4748         int realidx2_reg = alloc_preg (cfg);
4749         int sum_reg = alloc_preg (cfg);
4750         int index1, index2, tmpreg;
4751         MonoInst *ins;
4752         guint32 size;
4753
4754         mono_class_init (klass);
4755         size = mono_class_array_element_size (klass);
4756
4757         index1 = index_ins1->dreg;
4758         index2 = index_ins2->dreg;
4759
4760 #if SIZEOF_REGISTER == 8
4761         /* The array reg is 64 bits but the index reg is only 32 */
4762         if (COMPILE_LLVM (cfg)) {
4763                 /* Not needed */
4764         } else {
4765                 tmpreg = alloc_preg (cfg);
4766                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
4767                 index1 = tmpreg;
4768                 tmpreg = alloc_preg (cfg);
4769                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
4770                 index2 = tmpreg;
4771         }
4772 #else
4773         // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
4774         tmpreg = -1;
4775 #endif
4776
4777         /* range checking */
4778         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, 
4779                                        arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4780
4781         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg, 
4782                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4783         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
4784         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg, 
4785                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4786         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
4787         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4788
4789         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg, 
4790                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
4791         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
4792         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg, 
4793                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
4794         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
4795         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
4796
4797         MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
4798         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
4799         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
4800         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
4801         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
4802
4803         ins->type = STACK_MP;
4804         ins->klass = klass;
4805         MONO_ADD_INS (cfg->cbb, ins);
4806
4807         return ins;
4808 }
4809
4810 static MonoInst*
4811 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
4812 {
4813         int rank;
4814         MonoInst *addr;
4815         MonoMethod *addr_method;
4816         int element_size;
4817         MonoClass *eclass = cmethod->klass->element_class;
4818
4819         rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
4820
4821         if (rank == 1)
4822                 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
4823
4824         /* emit_ldelema_2 depends on OP_LMUL */
4825         if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
4826                 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
4827         }
4828
4829         if (mini_is_gsharedvt_variable_klass (eclass))
4830                 element_size = 0;
4831         else
4832                 element_size = mono_class_array_element_size (eclass);
4833         addr_method = mono_marshal_get_array_address (rank, element_size);
4834         addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
4835
4836         return addr;
4837 }
4838
4839 static MonoBreakPolicy
4840 always_insert_breakpoint (MonoMethod *method)
4841 {
4842         return MONO_BREAK_POLICY_ALWAYS;
4843 }
4844
4845 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
4846
4847 /**
4848  * mono_set_break_policy:
4849  * policy_callback: the new callback function
4850  *
4851  * Allow embedders to decide wherther to actually obey breakpoint instructions
4852  * (both break IL instructions and Debugger.Break () method calls), for example
4853  * to not allow an app to be aborted by a perfectly valid IL opcode when executing
4854  * untrusted or semi-trusted code.
4855  *
4856  * @policy_callback will be called every time a break point instruction needs to
4857  * be inserted with the method argument being the method that calls Debugger.Break()
4858  * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
4859  * if it wants the breakpoint to not be effective in the given method.
4860  * #MONO_BREAK_POLICY_ALWAYS is the default.
4861  */
4862 void
4863 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
4864 {
4865         if (policy_callback)
4866                 break_policy_func = policy_callback;
4867         else
4868                 break_policy_func = always_insert_breakpoint;
4869 }
4870
4871 static gboolean
4872 should_insert_brekpoint (MonoMethod *method) {
4873         switch (break_policy_func (method)) {
4874         case MONO_BREAK_POLICY_ALWAYS:
4875                 return TRUE;
4876         case MONO_BREAK_POLICY_NEVER:
4877                 return FALSE;
4878         case MONO_BREAK_POLICY_ON_DBG:
4879                 g_warning ("mdb no longer supported");
4880                 return FALSE;
4881         default:
4882                 g_warning ("Incorrect value returned from break policy callback");
4883                 return FALSE;
4884         }
4885 }
4886
4887 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
4888 static MonoInst*
4889 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4890 {
4891         MonoInst *addr, *store, *load;
4892         MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
4893
4894         /* the bounds check is already done by the callers */
4895         addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4896         if (is_set) {
4897                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
4898                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
4899                 if (mini_type_is_reference (&eklass->byval_arg))
4900                         emit_write_barrier (cfg, addr, load);
4901         } else {
4902                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
4903                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
4904         }
4905         return store;
4906 }
4907
4908
4909 static gboolean
4910 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4911 {
4912         return mini_type_is_reference (&klass->byval_arg);
4913 }
4914
4915 static MonoInst*
4916 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
4917 {
4918         if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
4919                 !(MONO_INS_IS_PCONST_NULL (sp [2]))) {
4920                 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
4921                 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
4922                 MonoInst *iargs [3];
4923
4924                 if (!helper->slot)
4925                         mono_class_setup_vtable (obj_array);
4926                 g_assert (helper->slot);
4927
4928                 if (sp [0]->type != STACK_OBJ)
4929                         return NULL;
4930                 if (sp [2]->type != STACK_OBJ)
4931                         return NULL;
4932
4933                 iargs [2] = sp [2];
4934                 iargs [1] = sp [1];
4935                 iargs [0] = sp [0];
4936
4937                 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
4938         } else {
4939                 MonoInst *ins;
4940
4941                 if (mini_is_gsharedvt_variable_klass (klass)) {
4942                         MonoInst *addr;
4943
4944                         // FIXME-VT: OP_ICONST optimization
4945                         addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
4946                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4947                         ins->opcode = OP_STOREV_MEMBASE;
4948                 } else if (sp [1]->opcode == OP_ICONST) {
4949                         int array_reg = sp [0]->dreg;
4950                         int index_reg = sp [1]->dreg;
4951                         int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
4952
4953                         if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
4954                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
4955
4956                         if (safety_checks)
4957                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
4958                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
4959                 } else {
4960                         MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
4961                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
4962                         if (generic_class_is_reference_type (cfg, klass))
4963                                 emit_write_barrier (cfg, addr, sp [2]);
4964                 }
4965                 return ins;
4966         }
4967 }
4968
4969 static MonoInst*
4970 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
4971 {
4972         MonoClass *eklass;
4973         
4974         if (is_set)
4975                 eklass = mono_class_from_mono_type (fsig->params [2]);
4976         else
4977                 eklass = mono_class_from_mono_type (fsig->ret);
4978
4979         if (is_set) {
4980                 return emit_array_store (cfg, eklass, args, FALSE);
4981         } else {
4982                 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
4983                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
4984                 return ins;
4985         }
4986 }
4987
4988 static gboolean
4989 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
4990 {
4991         uint32_t align;
4992         int param_size, return_size;
4993
4994         param_klass = mono_class_from_mono_type (mini_get_underlying_type (&param_klass->byval_arg));
4995         return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
4996
4997         if (cfg->verbose_level > 3)
4998                 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
4999
5000         //Don't allow mixing reference types with value types
5001         if (param_klass->valuetype != return_klass->valuetype) {
5002                 if (cfg->verbose_level > 3)
5003                         printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5004                 return FALSE;
5005         }
5006
5007         if (!param_klass->valuetype) {
5008                 if (cfg->verbose_level > 3)
5009                         printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5010                 return TRUE;
5011         }
5012
5013         //That are blitable
5014         if (param_klass->has_references || return_klass->has_references)
5015                 return FALSE;
5016
5017         /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5018         if ((MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5019                 (!MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5020                         if (cfg->verbose_level > 3)
5021                                 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5022                 return FALSE;
5023         }
5024
5025         if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5026                 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5027                 if (cfg->verbose_level > 3)
5028                         printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5029                 return FALSE;
5030         }
5031
5032         param_size = mono_class_value_size (param_klass, &align);
5033         return_size = mono_class_value_size (return_klass, &align);
5034
5035         //We can do it if sizes match
5036         if (param_size == return_size) {
5037                 if (cfg->verbose_level > 3)
5038                         printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5039                 return TRUE;
5040         }
5041
5042         //No simple way to handle struct if sizes don't match
5043         if (MONO_TYPE_ISSTRUCT (&param_klass->byval_arg)) {
5044                 if (cfg->verbose_level > 3)
5045                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5046                 return FALSE;
5047         }
5048
5049         /*
5050          * Same reg size category.
5051          * A quick note on why we don't require widening here.
5052          * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5053          *
5054          * Since the source value comes from a function argument, the JIT will already have
5055          * the value in a VREG and performed any widening needed before (say, when loading from a field).
5056          */
5057         if (param_size <= 4 && return_size <= 4) {
5058                 if (cfg->verbose_level > 3)
5059                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5060                 return TRUE;
5061         }
5062
5063         return FALSE;
5064 }
5065
5066 static MonoInst*
5067 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5068 {
5069         MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5070         MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5071
5072         if (mini_is_gsharedvt_variable_type (fsig->ret))
5073                 return NULL;
5074
5075         //Valuetypes that are semantically equivalent or numbers than can be widened to
5076         if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5077                 return args [0];
5078
5079         //Arrays of valuetypes that are semantically equivalent
5080         if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5081                 return args [0];
5082
5083         return NULL;
5084 }
5085
5086 static MonoInst*
5087 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5088 {
5089 #ifdef MONO_ARCH_SIMD_INTRINSICS
5090         MonoInst *ins = NULL;
5091
5092         if (cfg->opt & MONO_OPT_SIMD) {
5093                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5094                 if (ins)
5095                         return ins;
5096         }
5097 #endif
5098
5099         return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5100 }
5101
5102 static MonoInst*
5103 emit_memory_barrier (MonoCompile *cfg, int kind)
5104 {
5105         MonoInst *ins = NULL;
5106         MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5107         MONO_ADD_INS (cfg->cbb, ins);
5108         ins->backend.memory_barrier_kind = kind;
5109
5110         return ins;
5111 }
5112
5113 static MonoInst*
5114 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5115 {
5116         MonoInst *ins = NULL;
5117         int opcode = 0;
5118
5119         /* The LLVM backend supports these intrinsics */
5120         if (cmethod->klass == mono_defaults.math_class) {
5121                 if (strcmp (cmethod->name, "Sin") == 0) {
5122                         opcode = OP_SIN;
5123                 } else if (strcmp (cmethod->name, "Cos") == 0) {
5124                         opcode = OP_COS;
5125                 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5126                         opcode = OP_SQRT;
5127                 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5128                         opcode = OP_ABS;
5129                 }
5130
5131                 if (opcode && fsig->param_count == 1) {
5132                         MONO_INST_NEW (cfg, ins, opcode);
5133                         ins->type = STACK_R8;
5134                         ins->dreg = mono_alloc_dreg (cfg, ins->type);
5135                         ins->sreg1 = args [0]->dreg;
5136                         MONO_ADD_INS (cfg->cbb, ins);
5137                 }
5138
5139                 opcode = 0;
5140                 if (cfg->opt & MONO_OPT_CMOV) {
5141                         if (strcmp (cmethod->name, "Min") == 0) {
5142                                 if (fsig->params [0]->type == MONO_TYPE_I4)
5143                                         opcode = OP_IMIN;
5144                                 if (fsig->params [0]->type == MONO_TYPE_U4)
5145                                         opcode = OP_IMIN_UN;
5146                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
5147                                         opcode = OP_LMIN;
5148                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
5149                                         opcode = OP_LMIN_UN;
5150                         } else if (strcmp (cmethod->name, "Max") == 0) {
5151                                 if (fsig->params [0]->type == MONO_TYPE_I4)
5152                                         opcode = OP_IMAX;
5153                                 if (fsig->params [0]->type == MONO_TYPE_U4)
5154                                         opcode = OP_IMAX_UN;
5155                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
5156                                         opcode = OP_LMAX;
5157                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
5158                                         opcode = OP_LMAX_UN;
5159                         }
5160                 }
5161
5162                 if (opcode && fsig->param_count == 2) {
5163                         MONO_INST_NEW (cfg, ins, opcode);
5164                         ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5165                         ins->dreg = mono_alloc_dreg (cfg, ins->type);
5166                         ins->sreg1 = args [0]->dreg;
5167                         ins->sreg2 = args [1]->dreg;
5168                         MONO_ADD_INS (cfg->cbb, ins);
5169                 }
5170         }
5171
5172         return ins;
5173 }
5174
5175 static MonoInst*
5176 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5177 {
5178         if (cmethod->klass == mono_defaults.array_class) {
5179                 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5180                         return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5181                 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5182                         return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5183                 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5184                         return emit_array_unsafe_mov (cfg, fsig, args);
5185         }
5186
5187         return NULL;
5188 }
5189
5190 static MonoInst*
5191 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5192 {
5193         MonoInst *ins = NULL;
5194
5195          MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
5196
5197         if (cmethod->klass == mono_defaults.string_class) {
5198                 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5199                         int dreg = alloc_ireg (cfg);
5200                         int index_reg = alloc_preg (cfg);
5201                         int add_reg = alloc_preg (cfg);
5202
5203 #if SIZEOF_REGISTER == 8
5204                         if (COMPILE_LLVM (cfg)) {
5205                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
5206                         } else {
5207                                 /* The array reg is 64 bits but the index reg is only 32 */
5208                                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5209                         }
5210 #else
5211                         index_reg = args [1]->dreg;
5212 #endif  
5213                         MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5214
5215 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5216                         EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5217                         add_reg = ins->dreg;
5218                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
5219                                                                    add_reg, 0);
5220 #else
5221                         int mult_reg = alloc_preg (cfg);
5222                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5223                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5224                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
5225                                                                    add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5226 #endif
5227                         type_from_op (cfg, ins, NULL, NULL);
5228                         return ins;
5229                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5230                         int dreg = alloc_ireg (cfg);
5231                         /* Decompose later to allow more optimizations */
5232                         EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
5233                         ins->type = STACK_I4;
5234                         ins->flags |= MONO_INST_FAULT;
5235                         cfg->cbb->has_array_access = TRUE;
5236                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
5237
5238                         return ins;
5239                 } else 
5240                         return NULL;
5241         } else if (cmethod->klass == mono_defaults.object_class) {
5242                 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
5243                         int dreg = alloc_ireg_ref (cfg);
5244                         int vt_reg = alloc_preg (cfg);
5245                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5246                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
5247                         type_from_op (cfg, ins, NULL, NULL);
5248
5249                         return ins;
5250                 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
5251                         int dreg = alloc_ireg (cfg);
5252                         int t1 = alloc_ireg (cfg);
5253         
5254                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
5255                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
5256                         ins->type = STACK_I4;
5257
5258                         return ins;
5259                 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
5260                         MONO_INST_NEW (cfg, ins, OP_NOP);
5261                         MONO_ADD_INS (cfg->cbb, ins);
5262                         return ins;
5263                 } else
5264                         return NULL;
5265         } else if (cmethod->klass == mono_defaults.array_class) {
5266                 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
5267                         return emit_array_generic_access (cfg, fsig, args, FALSE);
5268                 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
5269                         return emit_array_generic_access (cfg, fsig, args, TRUE);
5270
5271 #ifndef MONO_BIG_ARRAYS
5272                 /*
5273                  * This is an inline version of GetLength/GetLowerBound(0) used frequently in
5274                  * Array methods.
5275                  */
5276                 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
5277                          (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
5278                          args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
5279                         int dreg = alloc_ireg (cfg);
5280                         int bounds_reg = alloc_ireg_mp (cfg);
5281                         MonoBasicBlock *end_bb, *szarray_bb;
5282                         gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
5283
5284                         NEW_BBLOCK (cfg, end_bb);
5285                         NEW_BBLOCK (cfg, szarray_bb);
5286
5287                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
5288                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5289                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
5290                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
5291                         /* Non-szarray case */
5292                         if (get_length)
5293                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5294                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5295                         else
5296                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5297                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5298                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
5299                         MONO_START_BB (cfg, szarray_bb);
5300                         /* Szarray case */
5301                         if (get_length)
5302                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
5303                                                                            args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5304                         else
5305                                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
5306                         MONO_START_BB (cfg, end_bb);
5307
5308                         EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
5309                         ins->type = STACK_I4;
5310                         
5311                         return ins;
5312                 }
5313 #endif
5314
5315                 if (cmethod->name [0] != 'g')
5316                         return NULL;
5317
5318                 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
5319                         int dreg = alloc_ireg (cfg);
5320                         int vtable_reg = alloc_preg (cfg);
5321                         MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg, 
5322                                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5323                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
5324                                                                    vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
5325                         type_from_op (cfg, ins, NULL, NULL);
5326
5327                         return ins;
5328                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5329                         int dreg = alloc_ireg (cfg);
5330
5331                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg, 
5332                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
5333                         type_from_op (cfg, ins, NULL, NULL);
5334
5335                         return ins;
5336                 } else
5337                         return NULL;
5338         } else if (cmethod->klass == runtime_helpers_class) {
5339                 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
5340                         EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
5341                         return ins;
5342                 } else
5343                         return NULL;
5344         } else if (cmethod->klass == mono_defaults.monitor_class) {
5345                 gboolean is_enter = FALSE;
5346                 gboolean is_v4 = FALSE;
5347
5348                 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 2 && fsig->params [1]->byref) {
5349                         is_enter = TRUE;
5350                         is_v4 = TRUE;
5351                 }
5352                 if (!strcmp (cmethod->name, "Enter") && fsig->param_count == 1)
5353                         is_enter = TRUE;
5354
5355                 if (is_enter) {
5356                         /*
5357                          * To make async stack traces work, icalls which can block should have a wrapper.
5358                          * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
5359                          */
5360                         MonoBasicBlock *end_bb;
5361
5362                         NEW_BBLOCK (cfg, end_bb);
5363
5364                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
5365                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
5366                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
5367                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_internal : (gpointer)mono_monitor_enter_internal, args);
5368                         MONO_START_BB (cfg, end_bb);
5369                         return ins;
5370                 }
5371         } else if (cmethod->klass == mono_defaults.thread_class) {
5372                 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
5373                         MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
5374                         MONO_ADD_INS (cfg->cbb, ins);
5375                         return ins;
5376                 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
5377                         return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5378                 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
5379                         guint32 opcode = 0;
5380                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5381
5382                         if (fsig->params [0]->type == MONO_TYPE_I1)
5383                                 opcode = OP_LOADI1_MEMBASE;
5384                         else if (fsig->params [0]->type == MONO_TYPE_U1)
5385                                 opcode = OP_LOADU1_MEMBASE;
5386                         else if (fsig->params [0]->type == MONO_TYPE_I2)
5387                                 opcode = OP_LOADI2_MEMBASE;
5388                         else if (fsig->params [0]->type == MONO_TYPE_U2)
5389                                 opcode = OP_LOADU2_MEMBASE;
5390                         else if (fsig->params [0]->type == MONO_TYPE_I4)
5391                                 opcode = OP_LOADI4_MEMBASE;
5392                         else if (fsig->params [0]->type == MONO_TYPE_U4)
5393                                 opcode = OP_LOADU4_MEMBASE;
5394                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5395                                 opcode = OP_LOADI8_MEMBASE;
5396                         else if (fsig->params [0]->type == MONO_TYPE_R4)
5397                                 opcode = OP_LOADR4_MEMBASE;
5398                         else if (fsig->params [0]->type == MONO_TYPE_R8)
5399                                 opcode = OP_LOADR8_MEMBASE;
5400                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5401                                 opcode = OP_LOAD_MEMBASE;
5402
5403                         if (opcode) {
5404                                 MONO_INST_NEW (cfg, ins, opcode);
5405                                 ins->inst_basereg = args [0]->dreg;
5406                                 ins->inst_offset = 0;
5407                                 MONO_ADD_INS (cfg->cbb, ins);
5408
5409                                 switch (fsig->params [0]->type) {
5410                                 case MONO_TYPE_I1:
5411                                 case MONO_TYPE_U1:
5412                                 case MONO_TYPE_I2:
5413                                 case MONO_TYPE_U2:
5414                                 case MONO_TYPE_I4:
5415                                 case MONO_TYPE_U4:
5416                                         ins->dreg = mono_alloc_ireg (cfg);
5417                                         ins->type = STACK_I4;
5418                                         break;
5419                                 case MONO_TYPE_I8:
5420                                 case MONO_TYPE_U8:
5421                                         ins->dreg = mono_alloc_lreg (cfg);
5422                                         ins->type = STACK_I8;
5423                                         break;
5424                                 case MONO_TYPE_I:
5425                                 case MONO_TYPE_U:
5426                                         ins->dreg = mono_alloc_ireg (cfg);
5427 #if SIZEOF_REGISTER == 8
5428                                         ins->type = STACK_I8;
5429 #else
5430                                         ins->type = STACK_I4;
5431 #endif
5432                                         break;
5433                                 case MONO_TYPE_R4:
5434                                 case MONO_TYPE_R8:
5435                                         ins->dreg = mono_alloc_freg (cfg);
5436                                         ins->type = STACK_R8;
5437                                         break;
5438                                 default:
5439                                         g_assert (mini_type_is_reference (fsig->params [0]));
5440                                         ins->dreg = mono_alloc_ireg_ref (cfg);
5441                                         ins->type = STACK_OBJ;
5442                                         break;
5443                                 }
5444
5445                                 if (opcode == OP_LOADI8_MEMBASE)
5446                                         ins = mono_decompose_opcode (cfg, ins);
5447
5448                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5449
5450                                 return ins;
5451                         }
5452                 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
5453                         guint32 opcode = 0;
5454                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5455
5456                         if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
5457                                 opcode = OP_STOREI1_MEMBASE_REG;
5458                         else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
5459                                 opcode = OP_STOREI2_MEMBASE_REG;
5460                         else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
5461                                 opcode = OP_STOREI4_MEMBASE_REG;
5462                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
5463                                 opcode = OP_STOREI8_MEMBASE_REG;
5464                         else if (fsig->params [0]->type == MONO_TYPE_R4)
5465                                 opcode = OP_STORER4_MEMBASE_REG;
5466                         else if (fsig->params [0]->type == MONO_TYPE_R8)
5467                                 opcode = OP_STORER8_MEMBASE_REG;
5468                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
5469                                 opcode = OP_STORE_MEMBASE_REG;
5470
5471                         if (opcode) {
5472                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5473
5474                                 MONO_INST_NEW (cfg, ins, opcode);
5475                                 ins->sreg1 = args [1]->dreg;
5476                                 ins->inst_destbasereg = args [0]->dreg;
5477                                 ins->inst_offset = 0;
5478                                 MONO_ADD_INS (cfg->cbb, ins);
5479
5480                                 if (opcode == OP_STOREI8_MEMBASE_REG)
5481                                         ins = mono_decompose_opcode (cfg, ins);
5482
5483                                 return ins;
5484                         }
5485                 }
5486         } else if (cmethod->klass->image == mono_defaults.corlib &&
5487                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5488                            (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
5489                 ins = NULL;
5490
5491 #if SIZEOF_REGISTER == 8
5492                 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
5493                         if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
5494                                 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
5495                                 ins->dreg = mono_alloc_preg (cfg);
5496                                 ins->sreg1 = args [0]->dreg;
5497                                 ins->type = STACK_I8;
5498                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
5499                                 MONO_ADD_INS (cfg->cbb, ins);
5500                         } else {
5501                                 MonoInst *load_ins;
5502
5503                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5504
5505                                 /* 64 bit reads are already atomic */
5506                                 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
5507                                 load_ins->dreg = mono_alloc_preg (cfg);
5508                                 load_ins->inst_basereg = args [0]->dreg;
5509                                 load_ins->inst_offset = 0;
5510                                 load_ins->type = STACK_I8;
5511                                 MONO_ADD_INS (cfg->cbb, load_ins);
5512
5513                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5514
5515                                 ins = load_ins;
5516                         }
5517                 }
5518 #endif
5519
5520                 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
5521                         MonoInst *ins_iconst;
5522                         guint32 opcode = 0;
5523
5524                         if (fsig->params [0]->type == MONO_TYPE_I4) {
5525                                 opcode = OP_ATOMIC_ADD_I4;
5526                                 cfg->has_atomic_add_i4 = TRUE;
5527                         }
5528 #if SIZEOF_REGISTER == 8
5529                         else if (fsig->params [0]->type == MONO_TYPE_I8)
5530                                 opcode = OP_ATOMIC_ADD_I8;
5531 #endif
5532                         if (opcode) {
5533                                 if (!mono_arch_opcode_supported (opcode))
5534                                         return NULL;
5535                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5536                                 ins_iconst->inst_c0 = 1;
5537                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
5538                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
5539
5540                                 MONO_INST_NEW (cfg, ins, opcode);
5541                                 ins->dreg = mono_alloc_ireg (cfg);
5542                                 ins->inst_basereg = args [0]->dreg;
5543                                 ins->inst_offset = 0;
5544                                 ins->sreg2 = ins_iconst->dreg;
5545                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5546                                 MONO_ADD_INS (cfg->cbb, ins);
5547                         }
5548                 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
5549                         MonoInst *ins_iconst;
5550                         guint32 opcode = 0;
5551
5552                         if (fsig->params [0]->type == MONO_TYPE_I4) {
5553                                 opcode = OP_ATOMIC_ADD_I4;
5554                                 cfg->has_atomic_add_i4 = TRUE;
5555                         }
5556 #if SIZEOF_REGISTER == 8
5557                         else if (fsig->params [0]->type == MONO_TYPE_I8)
5558                                 opcode = OP_ATOMIC_ADD_I8;
5559 #endif
5560                         if (opcode) {
5561                                 if (!mono_arch_opcode_supported (opcode))
5562                                         return NULL;
5563                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
5564                                 ins_iconst->inst_c0 = -1;
5565                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
5566                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
5567
5568                                 MONO_INST_NEW (cfg, ins, opcode);
5569                                 ins->dreg = mono_alloc_ireg (cfg);
5570                                 ins->inst_basereg = args [0]->dreg;
5571                                 ins->inst_offset = 0;
5572                                 ins->sreg2 = ins_iconst->dreg;
5573                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5574                                 MONO_ADD_INS (cfg->cbb, ins);
5575                         }
5576                 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
5577                         guint32 opcode = 0;
5578
5579                         if (fsig->params [0]->type == MONO_TYPE_I4) {
5580                                 opcode = OP_ATOMIC_ADD_I4;
5581                                 cfg->has_atomic_add_i4 = TRUE;
5582                         }
5583 #if SIZEOF_REGISTER == 8
5584                         else if (fsig->params [0]->type == MONO_TYPE_I8)
5585                                 opcode = OP_ATOMIC_ADD_I8;
5586 #endif
5587                         if (opcode) {
5588                                 if (!mono_arch_opcode_supported (opcode))
5589                                         return NULL;
5590                                 MONO_INST_NEW (cfg, ins, opcode);
5591                                 ins->dreg = mono_alloc_ireg (cfg);
5592                                 ins->inst_basereg = args [0]->dreg;
5593                                 ins->inst_offset = 0;
5594                                 ins->sreg2 = args [1]->dreg;
5595                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
5596                                 MONO_ADD_INS (cfg->cbb, ins);
5597                         }
5598                 }
5599                 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
5600                         MonoInst *f2i = NULL, *i2f;
5601                         guint32 opcode, f2i_opcode, i2f_opcode;
5602                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
5603                         gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
5604
5605                         if (fsig->params [0]->type == MONO_TYPE_I4 ||
5606                             fsig->params [0]->type == MONO_TYPE_R4) {
5607                                 opcode = OP_ATOMIC_EXCHANGE_I4;
5608                                 f2i_opcode = OP_MOVE_F_TO_I4;
5609                                 i2f_opcode = OP_MOVE_I4_TO_F;
5610                                 cfg->has_atomic_exchange_i4 = TRUE;
5611                         }
5612 #if SIZEOF_REGISTER == 8
5613                         else if (is_ref ||
5614                                  fsig->params [0]->type == MONO_TYPE_I8 ||
5615                                  fsig->params [0]->type == MONO_TYPE_R8 ||
5616                                  fsig->params [0]->type == MONO_TYPE_I) {
5617                                 opcode = OP_ATOMIC_EXCHANGE_I8;
5618                                 f2i_opcode = OP_MOVE_F_TO_I8;
5619                                 i2f_opcode = OP_MOVE_I8_TO_F;
5620                         }
5621 #else
5622                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
5623                                 opcode = OP_ATOMIC_EXCHANGE_I4;
5624                                 cfg->has_atomic_exchange_i4 = TRUE;
5625                         }
5626 #endif
5627                         else
5628                                 return NULL;
5629
5630                         if (!mono_arch_opcode_supported (opcode))
5631                                 return NULL;
5632
5633                         if (is_float) {
5634                                 /* TODO: Decompose these opcodes instead of bailing here. */
5635                                 if (COMPILE_SOFT_FLOAT (cfg))
5636                                         return NULL;
5637
5638                                 MONO_INST_NEW (cfg, f2i, f2i_opcode);
5639                                 f2i->dreg = mono_alloc_ireg (cfg);
5640                                 f2i->sreg1 = args [1]->dreg;
5641                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
5642                                         f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5643                                 MONO_ADD_INS (cfg->cbb, f2i);
5644                         }
5645
5646                         MONO_INST_NEW (cfg, ins, opcode);
5647                         ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
5648                         ins->inst_basereg = args [0]->dreg;
5649                         ins->inst_offset = 0;
5650                         ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
5651                         MONO_ADD_INS (cfg->cbb, ins);
5652
5653                         switch (fsig->params [0]->type) {
5654                         case MONO_TYPE_I4:
5655                                 ins->type = STACK_I4;
5656                                 break;
5657                         case MONO_TYPE_I8:
5658                                 ins->type = STACK_I8;
5659                                 break;
5660                         case MONO_TYPE_I:
5661 #if SIZEOF_REGISTER == 8
5662                                 ins->type = STACK_I8;
5663 #else
5664                                 ins->type = STACK_I4;
5665 #endif
5666                                 break;
5667                         case MONO_TYPE_R4:
5668                         case MONO_TYPE_R8:
5669                                 ins->type = STACK_R8;
5670                                 break;
5671                         default:
5672                                 g_assert (mini_type_is_reference (fsig->params [0]));
5673                                 ins->type = STACK_OBJ;
5674                                 break;
5675                         }
5676
5677                         if (is_float) {
5678                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5679                                 i2f->dreg = mono_alloc_freg (cfg);
5680                                 i2f->sreg1 = ins->dreg;
5681                                 i2f->type = STACK_R8;
5682                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
5683                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5684                                 MONO_ADD_INS (cfg->cbb, i2f);
5685
5686                                 ins = i2f;
5687                         }
5688
5689                         if (cfg->gen_write_barriers && is_ref)
5690                                 emit_write_barrier (cfg, args [0], args [1]);
5691                 }
5692                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
5693                         MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
5694                         guint32 opcode, f2i_opcode, i2f_opcode;
5695                         gboolean is_ref = mini_type_is_reference (fsig->params [1]);
5696                         gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
5697
5698                         if (fsig->params [1]->type == MONO_TYPE_I4 ||
5699                             fsig->params [1]->type == MONO_TYPE_R4) {
5700                                 opcode = OP_ATOMIC_CAS_I4;
5701                                 f2i_opcode = OP_MOVE_F_TO_I4;
5702                                 i2f_opcode = OP_MOVE_I4_TO_F;
5703                                 cfg->has_atomic_cas_i4 = TRUE;
5704                         }
5705 #if SIZEOF_REGISTER == 8
5706                         else if (is_ref ||
5707                                  fsig->params [1]->type == MONO_TYPE_I8 ||
5708                                  fsig->params [1]->type == MONO_TYPE_R8 ||
5709                                  fsig->params [1]->type == MONO_TYPE_I) {
5710                                 opcode = OP_ATOMIC_CAS_I8;
5711                                 f2i_opcode = OP_MOVE_F_TO_I8;
5712                                 i2f_opcode = OP_MOVE_I8_TO_F;
5713                         }
5714 #else
5715                         else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
5716                                 opcode = OP_ATOMIC_CAS_I4;
5717                                 cfg->has_atomic_cas_i4 = TRUE;
5718                         }
5719 #endif
5720                         else
5721                                 return NULL;
5722
5723                         if (!mono_arch_opcode_supported (opcode))
5724                                 return NULL;
5725
5726                         if (is_float) {
5727                                 /* TODO: Decompose these opcodes instead of bailing here. */
5728                                 if (COMPILE_SOFT_FLOAT (cfg))
5729                                         return NULL;
5730
5731                                 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
5732                                 f2i_new->dreg = mono_alloc_ireg (cfg);
5733                                 f2i_new->sreg1 = args [1]->dreg;
5734                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
5735                                         f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5736                                 MONO_ADD_INS (cfg->cbb, f2i_new);
5737
5738                                 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
5739                                 f2i_cmp->dreg = mono_alloc_ireg (cfg);
5740                                 f2i_cmp->sreg1 = args [2]->dreg;
5741                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
5742                                         f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5743                                 MONO_ADD_INS (cfg->cbb, f2i_cmp);
5744                         }
5745
5746                         MONO_INST_NEW (cfg, ins, opcode);
5747                         ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
5748                         ins->sreg1 = args [0]->dreg;
5749                         ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
5750                         ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
5751                         MONO_ADD_INS (cfg->cbb, ins);
5752
5753                         switch (fsig->params [1]->type) {
5754                         case MONO_TYPE_I4:
5755                                 ins->type = STACK_I4;
5756                                 break;
5757                         case MONO_TYPE_I8:
5758                                 ins->type = STACK_I8;
5759                                 break;
5760                         case MONO_TYPE_I:
5761 #if SIZEOF_REGISTER == 8
5762                                 ins->type = STACK_I8;
5763 #else
5764                                 ins->type = STACK_I4;
5765 #endif
5766                                 break;
5767                         case MONO_TYPE_R4:
5768                                 ins->type = cfg->r4_stack_type;
5769                                 break;
5770                         case MONO_TYPE_R8:
5771                                 ins->type = STACK_R8;
5772                                 break;
5773                         default:
5774                                 g_assert (mini_type_is_reference (fsig->params [1]));
5775                                 ins->type = STACK_OBJ;
5776                                 break;
5777                         }
5778
5779                         if (is_float) {
5780                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
5781                                 i2f->dreg = mono_alloc_freg (cfg);
5782                                 i2f->sreg1 = ins->dreg;
5783                                 i2f->type = STACK_R8;
5784                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
5785                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
5786                                 MONO_ADD_INS (cfg->cbb, i2f);
5787
5788                                 ins = i2f;
5789                         }
5790
5791                         if (cfg->gen_write_barriers && is_ref)
5792                                 emit_write_barrier (cfg, args [0], args [1]);
5793                 }
5794                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
5795                          fsig->params [1]->type == MONO_TYPE_I4) {
5796                         MonoInst *cmp, *ceq;
5797
5798                         if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
5799                                 return NULL;
5800
5801                         /* int32 r = CAS (location, value, comparand); */
5802                         MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
5803                         ins->dreg = alloc_ireg (cfg);
5804                         ins->sreg1 = args [0]->dreg;
5805                         ins->sreg2 = args [1]->dreg;
5806                         ins->sreg3 = args [2]->dreg;
5807                         ins->type = STACK_I4;
5808                         MONO_ADD_INS (cfg->cbb, ins);
5809
5810                         /* bool result = r == comparand; */
5811                         MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
5812                         cmp->sreg1 = ins->dreg;
5813                         cmp->sreg2 = args [2]->dreg;
5814                         cmp->type = STACK_I4;
5815                         MONO_ADD_INS (cfg->cbb, cmp);
5816
5817                         MONO_INST_NEW (cfg, ceq, OP_ICEQ);
5818                         ceq->dreg = alloc_ireg (cfg);
5819                         ceq->type = STACK_I4;
5820                         MONO_ADD_INS (cfg->cbb, ceq);
5821
5822                         /* *success = result; */
5823                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
5824
5825                         cfg->has_atomic_cas_i4 = TRUE;
5826                 }
5827                 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
5828                         ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
5829
5830                 if (ins)
5831                         return ins;
5832         } else if (cmethod->klass->image == mono_defaults.corlib &&
5833                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
5834                            (strcmp (cmethod->klass->name, "Volatile") == 0)) {
5835                 ins = NULL;
5836
5837                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
5838                         guint32 opcode = 0;
5839                         MonoType *t = fsig->params [0];
5840                         gboolean is_ref;
5841                         gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
5842
5843                         g_assert (t->byref);
5844                         /* t is a byref type, so the reference check is more complicated */
5845                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5846                         if (t->type == MONO_TYPE_I1)
5847                                 opcode = OP_ATOMIC_LOAD_I1;
5848                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5849                                 opcode = OP_ATOMIC_LOAD_U1;
5850                         else if (t->type == MONO_TYPE_I2)
5851                                 opcode = OP_ATOMIC_LOAD_I2;
5852                         else if (t->type == MONO_TYPE_U2)
5853                                 opcode = OP_ATOMIC_LOAD_U2;
5854                         else if (t->type == MONO_TYPE_I4)
5855                                 opcode = OP_ATOMIC_LOAD_I4;
5856                         else if (t->type == MONO_TYPE_U4)
5857                                 opcode = OP_ATOMIC_LOAD_U4;
5858                         else if (t->type == MONO_TYPE_R4)
5859                                 opcode = OP_ATOMIC_LOAD_R4;
5860                         else if (t->type == MONO_TYPE_R8)
5861                                 opcode = OP_ATOMIC_LOAD_R8;
5862 #if SIZEOF_REGISTER == 8
5863                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5864                                 opcode = OP_ATOMIC_LOAD_I8;
5865                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5866                                 opcode = OP_ATOMIC_LOAD_U8;
5867 #else
5868                         else if (t->type == MONO_TYPE_I)
5869                                 opcode = OP_ATOMIC_LOAD_I4;
5870                         else if (is_ref || t->type == MONO_TYPE_U)
5871                                 opcode = OP_ATOMIC_LOAD_U4;
5872 #endif
5873
5874                         if (opcode) {
5875                                 if (!mono_arch_opcode_supported (opcode))
5876                                         return NULL;
5877
5878                                 MONO_INST_NEW (cfg, ins, opcode);
5879                                 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
5880                                 ins->sreg1 = args [0]->dreg;
5881                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
5882                                 MONO_ADD_INS (cfg->cbb, ins);
5883
5884                                 switch (t->type) {
5885                                 case MONO_TYPE_BOOLEAN:
5886                                 case MONO_TYPE_I1:
5887                                 case MONO_TYPE_U1:
5888                                 case MONO_TYPE_I2:
5889                                 case MONO_TYPE_U2:
5890                                 case MONO_TYPE_I4:
5891                                 case MONO_TYPE_U4:
5892                                         ins->type = STACK_I4;
5893                                         break;
5894                                 case MONO_TYPE_I8:
5895                                 case MONO_TYPE_U8:
5896                                         ins->type = STACK_I8;
5897                                         break;
5898                                 case MONO_TYPE_I:
5899                                 case MONO_TYPE_U:
5900 #if SIZEOF_REGISTER == 8
5901                                         ins->type = STACK_I8;
5902 #else
5903                                         ins->type = STACK_I4;
5904 #endif
5905                                         break;
5906                                 case MONO_TYPE_R4:
5907                                         ins->type = cfg->r4_stack_type;
5908                                         break;
5909                                 case MONO_TYPE_R8:
5910                                         ins->type = STACK_R8;
5911                                         break;
5912                                 default:
5913                                         g_assert (is_ref);
5914                                         ins->type = STACK_OBJ;
5915                                         break;
5916                                 }
5917                         }
5918                 }
5919
5920                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
5921                         guint32 opcode = 0;
5922                         MonoType *t = fsig->params [0];
5923                         gboolean is_ref;
5924
5925                         g_assert (t->byref);
5926                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
5927                         if (t->type == MONO_TYPE_I1)
5928                                 opcode = OP_ATOMIC_STORE_I1;
5929                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
5930                                 opcode = OP_ATOMIC_STORE_U1;
5931                         else if (t->type == MONO_TYPE_I2)
5932                                 opcode = OP_ATOMIC_STORE_I2;
5933                         else if (t->type == MONO_TYPE_U2)
5934                                 opcode = OP_ATOMIC_STORE_U2;
5935                         else if (t->type == MONO_TYPE_I4)
5936                                 opcode = OP_ATOMIC_STORE_I4;
5937                         else if (t->type == MONO_TYPE_U4)
5938                                 opcode = OP_ATOMIC_STORE_U4;
5939                         else if (t->type == MONO_TYPE_R4)
5940                                 opcode = OP_ATOMIC_STORE_R4;
5941                         else if (t->type == MONO_TYPE_R8)
5942                                 opcode = OP_ATOMIC_STORE_R8;
5943 #if SIZEOF_REGISTER == 8
5944                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
5945                                 opcode = OP_ATOMIC_STORE_I8;
5946                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
5947                                 opcode = OP_ATOMIC_STORE_U8;
5948 #else
5949                         else if (t->type == MONO_TYPE_I)
5950                                 opcode = OP_ATOMIC_STORE_I4;
5951                         else if (is_ref || t->type == MONO_TYPE_U)
5952                                 opcode = OP_ATOMIC_STORE_U4;
5953 #endif
5954
5955                         if (opcode) {
5956                                 if (!mono_arch_opcode_supported (opcode))
5957                                         return NULL;
5958
5959                                 MONO_INST_NEW (cfg, ins, opcode);
5960                                 ins->dreg = args [0]->dreg;
5961                                 ins->sreg1 = args [1]->dreg;
5962                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
5963                                 MONO_ADD_INS (cfg->cbb, ins);
5964
5965                                 if (cfg->gen_write_barriers && is_ref)
5966                                         emit_write_barrier (cfg, args [0], args [1]);
5967                         }
5968                 }
5969
5970                 if (ins)
5971                         return ins;
5972         } else if (cmethod->klass->image == mono_defaults.corlib &&
5973                            (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
5974                            (strcmp (cmethod->klass->name, "Debugger") == 0)) {
5975                 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
5976                         if (should_insert_brekpoint (cfg->method)) {
5977                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
5978                         } else {
5979                                 MONO_INST_NEW (cfg, ins, OP_NOP);
5980                                 MONO_ADD_INS (cfg->cbb, ins);
5981                         }
5982                         return ins;
5983                 }
5984         } else if (cmethod->klass->image == mono_defaults.corlib &&
5985                    (strcmp (cmethod->klass->name_space, "System") == 0) &&
5986                    (strcmp (cmethod->klass->name, "Environment") == 0)) {
5987                 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
5988 #ifdef TARGET_WIN32
5989                         EMIT_NEW_ICONST (cfg, ins, 1);
5990 #else
5991                         EMIT_NEW_ICONST (cfg, ins, 0);
5992 #endif
5993                 }
5994         } else if (cmethod->klass->image == mono_defaults.corlib &&
5995                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
5996                            (strcmp (cmethod->klass->name, "Assembly") == 0)) {
5997                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
5998                         /* No stack walks are currently available, so implement this as an intrinsic */
5999                         MonoInst *assembly_ins;
6000
6001                         EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6002                         ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6003                         return ins;
6004                 }
6005         } else if (cmethod->klass->image == mono_defaults.corlib &&
6006                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6007                            (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6008                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6009                         /* No stack walks are currently available, so implement this as an intrinsic */
6010                         MonoInst *method_ins;
6011                         MonoMethod *declaring = cfg->method;
6012
6013                         /* This returns the declaring generic method */
6014                         if (declaring->is_inflated)
6015                                 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6016                         EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6017                         ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6018                         cfg->no_inline = TRUE;
6019                         if (cfg->method != cfg->current_method)
6020                                 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6021                         return ins;
6022                 }
6023         } else if (cmethod->klass == mono_defaults.math_class) {
6024                 /* 
6025                  * There is general branchless code for Min/Max, but it does not work for 
6026                  * all inputs:
6027                  * http://everything2.com/?node_id=1051618
6028                  */
6029         } else if (cmethod->klass == mono_defaults.systemtype_class && !strcmp (cmethod->name, "op_Equality")) {
6030                 EMIT_NEW_BIALU (cfg, ins, OP_COMPARE, -1, args [0]->dreg, args [1]->dreg);
6031                 MONO_INST_NEW (cfg, ins, OP_PCEQ);
6032                 ins->dreg = alloc_preg (cfg);
6033                 ins->type = STACK_I4;
6034                 MONO_ADD_INS (cfg->cbb, ins);
6035                 return ins;
6036         } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6037                     !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6038                                 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6039                                 !strcmp (cmethod->klass->name, "Selector")) ||
6040                            ((!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") ||
6041                                  !strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.Mac")) &&
6042                                 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6043                                 !strcmp (cmethod->klass->name, "Selector"))
6044                            ) {
6045                 if ((cfg->backend->have_objc_get_selector || cfg->compile_llvm) &&
6046                         !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6047                     (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6048                     cfg->compile_aot) {
6049                         MonoInst *pi;
6050                         MonoJumpInfoToken *ji;
6051                         char *s;
6052
6053                         if (args [0]->opcode == OP_GOT_ENTRY) {
6054                                 pi = (MonoInst *)args [0]->inst_p1;
6055                                 g_assert (pi->opcode == OP_PATCH_INFO);
6056                                 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6057                                 ji = (MonoJumpInfoToken *)pi->inst_p0;
6058                         } else {
6059                                 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6060                                 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6061                         }
6062
6063                         NULLIFY_INS (args [0]);
6064
6065                         s = mono_ldstr_utf8 (ji->image, mono_metadata_token_index (ji->token), &cfg->error);
6066                         return_val_if_nok (&cfg->error, NULL);
6067
6068                         MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6069                         ins->dreg = mono_alloc_ireg (cfg);
6070                         // FIXME: Leaks
6071                         ins->inst_p0 = s;
6072                         MONO_ADD_INS (cfg->cbb, ins);
6073                         return ins;
6074                 }
6075         }
6076
6077 #ifdef MONO_ARCH_SIMD_INTRINSICS
6078         if (cfg->opt & MONO_OPT_SIMD) {
6079                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6080                 if (ins)
6081                         return ins;
6082         }
6083 #endif
6084
6085         ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6086         if (ins)
6087                 return ins;
6088
6089         if (COMPILE_LLVM (cfg)) {
6090                 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6091                 if (ins)
6092                         return ins;
6093         }
6094
6095         return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6096 }
6097
6098 /*
6099  * This entry point could be used later for arbitrary method
6100  * redirection.
6101  */
6102 inline static MonoInst*
6103 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,  
6104                                         MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6105 {
6106         if (method->klass == mono_defaults.string_class) {
6107                 /* managed string allocation support */
6108                 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6109                         MonoInst *iargs [2];
6110                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6111                         MonoMethod *managed_alloc = NULL;
6112
6113                         g_assert (vtable); /*Should not fail since it System.String*/
6114 #ifndef MONO_CROSS_COMPILE
6115                         managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6116 #endif
6117                         if (!managed_alloc)
6118                                 return NULL;
6119                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6120                         iargs [1] = args [0];
6121                         return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6122                 }
6123         }
6124         return NULL;
6125 }
6126
6127 static void
6128 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6129 {
6130         MonoInst *store, *temp;
6131         int i;
6132
6133         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6134                 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6135
6136                 /*
6137                  * FIXME: We should use *args++ = sp [0], but that would mean the arg
6138                  * would be different than the MonoInst's used to represent arguments, and
6139                  * the ldelema implementation can't deal with that.
6140                  * Solution: When ldelema is used on an inline argument, create a var for 
6141                  * it, emit ldelema on that var, and emit the saving code below in
6142                  * inline_method () if needed.
6143                  */
6144                 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6145                 cfg->args [i] = temp;
6146                 /* This uses cfg->args [i] which is set by the preceeding line */
6147                 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6148                 store->cil_code = sp [0]->cil_code;
6149                 sp++;
6150         }
6151 }
6152
6153 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6154 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6155
6156 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6157 static gboolean
6158 check_inline_called_method_name_limit (MonoMethod *called_method)
6159 {
6160         int strncmp_result;
6161         static const char *limit = NULL;
6162         
6163         if (limit == NULL) {
6164                 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6165
6166                 if (limit_string != NULL)
6167                         limit = limit_string;
6168                 else
6169                         limit = "";
6170         }
6171
6172         if (limit [0] != '\0') {
6173                 char *called_method_name = mono_method_full_name (called_method, TRUE);
6174
6175                 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6176                 g_free (called_method_name);
6177         
6178                 //return (strncmp_result <= 0);
6179                 return (strncmp_result == 0);
6180         } else {
6181                 return TRUE;
6182         }
6183 }
6184 #endif
6185
6186 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6187 static gboolean
6188 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6189 {
6190         int strncmp_result;
6191         static const char *limit = NULL;
6192         
6193         if (limit == NULL) {
6194                 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6195                 if (limit_string != NULL) {
6196                         limit = limit_string;
6197                 } else {
6198                         limit = "";
6199                 }
6200         }
6201
6202         if (limit [0] != '\0') {
6203                 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6204
6205                 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6206                 g_free (caller_method_name);
6207         
6208                 //return (strncmp_result <= 0);
6209                 return (strncmp_result == 0);
6210         } else {
6211                 return TRUE;
6212         }
6213 }
6214 #endif
6215
6216 static void
6217 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6218 {
6219         static double r8_0 = 0.0;
6220         static float r4_0 = 0.0;
6221         MonoInst *ins;
6222         int t;
6223
6224         rtype = mini_get_underlying_type (rtype);
6225         t = rtype->type;
6226
6227         if (rtype->byref) {
6228                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6229         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6230                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6231         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6232                 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6233         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6234                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6235                 ins->type = STACK_R4;
6236                 ins->inst_p0 = (void*)&r4_0;
6237                 ins->dreg = dreg;
6238                 MONO_ADD_INS (cfg->cbb, ins);
6239         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6240                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6241                 ins->type = STACK_R8;
6242                 ins->inst_p0 = (void*)&r8_0;
6243                 ins->dreg = dreg;
6244                 MONO_ADD_INS (cfg->cbb, ins);
6245         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6246                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6247                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6248         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6249                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6250         } else {
6251                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6252         }
6253 }
6254
6255 static void
6256 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6257 {
6258         int t;
6259
6260         rtype = mini_get_underlying_type (rtype);
6261         t = rtype->type;
6262
6263         if (rtype->byref) {
6264                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
6265         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6266                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
6267         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6268                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
6269         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6270                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
6271         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6272                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
6273         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6274                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6275                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6276         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6277                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
6278         } else {
6279                 emit_init_rvar (cfg, dreg, rtype);
6280         }
6281 }
6282
6283 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
6284 static void
6285 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
6286 {
6287         MonoInst *var = cfg->locals [local];
6288         if (COMPILE_SOFT_FLOAT (cfg)) {
6289                 MonoInst *store;
6290                 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
6291                 emit_init_rvar (cfg, reg, type);
6292                 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
6293         } else {
6294                 if (init)
6295                         emit_init_rvar (cfg, var->dreg, type);
6296                 else
6297                         emit_dummy_init_rvar (cfg, var->dreg, type);
6298         }
6299 }
6300
6301 int
6302 mini_inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, guchar *ip, guint real_offset, gboolean inline_always)
6303 {
6304         return inline_method (cfg, cmethod, fsig, sp, ip, real_offset, inline_always);
6305 }
6306
6307 /*
6308  * inline_method:
6309  *
6310  * Return the cost of inlining CMETHOD, or zero if it should not be inlined.
6311  */
6312 static int
6313 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
6314                guchar *ip, guint real_offset, gboolean inline_always)
6315 {
6316         MonoError error;
6317         MonoInst *ins, *rvar = NULL;
6318         MonoMethodHeader *cheader;
6319         MonoBasicBlock *ebblock, *sbblock;
6320         int i, costs;
6321         MonoMethod *prev_inlined_method;
6322         MonoInst **prev_locals, **prev_args;
6323         MonoType **prev_arg_types;
6324         guint prev_real_offset;
6325         GHashTable *prev_cbb_hash;
6326         MonoBasicBlock **prev_cil_offset_to_bb;
6327         MonoBasicBlock *prev_cbb;
6328         const unsigned char *prev_ip;
6329         unsigned char *prev_cil_start;
6330         guint32 prev_cil_offset_to_bb_len;
6331         MonoMethod *prev_current_method;
6332         MonoGenericContext *prev_generic_context;
6333         gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
6334
6335         g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
6336
6337 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6338         if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
6339                 return 0;
6340 #endif
6341 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6342         if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
6343                 return 0;
6344 #endif
6345
6346         if (!fsig)
6347                 fsig = mono_method_signature (cmethod);
6348
6349         if (cfg->verbose_level > 2)
6350                 printf ("INLINE START %p %s -> %s\n", cmethod,  mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6351
6352         if (!cmethod->inline_info) {
6353                 cfg->stat_inlineable_methods++;
6354                 cmethod->inline_info = 1;
6355         }
6356
6357         /* allocate local variables */
6358         cheader = mono_method_get_header_checked (cmethod, &error);
6359         if (!cheader) {
6360                 if (inline_always) {
6361                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
6362                         mono_error_move (&cfg->error, &error);
6363                 } else {
6364                         mono_error_cleanup (&error);
6365                 }
6366                 return 0;
6367         }
6368
6369         /*Must verify before creating locals as it can cause the JIT to assert.*/
6370         if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
6371                 mono_metadata_free_mh (cheader);
6372                 return 0;
6373         }
6374
6375         /* allocate space to store the return value */
6376         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
6377                 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
6378         }
6379
6380         prev_locals = cfg->locals;
6381         cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
6382         for (i = 0; i < cheader->num_locals; ++i)
6383                 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
6384
6385         /* allocate start and end blocks */
6386         /* This is needed so if the inline is aborted, we can clean up */
6387         NEW_BBLOCK (cfg, sbblock);
6388         sbblock->real_offset = real_offset;
6389
6390         NEW_BBLOCK (cfg, ebblock);
6391         ebblock->block_num = cfg->num_bblocks++;
6392         ebblock->real_offset = real_offset;
6393
6394         prev_args = cfg->args;
6395         prev_arg_types = cfg->arg_types;
6396         prev_inlined_method = cfg->inlined_method;
6397         cfg->inlined_method = cmethod;
6398         cfg->ret_var_set = FALSE;
6399         cfg->inline_depth ++;
6400         prev_real_offset = cfg->real_offset;
6401         prev_cbb_hash = cfg->cbb_hash;
6402         prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
6403         prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
6404         prev_cil_start = cfg->cil_start;
6405         prev_ip = cfg->ip;
6406         prev_cbb = cfg->cbb;
6407         prev_current_method = cfg->current_method;
6408         prev_generic_context = cfg->generic_context;
6409         prev_ret_var_set = cfg->ret_var_set;
6410         prev_disable_inline = cfg->disable_inline;
6411
6412         if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
6413                 virtual_ = TRUE;
6414
6415         costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
6416
6417         ret_var_set = cfg->ret_var_set;
6418
6419         cfg->inlined_method = prev_inlined_method;
6420         cfg->real_offset = prev_real_offset;
6421         cfg->cbb_hash = prev_cbb_hash;
6422         cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
6423         cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
6424         cfg->cil_start = prev_cil_start;
6425         cfg->ip = prev_ip;
6426         cfg->locals = prev_locals;
6427         cfg->args = prev_args;
6428         cfg->arg_types = prev_arg_types;
6429         cfg->current_method = prev_current_method;
6430         cfg->generic_context = prev_generic_context;
6431         cfg->ret_var_set = prev_ret_var_set;
6432         cfg->disable_inline = prev_disable_inline;
6433         cfg->inline_depth --;
6434
6435         if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
6436                 if (cfg->verbose_level > 2)
6437                         printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
6438
6439                 cfg->stat_inlined_methods++;
6440
6441                 /* always add some code to avoid block split failures */
6442                 MONO_INST_NEW (cfg, ins, OP_NOP);
6443                 MONO_ADD_INS (prev_cbb, ins);
6444
6445                 prev_cbb->next_bb = sbblock;
6446                 link_bblock (cfg, prev_cbb, sbblock);
6447
6448                 /* 
6449                  * Get rid of the begin and end bblocks if possible to aid local
6450                  * optimizations.
6451                  */
6452                 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
6453
6454                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
6455                         mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
6456
6457                 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
6458                         MonoBasicBlock *prev = ebblock->in_bb [0];
6459
6460                         if (prev->next_bb == ebblock) {
6461                                 mono_merge_basic_blocks (cfg, prev, ebblock);
6462                                 cfg->cbb = prev;
6463                                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
6464                                         mono_merge_basic_blocks (cfg, prev_cbb, prev);
6465                                         cfg->cbb = prev_cbb;
6466                                 }
6467                         } else {
6468                                 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
6469                                 cfg->cbb = ebblock;
6470                         }
6471                 } else {
6472                         /* 
6473                          * Its possible that the rvar is set in some prev bblock, but not in others.
6474                          * (#1835).
6475                          */
6476                         if (rvar) {
6477                                 MonoBasicBlock *bb;
6478
6479                                 for (i = 0; i < ebblock->in_count; ++i) {
6480                                         bb = ebblock->in_bb [i];
6481
6482                                         if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
6483                                                 cfg->cbb = bb;
6484
6485                                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6486                                         }
6487                                 }
6488                         }
6489
6490                         cfg->cbb = ebblock;
6491                 }
6492
6493                 if (rvar) {
6494                         /*
6495                          * If the inlined method contains only a throw, then the ret var is not 
6496                          * set, so set it to a dummy value.
6497                          */
6498                         if (!ret_var_set)
6499                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
6500
6501                         EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
6502                         *sp++ = ins;
6503                 }
6504                 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6505                 return costs + 1;
6506         } else {
6507                 if (cfg->verbose_level > 2)
6508                         printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
6509                 cfg->exception_type = MONO_EXCEPTION_NONE;
6510
6511                 /* This gets rid of the newly added bblocks */
6512                 cfg->cbb = prev_cbb;
6513         }
6514         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
6515         return 0;
6516 }
6517
6518 /*
6519  * Some of these comments may well be out-of-date.
6520  * Design decisions: we do a single pass over the IL code (and we do bblock 
6521  * splitting/merging in the few cases when it's required: a back jump to an IL
6522  * address that was not already seen as bblock starting point).
6523  * Code is validated as we go (full verification is still better left to metadata/verify.c).
6524  * Complex operations are decomposed in simpler ones right away. We need to let the 
6525  * arch-specific code peek and poke inside this process somehow (except when the 
6526  * optimizations can take advantage of the full semantic info of coarse opcodes).
6527  * All the opcodes of the form opcode.s are 'normalized' to opcode.
6528  * MonoInst->opcode initially is the IL opcode or some simplification of that 
6529  * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific 
6530  * opcode with value bigger than OP_LAST.
6531  * At this point the IR can be handed over to an interpreter, a dumb code generator
6532  * or to the optimizing code generator that will translate it to SSA form.
6533  *
6534  * Profiling directed optimizations.
6535  * We may compile by default with few or no optimizations and instrument the code
6536  * or the user may indicate what methods to optimize the most either in a config file
6537  * or through repeated runs where the compiler applies offline the optimizations to 
6538  * each method and then decides if it was worth it.
6539  */
6540
6541 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
6542 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
6543 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
6544 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
6545 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
6546 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
6547 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
6548 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
6549
6550 /* offset from br.s -> br like opcodes */
6551 #define BIG_BRANCH_OFFSET 13
6552
6553 static gboolean
6554 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
6555 {
6556         MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
6557
6558         return b == NULL || b == bb;
6559 }
6560
6561 static int
6562 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
6563 {
6564         unsigned char *ip = start;
6565         unsigned char *target;
6566         int i;
6567         guint cli_addr;
6568         MonoBasicBlock *bblock;
6569         const MonoOpcode *opcode;
6570
6571         while (ip < end) {
6572                 cli_addr = ip - start;
6573                 i = mono_opcode_value ((const guint8 **)&ip, end);
6574                 if (i < 0)
6575                         UNVERIFIED;
6576                 opcode = &mono_opcodes [i];
6577                 switch (opcode->argument) {
6578                 case MonoInlineNone:
6579                         ip++; 
6580                         break;
6581                 case MonoInlineString:
6582                 case MonoInlineType:
6583                 case MonoInlineField:
6584                 case MonoInlineMethod:
6585                 case MonoInlineTok:
6586                 case MonoInlineSig:
6587                 case MonoShortInlineR:
6588                 case MonoInlineI:
6589                         ip += 5;
6590                         break;
6591                 case MonoInlineVar:
6592                         ip += 3;
6593                         break;
6594                 case MonoShortInlineVar:
6595                 case MonoShortInlineI:
6596                         ip += 2;
6597                         break;
6598                 case MonoShortInlineBrTarget:
6599                         target = start + cli_addr + 2 + (signed char)ip [1];
6600                         GET_BBLOCK (cfg, bblock, target);
6601                         ip += 2;
6602                         if (ip < end)
6603                                 GET_BBLOCK (cfg, bblock, ip);
6604                         break;
6605                 case MonoInlineBrTarget:
6606                         target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
6607                         GET_BBLOCK (cfg, bblock, target);
6608                         ip += 5;
6609                         if (ip < end)
6610                                 GET_BBLOCK (cfg, bblock, ip);
6611                         break;
6612                 case MonoInlineSwitch: {
6613                         guint32 n = read32 (ip + 1);
6614                         guint32 j;
6615                         ip += 5;
6616                         cli_addr += 5 + 4 * n;
6617                         target = start + cli_addr;
6618                         GET_BBLOCK (cfg, bblock, target);
6619                         
6620                         for (j = 0; j < n; ++j) {
6621                                 target = start + cli_addr + (gint32)read32 (ip);
6622                                 GET_BBLOCK (cfg, bblock, target);
6623                                 ip += 4;
6624                         }
6625                         break;
6626                 }
6627                 case MonoInlineR:
6628                 case MonoInlineI8:
6629                         ip += 9;
6630                         break;
6631                 default:
6632                         g_assert_not_reached ();
6633                 }
6634
6635                 if (i == CEE_THROW) {
6636                         unsigned char *bb_start = ip - 1;
6637                         
6638                         /* Find the start of the bblock containing the throw */
6639                         bblock = NULL;
6640                         while ((bb_start >= start) && !bblock) {
6641                                 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
6642                                 bb_start --;
6643                         }
6644                         if (bblock)
6645                                 bblock->out_of_line = 1;
6646                 }
6647         }
6648         return 0;
6649 unverified:
6650 exception_exit:
6651         *pos = ip;
6652         return 1;
6653 }
6654
6655 static inline MonoMethod *
6656 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
6657 {
6658         MonoMethod *method;
6659
6660         mono_error_init (error);
6661
6662         if (m->wrapper_type != MONO_WRAPPER_NONE) {
6663                 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
6664                 if (context) {
6665                         method = mono_class_inflate_generic_method_checked (method, context, error);
6666                 }
6667         } else {
6668                 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
6669         }
6670
6671         return method;
6672 }
6673
6674 static inline MonoMethod *
6675 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
6676 {
6677         MonoError error;
6678         MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
6679
6680         if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
6681                 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
6682                 method = NULL;
6683         }
6684
6685         if (!method && !cfg)
6686                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6687
6688         return method;
6689 }
6690
6691 static inline MonoClass*
6692 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
6693 {
6694         MonoError error;
6695         MonoClass *klass;
6696
6697         if (method->wrapper_type != MONO_WRAPPER_NONE) {
6698                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6699                 if (context) {
6700                         klass = mono_class_inflate_generic_class_checked (klass, context, &error);
6701                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
6702                 }
6703         } else {
6704                 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
6705                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
6706         }
6707         if (klass)
6708                 mono_class_init (klass);
6709         return klass;
6710 }
6711
6712 static inline MonoMethodSignature*
6713 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context, MonoError *error)
6714 {
6715         MonoMethodSignature *fsig;
6716
6717         mono_error_init (error);
6718         if (method->wrapper_type != MONO_WRAPPER_NONE) {
6719                 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6720         } else {
6721                 fsig = mono_metadata_parse_signature_checked (method->klass->image, token, error);
6722                 return_val_if_nok (error, NULL);
6723         }
6724         if (context) {
6725                 fsig = mono_inflate_generic_signature(fsig, context, error);
6726         }
6727         return fsig;
6728 }
6729
6730 static MonoMethod*
6731 throw_exception (void)
6732 {
6733         static MonoMethod *method = NULL;
6734
6735         if (!method) {
6736                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
6737                 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
6738         }
6739         g_assert (method);
6740         return method;
6741 }
6742
6743 static void
6744 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
6745 {
6746         MonoMethod *thrower = throw_exception ();
6747         MonoInst *args [1];
6748
6749         EMIT_NEW_PCONST (cfg, args [0], ex);
6750         mono_emit_method_call (cfg, thrower, args, NULL);
6751 }
6752
6753 /*
6754  * Return the original method is a wrapper is specified. We can only access 
6755  * the custom attributes from the original method.
6756  */
6757 static MonoMethod*
6758 get_original_method (MonoMethod *method)
6759 {
6760         if (method->wrapper_type == MONO_WRAPPER_NONE)
6761                 return method;
6762
6763         /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
6764         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
6765                 return NULL;
6766
6767         /* in other cases we need to find the original method */
6768         return mono_marshal_method_from_wrapper (method);
6769 }
6770
6771 static void
6772 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
6773 {
6774         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6775         MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
6776         if (ex)
6777                 emit_throw_exception (cfg, ex);
6778 }
6779
6780 static void
6781 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
6782 {
6783         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
6784         MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
6785         if (ex)
6786                 emit_throw_exception (cfg, ex);
6787 }
6788
6789 /*
6790  * Check that the IL instructions at ip are the array initialization
6791  * sequence and return the pointer to the data and the size.
6792  */
6793 static const char*
6794 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
6795 {
6796         /*
6797          * newarr[System.Int32]
6798          * dup
6799          * ldtoken field valuetype ...
6800          * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
6801          */
6802         if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
6803                 MonoError error;
6804                 guint32 token = read32 (ip + 7);
6805                 guint32 field_token = read32 (ip + 2);
6806                 guint32 field_index = field_token & 0xffffff;
6807                 guint32 rva;
6808                 const char *data_ptr;
6809                 int size = 0;
6810                 MonoMethod *cmethod;
6811                 MonoClass *dummy_class;
6812                 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
6813                 int dummy_align;
6814
6815                 if (!field) {
6816                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
6817                         return NULL;
6818                 }
6819
6820                 *out_field_token = field_token;
6821
6822                 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
6823                 if (!cmethod)
6824                         return NULL;
6825                 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
6826                         return NULL;
6827                 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
6828                 case MONO_TYPE_BOOLEAN:
6829                 case MONO_TYPE_I1:
6830                 case MONO_TYPE_U1:
6831                         size = 1; break;
6832                 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
6833 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
6834                 case MONO_TYPE_CHAR:
6835                 case MONO_TYPE_I2:
6836                 case MONO_TYPE_U2:
6837                         size = 2; break;
6838                 case MONO_TYPE_I4:
6839                 case MONO_TYPE_U4:
6840                 case MONO_TYPE_R4:
6841                         size = 4; break;
6842                 case MONO_TYPE_R8:
6843                 case MONO_TYPE_I8:
6844                 case MONO_TYPE_U8:
6845                         size = 8; break;
6846 #endif
6847                 default:
6848                         return NULL;
6849                 }
6850                 size *= len;
6851                 if (size > mono_type_size (field->type, &dummy_align))
6852                     return NULL;
6853                 *out_size = size;
6854                 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
6855                 if (!image_is_dynamic (method->klass->image)) {
6856                         field_index = read32 (ip + 2) & 0xffffff;
6857                         mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
6858                         data_ptr = mono_image_rva_map (method->klass->image, rva);
6859                         /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
6860                         /* for aot code we do the lookup on load */
6861                         if (aot && data_ptr)
6862                                 return (const char *)GUINT_TO_POINTER (rva);
6863                 } else {
6864                         /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */ 
6865                         g_assert (!aot);
6866                         data_ptr = mono_field_get_data (field);
6867                 }
6868                 return data_ptr;
6869         }
6870         return NULL;
6871 }
6872
6873 static void
6874 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
6875 {
6876         MonoError error;
6877         char *method_fname = mono_method_full_name (method, TRUE);
6878         char *method_code;
6879         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
6880
6881         if (!header) {
6882                 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
6883                 mono_error_cleanup (&error);
6884         } else if (header->code_size == 0)
6885                 method_code = g_strdup ("method body is empty.");
6886         else
6887                 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
6888         mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
6889         g_free (method_fname);
6890         g_free (method_code);
6891         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
6892 }
6893
6894 static void
6895 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
6896 {
6897         MonoInst *ins;
6898         guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
6899         if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0]  &&
6900                         ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
6901                 /* Optimize reg-reg moves away */
6902                 /* 
6903                  * Can't optimize other opcodes, since sp[0] might point to
6904                  * the last ins of a decomposed opcode.
6905                  */
6906                 sp [0]->dreg = (cfg)->locals [n]->dreg;
6907         } else {
6908                 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
6909         }
6910 }
6911
6912 /*
6913  * ldloca inhibits many optimizations so try to get rid of it in common
6914  * cases.
6915  */
6916 static inline unsigned char *
6917 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
6918 {
6919         int local, token;
6920         MonoClass *klass;
6921         MonoType *type;
6922
6923         if (size == 1) {
6924                 local = ip [1];
6925                 ip += 2;
6926         } else {
6927                 local = read16 (ip + 2);
6928                 ip += 4;
6929         }
6930         
6931         if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
6932                 /* From the INITOBJ case */
6933                 token = read32 (ip + 2);
6934                 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
6935                 CHECK_TYPELOAD (klass);
6936                 type = mini_get_underlying_type (&klass->byval_arg);
6937                 emit_init_local (cfg, local, type, TRUE);
6938                 return ip + 6;
6939         }
6940  exception_exit:
6941         return NULL;
6942 }
6943
6944 static MonoInst*
6945 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
6946 {
6947         MonoInst *icall_args [16];
6948         MonoInst *call_target, *ins, *vtable_ins;
6949         int arg_reg, this_reg, vtable_reg;
6950         gboolean is_iface = mono_class_is_interface (cmethod->klass);
6951         gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
6952         gboolean variant_iface = FALSE;
6953         guint32 slot;
6954         int offset;
6955
6956         /*
6957          * In llvm-only mode, vtables contain function descriptors instead of
6958          * method addresses/trampolines.
6959          */
6960         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
6961
6962         if (is_iface)
6963                 slot = mono_method_get_imt_slot (cmethod);
6964         else
6965                 slot = mono_method_get_vtable_index (cmethod);
6966
6967         this_reg = sp [0]->dreg;
6968
6969         if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
6970                 variant_iface = TRUE;
6971
6972         if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
6973                 /*
6974                  * The simplest case, a normal virtual call.
6975                  */
6976                 int slot_reg = alloc_preg (cfg);
6977                 int addr_reg = alloc_preg (cfg);
6978                 int arg_reg = alloc_preg (cfg);
6979                 MonoBasicBlock *non_null_bb;
6980
6981                 vtable_reg = alloc_preg (cfg);
6982                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6983                 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
6984
6985                 /* Load the vtable slot, which contains a function descriptor. */
6986                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
6987
6988                 NEW_BBLOCK (cfg, non_null_bb);
6989
6990                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
6991                 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
6992                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
6993
6994                 /* Slow path */
6995                 // FIXME: Make the wrapper use the preserveall cconv
6996                 // FIXME: Use one icall per slot for small slot numbers ?
6997                 icall_args [0] = vtable_ins;
6998                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
6999                 /* Make the icall return the vtable slot value to save some code space */
7000                 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7001                 ins->dreg = slot_reg;
7002                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7003
7004                 /* Fastpath */
7005                 MONO_START_BB (cfg, non_null_bb);
7006                 /* Load the address + arg from the vtable slot */
7007                 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7008                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7009
7010                 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7011         }
7012
7013         if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt) {
7014                 /*
7015                  * A simple interface call
7016                  *
7017                  * We make a call through an imt slot to obtain the function descriptor we need to call.
7018                  * The imt slot contains a function descriptor for a runtime function + arg.
7019                  */
7020                 int slot_reg = alloc_preg (cfg);
7021                 int addr_reg = alloc_preg (cfg);
7022                 int arg_reg = alloc_preg (cfg);
7023                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7024
7025                 vtable_reg = alloc_preg (cfg);
7026                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7027                 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7028
7029                 /*
7030                  * The slot is already initialized when the vtable is created so there is no need
7031                  * to check it here.
7032                  */
7033
7034                 /* Load the imt slot, which contains a function descriptor. */
7035                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7036
7037                 /* Load the address + arg of the imt thunk from the imt slot */
7038                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7039                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7040                 /*
7041                  * IMT thunks in llvm-only mode are C functions which take an info argument
7042                  * plus the imt method and return the ftndesc to call.
7043                  */
7044                 icall_args [0] = thunk_arg_ins;
7045                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7046                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7047                 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7048
7049                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7050         }
7051
7052         if ((fsig->generic_param_count || variant_iface) && !is_gsharedvt) {
7053                 /*
7054                  * This is similar to the interface case, the vtable slot points to an imt thunk which is
7055                  * dynamically extended as more instantiations are discovered.
7056                  * This handles generic virtual methods both on classes and interfaces.
7057                  */
7058                 int slot_reg = alloc_preg (cfg);
7059                 int addr_reg = alloc_preg (cfg);
7060                 int arg_reg = alloc_preg (cfg);
7061                 int ftndesc_reg = alloc_preg (cfg);
7062                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7063                 MonoBasicBlock *slowpath_bb, *end_bb;
7064
7065                 NEW_BBLOCK (cfg, slowpath_bb);
7066                 NEW_BBLOCK (cfg, end_bb);
7067
7068                 vtable_reg = alloc_preg (cfg);
7069                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7070                 if (is_iface)
7071                         offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7072                 else
7073                         offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7074
7075                 /* Load the slot, which contains a function descriptor. */
7076                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7077
7078                 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7079                 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7080                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7081                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7082
7083                 /* Fastpath */
7084                 /* Same as with iface calls */
7085                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7086                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7087                 icall_args [0] = thunk_arg_ins;
7088                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7089                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7090                 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_trampoline, icall_args, thunk_addr_ins, NULL, NULL);
7091                 ftndesc_ins->dreg = ftndesc_reg;
7092                 /*
7093                  * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7094                  * they don't know about yet. Fall back to the slowpath in that case.
7095                  */
7096                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7097                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7098
7099                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7100
7101                 /* Slowpath */
7102                 MONO_START_BB (cfg, slowpath_bb);
7103                 icall_args [0] = vtable_ins;
7104                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7105                 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7106                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7107                 if (is_iface)
7108                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7109                 else
7110                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7111                 ftndesc_ins->dreg = ftndesc_reg;
7112                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7113
7114                 /* Common case */
7115                 MONO_START_BB (cfg, end_bb);
7116                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7117         }
7118
7119         /*
7120          * Non-optimized cases
7121          */
7122         icall_args [0] = sp [0];
7123         EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7124
7125         icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7126                                                                                         cmethod, MONO_RGCTX_INFO_METHOD);
7127
7128         arg_reg = alloc_preg (cfg);
7129         MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7130         EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7131
7132         g_assert (is_gsharedvt);
7133         if (is_iface)
7134                 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7135         else
7136                 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7137
7138         /*
7139          * Pass the extra argument even if the callee doesn't receive it, most
7140          * calling conventions allow this.
7141          */
7142         return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7143 }
7144
7145 static gboolean
7146 is_exception_class (MonoClass *klass)
7147 {
7148         while (klass) {
7149                 if (klass == mono_defaults.exception_class)
7150                         return TRUE;
7151                 klass = klass->parent;
7152         }
7153         return FALSE;
7154 }
7155
7156 /*
7157  * is_jit_optimizer_disabled:
7158  *
7159  *   Determine whenever M's assembly has a DebuggableAttribute with the
7160  * IsJITOptimizerDisabled flag set.
7161  */
7162 static gboolean
7163 is_jit_optimizer_disabled (MonoMethod *m)
7164 {
7165         MonoError error;
7166         MonoAssembly *ass = m->klass->image->assembly;
7167         MonoCustomAttrInfo* attrs;
7168         MonoClass *klass;
7169         int i;
7170         gboolean val = FALSE;
7171
7172         g_assert (ass);
7173         if (ass->jit_optimizer_disabled_inited)
7174                 return ass->jit_optimizer_disabled;
7175
7176         klass = mono_class_try_get_debuggable_attribute_class ();
7177
7178         if (!klass) {
7179                 /* Linked away */
7180                 ass->jit_optimizer_disabled = FALSE;
7181                 mono_memory_barrier ();
7182                 ass->jit_optimizer_disabled_inited = TRUE;
7183                 return FALSE;
7184         }
7185
7186         attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
7187         mono_error_cleanup (&error); /* FIXME don't swallow the error */
7188         if (attrs) {
7189                 for (i = 0; i < attrs->num_attrs; ++i) {
7190                         MonoCustomAttrEntry *attr = &attrs->attrs [i];
7191                         const gchar *p;
7192                         MonoMethodSignature *sig;
7193
7194                         if (!attr->ctor || attr->ctor->klass != klass)
7195                                 continue;
7196                         /* Decode the attribute. See reflection.c */
7197                         p = (const char*)attr->data;
7198                         g_assert (read16 (p) == 0x0001);
7199                         p += 2;
7200
7201                         // FIXME: Support named parameters
7202                         sig = mono_method_signature (attr->ctor);
7203                         if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7204                                 continue;
7205                         /* Two boolean arguments */
7206                         p ++;
7207                         val = *p;
7208                 }
7209                 mono_custom_attrs_free (attrs);
7210         }
7211
7212         ass->jit_optimizer_disabled = val;
7213         mono_memory_barrier ();
7214         ass->jit_optimizer_disabled_inited = TRUE;
7215
7216         return val;
7217 }
7218
7219 static gboolean
7220 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7221 {
7222         gboolean supported_tail_call;
7223         int i;
7224
7225         supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7226
7227         for (i = 0; i < fsig->param_count; ++i) {
7228                 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7229                         /* These can point to the current method's stack */
7230                         supported_tail_call = FALSE;
7231         }
7232         if (fsig->hasthis && cmethod->klass->valuetype)
7233                 /* this might point to the current method's stack */
7234                 supported_tail_call = FALSE;
7235         if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7236                 supported_tail_call = FALSE;
7237         if (cfg->method->save_lmf)
7238                 supported_tail_call = FALSE;
7239         if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7240                 supported_tail_call = FALSE;
7241         if (call_opcode != CEE_CALL)
7242                 supported_tail_call = FALSE;
7243
7244         /* Debugging support */
7245 #if 0
7246         if (supported_tail_call) {
7247                 if (!mono_debug_count ())
7248                         supported_tail_call = FALSE;
7249         }
7250 #endif
7251
7252         return supported_tail_call;
7253 }
7254
7255 /*
7256  * handle_ctor_call:
7257  *
7258  *   Handle calls made to ctors from NEWOBJ opcodes.
7259  */
7260 static void
7261 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
7262                                   MonoInst **sp, guint8 *ip, int *inline_costs)
7263 {
7264         MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
7265
7266         if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7267                                         mono_method_is_generic_sharable (cmethod, TRUE)) {
7268                 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7269                         mono_class_vtable (cfg->domain, cmethod->klass);
7270                         CHECK_TYPELOAD (cmethod->klass);
7271
7272                         vtable_arg = emit_get_rgctx_method (cfg, context_used,
7273                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
7274                 } else {
7275                         if (context_used) {
7276                                 vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used,
7277                                                                                                    cmethod->klass, MONO_RGCTX_INFO_VTABLE);
7278                         } else {
7279                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7280
7281                                 CHECK_TYPELOAD (cmethod->klass);
7282                                 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
7283                         }
7284                 }
7285         }
7286
7287         /* Avoid virtual calls to ctors if possible */
7288         if (mono_class_is_marshalbyref (cmethod->klass))
7289                 callvirt_this_arg = sp [0];
7290
7291         if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
7292                 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
7293                 CHECK_CFG_EXCEPTION;
7294         } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7295                            mono_method_check_inlining (cfg, cmethod) &&
7296                            !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
7297                 int costs;
7298
7299                 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
7300                         cfg->real_offset += 5;
7301
7302                         *inline_costs += costs - 5;
7303                 } else {
7304                         INLINE_FAILURE ("inline failure");
7305                         // FIXME-VT: Clean this up
7306                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
7307                                 GSHAREDVT_FAILURE(*ip);
7308                         mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
7309                 }
7310         } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
7311                 MonoInst *addr;
7312
7313                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
7314
7315                 if (cfg->llvm_only) {
7316                         // FIXME: Avoid initializing vtable_arg
7317                         emit_llvmonly_calli (cfg, fsig, sp, addr);
7318                 } else {
7319                         mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
7320                 }
7321         } else if (context_used &&
7322                            ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
7323                                  !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
7324                 MonoInst *cmethod_addr;
7325
7326                 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
7327
7328                 if (cfg->llvm_only) {
7329                         MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
7330                                                                                                         MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7331                         emit_llvmonly_calli (cfg, fsig, sp, addr);
7332                 } else {
7333                         cmethod_addr = emit_get_rgctx_method (cfg, context_used,
7334                                                                                                   cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
7335
7336                         mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
7337                 }
7338         } else {
7339                 INLINE_FAILURE ("ctor call");
7340                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
7341                                                                                   callvirt_this_arg, NULL, vtable_arg);
7342         }
7343  exception_exit:
7344         return;
7345 }
7346
7347 static void
7348 emit_setret (MonoCompile *cfg, MonoInst *val)
7349 {
7350         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
7351         MonoInst *ins;
7352
7353         if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
7354                 MonoInst *ret_addr;
7355
7356                 if (!cfg->vret_addr) {
7357                         EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
7358                 } else {
7359                         EMIT_NEW_RETLOADA (cfg, ret_addr);
7360
7361                         EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
7362                         ins->klass = mono_class_from_mono_type (ret_type);
7363                 }
7364         } else {
7365 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
7366                 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
7367                         MonoInst *iargs [1];
7368                         MonoInst *conv;
7369
7370                         iargs [0] = val;
7371                         conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
7372                         mono_arch_emit_setret (cfg, cfg->method, conv);
7373                 } else {
7374                         mono_arch_emit_setret (cfg, cfg->method, val);
7375                 }
7376 #else
7377                 mono_arch_emit_setret (cfg, cfg->method, val);
7378 #endif
7379         }
7380 }
7381
7382 /*
7383  * mono_method_to_ir:
7384  *
7385  * Translate the .net IL into linear IR.
7386  *
7387  * @start_bblock: if not NULL, the starting basic block, used during inlining.
7388  * @end_bblock: if not NULL, the ending basic block, used during inlining.
7389  * @return_var: if not NULL, the place where the return value is stored, used during inlining.   
7390  * @inline_args: if not NULL, contains the arguments to the inline call
7391  * @inline_offset: if not zero, the real offset from the inline call, or zero otherwise.
7392  * @is_virtual_call: whether this method is being called as a result of a call to callvirt
7393  *
7394  * This method is used to turn ECMA IL into Mono's internal Linear IR
7395  * reprensetation.  It is used both for entire methods, as well as
7396  * inlining existing methods.  In the former case, the @start_bblock,
7397  * @end_bblock, @return_var, @inline_args are all set to NULL, and the
7398  * inline_offset is set to zero.
7399  * 
7400  * Returns: the inline cost, or -1 if there was an error processing this method.
7401  */
7402 int
7403 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
7404                    MonoInst *return_var, MonoInst **inline_args, 
7405                    guint inline_offset, gboolean is_virtual_call)
7406 {
7407         MonoError error;
7408         MonoInst *ins, **sp, **stack_start;
7409         MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
7410         MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
7411         MonoMethod *cmethod, *method_definition;
7412         MonoInst **arg_array;
7413         MonoMethodHeader *header;
7414         MonoImage *image;
7415         guint32 token, ins_flag;
7416         MonoClass *klass;
7417         MonoClass *constrained_class = NULL;
7418         unsigned char *ip, *end, *target, *err_pos;
7419         MonoMethodSignature *sig;
7420         MonoGenericContext *generic_context = NULL;
7421         MonoGenericContainer *generic_container = NULL;
7422         MonoType **param_types;
7423         int i, n, start_new_bblock, dreg;
7424         int num_calls = 0, inline_costs = 0;
7425         int breakpoint_id = 0;
7426         guint num_args;
7427         GSList *class_inits = NULL;
7428         gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
7429         int context_used;
7430         gboolean init_locals, seq_points, skip_dead_blocks;
7431         gboolean sym_seq_points = FALSE;
7432         MonoDebugMethodInfo *minfo;
7433         MonoBitSet *seq_point_locs = NULL;
7434         MonoBitSet *seq_point_set_locs = NULL;
7435
7436         cfg->disable_inline = is_jit_optimizer_disabled (method);
7437
7438         /* serialization and xdomain stuff may need access to private fields and methods */
7439         dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
7440         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
7441         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
7442         dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
7443         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
7444         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
7445
7446         /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
7447         dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
7448         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
7449         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
7450         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
7451
7452         image = method->klass->image;
7453         header = mono_method_get_header_checked (method, &cfg->error);
7454         if (!header) {
7455                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7456                 goto exception_exit;
7457         } else {
7458                 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7459         }
7460
7461         generic_container = mono_method_get_generic_container (method);
7462         sig = mono_method_signature (method);
7463         num_args = sig->hasthis + sig->param_count;
7464         ip = (unsigned char*)header->code;
7465         cfg->cil_start = ip;
7466         end = ip + header->code_size;
7467         cfg->stat_cil_code_size += header->code_size;
7468
7469         seq_points = cfg->gen_seq_points && cfg->method == method;
7470
7471         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
7472                 /* We could hit a seq point before attaching to the JIT (#8338) */
7473                 seq_points = FALSE;
7474         }
7475
7476         if (cfg->gen_sdb_seq_points && cfg->method == method) {
7477                 minfo = mono_debug_lookup_method (method);
7478                 if (minfo) {
7479                         MonoSymSeqPoint *sps;
7480                         int i, n_il_offsets;
7481
7482                         mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
7483                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7484                         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);
7485                         sym_seq_points = TRUE;
7486                         for (i = 0; i < n_il_offsets; ++i) {
7487                                 if (sps [i].il_offset < header->code_size)
7488                                         mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
7489                         }
7490                         g_free (sps);
7491                 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
7492                         /* Methods without line number info like auto-generated property accessors */
7493                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
7494                         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);
7495                         sym_seq_points = TRUE;
7496                 }
7497         }
7498
7499         /* 
7500          * Methods without init_locals set could cause asserts in various passes
7501          * (#497220). To work around this, we emit dummy initialization opcodes
7502          * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
7503          * on some platforms.
7504          */
7505         if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
7506                 init_locals = header->init_locals;
7507         else
7508                 init_locals = TRUE;
7509
7510         method_definition = method;
7511         while (method_definition->is_inflated) {
7512                 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
7513                 method_definition = imethod->declaring;
7514         }
7515
7516         /* SkipVerification is not allowed if core-clr is enabled */
7517         if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
7518                 dont_verify = TRUE;
7519                 dont_verify_stloc = TRUE;
7520         }
7521
7522         if (sig->is_inflated)
7523                 generic_context = mono_method_get_context (method);
7524         else if (generic_container)
7525                 generic_context = &generic_container->context;
7526         cfg->generic_context = generic_context;
7527
7528         if (!cfg->gshared)
7529                 g_assert (!sig->has_type_parameters);
7530
7531         if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
7532                 g_assert (method->is_inflated);
7533                 g_assert (mono_method_get_context (method)->method_inst);
7534         }
7535         if (method->is_inflated && mono_method_get_context (method)->method_inst)
7536                 g_assert (sig->generic_param_count);
7537
7538         if (cfg->method == method) {
7539                 cfg->real_offset = 0;
7540         } else {
7541                 cfg->real_offset = inline_offset;
7542         }
7543
7544         cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
7545         cfg->cil_offset_to_bb_len = header->code_size;
7546
7547         cfg->current_method = method;
7548
7549         if (cfg->verbose_level > 2)
7550                 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
7551
7552         param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
7553         if (sig->hasthis)
7554                 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
7555         for (n = 0; n < sig->param_count; ++n)
7556                 param_types [n + sig->hasthis] = sig->params [n];
7557         cfg->arg_types = param_types;
7558
7559         cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
7560         if (cfg->method == method) {
7561
7562                 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
7563                         cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
7564
7565                 /* ENTRY BLOCK */
7566                 NEW_BBLOCK (cfg, start_bblock);
7567                 cfg->bb_entry = start_bblock;
7568                 start_bblock->cil_code = NULL;
7569                 start_bblock->cil_length = 0;
7570
7571                 /* EXIT BLOCK */
7572                 NEW_BBLOCK (cfg, end_bblock);
7573                 cfg->bb_exit = end_bblock;
7574                 end_bblock->cil_code = NULL;
7575                 end_bblock->cil_length = 0;
7576                 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
7577                 g_assert (cfg->num_bblocks == 2);
7578
7579                 arg_array = cfg->args;
7580
7581                 if (header->num_clauses) {
7582                         cfg->spvars = g_hash_table_new (NULL, NULL);
7583                         cfg->exvars = g_hash_table_new (NULL, NULL);
7584                 }
7585                 /* handle exception clauses */
7586                 for (i = 0; i < header->num_clauses; ++i) {
7587                         MonoBasicBlock *try_bb;
7588                         MonoExceptionClause *clause = &header->clauses [i];
7589                         GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
7590
7591                         try_bb->real_offset = clause->try_offset;
7592                         try_bb->try_start = TRUE;
7593                         try_bb->region = ((i + 1) << 8) | clause->flags;
7594                         GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
7595                         tblock->real_offset = clause->handler_offset;
7596                         tblock->flags |= BB_EXCEPTION_HANDLER;
7597
7598                         /*
7599                          * Linking the try block with the EH block hinders inlining as we won't be able to 
7600                          * merge the bblocks from inlining and produce an artificial hole for no good reason.
7601                          */
7602                         if (COMPILE_LLVM (cfg))
7603                                 link_bblock (cfg, try_bb, tblock);
7604
7605                         if (*(ip + clause->handler_offset) == CEE_POP)
7606                                 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
7607
7608                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
7609                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
7610                             clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
7611                                 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7612                                 MONO_ADD_INS (tblock, ins);
7613
7614                                 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
7615                                         /* finally clauses already have a seq point */
7616                                         /* seq points for filter clauses are emitted below */
7617                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7618                                         MONO_ADD_INS (tblock, ins);
7619                                 }
7620
7621                                 /* todo: is a fault block unsafe to optimize? */
7622                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
7623                                         tblock->flags |= BB_EXCEPTION_UNSAFE;
7624                         }
7625
7626                         /*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);
7627                           while (p < end) {
7628                           printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
7629                           }*/
7630                         /* catch and filter blocks get the exception object on the stack */
7631                         if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
7632                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7633
7634                                 /* mostly like handle_stack_args (), but just sets the input args */
7635                                 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
7636                                 tblock->in_scount = 1;
7637                                 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7638                                 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7639
7640                                 cfg->cbb = tblock;
7641
7642 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
7643                                 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
7644                                 if (!cfg->compile_llvm) {
7645                                         MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
7646                                         ins->dreg = tblock->in_stack [0]->dreg;
7647                                         MONO_ADD_INS (tblock, ins);
7648                                 }
7649 #else
7650                                 MonoInst *dummy_use;
7651
7652                                 /* 
7653                                  * Add a dummy use for the exvar so its liveness info will be
7654                                  * correct.
7655                                  */
7656                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
7657 #endif
7658
7659                                 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7660                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
7661                                         MONO_ADD_INS (tblock, ins);
7662                                 }
7663                                 
7664                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7665                                         GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
7666                                         tblock->flags |= BB_EXCEPTION_HANDLER;
7667                                         tblock->real_offset = clause->data.filter_offset;
7668                                         tblock->in_scount = 1;
7669                                         tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
7670                                         /* The filter block shares the exvar with the handler block */
7671                                         tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
7672                                         MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
7673                                         MONO_ADD_INS (tblock, ins);
7674                                 }
7675                         }
7676
7677                         if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
7678                                         clause->data.catch_class &&
7679                                         cfg->gshared &&
7680                                         mono_class_check_context_used (clause->data.catch_class)) {
7681                                 /*
7682                                  * In shared generic code with catch
7683                                  * clauses containing type variables
7684                                  * the exception handling code has to
7685                                  * be able to get to the rgctx.
7686                                  * Therefore we have to make sure that
7687                                  * the vtable/mrgctx argument (for
7688                                  * static or generic methods) or the
7689                                  * "this" argument (for non-static
7690                                  * methods) are live.
7691                                  */
7692                                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7693                                                 mini_method_get_context (method)->method_inst ||
7694                                                 method->klass->valuetype) {
7695                                         mono_get_vtable_var (cfg);
7696                                 } else {
7697                                         MonoInst *dummy_use;
7698
7699                                         EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
7700                                 }
7701                         }
7702                 }
7703         } else {
7704                 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
7705                 cfg->cbb = start_bblock;
7706                 cfg->args = arg_array;
7707                 mono_save_args (cfg, sig, inline_args);
7708         }
7709
7710         /* FIRST CODE BLOCK */
7711         NEW_BBLOCK (cfg, tblock);
7712         tblock->cil_code = ip;
7713         cfg->cbb = tblock;
7714         cfg->ip = ip;
7715
7716         ADD_BBLOCK (cfg, tblock);
7717
7718         if (cfg->method == method) {
7719                 breakpoint_id = mono_debugger_method_has_breakpoint (method);
7720                 if (breakpoint_id) {
7721                         MONO_INST_NEW (cfg, ins, OP_BREAK);
7722                         MONO_ADD_INS (cfg->cbb, ins);
7723                 }
7724         }
7725
7726         /* we use a separate basic block for the initialization code */
7727         NEW_BBLOCK (cfg, init_localsbb);
7728         if (cfg->method == method)
7729                 cfg->bb_init = init_localsbb;
7730         init_localsbb->real_offset = cfg->real_offset;
7731         start_bblock->next_bb = init_localsbb;
7732         init_localsbb->next_bb = cfg->cbb;
7733         link_bblock (cfg, start_bblock, init_localsbb);
7734         link_bblock (cfg, init_localsbb, cfg->cbb);
7735                 
7736         cfg->cbb = init_localsbb;
7737
7738         if (cfg->gsharedvt && cfg->method == method) {
7739                 MonoGSharedVtMethodInfo *info;
7740                 MonoInst *var, *locals_var;
7741                 int dreg;
7742
7743                 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
7744                 info->method = cfg->method;
7745                 info->count_entries = 16;
7746                 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
7747                 cfg->gsharedvt_info = info;
7748
7749                 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7750                 /* prevent it from being register allocated */
7751                 //var->flags |= MONO_INST_VOLATILE;
7752                 cfg->gsharedvt_info_var = var;
7753
7754                 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
7755                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
7756
7757                 /* Allocate locals */
7758                 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
7759                 /* prevent it from being register allocated */
7760                 //locals_var->flags |= MONO_INST_VOLATILE;
7761                 cfg->gsharedvt_locals_var = locals_var;
7762
7763                 dreg = alloc_ireg (cfg);
7764                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
7765
7766                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7767                 ins->dreg = locals_var->dreg;
7768                 ins->sreg1 = dreg;
7769                 MONO_ADD_INS (cfg->cbb, ins);
7770                 cfg->gsharedvt_locals_var_ins = ins;
7771                 
7772                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7773                 /*
7774                 if (init_locals)
7775                         ins->flags |= MONO_INST_INIT;
7776                 */
7777         }
7778
7779         if (mono_security_core_clr_enabled ()) {
7780                 /* check if this is native code, e.g. an icall or a p/invoke */
7781                 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
7782                         MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
7783                         if (wrapped) {
7784                                 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
7785                                 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
7786
7787                                 /* if this ia a native call then it can only be JITted from platform code */
7788                                 if ((icall || pinvk) && method->klass && method->klass->image) {
7789                                         if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
7790                                                 MonoException *ex = icall ? mono_get_exception_security () : 
7791                                                         mono_get_exception_method_access ();
7792                                                 emit_throw_exception (cfg, ex);
7793                                         }
7794                                 }
7795                         }
7796                 }
7797         }
7798
7799         CHECK_CFG_EXCEPTION;
7800
7801         if (header->code_size == 0)
7802                 UNVERIFIED;
7803
7804         if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
7805                 ip = err_pos;
7806                 UNVERIFIED;
7807         }
7808
7809         if (cfg->method == method)
7810                 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
7811
7812         for (n = 0; n < header->num_locals; ++n) {
7813                 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
7814                         UNVERIFIED;
7815         }
7816         class_inits = NULL;
7817
7818         /* We force the vtable variable here for all shared methods
7819            for the possibility that they might show up in a stack
7820            trace where their exact instantiation is needed. */
7821         if (cfg->gshared && method == cfg->method) {
7822                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
7823                                 mini_method_get_context (method)->method_inst ||
7824                                 method->klass->valuetype) {
7825                         mono_get_vtable_var (cfg);
7826                 } else {
7827                         /* FIXME: Is there a better way to do this?
7828                            We need the variable live for the duration
7829                            of the whole method. */
7830                         cfg->args [0]->flags |= MONO_INST_VOLATILE;
7831                 }
7832         }
7833
7834         /* add a check for this != NULL to inlined methods */
7835         if (is_virtual_call) {
7836                 MonoInst *arg_ins;
7837
7838                 NEW_ARGLOAD (cfg, arg_ins, 0);
7839                 MONO_ADD_INS (cfg->cbb, arg_ins);
7840                 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
7841         }
7842
7843         skip_dead_blocks = !dont_verify;
7844         if (skip_dead_blocks) {
7845                 original_bb = bb = mono_basic_block_split (method, &cfg->error, header);
7846                 CHECK_CFG_ERROR;
7847                 g_assert (bb);
7848         }
7849
7850         /* we use a spare stack slot in SWITCH and NEWOBJ and others */
7851         stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
7852
7853         ins_flag = 0;
7854         start_new_bblock = 0;
7855         while (ip < end) {
7856                 if (cfg->method == method)
7857                         cfg->real_offset = ip - header->code;
7858                 else
7859                         cfg->real_offset = inline_offset;
7860                 cfg->ip = ip;
7861
7862                 context_used = 0;
7863
7864                 if (start_new_bblock) {
7865                         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
7866                         if (start_new_bblock == 2) {
7867                                 g_assert (ip == tblock->cil_code);
7868                         } else {
7869                                 GET_BBLOCK (cfg, tblock, ip);
7870                         }
7871                         cfg->cbb->next_bb = tblock;
7872                         cfg->cbb = tblock;
7873                         start_new_bblock = 0;
7874                         for (i = 0; i < cfg->cbb->in_scount; ++i) {
7875                                 if (cfg->verbose_level > 3)
7876                                         printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7877                                 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7878                                 *sp++ = ins;
7879                         }
7880                         if (class_inits)
7881                                 g_slist_free (class_inits);
7882                         class_inits = NULL;
7883                 } else {
7884                         if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
7885                                 link_bblock (cfg, cfg->cbb, tblock);
7886                                 if (sp != stack_start) {
7887                                         handle_stack_args (cfg, stack_start, sp - stack_start);
7888                                         sp = stack_start;
7889                                         CHECK_UNVERIFIABLE (cfg);
7890                                 }
7891                                 cfg->cbb->next_bb = tblock;
7892                                 cfg->cbb = tblock;
7893                                 for (i = 0; i < cfg->cbb->in_scount; ++i) {
7894                                         if (cfg->verbose_level > 3)
7895                                                 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
7896                                         EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
7897                                         *sp++ = ins;
7898                                 }
7899                                 g_slist_free (class_inits);
7900                                 class_inits = NULL;
7901                         }
7902                 }
7903
7904                 if (skip_dead_blocks) {
7905                         int ip_offset = ip - header->code;
7906
7907                         if (ip_offset == bb->end)
7908                                 bb = bb->next;
7909
7910                         if (bb->dead) {
7911                                 int op_size = mono_opcode_size (ip, end);
7912                                 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
7913
7914                                 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
7915
7916                                 if (ip_offset + op_size == bb->end) {
7917                                         MONO_INST_NEW (cfg, ins, OP_NOP);
7918                                         MONO_ADD_INS (cfg->cbb, ins);
7919                                         start_new_bblock = 1;
7920                                 }
7921
7922                                 ip += op_size;
7923                                 continue;
7924                         }
7925                 }
7926                 /*
7927                  * Sequence points are points where the debugger can place a breakpoint.
7928                  * Currently, we generate these automatically at points where the IL
7929                  * stack is empty.
7930                  */
7931                 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
7932                         /*
7933                          * Make methods interruptable at the beginning, and at the targets of
7934                          * backward branches.
7935                          * Also, do this at the start of every bblock in methods with clauses too,
7936                          * to be able to handle instructions with inprecise control flow like
7937                          * throw/endfinally.
7938                          * Backward branches are handled at the end of method-to-ir ().
7939                          */
7940                         gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
7941                         gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
7942
7943                         /* Avoid sequence points on empty IL like .volatile */
7944                         // FIXME: Enable this
7945                         //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
7946                         NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
7947                         if ((sp != stack_start) && !sym_seq_point)
7948                                 ins->flags |= MONO_INST_NONEMPTY_STACK;
7949                         MONO_ADD_INS (cfg->cbb, ins);
7950
7951                         if (sym_seq_points)
7952                                 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
7953                 }
7954
7955                 cfg->cbb->real_offset = cfg->real_offset;
7956
7957                 if ((cfg->method == method) && cfg->coverage_info) {
7958                         guint32 cil_offset = ip - header->code;
7959                         cfg->coverage_info->data [cil_offset].cil_code = ip;
7960
7961                         /* TODO: Use an increment here */
7962 #if defined(TARGET_X86)
7963                         MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
7964                         ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
7965                         ins->inst_imm = 1;
7966                         MONO_ADD_INS (cfg->cbb, ins);
7967 #else
7968                         EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
7969                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
7970 #endif
7971                 }
7972
7973                 if (cfg->verbose_level > 3)
7974                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
7975
7976                 switch (*ip) {
7977                 case CEE_NOP:
7978                         if (seq_points && !sym_seq_points && sp != stack_start) {
7979                                 /*
7980                                  * The C# compiler uses these nops to notify the JIT that it should
7981                                  * insert seq points.
7982                                  */
7983                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
7984                                 MONO_ADD_INS (cfg->cbb, ins);
7985                         }
7986                         if (cfg->keep_cil_nops)
7987                                 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
7988                         else
7989                                 MONO_INST_NEW (cfg, ins, OP_NOP);
7990                         ip++;
7991                         MONO_ADD_INS (cfg->cbb, ins);
7992                         break;
7993                 case CEE_BREAK:
7994                         if (should_insert_brekpoint (cfg->method)) {
7995                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
7996                         } else {
7997                                 MONO_INST_NEW (cfg, ins, OP_NOP);
7998                         }
7999                         ip++;
8000                         MONO_ADD_INS (cfg->cbb, ins);
8001                         break;
8002                 case CEE_LDARG_0:
8003                 case CEE_LDARG_1:
8004                 case CEE_LDARG_2:
8005                 case CEE_LDARG_3:
8006                         CHECK_STACK_OVF (1);
8007                         n = (*ip)-CEE_LDARG_0;
8008                         CHECK_ARG (n);
8009                         EMIT_NEW_ARGLOAD (cfg, ins, n);
8010                         ip++;
8011                         *sp++ = ins;
8012                         break;
8013                 case CEE_LDLOC_0:
8014                 case CEE_LDLOC_1:
8015                 case CEE_LDLOC_2:
8016                 case CEE_LDLOC_3:
8017                         CHECK_STACK_OVF (1);
8018                         n = (*ip)-CEE_LDLOC_0;
8019                         CHECK_LOCAL (n);
8020                         EMIT_NEW_LOCLOAD (cfg, ins, n);
8021                         ip++;
8022                         *sp++ = ins;
8023                         break;
8024                 case CEE_STLOC_0:
8025                 case CEE_STLOC_1:
8026                 case CEE_STLOC_2:
8027                 case CEE_STLOC_3: {
8028                         CHECK_STACK (1);
8029                         n = (*ip)-CEE_STLOC_0;
8030                         CHECK_LOCAL (n);
8031                         --sp;
8032                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8033                                 UNVERIFIED;
8034                         emit_stloc_ir (cfg, sp, header, n);
8035                         ++ip;
8036                         inline_costs += 1;
8037                         break;
8038                         }
8039                 case CEE_LDARG_S:
8040                         CHECK_OPSIZE (2);
8041                         CHECK_STACK_OVF (1);
8042                         n = ip [1];
8043                         CHECK_ARG (n);
8044                         EMIT_NEW_ARGLOAD (cfg, ins, n);
8045                         *sp++ = ins;
8046                         ip += 2;
8047                         break;
8048                 case CEE_LDARGA_S:
8049                         CHECK_OPSIZE (2);
8050                         CHECK_STACK_OVF (1);
8051                         n = ip [1];
8052                         CHECK_ARG (n);
8053                         NEW_ARGLOADA (cfg, ins, n);
8054                         MONO_ADD_INS (cfg->cbb, ins);
8055                         *sp++ = ins;
8056                         ip += 2;
8057                         break;
8058                 case CEE_STARG_S:
8059                         CHECK_OPSIZE (2);
8060                         CHECK_STACK (1);
8061                         --sp;
8062                         n = ip [1];
8063                         CHECK_ARG (n);
8064                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8065                                 UNVERIFIED;
8066                         EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8067                         ip += 2;
8068                         break;
8069                 case CEE_LDLOC_S:
8070                         CHECK_OPSIZE (2);
8071                         CHECK_STACK_OVF (1);
8072                         n = ip [1];
8073                         CHECK_LOCAL (n);
8074                         EMIT_NEW_LOCLOAD (cfg, ins, n);
8075                         *sp++ = ins;
8076                         ip += 2;
8077                         break;
8078                 case CEE_LDLOCA_S: {
8079                         unsigned char *tmp_ip;
8080                         CHECK_OPSIZE (2);
8081                         CHECK_STACK_OVF (1);
8082                         CHECK_LOCAL (ip [1]);
8083
8084                         if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8085                                 ip = tmp_ip;
8086                                 inline_costs += 1;
8087                                 break;
8088                         }
8089
8090                         EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8091                         *sp++ = ins;
8092                         ip += 2;
8093                         break;
8094                 }
8095                 case CEE_STLOC_S:
8096                         CHECK_OPSIZE (2);
8097                         CHECK_STACK (1);
8098                         --sp;
8099                         CHECK_LOCAL (ip [1]);
8100                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8101                                 UNVERIFIED;
8102                         emit_stloc_ir (cfg, sp, header, ip [1]);
8103                         ip += 2;
8104                         inline_costs += 1;
8105                         break;
8106                 case CEE_LDNULL:
8107                         CHECK_STACK_OVF (1);
8108                         EMIT_NEW_PCONST (cfg, ins, NULL);
8109                         ins->type = STACK_OBJ;
8110                         ++ip;
8111                         *sp++ = ins;
8112                         break;
8113                 case CEE_LDC_I4_M1:
8114                         CHECK_STACK_OVF (1);
8115                         EMIT_NEW_ICONST (cfg, ins, -1);
8116                         ++ip;
8117                         *sp++ = ins;
8118                         break;
8119                 case CEE_LDC_I4_0:
8120                 case CEE_LDC_I4_1:
8121                 case CEE_LDC_I4_2:
8122                 case CEE_LDC_I4_3:
8123                 case CEE_LDC_I4_4:
8124                 case CEE_LDC_I4_5:
8125                 case CEE_LDC_I4_6:
8126                 case CEE_LDC_I4_7:
8127                 case CEE_LDC_I4_8:
8128                         CHECK_STACK_OVF (1);
8129                         EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8130                         ++ip;
8131                         *sp++ = ins;
8132                         break;
8133                 case CEE_LDC_I4_S:
8134                         CHECK_OPSIZE (2);
8135                         CHECK_STACK_OVF (1);
8136                         ++ip;
8137                         EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8138                         ++ip;
8139                         *sp++ = ins;
8140                         break;
8141                 case CEE_LDC_I4:
8142                         CHECK_OPSIZE (5);
8143                         CHECK_STACK_OVF (1);
8144                         EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8145                         ip += 5;
8146                         *sp++ = ins;
8147                         break;
8148                 case CEE_LDC_I8:
8149                         CHECK_OPSIZE (9);
8150                         CHECK_STACK_OVF (1);
8151                         MONO_INST_NEW (cfg, ins, OP_I8CONST);
8152                         ins->type = STACK_I8;
8153                         ins->dreg = alloc_dreg (cfg, STACK_I8);
8154                         ++ip;
8155                         ins->inst_l = (gint64)read64 (ip);
8156                         MONO_ADD_INS (cfg->cbb, ins);
8157                         ip += 8;
8158                         *sp++ = ins;
8159                         break;
8160                 case CEE_LDC_R4: {
8161                         float *f;
8162                         gboolean use_aotconst = FALSE;
8163
8164 #ifdef TARGET_POWERPC
8165                         /* FIXME: Clean this up */
8166                         if (cfg->compile_aot)
8167                                 use_aotconst = TRUE;
8168 #endif
8169
8170                         /* FIXME: we should really allocate this only late in the compilation process */
8171                         f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8172                         CHECK_OPSIZE (5);
8173                         CHECK_STACK_OVF (1);
8174
8175                         if (use_aotconst) {
8176                                 MonoInst *cons;
8177                                 int dreg;
8178
8179                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8180
8181                                 dreg = alloc_freg (cfg);
8182                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8183                                 ins->type = cfg->r4_stack_type;
8184                         } else {
8185                                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8186                                 ins->type = cfg->r4_stack_type;
8187                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8188                                 ins->inst_p0 = f;
8189                                 MONO_ADD_INS (cfg->cbb, ins);
8190                         }
8191                         ++ip;
8192                         readr4 (ip, f);
8193                         ip += 4;
8194                         *sp++ = ins;                    
8195                         break;
8196                 }
8197                 case CEE_LDC_R8: {
8198                         double *d;
8199                         gboolean use_aotconst = FALSE;
8200
8201 #ifdef TARGET_POWERPC
8202                         /* FIXME: Clean this up */
8203                         if (cfg->compile_aot)
8204                                 use_aotconst = TRUE;
8205 #endif
8206
8207                         /* FIXME: we should really allocate this only late in the compilation process */
8208                         d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8209                         CHECK_OPSIZE (9);
8210                         CHECK_STACK_OVF (1);
8211
8212                         if (use_aotconst) {
8213                                 MonoInst *cons;
8214                                 int dreg;
8215
8216                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8217
8218                                 dreg = alloc_freg (cfg);
8219                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8220                                 ins->type = STACK_R8;
8221                         } else {
8222                                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8223                                 ins->type = STACK_R8;
8224                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8225                                 ins->inst_p0 = d;
8226                                 MONO_ADD_INS (cfg->cbb, ins);
8227                         }
8228                         ++ip;
8229                         readr8 (ip, d);
8230                         ip += 8;
8231                         *sp++ = ins;
8232                         break;
8233                 }
8234                 case CEE_DUP: {
8235                         MonoInst *temp, *store;
8236                         CHECK_STACK (1);
8237                         CHECK_STACK_OVF (1);
8238                         sp--;
8239                         ins = *sp;
8240
8241                         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8242                         EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8243
8244                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8245                         *sp++ = ins;
8246
8247                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8248                         *sp++ = ins;
8249
8250                         ++ip;
8251                         inline_costs += 2;
8252                         break;
8253                 }
8254                 case CEE_POP:
8255                         CHECK_STACK (1);
8256                         ip++;
8257                         --sp;
8258
8259 #ifdef TARGET_X86
8260                         if (sp [0]->type == STACK_R8)
8261                                 /* we need to pop the value from the x86 FP stack */
8262                                 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8263 #endif
8264                         break;
8265                 case CEE_JMP: {
8266                         MonoCallInst *call;
8267                         MonoMethodSignature *fsig;
8268                         int i, n;
8269
8270                         INLINE_FAILURE ("jmp");
8271                         GSHAREDVT_FAILURE (*ip);
8272
8273                         CHECK_OPSIZE (5);
8274                         if (stack_start != sp)
8275                                 UNVERIFIED;
8276                         token = read32 (ip + 1);
8277                         /* FIXME: check the signature matches */
8278                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8279                         CHECK_CFG_ERROR;
8280  
8281                         if (cfg->gshared && mono_method_check_context_used (cmethod))
8282                                 GENERIC_SHARING_FAILURE (CEE_JMP);
8283
8284                         emit_instrumentation_call (cfg, mono_profiler_method_leave);
8285
8286                         fsig = mono_method_signature (cmethod);
8287                         n = fsig->param_count + fsig->hasthis;
8288                         if (cfg->llvm_only) {
8289                                 MonoInst **args;
8290
8291                                 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8292                                 for (i = 0; i < n; ++i)
8293                                         EMIT_NEW_ARGLOAD (cfg, args [i], i);
8294                                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
8295                                 /*
8296                                  * The code in mono-basic-block.c treats the rest of the code as dead, but we
8297                                  * have to emit a normal return since llvm expects it.
8298                                  */
8299                                 if (cfg->ret)
8300                                         emit_setret (cfg, ins);
8301                                 MONO_INST_NEW (cfg, ins, OP_BR);
8302                                 ins->inst_target_bb = end_bblock;
8303                                 MONO_ADD_INS (cfg->cbb, ins);
8304                                 link_bblock (cfg, cfg->cbb, end_bblock);
8305                                 ip += 5;
8306                                 break;
8307                         } else if (cfg->backend->have_op_tail_call) {
8308                                 /* Handle tail calls similarly to calls */
8309                                 DISABLE_AOT (cfg);
8310
8311                                 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
8312                                 call->method = cmethod;
8313                                 call->tail_call = TRUE;
8314                                 call->signature = mono_method_signature (cmethod);
8315                                 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
8316                                 call->inst.inst_p0 = cmethod;
8317                                 for (i = 0; i < n; ++i)
8318                                         EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
8319
8320                                 if (mini_type_is_vtype (mini_get_underlying_type (call->signature->ret)))
8321                                         call->vret_var = cfg->vret_addr;
8322
8323                                 mono_arch_emit_call (cfg, call);
8324                                 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
8325                                 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
8326                         } else {
8327                                 for (i = 0; i < num_args; ++i)
8328                                         /* Prevent arguments from being optimized away */
8329                                         arg_array [i]->flags |= MONO_INST_VOLATILE;
8330
8331                                 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
8332                                 ins = (MonoInst*)call;
8333                                 ins->inst_p0 = cmethod;
8334                                 MONO_ADD_INS (cfg->cbb, ins);
8335                         }
8336
8337                         ip += 5;
8338                         start_new_bblock = 1;
8339                         break;
8340                 }
8341                 case CEE_CALLI: {
8342                         MonoInst *addr;
8343                         MonoMethodSignature *fsig;
8344
8345                         CHECK_OPSIZE (5);
8346                         token = read32 (ip + 1);
8347
8348                         ins = NULL;
8349
8350                         //GSHAREDVT_FAILURE (*ip);
8351                         cmethod = NULL;
8352                         CHECK_STACK (1);
8353                         --sp;
8354                         addr = *sp;
8355                         fsig = mini_get_signature (method, token, generic_context, &cfg->error);
8356                         CHECK_CFG_ERROR;
8357
8358                         if (method->dynamic && fsig->pinvoke) {
8359                                 MonoInst *args [3];
8360
8361                                 /*
8362                                  * This is a call through a function pointer using a pinvoke
8363                                  * signature. Have to create a wrapper and call that instead.
8364                                  * FIXME: This is very slow, need to create a wrapper at JIT time
8365                                  * instead based on the signature.
8366                                  */
8367                                 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
8368                                 EMIT_NEW_PCONST (cfg, args [1], fsig);
8369                                 args [2] = addr;
8370                                 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
8371                         }
8372
8373                         n = fsig->param_count + fsig->hasthis;
8374
8375                         CHECK_STACK (n);
8376
8377                         //g_assert (!virtual_ || fsig->hasthis);
8378
8379                         sp -= n;
8380
8381                         inline_costs += 10 * num_calls++;
8382
8383                         /*
8384                          * Making generic calls out of gsharedvt methods.
8385                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
8386                          * patching gshared method addresses into a gsharedvt method.
8387                          */
8388                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8389                                 /*
8390                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
8391                                  */
8392                                 MonoInst *callee = addr;
8393
8394                                 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
8395                                         /* Not tested */
8396                                         GSHAREDVT_FAILURE (*ip);
8397
8398                                 if (cfg->llvm_only)
8399                                         // FIXME:
8400                                         GSHAREDVT_FAILURE (*ip);
8401
8402                                 addr = emit_get_rgctx_sig (cfg, context_used,
8403                                                                                           fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
8404                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
8405                                 goto calli_end;
8406                         }
8407
8408                         /* Prevent inlining of methods with indirect calls */
8409                         INLINE_FAILURE ("indirect call");
8410
8411                         if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
8412                                 MonoJumpInfoType info_type;
8413                                 gpointer info_data;
8414
8415                                 /*
8416                                  * Instead of emitting an indirect call, emit a direct call
8417                                  * with the contents of the aotconst as the patch info.
8418                                  */
8419                                 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
8420                                         info_type = (MonoJumpInfoType)addr->inst_c1;
8421                                         info_data = addr->inst_p0;
8422                                 } else {
8423                                         info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
8424                                         info_data = addr->inst_right->inst_left;
8425                                 }
8426
8427                                 if (info_type == MONO_PATCH_INFO_ICALL_ADDR) {
8428                                         ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR_CALL, info_data, fsig, sp);
8429                                         NULLIFY_INS (addr);
8430                                         goto calli_end;
8431                                 } else if (info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8432                                         ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
8433                                         NULLIFY_INS (addr);
8434                                         goto calli_end;
8435                                 }
8436                         }
8437                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8438
8439                         calli_end:
8440
8441                         /* End of call, INS should contain the result of the call, if any */
8442
8443                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8444                                 g_assert (ins);
8445                                 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
8446                         }
8447
8448                         CHECK_CFG_EXCEPTION;
8449
8450                         ip += 5;
8451                         ins_flag = 0;
8452                         constrained_class = NULL;
8453                         break;
8454                 }
8455                 case CEE_CALL:
8456                 case CEE_CALLVIRT: {
8457                         MonoInst *addr = NULL;
8458                         MonoMethodSignature *fsig = NULL;
8459                         int array_rank = 0;
8460                         int virtual_ = *ip == CEE_CALLVIRT;
8461                         gboolean pass_imt_from_rgctx = FALSE;
8462                         MonoInst *imt_arg = NULL;
8463                         MonoInst *keep_this_alive = NULL;
8464                         gboolean pass_vtable = FALSE;
8465                         gboolean pass_mrgctx = FALSE;
8466                         MonoInst *vtable_arg = NULL;
8467                         gboolean check_this = FALSE;
8468                         gboolean supported_tail_call = FALSE;
8469                         gboolean tail_call = FALSE;
8470                         gboolean need_seq_point = FALSE;
8471                         guint32 call_opcode = *ip;
8472                         gboolean emit_widen = TRUE;
8473                         gboolean push_res = TRUE;
8474                         gboolean skip_ret = FALSE;
8475                         gboolean delegate_invoke = FALSE;
8476                         gboolean direct_icall = FALSE;
8477                         gboolean constrained_partial_call = FALSE;
8478                         MonoMethod *cil_method;
8479
8480                         CHECK_OPSIZE (5);
8481                         token = read32 (ip + 1);
8482
8483                         ins = NULL;
8484
8485                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
8486                         CHECK_CFG_ERROR;
8487
8488                         cil_method = cmethod;
8489                                 
8490                         if (constrained_class) {
8491                                 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8492                                         if (!mini_is_gsharedvt_klass (constrained_class)) {
8493                                                 g_assert (!cmethod->klass->valuetype);
8494                                                 if (!mini_type_is_reference (&constrained_class->byval_arg))
8495                                                         constrained_partial_call = TRUE;
8496                                         }
8497                                 }
8498
8499                                 if (method->wrapper_type != MONO_WRAPPER_NONE) {
8500                                         if (cfg->verbose_level > 2)
8501                                                 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8502                                         if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
8503                                                    constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
8504                                                   cfg->gshared)) {
8505                                                 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
8506                                                 CHECK_CFG_ERROR;
8507                                         }
8508                                 } else {
8509                                         if (cfg->verbose_level > 2)
8510                                                 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
8511
8512                                         if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
8513                                                 /* 
8514                                                  * This is needed since get_method_constrained can't find 
8515                                                  * the method in klass representing a type var.
8516                                                  * The type var is guaranteed to be a reference type in this
8517                                                  * case.
8518                                                  */
8519                                                 if (!mini_is_gsharedvt_klass (constrained_class))
8520                                                         g_assert (!cmethod->klass->valuetype);
8521                                         } else {
8522                                                 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
8523                                                 CHECK_CFG_ERROR;
8524                                         }
8525                                 }
8526                         }
8527                                         
8528                         if (!dont_verify && !cfg->skip_visibility) {
8529                                 MonoMethod *target_method = cil_method;
8530                                 if (method->is_inflated) {
8531                                         target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
8532                                         CHECK_CFG_ERROR;
8533                                 }
8534                                 if (!mono_method_can_access_method (method_definition, target_method) &&
8535                                         !mono_method_can_access_method (method, cil_method))
8536                                         emit_method_access_failure (cfg, method, cil_method);
8537                         }
8538
8539                         if (mono_security_core_clr_enabled ())
8540                                 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
8541
8542                         if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
8543                                 /* MS.NET seems to silently convert this to a callvirt */
8544                                 virtual_ = 1;
8545
8546                         {
8547                                 /*
8548                                  * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
8549                                  * converts to a callvirt.
8550                                  *
8551                                  * tests/bug-515884.il is an example of this behavior
8552                                  */
8553                                 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
8554                                 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
8555                                 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
8556                                         virtual_ = 1;
8557                         }
8558
8559                         if (!cmethod->klass->inited)
8560                                 if (!mono_class_init (cmethod->klass))
8561                                         TYPE_LOAD_ERROR (cmethod->klass);
8562
8563                         fsig = mono_method_signature (cmethod);
8564                         if (!fsig)
8565                                 LOAD_ERROR;
8566                         if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
8567                                 mini_class_is_system_array (cmethod->klass)) {
8568                                 array_rank = cmethod->klass->rank;
8569                         } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
8570                                 direct_icall = TRUE;
8571                         } else if (fsig->pinvoke) {
8572                                 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
8573                                 fsig = mono_method_signature (wrapper);
8574                         } else if (constrained_class) {
8575                         } else {
8576                                 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
8577                                 CHECK_CFG_ERROR;
8578                         }
8579
8580                         if (cfg->llvm_only && !cfg->method->wrapper_type && (!cmethod || cmethod->is_inflated))
8581                                 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
8582
8583                         /* See code below */
8584                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8585                                 MonoBasicBlock *tbb;
8586
8587                                 GET_BBLOCK (cfg, tbb, ip + 5);
8588                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8589                                         /*
8590                                          * We want to extend the try block to cover the call, but we can't do it if the
8591                                          * call is made directly since its followed by an exception check.
8592                                          */
8593                                         direct_icall = FALSE;
8594                                 }
8595                         }
8596
8597                         mono_save_token_info (cfg, image, token, cil_method);
8598
8599                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
8600                                 need_seq_point = TRUE;
8601
8602                         /* Don't support calls made using type arguments for now */
8603                         /*
8604                           if (cfg->gsharedvt) {
8605                           if (mini_is_gsharedvt_signature (fsig))
8606                           GSHAREDVT_FAILURE (*ip);
8607                           }
8608                         */
8609
8610                         if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
8611                                 g_assert_not_reached ();
8612
8613                         n = fsig->param_count + fsig->hasthis;
8614
8615                         if (!cfg->gshared && mono_class_is_gtd (cmethod->klass))
8616                                 UNVERIFIED;
8617
8618                         if (!cfg->gshared)
8619                                 g_assert (!mono_method_check_context_used (cmethod));
8620
8621                         CHECK_STACK (n);
8622
8623                         //g_assert (!virtual_ || fsig->hasthis);
8624
8625                         sp -= n;
8626
8627                         /*
8628                          * We have the `constrained.' prefix opcode.
8629                          */
8630                         if (constrained_class) {
8631                                 if (mini_is_gsharedvt_klass (constrained_class)) {
8632                                         if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
8633                                                 /* The 'Own method' case below */
8634                                         } else if (cmethod->klass->image != mono_defaults.corlib && !mono_class_is_interface (cmethod->klass) && !cmethod->klass->valuetype) {
8635                                                 /* 'The type parameter is instantiated as a reference type' case below. */
8636                                         } else {
8637                                                 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
8638                                                 CHECK_CFG_EXCEPTION;
8639                                                 g_assert (ins);
8640                                                 goto call_end;
8641                                         }
8642                                 }
8643
8644                                 if (constrained_partial_call) {
8645                                         gboolean need_box = TRUE;
8646
8647                                         /*
8648                                          * The receiver is a valuetype, but the exact type is not known at compile time. This means the
8649                                          * called method is not known at compile time either. The called method could end up being
8650                                          * one of the methods on the parent classes (object/valuetype/enum), in which case we need
8651                                          * to box the receiver.
8652                                          * A simple solution would be to box always and make a normal virtual call, but that would
8653                                          * be bad performance wise.
8654                                          */
8655                                         if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass)) {
8656                                                 /*
8657                                                  * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
8658                                                  */
8659                                                 need_box = FALSE;
8660                                         }
8661
8662                                         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)) {
8663                                                 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
8664                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8665                                                 ins->klass = constrained_class;
8666                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8667                                                 CHECK_CFG_EXCEPTION;
8668                                         } else if (need_box) {
8669                                                 MonoInst *box_type;
8670                                                 MonoBasicBlock *is_ref_bb, *end_bb;
8671                                                 MonoInst *nonbox_call;
8672
8673                                                 /*
8674                                                  * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
8675                                                  * if needed.
8676                                                  * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
8677                                                  * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
8678                                                  */
8679                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8680
8681                                                 NEW_BBLOCK (cfg, is_ref_bb);
8682                                                 NEW_BBLOCK (cfg, end_bb);
8683
8684                                                 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);
8685                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
8686                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
8687
8688                                                 /* Non-ref case */
8689                                                 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8690
8691                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8692
8693                                                 /* Ref case */
8694                                                 MONO_START_BB (cfg, is_ref_bb);
8695                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8696                                                 ins->klass = constrained_class;
8697                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8698                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8699
8700                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
8701
8702                                                 MONO_START_BB (cfg, end_bb);
8703                                                 cfg->cbb = end_bb;
8704
8705                                                 nonbox_call->dreg = ins->dreg;
8706                                                 goto call_end;
8707                                         } else {
8708                                                 g_assert (mono_class_is_interface (cmethod->klass));
8709                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
8710                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8711                                                 goto call_end;
8712                                         }
8713                                 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
8714                                         /*
8715                                          * The type parameter is instantiated as a valuetype,
8716                                          * but that type doesn't override the method we're
8717                                          * calling, so we need to box `this'.
8718                                          */
8719                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8720                                         ins->klass = constrained_class;
8721                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8722                                         CHECK_CFG_EXCEPTION;
8723                                 } else if (!constrained_class->valuetype) {
8724                                         int dreg = alloc_ireg_ref (cfg);
8725
8726                                         /*
8727                                          * The type parameter is instantiated as a reference
8728                                          * type.  We have a managed pointer on the stack, so
8729                                          * we need to dereference it here.
8730                                          */
8731                                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
8732                                         ins->type = STACK_OBJ;
8733                                         sp [0] = ins;
8734                                 } else {
8735                                         if (cmethod->klass->valuetype) {
8736                                                 /* Own method */
8737                                         } else {
8738                                                 /* Interface method */
8739                                                 int ioffset, slot;
8740
8741                                                 mono_class_setup_vtable (constrained_class);
8742                                                 CHECK_TYPELOAD (constrained_class);
8743                                                 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
8744                                                 if (ioffset == -1)
8745                                                         TYPE_LOAD_ERROR (constrained_class);
8746                                                 slot = mono_method_get_vtable_slot (cmethod);
8747                                                 if (slot == -1)
8748                                                         TYPE_LOAD_ERROR (cmethod->klass);
8749                                                 cmethod = constrained_class->vtable [ioffset + slot];
8750
8751                                                 if (cmethod->klass == mono_defaults.enum_class) {
8752                                                         /* Enum implements some interfaces, so treat this as the first case */
8753                                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
8754                                                         ins->klass = constrained_class;
8755                                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
8756                                                         CHECK_CFG_EXCEPTION;
8757                                                 }
8758                                         }
8759                                         virtual_ = 0;
8760                                 }
8761                                 constrained_class = NULL;
8762                         }
8763
8764                         if (check_call_signature (cfg, fsig, sp))
8765                                 UNVERIFIED;
8766
8767                         if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
8768                                 delegate_invoke = TRUE;
8769
8770                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
8771                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8772                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
8773                                         emit_widen = FALSE;
8774                                 }
8775
8776                                 goto call_end;
8777                         }
8778
8779                         /* 
8780                          * If the callee is a shared method, then its static cctor
8781                          * might not get called after the call was patched.
8782                          */
8783                         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)) {
8784                                 emit_class_init (cfg, cmethod->klass);
8785                                 CHECK_TYPELOAD (cmethod->klass);
8786                         }
8787
8788                         check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
8789
8790                         if (cfg->gshared) {
8791                                 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
8792
8793                                 context_used = mini_method_check_context_used (cfg, cmethod);
8794
8795                                 if (context_used && mono_class_is_interface (cmethod->klass)) {
8796                                         /* Generic method interface
8797                                            calls are resolved via a
8798                                            helper function and don't
8799                                            need an imt. */
8800                                         if (!cmethod_context || !cmethod_context->method_inst)
8801                                                 pass_imt_from_rgctx = TRUE;
8802                                 }
8803
8804                                 /*
8805                                  * If a shared method calls another
8806                                  * shared method then the caller must
8807                                  * have a generic sharing context
8808                                  * because the magic trampoline
8809                                  * requires it.  FIXME: We shouldn't
8810                                  * have to force the vtable/mrgctx
8811                                  * variable here.  Instead there
8812                                  * should be a flag in the cfg to
8813                                  * request a generic sharing context.
8814                                  */
8815                                 if (context_used &&
8816                                                 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
8817                                         mono_get_vtable_var (cfg);
8818                         }
8819
8820                         if (pass_vtable) {
8821                                 if (context_used) {
8822                                         vtable_arg = mini_emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8823                                 } else {
8824                                         MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8825
8826                                         CHECK_TYPELOAD (cmethod->klass);
8827                                         EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8828                                 }
8829                         }
8830
8831                         if (pass_mrgctx) {
8832                                 g_assert (!vtable_arg);
8833
8834                                 if (!cfg->compile_aot) {
8835                                         /* 
8836                                          * emit_get_rgctx_method () calls mono_class_vtable () so check 
8837                                          * for type load errors before.
8838                                          */
8839                                         mono_class_setup_vtable (cmethod->klass);
8840                                         CHECK_TYPELOAD (cmethod->klass);
8841                                 }
8842
8843                                 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8844
8845                                 /* !marshalbyref is needed to properly handle generic methods + remoting */
8846                                 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
8847                                          MONO_METHOD_IS_FINAL (cmethod)) &&
8848                                         !mono_class_is_marshalbyref (cmethod->klass)) {
8849                                         if (virtual_)
8850                                                 check_this = TRUE;
8851                                         virtual_ = 0;
8852                                 }
8853                         }
8854
8855                         if (pass_imt_from_rgctx) {
8856                                 g_assert (!pass_vtable);
8857
8858                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
8859                                         cmethod, MONO_RGCTX_INFO_METHOD);
8860                         }
8861
8862                         if (check_this)
8863                                 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
8864
8865                         /* Calling virtual generic methods */
8866                         if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
8867                             !(MONO_METHOD_IS_FINAL (cmethod) && 
8868                               cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
8869                             fsig->generic_param_count && 
8870                                 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
8871                                 !cfg->llvm_only) {
8872                                 MonoInst *this_temp, *this_arg_temp, *store;
8873                                 MonoInst *iargs [4];
8874
8875                                 g_assert (fsig->is_inflated);
8876
8877                                 /* Prevent inlining of methods that contain indirect calls */
8878                                 INLINE_FAILURE ("virtual generic call");
8879
8880                                 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8881                                         GSHAREDVT_FAILURE (*ip);
8882
8883                                 if (cfg->backend->have_generalized_imt_trampoline && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
8884                                         g_assert (!imt_arg);
8885                                         if (!context_used)
8886                                                 g_assert (cmethod->is_inflated);
8887                                         imt_arg = emit_get_rgctx_method (cfg, context_used,
8888                                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
8889                                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
8890                                 } else {
8891                                         this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
8892                                         NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
8893                                         MONO_ADD_INS (cfg->cbb, store);
8894
8895                                         /* FIXME: This should be a managed pointer */
8896                                         this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8897
8898                                         EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
8899                                         iargs [1] = emit_get_rgctx_method (cfg, context_used,
8900                                                                                                            cmethod, MONO_RGCTX_INFO_METHOD);
8901                                         EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
8902                                         addr = mono_emit_jit_icall (cfg,
8903                                                                                                 mono_helper_compile_generic_method, iargs);
8904
8905                                         EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
8906
8907                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
8908                                 }
8909
8910                                 goto call_end;
8911                         }
8912
8913                         /*
8914                          * Implement a workaround for the inherent races involved in locking:
8915                          * Monitor.Enter ()
8916                          * try {
8917                          * } finally {
8918                          *    Monitor.Exit ()
8919                          * }
8920                          * If a thread abort happens between the call to Monitor.Enter () and the start of the
8921                          * try block, the Exit () won't be executed, see:
8922                          * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
8923                          * To work around this, we extend such try blocks to include the last x bytes
8924                          * of the Monitor.Enter () call.
8925                          */
8926                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
8927                                 MonoBasicBlock *tbb;
8928
8929                                 GET_BBLOCK (cfg, tbb, ip + 5);
8930                                 /* 
8931                                  * Only extend try blocks with a finally, to avoid catching exceptions thrown
8932                                  * from Monitor.Enter like ArgumentNullException.
8933                                  */
8934                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
8935                                         /* Mark this bblock as needing to be extended */
8936                                         tbb->extend_try_block = TRUE;
8937                                 }
8938                         }
8939
8940                         /* Conversion to a JIT intrinsic */
8941                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
8942                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8943                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
8944                                         emit_widen = FALSE;
8945                                 }
8946                                 goto call_end;
8947                         }
8948                         CHECK_CFG_ERROR;
8949                         
8950                         /* Inlining */
8951                         if ((cfg->opt & MONO_OPT_INLINE) &&
8952                                 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
8953                             mono_method_check_inlining (cfg, cmethod)) {
8954                                 int costs;
8955                                 gboolean always = FALSE;
8956
8957                                 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
8958                                         (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
8959                                         /* Prevent inlining of methods that call wrappers */
8960                                         INLINE_FAILURE ("wrapper call");
8961                                         cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
8962                                         always = TRUE;
8963                                 }
8964
8965                                 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
8966                                 if (costs) {
8967                                         cfg->real_offset += 5;
8968
8969                                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
8970                                                 /* *sp is already set by inline_method */
8971                                                 sp++;
8972                                                 push_res = FALSE;
8973                                         }
8974
8975                                         inline_costs += costs;
8976
8977                                         goto call_end;
8978                                 }
8979                         }
8980
8981                         /* Tail recursion elimination */
8982                         if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
8983                                 gboolean has_vtargs = FALSE;
8984                                 int i;
8985
8986                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
8987                                 INLINE_FAILURE ("tail call");
8988
8989                                 /* keep it simple */
8990                                 for (i =  fsig->param_count - 1; i >= 0; i--) {
8991                                         if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i])) 
8992                                                 has_vtargs = TRUE;
8993                                 }
8994
8995                                 if (!has_vtargs) {
8996                                         if (need_seq_point) {
8997                                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
8998                                                 need_seq_point = FALSE;
8999                                         }
9000                                         for (i = 0; i < n; ++i)
9001                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9002                                         MONO_INST_NEW (cfg, ins, OP_BR);
9003                                         MONO_ADD_INS (cfg->cbb, ins);
9004                                         tblock = start_bblock->out_bb [0];
9005                                         link_bblock (cfg, cfg->cbb, tblock);
9006                                         ins->inst_target_bb = tblock;
9007                                         start_new_bblock = 1;
9008
9009                                         /* skip the CEE_RET, too */
9010                                         if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9011                                                 skip_ret = TRUE;
9012                                         push_res = FALSE;
9013                                         goto call_end;
9014                                 }
9015                         }
9016
9017                         inline_costs += 10 * num_calls++;
9018
9019                         /*
9020                          * Synchronized wrappers.
9021                          * Its hard to determine where to replace a method with its synchronized
9022                          * wrapper without causing an infinite recursion. The current solution is
9023                          * to add the synchronized wrapper in the trampolines, and to
9024                          * change the called method to a dummy wrapper, and resolve that wrapper
9025                          * to the real method in mono_jit_compile_method ().
9026                          */
9027                         if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9028                                 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9029                                 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9030                                         cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9031                         }
9032
9033                         /*
9034                          * Making generic calls out of gsharedvt methods.
9035                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9036                          * patching gshared method addresses into a gsharedvt method.
9037                          */
9038                         if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || mono_class_is_ginst (cmethod->klass)) &&
9039                                 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9040                                 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9041                                 MonoRgctxInfoType info_type;
9042
9043                                 if (virtual_) {
9044                                         //if (mono_class_is_interface (cmethod->klass))
9045                                                 //GSHAREDVT_FAILURE (*ip);
9046                                         // disable for possible remoting calls
9047                                         if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9048                                                 GSHAREDVT_FAILURE (*ip);
9049                                         if (fsig->generic_param_count) {
9050                                                 /* virtual generic call */
9051                                                 g_assert (!imt_arg);
9052                                                 /* Same as the virtual generic case above */
9053                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9054                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
9055                                                 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9056                                                 vtable_arg = NULL;
9057                                         } else if (mono_class_is_interface (cmethod->klass) && !imt_arg) {
9058                                                 /* This can happen when we call a fully instantiated iface method */
9059                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9060                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
9061                                                 vtable_arg = NULL;
9062                                         }
9063                                 }
9064
9065                                 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9066                                         keep_this_alive = sp [0];
9067
9068                                 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9069                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9070                                 else
9071                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9072                                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9073
9074                                 if (cfg->llvm_only) {
9075                                         // FIXME: Avoid initializing vtable_arg
9076                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9077                                 } else {
9078                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9079                                 }
9080                                 goto call_end;
9081                         }
9082
9083                         /* Generic sharing */
9084
9085                         /*
9086                          * Use this if the callee is gsharedvt sharable too, since
9087                          * at runtime we might find an instantiation so the call cannot
9088                          * be patched (the 'no_patch' code path in mini-trampolines.c).
9089                          */
9090                         if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9091                                 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9092                                  !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9093                                 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9094                                  !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9095                                 INLINE_FAILURE ("gshared");
9096
9097                                 g_assert (cfg->gshared && cmethod);
9098                                 g_assert (!addr);
9099
9100                                 /*
9101                                  * We are compiling a call to a
9102                                  * generic method from shared code,
9103                                  * which means that we have to look up
9104                                  * the method in the rgctx and do an
9105                                  * indirect call.
9106                                  */
9107                                 if (fsig->hasthis)
9108                                         MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9109
9110                                 if (cfg->llvm_only) {
9111                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9112                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9113                                         else
9114                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9115                                         // FIXME: Avoid initializing imt_arg/vtable_arg
9116                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9117                                 } else {
9118                                         addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9119                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9120                                 }
9121                                 goto call_end;
9122                         }
9123
9124                         /* Direct calls to icalls */
9125                         if (direct_icall) {
9126                                 MonoMethod *wrapper;
9127                                 int costs;
9128
9129                                 /* Inline the wrapper */
9130                                 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9131
9132                                 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9133                                 g_assert (costs > 0);
9134                                 cfg->real_offset += 5;
9135
9136                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9137                                         /* *sp is already set by inline_method */
9138                                         sp++;
9139                                         push_res = FALSE;
9140                                 }
9141
9142                                 inline_costs += costs;
9143
9144                                 goto call_end;
9145                         }
9146                                         
9147                         /* Array methods */
9148                         if (array_rank) {
9149                                 MonoInst *addr;
9150
9151                                 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */ 
9152                                         MonoInst *val = sp [fsig->param_count];
9153
9154                                         if (val->type == STACK_OBJ) {
9155                                                 MonoInst *iargs [2];
9156
9157                                                 iargs [0] = sp [0];
9158                                                 iargs [1] = val;
9159                                                 
9160                                                 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9161                                         }
9162                                         
9163                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9164                                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9165                                         if (cfg->gen_write_barriers && val->type == STACK_OBJ && !MONO_INS_IS_PCONST_NULL (val))
9166                                                 emit_write_barrier (cfg, addr, val);
9167                                         if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9168                                                 GSHAREDVT_FAILURE (*ip);
9169                                 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9170                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9171
9172                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9173                                 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9174                                         if (!cmethod->klass->element_class->valuetype && !readonly)
9175                                                 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9176                                         CHECK_TYPELOAD (cmethod->klass);
9177                                         
9178                                         readonly = FALSE;
9179                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9180                                         ins = addr;
9181                                 } else {
9182                                         g_assert_not_reached ();
9183                                 }
9184
9185                                 emit_widen = FALSE;
9186                                 goto call_end;
9187                         }
9188
9189                         ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9190                         if (ins)
9191                                 goto call_end;
9192
9193                         /* Tail prefix / tail call optimization */
9194
9195                         /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9196                         /* FIXME: runtime generic context pointer for jumps? */
9197                         /* FIXME: handle this for generic sharing eventually */
9198                         if ((ins_flag & MONO_INST_TAILCALL) &&
9199                                 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9200                                 supported_tail_call = TRUE;
9201
9202                         if (supported_tail_call) {
9203                                 MonoCallInst *call;
9204
9205                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9206                                 INLINE_FAILURE ("tail call");
9207
9208                                 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9209
9210                                 if (cfg->backend->have_op_tail_call) {
9211                                         /* Handle tail calls similarly to normal calls */
9212                                         tail_call = TRUE;
9213                                 } else {
9214                                         emit_instrumentation_call (cfg, mono_profiler_method_leave);
9215
9216                                         MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9217                                         call->tail_call = TRUE;
9218                                         call->method = cmethod;
9219                                         call->signature = mono_method_signature (cmethod);
9220
9221                                         /*
9222                                          * We implement tail calls by storing the actual arguments into the 
9223                                          * argument variables, then emitting a CEE_JMP.
9224                                          */
9225                                         for (i = 0; i < n; ++i) {
9226                                                 /* Prevent argument from being register allocated */
9227                                                 arg_array [i]->flags |= MONO_INST_VOLATILE;
9228                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9229                                         }
9230                                         ins = (MonoInst*)call;
9231                                         ins->inst_p0 = cmethod;
9232                                         ins->inst_p1 = arg_array [0];
9233                                         MONO_ADD_INS (cfg->cbb, ins);
9234                                         link_bblock (cfg, cfg->cbb, end_bblock);
9235                                         start_new_bblock = 1;
9236
9237                                         // FIXME: Eliminate unreachable epilogs
9238
9239                                         /*
9240                                          * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9241                                          * only reachable from this call.
9242                                          */
9243                                         GET_BBLOCK (cfg, tblock, ip + 5);
9244                                         if (tblock == cfg->cbb || tblock->in_count == 0)
9245                                                 skip_ret = TRUE;
9246                                         push_res = FALSE;
9247
9248                                         goto call_end;
9249                                 }
9250                         }
9251
9252                         /*
9253                          * Virtual calls in llvm-only mode.
9254                          */
9255                         if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9256                                 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9257                                 goto call_end;
9258                         }
9259
9260                         /* Common call */
9261                         if (!(cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
9262                                 INLINE_FAILURE ("call");
9263                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9264                                                                                           imt_arg, vtable_arg);
9265
9266                         if (tail_call && !cfg->llvm_only) {
9267                                 link_bblock (cfg, cfg->cbb, end_bblock);
9268                                 start_new_bblock = 1;
9269
9270                                 // FIXME: Eliminate unreachable epilogs
9271
9272                                 /*
9273                                  * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9274                                  * only reachable from this call.
9275                                  */
9276                                 GET_BBLOCK (cfg, tblock, ip + 5);
9277                                 if (tblock == cfg->cbb || tblock->in_count == 0)
9278                                         skip_ret = TRUE;
9279                                 push_res = FALSE;
9280                         }
9281
9282                         call_end:
9283
9284                         /* End of call, INS should contain the result of the call, if any */
9285
9286                         if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
9287                                 g_assert (ins);
9288                                 if (emit_widen)
9289                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9290                                 else
9291                                         *sp++ = ins;
9292                         }
9293
9294                         if (keep_this_alive) {
9295                                 MonoInst *dummy_use;
9296
9297                                 /* See mono_emit_method_call_full () */
9298                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
9299                         }
9300
9301                         if (cfg->llvm_only && cmethod && method_needs_stack_walk (cfg, cmethod)) {
9302                                 /*
9303                                  * Clang can convert these calls to tail calls which screw up the stack
9304                                  * walk. This happens even when the -fno-optimize-sibling-calls
9305                                  * option is passed to clang.
9306                                  * Work around this by emitting a dummy call.
9307                                  */
9308                                 mono_emit_jit_icall (cfg, mono_dummy_jit_icall, NULL);
9309                         }
9310
9311                         CHECK_CFG_EXCEPTION;
9312
9313                         ip += 5;
9314                         if (skip_ret) {
9315                                 g_assert (*ip == CEE_RET);
9316                                 ip += 1;
9317                         }
9318                         ins_flag = 0;
9319                         constrained_class = NULL;
9320                         if (need_seq_point)
9321                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
9322                         break;
9323                 }
9324                 case CEE_RET:
9325                         if (cfg->method != method) {
9326                                 /* return from inlined method */
9327                                 /* 
9328                                  * If in_count == 0, that means the ret is unreachable due to
9329                                  * being preceeded by a throw. In that case, inline_method () will
9330                                  * handle setting the return value 
9331                                  * (test case: test_0_inline_throw ()).
9332                                  */
9333                                 if (return_var && cfg->cbb->in_count) {
9334                                         MonoType *ret_type = mono_method_signature (method)->ret;
9335
9336                                         MonoInst *store;
9337                                         CHECK_STACK (1);
9338                                         --sp;
9339
9340                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9341                                                 UNVERIFIED;
9342
9343                                         //g_assert (returnvar != -1);
9344                                         EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
9345                                         cfg->ret_var_set = TRUE;
9346                                 } 
9347                         } else {
9348                                 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9349
9350                                 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
9351                                         emit_pop_lmf (cfg);
9352
9353                                 if (cfg->ret) {
9354                                         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
9355
9356                                         if (seq_points && !sym_seq_points) {
9357                                                 /* 
9358                                                  * Place a seq point here too even through the IL stack is not
9359                                                  * empty, so a step over on
9360                                                  * call <FOO>
9361                                                  * ret
9362                                                  * will work correctly.
9363                                                  */
9364                                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
9365                                                 MONO_ADD_INS (cfg->cbb, ins);
9366                                         }
9367
9368                                         g_assert (!return_var);
9369                                         CHECK_STACK (1);
9370                                         --sp;
9371
9372                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
9373                                                 UNVERIFIED;
9374
9375                                         emit_setret (cfg, *sp);
9376                                 }
9377                         }
9378                         if (sp != stack_start)
9379                                 UNVERIFIED;
9380                         MONO_INST_NEW (cfg, ins, OP_BR);
9381                         ip++;
9382                         ins->inst_target_bb = end_bblock;
9383                         MONO_ADD_INS (cfg->cbb, ins);
9384                         link_bblock (cfg, cfg->cbb, end_bblock);
9385                         start_new_bblock = 1;
9386                         break;
9387                 case CEE_BR_S:
9388                         CHECK_OPSIZE (2);
9389                         MONO_INST_NEW (cfg, ins, OP_BR);
9390                         ip++;
9391                         target = ip + 1 + (signed char)(*ip);
9392                         ++ip;
9393                         GET_BBLOCK (cfg, tblock, target);
9394                         link_bblock (cfg, cfg->cbb, tblock);
9395                         ins->inst_target_bb = tblock;
9396                         if (sp != stack_start) {
9397                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9398                                 sp = stack_start;
9399                                 CHECK_UNVERIFIABLE (cfg);
9400                         }
9401                         MONO_ADD_INS (cfg->cbb, ins);
9402                         start_new_bblock = 1;
9403                         inline_costs += BRANCH_COST;
9404                         break;
9405                 case CEE_BEQ_S:
9406                 case CEE_BGE_S:
9407                 case CEE_BGT_S:
9408                 case CEE_BLE_S:
9409                 case CEE_BLT_S:
9410                 case CEE_BNE_UN_S:
9411                 case CEE_BGE_UN_S:
9412                 case CEE_BGT_UN_S:
9413                 case CEE_BLE_UN_S:
9414                 case CEE_BLT_UN_S:
9415                         CHECK_OPSIZE (2);
9416                         CHECK_STACK (2);
9417                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
9418                         ip++;
9419                         target = ip + 1 + *(signed char*)ip;
9420                         ip++;
9421
9422                         ADD_BINCOND (NULL);
9423
9424                         sp = stack_start;
9425                         inline_costs += BRANCH_COST;
9426                         break;
9427                 case CEE_BR:
9428                         CHECK_OPSIZE (5);
9429                         MONO_INST_NEW (cfg, ins, OP_BR);
9430                         ip++;
9431
9432                         target = ip + 4 + (gint32)read32(ip);
9433                         ip += 4;
9434                         GET_BBLOCK (cfg, tblock, target);
9435                         link_bblock (cfg, cfg->cbb, tblock);
9436                         ins->inst_target_bb = tblock;
9437                         if (sp != stack_start) {
9438                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9439                                 sp = stack_start;
9440                                 CHECK_UNVERIFIABLE (cfg);
9441                         }
9442
9443                         MONO_ADD_INS (cfg->cbb, ins);
9444
9445                         start_new_bblock = 1;
9446                         inline_costs += BRANCH_COST;
9447                         break;
9448                 case CEE_BRFALSE_S:
9449                 case CEE_BRTRUE_S:
9450                 case CEE_BRFALSE:
9451                 case CEE_BRTRUE: {
9452                         MonoInst *cmp;
9453                         gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
9454                         gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
9455                         guint32 opsize = is_short ? 1 : 4;
9456
9457                         CHECK_OPSIZE (opsize);
9458                         CHECK_STACK (1);
9459                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
9460                                 UNVERIFIED;
9461                         ip ++;
9462                         target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
9463                         ip += opsize;
9464
9465                         sp--;
9466
9467                         GET_BBLOCK (cfg, tblock, target);
9468                         link_bblock (cfg, cfg->cbb, tblock);
9469                         GET_BBLOCK (cfg, tblock, ip);
9470                         link_bblock (cfg, cfg->cbb, tblock);
9471
9472                         if (sp != stack_start) {
9473                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9474                                 CHECK_UNVERIFIABLE (cfg);
9475                         }
9476
9477                         MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
9478                         cmp->sreg1 = sp [0]->dreg;
9479                         type_from_op (cfg, cmp, sp [0], NULL);
9480                         CHECK_TYPE (cmp);
9481
9482 #if SIZEOF_REGISTER == 4
9483                         if (cmp->opcode == OP_LCOMPARE_IMM) {
9484                                 /* Convert it to OP_LCOMPARE */
9485                                 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9486                                 ins->type = STACK_I8;
9487                                 ins->dreg = alloc_dreg (cfg, STACK_I8);
9488                                 ins->inst_l = 0;
9489                                 MONO_ADD_INS (cfg->cbb, ins);
9490                                 cmp->opcode = OP_LCOMPARE;
9491                                 cmp->sreg2 = ins->dreg;
9492                         }
9493 #endif
9494                         MONO_ADD_INS (cfg->cbb, cmp);
9495
9496                         MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
9497                         type_from_op (cfg, ins, sp [0], NULL);
9498                         MONO_ADD_INS (cfg->cbb, ins);
9499                         ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
9500                         GET_BBLOCK (cfg, tblock, target);
9501                         ins->inst_true_bb = tblock;
9502                         GET_BBLOCK (cfg, tblock, ip);
9503                         ins->inst_false_bb = tblock;
9504                         start_new_bblock = 2;
9505
9506                         sp = stack_start;
9507                         inline_costs += BRANCH_COST;
9508                         break;
9509                 }
9510                 case CEE_BEQ:
9511                 case CEE_BGE:
9512                 case CEE_BGT:
9513                 case CEE_BLE:
9514                 case CEE_BLT:
9515                 case CEE_BNE_UN:
9516                 case CEE_BGE_UN:
9517                 case CEE_BGT_UN:
9518                 case CEE_BLE_UN:
9519                 case CEE_BLT_UN:
9520                         CHECK_OPSIZE (5);
9521                         CHECK_STACK (2);
9522                         MONO_INST_NEW (cfg, ins, *ip);
9523                         ip++;
9524                         target = ip + 4 + (gint32)read32(ip);
9525                         ip += 4;
9526
9527                         ADD_BINCOND (NULL);
9528
9529                         sp = stack_start;
9530                         inline_costs += BRANCH_COST;
9531                         break;
9532                 case CEE_SWITCH: {
9533                         MonoInst *src1;
9534                         MonoBasicBlock **targets;
9535                         MonoBasicBlock *default_bblock;
9536                         MonoJumpInfoBBTable *table;
9537                         int offset_reg = alloc_preg (cfg);
9538                         int target_reg = alloc_preg (cfg);
9539                         int table_reg = alloc_preg (cfg);
9540                         int sum_reg = alloc_preg (cfg);
9541                         gboolean use_op_switch;
9542
9543                         CHECK_OPSIZE (5);
9544                         CHECK_STACK (1);
9545                         n = read32 (ip + 1);
9546                         --sp;
9547                         src1 = sp [0];
9548                         if ((src1->type != STACK_I4) && (src1->type != STACK_PTR)) 
9549                                 UNVERIFIED;
9550
9551                         ip += 5;
9552                         CHECK_OPSIZE (n * sizeof (guint32));
9553                         target = ip + n * sizeof (guint32);
9554
9555                         GET_BBLOCK (cfg, default_bblock, target);
9556                         default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
9557
9558                         targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
9559                         for (i = 0; i < n; ++i) {
9560                                 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
9561                                 targets [i] = tblock;
9562                                 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
9563                                 ip += 4;
9564                         }
9565
9566                         if (sp != stack_start) {
9567                                 /* 
9568                                  * Link the current bb with the targets as well, so handle_stack_args
9569                                  * will set their in_stack correctly.
9570                                  */
9571                                 link_bblock (cfg, cfg->cbb, default_bblock);
9572                                 for (i = 0; i < n; ++i)
9573                                         link_bblock (cfg, cfg->cbb, targets [i]);
9574
9575                                 handle_stack_args (cfg, stack_start, sp - stack_start);
9576                                 sp = stack_start;
9577                                 CHECK_UNVERIFIABLE (cfg);
9578
9579                                 /* Undo the links */
9580                                 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
9581                                 for (i = 0; i < n; ++i)
9582                                         mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
9583                         }
9584
9585                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
9586                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
9587
9588                         for (i = 0; i < n; ++i)
9589                                 link_bblock (cfg, cfg->cbb, targets [i]);
9590
9591                         table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
9592                         table->table = targets;
9593                         table->table_size = n;
9594
9595                         use_op_switch = FALSE;
9596 #ifdef TARGET_ARM
9597                         /* ARM implements SWITCH statements differently */
9598                         /* FIXME: Make it use the generic implementation */
9599                         if (!cfg->compile_aot)
9600                                 use_op_switch = TRUE;
9601 #endif
9602
9603                         if (COMPILE_LLVM (cfg))
9604                                 use_op_switch = TRUE;
9605
9606                         cfg->cbb->has_jump_table = 1;
9607
9608                         if (use_op_switch) {
9609                                 MONO_INST_NEW (cfg, ins, OP_SWITCH);
9610                                 ins->sreg1 = src1->dreg;
9611                                 ins->inst_p0 = table;
9612                                 ins->inst_many_bb = targets;
9613                                 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
9614                                 MONO_ADD_INS (cfg->cbb, ins);
9615                         } else {
9616                                 if (sizeof (gpointer) == 8)
9617                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
9618                                 else
9619                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
9620
9621 #if SIZEOF_REGISTER == 8
9622                                 /* The upper word might not be zero, and we add it to a 64 bit address later */
9623                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
9624 #endif
9625
9626                                 if (cfg->compile_aot) {
9627                                         MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
9628                                 } else {
9629                                         MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
9630                                         ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
9631                                         ins->inst_p0 = table;
9632                                         ins->dreg = table_reg;
9633                                         MONO_ADD_INS (cfg->cbb, ins);
9634                                 }
9635
9636                                 /* FIXME: Use load_memindex */
9637                                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
9638                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
9639                                 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
9640                         }
9641                         start_new_bblock = 1;
9642                         inline_costs += (BRANCH_COST * 2);
9643                         break;
9644                 }
9645                 case CEE_LDIND_I1:
9646                 case CEE_LDIND_U1:
9647                 case CEE_LDIND_I2:
9648                 case CEE_LDIND_U2:
9649                 case CEE_LDIND_I4:
9650                 case CEE_LDIND_U4:
9651                 case CEE_LDIND_I8:
9652                 case CEE_LDIND_I:
9653                 case CEE_LDIND_R4:
9654                 case CEE_LDIND_R8:
9655                 case CEE_LDIND_REF:
9656                         CHECK_STACK (1);
9657                         --sp;
9658
9659                         switch (*ip) {
9660                         case CEE_LDIND_R4:
9661                         case CEE_LDIND_R8:
9662                                 dreg = alloc_freg (cfg);
9663                                 break;
9664                         case CEE_LDIND_I8:
9665                                 dreg = alloc_lreg (cfg);
9666                                 break;
9667                         case CEE_LDIND_REF:
9668                                 dreg = alloc_ireg_ref (cfg);
9669                                 break;
9670                         default:
9671                                 dreg = alloc_preg (cfg);
9672                         }
9673
9674                         NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
9675                         ins->type = ldind_type [*ip - CEE_LDIND_I1];
9676                         if (*ip == CEE_LDIND_R4)
9677                                 ins->type = cfg->r4_stack_type;
9678                         ins->flags |= ins_flag;
9679                         MONO_ADD_INS (cfg->cbb, ins);
9680                         *sp++ = ins;
9681                         if (ins_flag & MONO_INST_VOLATILE) {
9682                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9683                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9684                         }
9685                         ins_flag = 0;
9686                         ++ip;
9687                         break;
9688                 case CEE_STIND_REF:
9689                 case CEE_STIND_I1:
9690                 case CEE_STIND_I2:
9691                 case CEE_STIND_I4:
9692                 case CEE_STIND_I8:
9693                 case CEE_STIND_R4:
9694                 case CEE_STIND_R8:
9695                 case CEE_STIND_I:
9696                         CHECK_STACK (2);
9697                         sp -= 2;
9698
9699                         if (ins_flag & MONO_INST_VOLATILE) {
9700                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
9701                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
9702                         }
9703
9704                         NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
9705                         ins->flags |= ins_flag;
9706                         ins_flag = 0;
9707
9708                         MONO_ADD_INS (cfg->cbb, ins);
9709
9710                         if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !MONO_INS_IS_PCONST_NULL (sp [1]))
9711                                 emit_write_barrier (cfg, sp [0], sp [1]);
9712
9713                         inline_costs += 1;
9714                         ++ip;
9715                         break;
9716
9717                 case CEE_MUL:
9718                         CHECK_STACK (2);
9719
9720                         MONO_INST_NEW (cfg, ins, (*ip));
9721                         sp -= 2;
9722                         ins->sreg1 = sp [0]->dreg;
9723                         ins->sreg2 = sp [1]->dreg;
9724                         type_from_op (cfg, ins, sp [0], sp [1]);
9725                         CHECK_TYPE (ins);
9726                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9727
9728                         /* Use the immediate opcodes if possible */
9729                         if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
9730                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9731                                 if (imm_opcode != -1) {
9732                                         ins->opcode = imm_opcode;
9733                                         ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
9734                                         ins->sreg2 = -1;
9735
9736                                         NULLIFY_INS (sp [1]);
9737                                 }
9738                         }
9739
9740                         MONO_ADD_INS ((cfg)->cbb, (ins));
9741
9742                         *sp++ = mono_decompose_opcode (cfg, ins);
9743                         ip++;
9744                         break;
9745                 case CEE_ADD:
9746                 case CEE_SUB:
9747                 case CEE_DIV:
9748                 case CEE_DIV_UN:
9749                 case CEE_REM:
9750                 case CEE_REM_UN:
9751                 case CEE_AND:
9752                 case CEE_OR:
9753                 case CEE_XOR:
9754                 case CEE_SHL:
9755                 case CEE_SHR:
9756                 case CEE_SHR_UN:
9757                         CHECK_STACK (2);
9758
9759                         MONO_INST_NEW (cfg, ins, (*ip));
9760                         sp -= 2;
9761                         ins->sreg1 = sp [0]->dreg;
9762                         ins->sreg2 = sp [1]->dreg;
9763                         type_from_op (cfg, ins, sp [0], sp [1]);
9764                         CHECK_TYPE (ins);
9765                         add_widen_op (cfg, ins, &sp [0], &sp [1]);
9766                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
9767
9768                         /* FIXME: Pass opcode to is_inst_imm */
9769
9770                         /* Use the immediate opcodes if possible */
9771                         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)) {
9772                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
9773                                 if (imm_opcode != -1) {
9774                                         ins->opcode = imm_opcode;
9775                                         if (sp [1]->opcode == OP_I8CONST) {
9776 #if SIZEOF_REGISTER == 8
9777                                                 ins->inst_imm = sp [1]->inst_l;
9778 #else
9779                                                 ins->inst_ls_word = sp [1]->inst_ls_word;
9780                                                 ins->inst_ms_word = sp [1]->inst_ms_word;
9781 #endif
9782                                         }
9783                                         else
9784                                                 ins->inst_imm = (gssize)(sp [1]->inst_c0);
9785                                         ins->sreg2 = -1;
9786
9787                                         /* Might be followed by an instruction added by add_widen_op */
9788                                         if (sp [1]->next == NULL)
9789                                                 NULLIFY_INS (sp [1]);
9790                                 }
9791                         }
9792                         MONO_ADD_INS ((cfg)->cbb, (ins));
9793
9794                         *sp++ = mono_decompose_opcode (cfg, ins);
9795                         ip++;
9796                         break;
9797                 case CEE_NEG:
9798                 case CEE_NOT:
9799                 case CEE_CONV_I1:
9800                 case CEE_CONV_I2:
9801                 case CEE_CONV_I4:
9802                 case CEE_CONV_R4:
9803                 case CEE_CONV_R8:
9804                 case CEE_CONV_U4:
9805                 case CEE_CONV_I8:
9806                 case CEE_CONV_U8:
9807                 case CEE_CONV_OVF_I8:
9808                 case CEE_CONV_OVF_U8:
9809                 case CEE_CONV_R_UN:
9810                         CHECK_STACK (1);
9811
9812                         /* Special case this earlier so we have long constants in the IR */
9813                         if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
9814                                 int data = sp [-1]->inst_c0;
9815                                 sp [-1]->opcode = OP_I8CONST;
9816                                 sp [-1]->type = STACK_I8;
9817 #if SIZEOF_REGISTER == 8
9818                                 if ((*ip) == CEE_CONV_U8)
9819                                         sp [-1]->inst_c0 = (guint32)data;
9820                                 else
9821                                         sp [-1]->inst_c0 = data;
9822 #else
9823                                 sp [-1]->inst_ls_word = data;
9824                                 if ((*ip) == CEE_CONV_U8)
9825                                         sp [-1]->inst_ms_word = 0;
9826                                 else
9827                                         sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
9828 #endif
9829                                 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
9830                         }
9831                         else {
9832                                 ADD_UNOP (*ip);
9833                         }
9834                         ip++;
9835                         break;
9836                 case CEE_CONV_OVF_I4:
9837                 case CEE_CONV_OVF_I1:
9838                 case CEE_CONV_OVF_I2:
9839                 case CEE_CONV_OVF_I:
9840                 case CEE_CONV_OVF_U:
9841                         CHECK_STACK (1);
9842
9843                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9844                                 ADD_UNOP (CEE_CONV_OVF_I8);
9845                                 ADD_UNOP (*ip);
9846                         } else {
9847                                 ADD_UNOP (*ip);
9848                         }
9849                         ip++;
9850                         break;
9851                 case CEE_CONV_OVF_U1:
9852                 case CEE_CONV_OVF_U2:
9853                 case CEE_CONV_OVF_U4:
9854                         CHECK_STACK (1);
9855
9856                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
9857                                 ADD_UNOP (CEE_CONV_OVF_U8);
9858                                 ADD_UNOP (*ip);
9859                         } else {
9860                                 ADD_UNOP (*ip);
9861                         }
9862                         ip++;
9863                         break;
9864                 case CEE_CONV_OVF_I1_UN:
9865                 case CEE_CONV_OVF_I2_UN:
9866                 case CEE_CONV_OVF_I4_UN:
9867                 case CEE_CONV_OVF_I8_UN:
9868                 case CEE_CONV_OVF_U1_UN:
9869                 case CEE_CONV_OVF_U2_UN:
9870                 case CEE_CONV_OVF_U4_UN:
9871                 case CEE_CONV_OVF_U8_UN:
9872                 case CEE_CONV_OVF_I_UN:
9873                 case CEE_CONV_OVF_U_UN:
9874                 case CEE_CONV_U2:
9875                 case CEE_CONV_U1:
9876                 case CEE_CONV_I:
9877                 case CEE_CONV_U:
9878                         CHECK_STACK (1);
9879                         ADD_UNOP (*ip);
9880                         CHECK_CFG_EXCEPTION;
9881                         ip++;
9882                         break;
9883                 case CEE_ADD_OVF:
9884                 case CEE_ADD_OVF_UN:
9885                 case CEE_MUL_OVF:
9886                 case CEE_MUL_OVF_UN:
9887                 case CEE_SUB_OVF:
9888                 case CEE_SUB_OVF_UN:
9889                         CHECK_STACK (2);
9890                         ADD_BINOP (*ip);
9891                         ip++;
9892                         break;
9893                 case CEE_CPOBJ:
9894                         GSHAREDVT_FAILURE (*ip);
9895                         CHECK_OPSIZE (5);
9896                         CHECK_STACK (2);
9897                         token = read32 (ip + 1);
9898                         klass = mini_get_class (method, token, generic_context);
9899                         CHECK_TYPELOAD (klass);
9900                         sp -= 2;
9901                         if (generic_class_is_reference_type (cfg, klass)) {
9902                                 MonoInst *store, *load;
9903                                 int dreg = alloc_ireg_ref (cfg);
9904
9905                                 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
9906                                 load->flags |= ins_flag;
9907                                 MONO_ADD_INS (cfg->cbb, load);
9908
9909                                 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
9910                                 store->flags |= ins_flag;
9911                                 MONO_ADD_INS (cfg->cbb, store);
9912
9913                                 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
9914                                         emit_write_barrier (cfg, sp [0], sp [1]);
9915                         } else {
9916                                 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
9917                         }
9918                         ins_flag = 0;
9919                         ip += 5;
9920                         break;
9921                 case CEE_LDOBJ: {
9922                         int loc_index = -1;
9923                         int stloc_len = 0;
9924
9925                         CHECK_OPSIZE (5);
9926                         CHECK_STACK (1);
9927                         --sp;
9928                         token = read32 (ip + 1);
9929                         klass = mini_get_class (method, token, generic_context);
9930                         CHECK_TYPELOAD (klass);
9931
9932                         /* Optimize the common ldobj+stloc combination */
9933                         switch (ip [5]) {
9934                         case CEE_STLOC_S:
9935                                 loc_index = ip [6];
9936                                 stloc_len = 2;
9937                                 break;
9938                         case CEE_STLOC_0:
9939                         case CEE_STLOC_1:
9940                         case CEE_STLOC_2:
9941                         case CEE_STLOC_3:
9942                                 loc_index = ip [5] - CEE_STLOC_0;
9943                                 stloc_len = 1;
9944                                 break;
9945                         default:
9946                                 break;
9947                         }
9948
9949                         if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
9950                                 CHECK_LOCAL (loc_index);
9951
9952                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
9953                                 ins->dreg = cfg->locals [loc_index]->dreg;
9954                                 ins->flags |= ins_flag;
9955                                 ip += 5;
9956                                 ip += stloc_len;
9957                                 if (ins_flag & MONO_INST_VOLATILE) {
9958                                         /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9959                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9960                                 }
9961                                 ins_flag = 0;
9962                                 break;
9963                         }
9964
9965                         /* Optimize the ldobj+stobj combination */
9966                         /* The reference case ends up being a load+store anyway */
9967                         /* Skip this if the operation is volatile. */
9968                         if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 5) && read32 (ip + 6) == token) && !generic_class_is_reference_type (cfg, klass) && !(ins_flag & MONO_INST_VOLATILE)) {
9969                                 CHECK_STACK (1);
9970
9971                                 sp --;
9972
9973                                 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
9974
9975                                 ip += 5 + 5;
9976                                 ins_flag = 0;
9977                                 break;
9978                         }
9979
9980                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
9981                         ins->flags |= ins_flag;
9982                         *sp++ = ins;
9983
9984                         if (ins_flag & MONO_INST_VOLATILE) {
9985                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
9986                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
9987                         }
9988
9989                         ip += 5;
9990                         ins_flag = 0;
9991                         inline_costs += 1;
9992                         break;
9993                 }
9994                 case CEE_LDSTR:
9995                         CHECK_STACK_OVF (1);
9996                         CHECK_OPSIZE (5);
9997                         n = read32 (ip + 1);
9998
9999                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10000                                 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10001                                 ins->type = STACK_OBJ;
10002                                 *sp = ins;
10003                         }
10004                         else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10005                                 MonoInst *iargs [1];
10006                                 char *str = (char *)mono_method_get_wrapper_data (method, n);
10007
10008                                 if (cfg->compile_aot)
10009                                         EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10010                                 else
10011                                         EMIT_NEW_PCONST (cfg, iargs [0], str);
10012                                 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10013                         } else {
10014                                 if (cfg->opt & MONO_OPT_SHARED) {
10015                                         MonoInst *iargs [3];
10016
10017                                         if (cfg->compile_aot) {
10018                                                 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10019                                         }
10020                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10021                                         EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10022                                         EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10023                                         *sp = mono_emit_jit_icall (cfg, ves_icall_mono_ldstr, iargs);
10024                                         mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10025                                         CHECK_CFG_ERROR;
10026                                 } else {
10027                                         if (cfg->cbb->out_of_line) {
10028                                                 MonoInst *iargs [2];
10029
10030                                                 if (image == mono_defaults.corlib) {
10031                                                         /* 
10032                                                          * Avoid relocations in AOT and save some space by using a 
10033                                                          * version of helper_ldstr specialized to mscorlib.
10034                                                          */
10035                                                         EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10036                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10037                                                 } else {
10038                                                         /* Avoid creating the string object */
10039                                                         EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10040                                                         EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10041                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10042                                                 }
10043                                         } 
10044                                         else
10045                                         if (cfg->compile_aot) {
10046                                                 NEW_LDSTRCONST (cfg, ins, image, n);
10047                                                 *sp = ins;
10048                                                 MONO_ADD_INS (cfg->cbb, ins);
10049                                         } 
10050                                         else {
10051                                                 NEW_PCONST (cfg, ins, NULL);
10052                                                 ins->type = STACK_OBJ;
10053                                                 ins->inst_p0 = mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10054                                                 CHECK_CFG_ERROR;
10055                                                 
10056                                                 if (!ins->inst_p0)
10057                                                         OUT_OF_MEMORY_FAILURE;
10058
10059                                                 *sp = ins;
10060                                                 MONO_ADD_INS (cfg->cbb, ins);
10061                                         }
10062                                 }
10063                         }
10064
10065                         sp++;
10066                         ip += 5;
10067                         break;
10068                 case CEE_NEWOBJ: {
10069                         MonoInst *iargs [2];
10070                         MonoMethodSignature *fsig;
10071                         MonoInst this_ins;
10072                         MonoInst *alloc;
10073                         MonoInst *vtable_arg = NULL;
10074
10075                         CHECK_OPSIZE (5);
10076                         token = read32 (ip + 1);
10077                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10078                         CHECK_CFG_ERROR;
10079
10080                         fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10081                         CHECK_CFG_ERROR;
10082
10083                         mono_save_token_info (cfg, image, token, cmethod);
10084
10085                         if (!mono_class_init (cmethod->klass))
10086                                 TYPE_LOAD_ERROR (cmethod->klass);
10087
10088                         context_used = mini_method_check_context_used (cfg, cmethod);
10089
10090                         if (mono_security_core_clr_enabled ())
10091                                 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10092
10093                         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)) {
10094                                 emit_class_init (cfg, cmethod->klass);
10095                                 CHECK_TYPELOAD (cmethod->klass);
10096                         }
10097
10098                         /*
10099                         if (cfg->gsharedvt) {
10100                                 if (mini_is_gsharedvt_variable_signature (sig))
10101                                         GSHAREDVT_FAILURE (*ip);
10102                         }
10103                         */
10104
10105                         n = fsig->param_count;
10106                         CHECK_STACK (n);
10107
10108                         /* 
10109                          * Generate smaller code for the common newobj <exception> instruction in
10110                          * argument checking code.
10111                          */
10112                         if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10113                                 is_exception_class (cmethod->klass) && n <= 2 &&
10114                                 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) && 
10115                                 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10116                                 MonoInst *iargs [3];
10117
10118                                 sp -= n;
10119
10120                                 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10121                                 switch (n) {
10122                                 case 0:
10123                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10124                                         break;
10125                                 case 1:
10126                                         iargs [1] = sp [0];
10127                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10128                                         break;
10129                                 case 2:
10130                                         iargs [1] = sp [0];
10131                                         iargs [2] = sp [1];
10132                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10133                                         break;
10134                                 default:
10135                                         g_assert_not_reached ();
10136                                 }
10137
10138                                 ip += 5;
10139                                 inline_costs += 5;
10140                                 break;
10141                         }
10142
10143                         /* move the args to allow room for 'this' in the first position */
10144                         while (n--) {
10145                                 --sp;
10146                                 sp [1] = sp [0];
10147                         }
10148
10149                         /* check_call_signature () requires sp[0] to be set */
10150                         this_ins.type = STACK_OBJ;
10151                         sp [0] = &this_ins;
10152                         if (check_call_signature (cfg, fsig, sp))
10153                                 UNVERIFIED;
10154
10155                         iargs [0] = NULL;
10156
10157                         if (mini_class_is_system_array (cmethod->klass)) {
10158                                 *sp = emit_get_rgctx_method (cfg, context_used,
10159                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
10160
10161                                 /* Avoid varargs in the common case */
10162                                 if (fsig->param_count == 1)
10163                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10164                                 else if (fsig->param_count == 2)
10165                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10166                                 else if (fsig->param_count == 3)
10167                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10168                                 else if (fsig->param_count == 4)
10169                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10170                                 else
10171                                         alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10172                         } else if (cmethod->string_ctor) {
10173                                 g_assert (!context_used);
10174                                 g_assert (!vtable_arg);
10175                                 /* we simply pass a null pointer */
10176                                 EMIT_NEW_PCONST (cfg, *sp, NULL); 
10177                                 /* now call the string ctor */
10178                                 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10179                         } else {
10180                                 if (cmethod->klass->valuetype) {
10181                                         iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10182                                         emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10183                                         EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10184
10185                                         alloc = NULL;
10186
10187                                         /* 
10188                                          * The code generated by mini_emit_virtual_call () expects
10189                                          * iargs [0] to be a boxed instance, but luckily the vcall
10190                                          * will be transformed into a normal call there.
10191                                          */
10192                                 } else if (context_used) {
10193                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10194                                         *sp = alloc;
10195                                 } else {
10196                                         MonoVTable *vtable = NULL;
10197
10198                                         if (!cfg->compile_aot)
10199                                                 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10200                                         CHECK_TYPELOAD (cmethod->klass);
10201
10202                                         /*
10203                                          * TypeInitializationExceptions thrown from the mono_runtime_class_init
10204                                          * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10205                                          * As a workaround, we call class cctors before allocating objects.
10206                                          */
10207                                         if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10208                                                 emit_class_init (cfg, cmethod->klass);
10209                                                 if (cfg->verbose_level > 2)
10210                                                         printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10211                                                 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10212                                         }
10213
10214                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10215                                         *sp = alloc;
10216                                 }
10217                                 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10218
10219                                 if (alloc)
10220                                         MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10221
10222                                 /* Now call the actual ctor */
10223                                 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10224                                 CHECK_CFG_EXCEPTION;
10225                         }
10226
10227                         if (alloc == NULL) {
10228                                 /* Valuetype */
10229                                 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10230                                 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10231                                 *sp++= ins;
10232                         } else {
10233                                 *sp++ = alloc;
10234                         }
10235                         
10236                         ip += 5;
10237                         inline_costs += 5;
10238                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10239                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10240                         break;
10241                 }
10242                 case CEE_CASTCLASS:
10243                 case CEE_ISINST: {
10244                         CHECK_STACK (1);
10245                         --sp;
10246                         CHECK_OPSIZE (5);
10247                         token = read32 (ip + 1);
10248                         klass = mini_get_class (method, token, generic_context);
10249                         CHECK_TYPELOAD (klass);
10250                         if (sp [0]->type != STACK_OBJ)
10251                                 UNVERIFIED;
10252
10253                         MONO_INST_NEW (cfg, ins, *ip == CEE_ISINST ? OP_ISINST : OP_CASTCLASS);
10254                         ins->dreg = alloc_preg (cfg);
10255                         ins->sreg1 = (*sp)->dreg;
10256                         ins->klass = klass;
10257                         ins->type = STACK_OBJ;
10258                         MONO_ADD_INS (cfg->cbb, ins);
10259
10260                         CHECK_CFG_EXCEPTION;
10261                         *sp++ = ins;
10262                         ip += 5;
10263
10264                         cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10265                         break;
10266                 }
10267                 case CEE_UNBOX_ANY: {
10268                         MonoInst *res, *addr;
10269
10270                         CHECK_STACK (1);
10271                         --sp;
10272                         CHECK_OPSIZE (5);
10273                         token = read32 (ip + 1);
10274                         klass = mini_get_class (method, token, generic_context);
10275                         CHECK_TYPELOAD (klass);
10276
10277                         mono_save_token_info (cfg, image, token, klass);
10278
10279                         context_used = mini_class_check_context_used (cfg, klass);
10280
10281                         if (mini_is_gsharedvt_klass (klass)) {
10282                                 res = handle_unbox_gsharedvt (cfg, klass, *sp);
10283                                 inline_costs += 2;
10284                         } else if (generic_class_is_reference_type (cfg, klass)) {
10285                                 if (MONO_INS_IS_PCONST_NULL (*sp)) {
10286                                         EMIT_NEW_PCONST (cfg, res, NULL);
10287                                         res->type = STACK_OBJ;
10288                                 } else {
10289                                         MONO_INST_NEW (cfg, res, OP_CASTCLASS);
10290                                         res->dreg = alloc_preg (cfg);
10291                                         res->sreg1 = (*sp)->dreg;
10292                                         res->klass = klass;
10293                                         res->type = STACK_OBJ;
10294                                         MONO_ADD_INS (cfg->cbb, res);
10295                                         cfg->flags |= MONO_CFG_HAS_TYPE_CHECK;
10296                                 }
10297                         } else if (mono_class_is_nullable (klass)) {
10298                                 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
10299                         } else {
10300                                 addr = handle_unbox (cfg, klass, sp, context_used);
10301                                 /* LDOBJ */
10302                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
10303                                 res = ins;
10304                                 inline_costs += 2;
10305                         }
10306
10307                         *sp ++ = res;
10308                         ip += 5;
10309                         break;
10310                 }
10311                 case CEE_BOX: {
10312                         MonoInst *val;
10313                         MonoClass *enum_class;
10314                         MonoMethod *has_flag;
10315
10316                         CHECK_STACK (1);
10317                         --sp;
10318                         val = *sp;
10319                         CHECK_OPSIZE (5);
10320                         token = read32 (ip + 1);
10321                         klass = mini_get_class (method, token, generic_context);
10322                         CHECK_TYPELOAD (klass);
10323
10324                         mono_save_token_info (cfg, image, token, klass);
10325
10326                         context_used = mini_class_check_context_used (cfg, klass);
10327
10328                         if (generic_class_is_reference_type (cfg, klass)) {
10329                                 *sp++ = val;
10330                                 ip += 5;
10331                                 break;
10332                         }
10333
10334                         if (klass == mono_defaults.void_class)
10335                                 UNVERIFIED;
10336                         if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
10337                                 UNVERIFIED;
10338                         /* frequent check in generic code: box (struct), brtrue */
10339
10340                         /*
10341                          * Look for:
10342                          *
10343                          *   <push int/long ptr>
10344                          *   <push int/long>
10345                          *   box MyFlags
10346                          *   constrained. MyFlags
10347                          *   callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
10348                          *
10349                          * If we find this sequence and the operand types on box and constrained
10350                          * are equal, we can emit a specialized instruction sequence instead of
10351                          * the very slow HasFlag () call.
10352                          */
10353                         if ((cfg->opt & MONO_OPT_INTRINS) &&
10354                             /* Cheap checks first. */
10355                             ip + 5 + 6 + 5 < end &&
10356                             ip [5] == CEE_PREFIX1 &&
10357                             ip [6] == CEE_CONSTRAINED_ &&
10358                             ip [11] == CEE_CALLVIRT &&
10359                             ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
10360                             mono_class_is_enum (klass) &&
10361                             (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
10362                             (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
10363                             has_flag->klass == mono_defaults.enum_class &&
10364                             !strcmp (has_flag->name, "HasFlag") &&
10365                             has_flag->signature->hasthis &&
10366                             has_flag->signature->param_count == 1) {
10367                                 CHECK_TYPELOAD (enum_class);
10368
10369                                 if (enum_class == klass) {
10370                                         MonoInst *enum_this, *enum_flag;
10371
10372                                         ip += 5 + 6 + 5;
10373                                         --sp;
10374
10375                                         enum_this = sp [0];
10376                                         enum_flag = sp [1];
10377
10378                                         *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
10379                                         break;
10380                                 }
10381                         }
10382
10383                         // FIXME: LLVM can't handle the inconsistent bb linking
10384                         if (!mono_class_is_nullable (klass) &&
10385                                 !mini_is_gsharedvt_klass (klass) &&
10386                                 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
10387                                 (ip [5] == CEE_BRTRUE || 
10388                                  ip [5] == CEE_BRTRUE_S ||
10389                                  ip [5] == CEE_BRFALSE ||
10390                                  ip [5] == CEE_BRFALSE_S)) {
10391                                 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
10392                                 int dreg;
10393                                 MonoBasicBlock *true_bb, *false_bb;
10394
10395                                 ip += 5;
10396
10397                                 if (cfg->verbose_level > 3) {
10398                                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
10399                                         printf ("<box+brtrue opt>\n");
10400                                 }
10401
10402                                 switch (*ip) {
10403                                 case CEE_BRTRUE_S:
10404                                 case CEE_BRFALSE_S:
10405                                         CHECK_OPSIZE (2);
10406                                         ip++;
10407                                         target = ip + 1 + (signed char)(*ip);
10408                                         ip++;
10409                                         break;
10410                                 case CEE_BRTRUE:
10411                                 case CEE_BRFALSE:
10412                                         CHECK_OPSIZE (5);
10413                                         ip++;
10414                                         target = ip + 4 + (gint)(read32 (ip));
10415                                         ip += 4;
10416                                         break;
10417                                 default:
10418                                         g_assert_not_reached ();
10419                                 }
10420
10421                                 /* 
10422                                  * We need to link both bblocks, since it is needed for handling stack
10423                                  * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
10424                                  * Branching to only one of them would lead to inconsistencies, so
10425                                  * generate an ICONST+BRTRUE, the branch opts will get rid of them.
10426                                  */
10427                                 GET_BBLOCK (cfg, true_bb, target);
10428                                 GET_BBLOCK (cfg, false_bb, ip);
10429
10430                                 mono_link_bblock (cfg, cfg->cbb, true_bb);
10431                                 mono_link_bblock (cfg, cfg->cbb, false_bb);
10432
10433                                 if (sp != stack_start) {
10434                                         handle_stack_args (cfg, stack_start, sp - stack_start);
10435                                         sp = stack_start;
10436                                         CHECK_UNVERIFIABLE (cfg);
10437                                 }
10438
10439                                 if (COMPILE_LLVM (cfg)) {
10440                                         dreg = alloc_ireg (cfg);
10441                                         MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
10442                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
10443
10444                                         MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
10445                                 } else {
10446                                         /* The JIT can't eliminate the iconst+compare */
10447                                         MONO_INST_NEW (cfg, ins, OP_BR);
10448                                         ins->inst_target_bb = is_true ? true_bb : false_bb;
10449                                         MONO_ADD_INS (cfg->cbb, ins);
10450                                 }
10451
10452                                 start_new_bblock = 1;
10453                                 break;
10454                         }
10455
10456                         *sp++ = handle_box (cfg, val, klass, context_used);
10457
10458                         CHECK_CFG_EXCEPTION;
10459                         ip += 5;
10460                         inline_costs += 1;
10461                         break;
10462                 }
10463                 case CEE_UNBOX: {
10464                         CHECK_STACK (1);
10465                         --sp;
10466                         CHECK_OPSIZE (5);
10467                         token = read32 (ip + 1);
10468                         klass = mini_get_class (method, token, generic_context);
10469                         CHECK_TYPELOAD (klass);
10470
10471                         mono_save_token_info (cfg, image, token, klass);
10472
10473                         context_used = mini_class_check_context_used (cfg, klass);
10474
10475                         if (mono_class_is_nullable (klass)) {
10476                                 MonoInst *val;
10477
10478                                 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
10479                                 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
10480
10481                                 *sp++= ins;
10482                         } else {
10483                                 ins = handle_unbox (cfg, klass, sp, context_used);
10484                                 *sp++ = ins;
10485                         }
10486                         ip += 5;
10487                         inline_costs += 2;
10488                         break;
10489                 }
10490                 case CEE_LDFLD:
10491                 case CEE_LDFLDA:
10492                 case CEE_STFLD:
10493                 case CEE_LDSFLD:
10494                 case CEE_LDSFLDA:
10495                 case CEE_STSFLD: {
10496                         MonoClassField *field;
10497 #ifndef DISABLE_REMOTING
10498                         int costs;
10499 #endif
10500                         guint foffset;
10501                         gboolean is_instance;
10502                         int op;
10503                         gpointer addr = NULL;
10504                         gboolean is_special_static;
10505                         MonoType *ftype;
10506                         MonoInst *store_val = NULL;
10507                         MonoInst *thread_ins;
10508
10509                         op = *ip;
10510                         is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
10511                         if (is_instance) {
10512                                 if (op == CEE_STFLD) {
10513                                         CHECK_STACK (2);
10514                                         sp -= 2;
10515                                         store_val = sp [1];
10516                                 } else {
10517                                         CHECK_STACK (1);
10518                                         --sp;
10519                                 }
10520                                 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
10521                                         UNVERIFIED;
10522                                 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
10523                                         UNVERIFIED;
10524                         } else {
10525                                 if (op == CEE_STSFLD) {
10526                                         CHECK_STACK (1);
10527                                         sp--;
10528                                         store_val = sp [0];
10529                                 }
10530                         }
10531
10532                         CHECK_OPSIZE (5);
10533                         token = read32 (ip + 1);
10534                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
10535                                 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
10536                                 klass = field->parent;
10537                         }
10538                         else {
10539                                 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
10540                                 CHECK_CFG_ERROR;
10541                         }
10542                         if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
10543                                 FIELD_ACCESS_FAILURE (method, field);
10544                         mono_class_init (klass);
10545
10546                         /* if the class is Critical then transparent code cannot access it's fields */
10547                         if (!is_instance && mono_security_core_clr_enabled ())
10548                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
10549
10550                         /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
10551                            any visible *instance* field  (in fact there's a single case for a static field in Marshal) XXX
10552                         if (mono_security_core_clr_enabled ())
10553                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
10554                         */
10555
10556                         ftype = mono_field_get_type (field);
10557
10558                         /*
10559                          * LDFLD etc. is usable on static fields as well, so convert those cases to
10560                          * the static case.
10561                          */
10562                         if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
10563                                 switch (op) {
10564                                 case CEE_LDFLD:
10565                                         op = CEE_LDSFLD;
10566                                         break;
10567                                 case CEE_STFLD:
10568                                         op = CEE_STSFLD;
10569                                         break;
10570                                 case CEE_LDFLDA:
10571                                         op = CEE_LDSFLDA;
10572                                         break;
10573                                 default:
10574                                         g_assert_not_reached ();
10575                                 }
10576                                 is_instance = FALSE;
10577                         }
10578
10579                         context_used = mini_class_check_context_used (cfg, klass);
10580
10581                         /* INSTANCE CASE */
10582
10583                         foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
10584                         if (op == CEE_STFLD) {
10585                                 if (target_type_is_incompatible (cfg, field->type, sp [1]))
10586                                         UNVERIFIED;
10587 #ifndef DISABLE_REMOTING
10588                                 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
10589                                         MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
10590                                         MonoInst *iargs [5];
10591
10592                                         GSHAREDVT_FAILURE (op);
10593
10594                                         iargs [0] = sp [0];
10595                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10596                                         EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10597                                         EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : 
10598                                                     field->offset);
10599                                         iargs [4] = sp [1];
10600
10601                                         if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10602                                                 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), 
10603                                                                                            iargs, ip, cfg->real_offset, TRUE);
10604                                                 CHECK_CFG_EXCEPTION;
10605                                                 g_assert (costs > 0);
10606                                                       
10607                                                 cfg->real_offset += 5;
10608
10609                                                 inline_costs += costs;
10610                                         } else {
10611                                                 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
10612                                         }
10613                                 } else
10614 #endif
10615                                 {
10616                                         MonoInst *store, *wbarrier_ptr_ins = NULL;
10617
10618                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10619
10620                                         if (ins_flag & MONO_INST_VOLATILE) {
10621                                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10622                                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10623                                         }
10624
10625                                         if (mini_is_gsharedvt_klass (klass)) {
10626                                                 MonoInst *offset_ins;
10627
10628                                                 context_used = mini_class_check_context_used (cfg, klass);
10629
10630                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10631                                                 /* The value is offset by 1 */
10632                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10633                                                 dreg = alloc_ireg_mp (cfg);
10634                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10635                                                 wbarrier_ptr_ins = ins;
10636                                                 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
10637                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
10638                                         } else {
10639                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
10640                                         }
10641                                         if (sp [0]->opcode != OP_LDADDR)
10642                                                 store->flags |= MONO_INST_FAULT;
10643
10644                                         if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !MONO_INS_IS_PCONST_NULL (sp [1])) {
10645                                                 if (mini_is_gsharedvt_klass (klass)) {
10646                                                         g_assert (wbarrier_ptr_ins);
10647                                                         emit_write_barrier (cfg, wbarrier_ptr_ins, sp [1]);
10648                                                 } else {
10649                                                         /* insert call to write barrier */
10650                                                         MonoInst *ptr;
10651                                                         int dreg;
10652
10653                                                         dreg = alloc_ireg_mp (cfg);
10654                                                         EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10655                                                         emit_write_barrier (cfg, ptr, sp [1]);
10656                                                 }
10657                                         }
10658
10659                                         store->flags |= ins_flag;
10660                                 }
10661                                 ins_flag = 0;
10662                                 ip += 5;
10663                                 break;
10664                         }
10665
10666 #ifndef DISABLE_REMOTING
10667                         if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
10668                                 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type); 
10669                                 MonoInst *iargs [4];
10670
10671                                 GSHAREDVT_FAILURE (op);
10672
10673                                 iargs [0] = sp [0];
10674                                 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
10675                                 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
10676                                 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
10677                                 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
10678                                         costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), 
10679                                                                                    iargs, ip, cfg->real_offset, TRUE);
10680                                         CHECK_CFG_EXCEPTION;
10681                                         g_assert (costs > 0);
10682                                                       
10683                                         cfg->real_offset += 5;
10684
10685                                         *sp++ = iargs [0];
10686
10687                                         inline_costs += costs;
10688                                 } else {
10689                                         ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
10690                                         *sp++ = ins;
10691                                 }
10692                         } else 
10693 #endif
10694                         if (is_instance) {
10695                                 if (sp [0]->type == STACK_VTYPE) {
10696                                         MonoInst *var;
10697
10698                                         /* Have to compute the address of the variable */
10699
10700                                         var = get_vreg_to_inst (cfg, sp [0]->dreg);
10701                                         if (!var)
10702                                                 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
10703                                         else
10704                                                 g_assert (var->klass == klass);
10705                                         
10706                                         EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
10707                                         sp [0] = ins;
10708                                 }
10709
10710                                 if (op == CEE_LDFLDA) {
10711                                         if (sp [0]->type == STACK_OBJ) {
10712                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
10713                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
10714                                         }
10715
10716                                         dreg = alloc_ireg_mp (cfg);
10717
10718                                         if (mini_is_gsharedvt_klass (klass)) {
10719                                                 MonoInst *offset_ins;
10720
10721                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10722                                                 /* The value is offset by 1 */
10723                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10724                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10725                                         } else {
10726                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
10727                                         }
10728                                         ins->klass = mono_class_from_mono_type (field->type);
10729                                         ins->type = STACK_MP;
10730                                         *sp++ = ins;
10731                                 } else {
10732                                         MonoInst *load;
10733
10734                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
10735
10736                                         if (sp [0]->opcode == OP_LDADDR && klass->simd_type && cfg->opt & MONO_OPT_SIMD) {
10737                                                 ins = mono_emit_simd_field_load (cfg, field, sp [0]);
10738                                                 if (ins) {
10739                                                         *sp++ = ins;
10740                                                         ins_flag = 0;
10741                                                         ip += 5;
10742                                                         break;
10743                                                 }
10744                                         }
10745
10746                                         if (mini_is_gsharedvt_klass (klass)) {
10747                                                 MonoInst *offset_ins;
10748
10749                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10750                                                 /* The value is offset by 1 */
10751                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10752                                                 dreg = alloc_ireg_mp (cfg);
10753                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
10754                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
10755                                         } else {
10756                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
10757                                         }
10758                                         load->flags |= ins_flag;
10759                                         if (sp [0]->opcode != OP_LDADDR)
10760                                                 load->flags |= MONO_INST_FAULT;
10761                                         *sp++ = load;
10762                                 }
10763                         }
10764
10765                         if (is_instance) {
10766                                 ins_flag = 0;
10767                                 ip += 5;
10768                                 break;
10769                         }
10770
10771                         /* STATIC CASE */
10772                         context_used = mini_class_check_context_used (cfg, klass);
10773
10774                         if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL) {
10775                                 mono_error_set_field_load (&cfg->error, field->parent, field->name, "Using static instructions with literal field");
10776                                 CHECK_CFG_ERROR;
10777                         }
10778
10779                         /* The special_static_fields field is init'd in mono_class_vtable, so it needs
10780                          * to be called here.
10781                          */
10782                         if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
10783                                 mono_class_vtable (cfg->domain, klass);
10784                                 CHECK_TYPELOAD (klass);
10785                         }
10786                         mono_domain_lock (cfg->domain);
10787                         if (cfg->domain->special_static_fields)
10788                                 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
10789                         mono_domain_unlock (cfg->domain);
10790
10791                         is_special_static = mono_class_field_is_special_static (field);
10792
10793                         if (is_special_static && ((gsize)addr & 0x80000000) == 0)
10794                                 thread_ins = mono_create_tls_get (cfg, TLS_KEY_THREAD);
10795                         else
10796                                 thread_ins = NULL;
10797
10798                         /* Generate IR to compute the field address */
10799                         if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
10800                                 /*
10801                                  * Fast access to TLS data
10802                                  * Inline version of get_thread_static_data () in
10803                                  * threads.c.
10804                                  */
10805                                 guint32 offset;
10806                                 int idx, static_data_reg, array_reg, dreg;
10807
10808                                 GSHAREDVT_FAILURE (op);
10809
10810                                 static_data_reg = alloc_ireg (cfg);
10811                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
10812
10813                                 if (cfg->compile_aot) {
10814                                         int offset_reg, offset2_reg, idx_reg;
10815
10816                                         /* For TLS variables, this will return the TLS offset */
10817                                         EMIT_NEW_SFLDACONST (cfg, ins, field);
10818                                         offset_reg = ins->dreg;
10819                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
10820                                         idx_reg = alloc_ireg (cfg);
10821                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
10822                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
10823                                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
10824                                         array_reg = alloc_ireg (cfg);
10825                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
10826                                         offset2_reg = alloc_ireg (cfg);
10827                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
10828                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
10829                                         dreg = alloc_ireg (cfg);
10830                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
10831                                 } else {
10832                                         offset = (gsize)addr & 0x7fffffff;
10833                                         idx = offset & 0x3f;
10834
10835                                         array_reg = alloc_ireg (cfg);
10836                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
10837                                         dreg = alloc_ireg (cfg);
10838                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
10839                                 }
10840                         } else if ((cfg->opt & MONO_OPT_SHARED) ||
10841                                         (cfg->compile_aot && is_special_static) ||
10842                                         (context_used && is_special_static)) {
10843                                 MonoInst *iargs [2];
10844
10845                                 g_assert (field->parent);
10846                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10847                                 if (context_used) {
10848                                         iargs [1] = emit_get_rgctx_field (cfg, context_used,
10849                                                 field, MONO_RGCTX_INFO_CLASS_FIELD);
10850                                 } else {
10851                                         EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10852                                 }
10853                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10854                         } else if (context_used) {
10855                                 MonoInst *static_data;
10856
10857                                 /*
10858                                 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
10859                                         method->klass->name_space, method->klass->name, method->name,
10860                                         depth, field->offset);
10861                                 */
10862
10863                                 if (mono_class_needs_cctor_run (klass, method))
10864                                         emit_class_init (cfg, klass);
10865
10866                                 /*
10867                                  * The pointer we're computing here is
10868                                  *
10869                                  *   super_info.static_data + field->offset
10870                                  */
10871                                 static_data = mini_emit_get_rgctx_klass (cfg, context_used,
10872                                         klass, MONO_RGCTX_INFO_STATIC_DATA);
10873
10874                                 if (mini_is_gsharedvt_klass (klass)) {
10875                                         MonoInst *offset_ins;
10876
10877                                         offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
10878                                         /* The value is offset by 1 */
10879                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
10880                                         dreg = alloc_ireg_mp (cfg);
10881                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
10882                                 } else if (field->offset == 0) {
10883                                         ins = static_data;
10884                                 } else {
10885                                         int addr_reg = mono_alloc_preg (cfg);
10886                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
10887                                 }
10888                                 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
10889                                 MonoInst *iargs [2];
10890
10891                                 g_assert (field->parent);
10892                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10893                                 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
10894                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
10895                         } else {
10896                                 MonoVTable *vtable = NULL;
10897
10898                                 if (!cfg->compile_aot)
10899                                         vtable = mono_class_vtable (cfg->domain, klass);
10900                                 CHECK_TYPELOAD (klass);
10901
10902                                 if (!addr) {
10903                                         if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
10904                                                 if (!(g_slist_find (class_inits, klass))) {
10905                                                         emit_class_init (cfg, klass);
10906                                                         if (cfg->verbose_level > 2)
10907                                                                 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
10908                                                         class_inits = g_slist_prepend (class_inits, klass);
10909                                                 }
10910                                         } else {
10911                                                 if (cfg->run_cctors) {
10912                                                         /* This makes so that inline cannot trigger */
10913                                                         /* .cctors: too many apps depend on them */
10914                                                         /* running with a specific order... */
10915                                                         g_assert (vtable);
10916                                                         if (! vtable->initialized)
10917                                                                 INLINE_FAILURE ("class init");
10918                                                         if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
10919                                                                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
10920                                                                 goto exception_exit;
10921                                                         }
10922                                                 }
10923                                         }
10924                                         if (cfg->compile_aot)
10925                                                 EMIT_NEW_SFLDACONST (cfg, ins, field);
10926                                         else {
10927                                                 g_assert (vtable);
10928                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
10929                                                 g_assert (addr);
10930                                                 EMIT_NEW_PCONST (cfg, ins, addr);
10931                                         }
10932                                 } else {
10933                                         MonoInst *iargs [1];
10934                                         EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
10935                                         ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
10936                                 }
10937                         }
10938
10939                         /* Generate IR to do the actual load/store operation */
10940
10941                         if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
10942                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10943                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10944                         }
10945
10946                         if (op == CEE_LDSFLDA) {
10947                                 ins->klass = mono_class_from_mono_type (ftype);
10948                                 ins->type = STACK_PTR;
10949                                 *sp++ = ins;
10950                         } else if (op == CEE_STSFLD) {
10951                                 MonoInst *store;
10952
10953                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
10954                                 store->flags |= ins_flag;
10955                         } else {
10956                                 gboolean is_const = FALSE;
10957                                 MonoVTable *vtable = NULL;
10958                                 gpointer addr = NULL;
10959
10960                                 if (!context_used) {
10961                                         vtable = mono_class_vtable (cfg->domain, klass);
10962                                         CHECK_TYPELOAD (klass);
10963                                 }
10964                                 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
10965                                                 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
10966                                         int ro_type = ftype->type;
10967                                         if (!addr)
10968                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
10969                                         if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
10970                                                 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
10971                                         }
10972
10973                                         GSHAREDVT_FAILURE (op);
10974
10975                                         /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
10976                                         is_const = TRUE;
10977                                         switch (ro_type) {
10978                                         case MONO_TYPE_BOOLEAN:
10979                                         case MONO_TYPE_U1:
10980                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
10981                                                 sp++;
10982                                                 break;
10983                                         case MONO_TYPE_I1:
10984                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
10985                                                 sp++;
10986                                                 break;                                          
10987                                         case MONO_TYPE_CHAR:
10988                                         case MONO_TYPE_U2:
10989                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
10990                                                 sp++;
10991                                                 break;
10992                                         case MONO_TYPE_I2:
10993                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
10994                                                 sp++;
10995                                                 break;
10996                                                 break;
10997                                         case MONO_TYPE_I4:
10998                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
10999                                                 sp++;
11000                                                 break;                                          
11001                                         case MONO_TYPE_U4:
11002                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11003                                                 sp++;
11004                                                 break;
11005                                         case MONO_TYPE_I:
11006                                         case MONO_TYPE_U:
11007                                         case MONO_TYPE_PTR:
11008                                         case MONO_TYPE_FNPTR:
11009                                                 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11010                                                 type_to_eval_stack_type ((cfg), field->type, *sp);
11011                                                 sp++;
11012                                                 break;
11013                                         case MONO_TYPE_STRING:
11014                                         case MONO_TYPE_OBJECT:
11015                                         case MONO_TYPE_CLASS:
11016                                         case MONO_TYPE_SZARRAY:
11017                                         case MONO_TYPE_ARRAY:
11018                                                 if (!mono_gc_is_moving ()) {
11019                                                         EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11020                                                         type_to_eval_stack_type ((cfg), field->type, *sp);
11021                                                         sp++;
11022                                                 } else {
11023                                                         is_const = FALSE;
11024                                                 }
11025                                                 break;
11026                                         case MONO_TYPE_I8:
11027                                         case MONO_TYPE_U8:
11028                                                 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11029                                                 sp++;
11030                                                 break;
11031                                         case MONO_TYPE_R4:
11032                                         case MONO_TYPE_R8:
11033                                         case MONO_TYPE_VALUETYPE:
11034                                         default:
11035                                                 is_const = FALSE;
11036                                                 break;
11037                                         }
11038                                 }
11039
11040                                 if (!is_const) {
11041                                         MonoInst *load;
11042
11043                                         CHECK_STACK_OVF (1);
11044
11045                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11046                                         load->flags |= ins_flag;
11047                                         ins_flag = 0;
11048                                         *sp++ = load;
11049                                 }
11050                         }
11051
11052                         if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11053                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11054                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11055                         }
11056
11057                         ins_flag = 0;
11058                         ip += 5;
11059                         break;
11060                 }
11061                 case CEE_STOBJ:
11062                         CHECK_STACK (2);
11063                         sp -= 2;
11064                         CHECK_OPSIZE (5);
11065                         token = read32 (ip + 1);
11066                         klass = mini_get_class (method, token, generic_context);
11067                         CHECK_TYPELOAD (klass);
11068                         if (ins_flag & MONO_INST_VOLATILE) {
11069                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11070                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11071                         }
11072                         /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11073                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11074                         ins->flags |= ins_flag;
11075                         if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11076                                 generic_class_is_reference_type (cfg, klass) && !MONO_INS_IS_PCONST_NULL (sp [1])) {
11077                                 /* insert call to write barrier */
11078                                 emit_write_barrier (cfg, sp [0], sp [1]);
11079                         }
11080                         ins_flag = 0;
11081                         ip += 5;
11082                         inline_costs += 1;
11083                         break;
11084
11085                         /*
11086                          * Array opcodes
11087                          */
11088                 case CEE_NEWARR: {
11089                         MonoInst *len_ins;
11090                         const char *data_ptr;
11091                         int data_size = 0;
11092                         guint32 field_token;
11093
11094                         CHECK_STACK (1);
11095                         --sp;
11096
11097                         CHECK_OPSIZE (5);
11098                         token = read32 (ip + 1);
11099
11100                         klass = mini_get_class (method, token, generic_context);
11101                         CHECK_TYPELOAD (klass);
11102
11103                         context_used = mini_class_check_context_used (cfg, klass);
11104
11105                         if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11106                                 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11107                                 ins->sreg1 = sp [0]->dreg;
11108                                 ins->type = STACK_I4;
11109                                 ins->dreg = alloc_ireg (cfg);
11110                                 MONO_ADD_INS (cfg->cbb, ins);
11111                                 *sp = mono_decompose_opcode (cfg, ins);
11112                         }
11113
11114                         if (context_used) {
11115                                 MonoInst *args [3];
11116                                 MonoClass *array_class = mono_array_class_get (klass, 1);
11117                                 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11118
11119                                 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11120
11121                                 /* vtable */
11122                                 args [0] = mini_emit_get_rgctx_klass (cfg, context_used,
11123                                         array_class, MONO_RGCTX_INFO_VTABLE);
11124                                 /* array len */
11125                                 args [1] = sp [0];
11126
11127                                 if (managed_alloc)
11128                                         ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11129                                 else
11130                                         ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11131                         } else {
11132                                 if (cfg->opt & MONO_OPT_SHARED) {
11133                                         /* Decompose now to avoid problems with references to the domainvar */
11134                                         MonoInst *iargs [3];
11135
11136                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11137                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11138                                         iargs [2] = sp [0];
11139
11140                                         ins = mono_emit_jit_icall (cfg, ves_icall_array_new, iargs);
11141                                 } else {
11142                                         /* Decompose later since it is needed by abcrem */
11143                                         MonoClass *array_type = mono_array_class_get (klass, 1);
11144                                         mono_class_vtable (cfg->domain, array_type);
11145                                         CHECK_TYPELOAD (array_type);
11146
11147                                         MONO_INST_NEW (cfg, ins, OP_NEWARR);
11148                                         ins->dreg = alloc_ireg_ref (cfg);
11149                                         ins->sreg1 = sp [0]->dreg;
11150                                         ins->inst_newa_class = klass;
11151                                         ins->type = STACK_OBJ;
11152                                         ins->klass = array_type;
11153                                         MONO_ADD_INS (cfg->cbb, ins);
11154                                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11155                                         cfg->cbb->has_array_access = TRUE;
11156
11157                                         /* Needed so mono_emit_load_get_addr () gets called */
11158                                         mono_get_got_var (cfg);
11159                                 }
11160                         }
11161
11162                         len_ins = sp [0];
11163                         ip += 5;
11164                         *sp++ = ins;
11165                         inline_costs += 1;
11166
11167                         /* 
11168                          * we inline/optimize the initialization sequence if possible.
11169                          * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11170                          * for small sizes open code the memcpy
11171                          * ensure the rva field is big enough
11172                          */
11173                         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))) {
11174                                 MonoMethod *memcpy_method = get_memcpy_method ();
11175                                 MonoInst *iargs [3];
11176                                 int add_reg = alloc_ireg_mp (cfg);
11177
11178                                 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11179                                 if (cfg->compile_aot) {
11180                                         EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11181                                 } else {
11182                                         EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11183                                 }
11184                                 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11185                                 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11186                                 ip += 11;
11187                         }
11188
11189                         break;
11190                 }
11191                 case CEE_LDLEN:
11192                         CHECK_STACK (1);
11193                         --sp;
11194                         if (sp [0]->type != STACK_OBJ)
11195                                 UNVERIFIED;
11196
11197                         MONO_INST_NEW (cfg, ins, OP_LDLEN);
11198                         ins->dreg = alloc_preg (cfg);
11199                         ins->sreg1 = sp [0]->dreg;
11200                         ins->type = STACK_I4;
11201                         /* This flag will be inherited by the decomposition */
11202                         ins->flags |= MONO_INST_FAULT;
11203                         MONO_ADD_INS (cfg->cbb, ins);
11204                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11205                         cfg->cbb->has_array_access = TRUE;
11206                         ip ++;
11207                         *sp++ = ins;
11208                         break;
11209                 case CEE_LDELEMA:
11210                         CHECK_STACK (2);
11211                         sp -= 2;
11212                         CHECK_OPSIZE (5);
11213                         if (sp [0]->type != STACK_OBJ)
11214                                 UNVERIFIED;
11215
11216                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11217
11218                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11219                         CHECK_TYPELOAD (klass);
11220                         /* we need to make sure that this array is exactly the type it needs
11221                          * to be for correctness. the wrappers are lax with their usage
11222                          * so we need to ignore them here
11223                          */
11224                         if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11225                                 MonoClass *array_class = mono_array_class_get (klass, 1);
11226                                 mini_emit_check_array_type (cfg, sp [0], array_class);
11227                                 CHECK_TYPELOAD (array_class);
11228                         }
11229
11230                         readonly = FALSE;
11231                         ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11232                         *sp++ = ins;
11233                         ip += 5;
11234                         break;
11235                 case CEE_LDELEM:
11236                 case CEE_LDELEM_I1:
11237                 case CEE_LDELEM_U1:
11238                 case CEE_LDELEM_I2:
11239                 case CEE_LDELEM_U2:
11240                 case CEE_LDELEM_I4:
11241                 case CEE_LDELEM_U4:
11242                 case CEE_LDELEM_I8:
11243                 case CEE_LDELEM_I:
11244                 case CEE_LDELEM_R4:
11245                 case CEE_LDELEM_R8:
11246                 case CEE_LDELEM_REF: {
11247                         MonoInst *addr;
11248
11249                         CHECK_STACK (2);
11250                         sp -= 2;
11251
11252                         if (*ip == CEE_LDELEM) {
11253                                 CHECK_OPSIZE (5);
11254                                 token = read32 (ip + 1);
11255                                 klass = mini_get_class (method, token, generic_context);
11256                                 CHECK_TYPELOAD (klass);
11257                                 mono_class_init (klass);
11258                         }
11259                         else
11260                                 klass = array_access_to_klass (*ip);
11261
11262                         if (sp [0]->type != STACK_OBJ)
11263                                 UNVERIFIED;
11264
11265                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11266
11267                         if (mini_is_gsharedvt_variable_klass (klass)) {
11268                                 // FIXME-VT: OP_ICONST optimization
11269                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11270                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11271                                 ins->opcode = OP_LOADV_MEMBASE;
11272                         } else if (sp [1]->opcode == OP_ICONST) {
11273                                 int array_reg = sp [0]->dreg;
11274                                 int index_reg = sp [1]->dreg;
11275                                 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11276
11277                                 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
11278                                         MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
11279
11280                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
11281                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
11282                         } else {
11283                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11284                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11285                         }
11286                         *sp++ = ins;
11287                         if (*ip == CEE_LDELEM)
11288                                 ip += 5;
11289                         else
11290                                 ++ip;
11291                         break;
11292                 }
11293                 case CEE_STELEM_I:
11294                 case CEE_STELEM_I1:
11295                 case CEE_STELEM_I2:
11296                 case CEE_STELEM_I4:
11297                 case CEE_STELEM_I8:
11298                 case CEE_STELEM_R4:
11299                 case CEE_STELEM_R8:
11300                 case CEE_STELEM_REF:
11301                 case CEE_STELEM: {
11302                         CHECK_STACK (3);
11303                         sp -= 3;
11304
11305                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11306
11307                         if (*ip == CEE_STELEM) {
11308                                 CHECK_OPSIZE (5);
11309                                 token = read32 (ip + 1);
11310                                 klass = mini_get_class (method, token, generic_context);
11311                                 CHECK_TYPELOAD (klass);
11312                                 mono_class_init (klass);
11313                         }
11314                         else
11315                                 klass = array_access_to_klass (*ip);
11316
11317                         if (sp [0]->type != STACK_OBJ)
11318                                 UNVERIFIED;
11319
11320                         emit_array_store (cfg, klass, sp, TRUE);
11321
11322                         if (*ip == CEE_STELEM)
11323                                 ip += 5;
11324                         else
11325                                 ++ip;
11326                         inline_costs += 1;
11327                         break;
11328                 }
11329                 case CEE_CKFINITE: {
11330                         CHECK_STACK (1);
11331                         --sp;
11332
11333                         if (cfg->llvm_only) {
11334                                 MonoInst *iargs [1];
11335
11336                                 iargs [0] = sp [0];
11337                                 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
11338                         } else  {
11339                                 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
11340                                 ins->sreg1 = sp [0]->dreg;
11341                                 ins->dreg = alloc_freg (cfg);
11342                                 ins->type = STACK_R8;
11343                                 MONO_ADD_INS (cfg->cbb, ins);
11344
11345                                 *sp++ = mono_decompose_opcode (cfg, ins);
11346                         }
11347
11348                         ++ip;
11349                         break;
11350                 }
11351                 case CEE_REFANYVAL: {
11352                         MonoInst *src_var, *src;
11353
11354                         int klass_reg = alloc_preg (cfg);
11355                         int dreg = alloc_preg (cfg);
11356
11357                         GSHAREDVT_FAILURE (*ip);
11358
11359                         CHECK_STACK (1);
11360                         MONO_INST_NEW (cfg, ins, *ip);
11361                         --sp;
11362                         CHECK_OPSIZE (5);
11363                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11364                         CHECK_TYPELOAD (klass);
11365
11366                         context_used = mini_class_check_context_used (cfg, klass);
11367
11368                         // FIXME:
11369                         src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11370                         if (!src_var)
11371                                 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
11372                         EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
11373                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
11374
11375                         if (context_used) {
11376                                 MonoInst *klass_ins;
11377
11378                                 klass_ins = mini_emit_get_rgctx_klass (cfg, context_used,
11379                                                 klass, MONO_RGCTX_INFO_KLASS);
11380
11381                                 // FIXME:
11382                                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
11383                                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
11384                         } else {
11385                                 mini_emit_class_check (cfg, klass_reg, klass);
11386                         }
11387                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
11388                         ins->type = STACK_MP;
11389                         ins->klass = klass;
11390                         *sp++ = ins;
11391                         ip += 5;
11392                         break;
11393                 }
11394                 case CEE_MKREFANY: {
11395                         MonoInst *loc, *addr;
11396
11397                         GSHAREDVT_FAILURE (*ip);
11398
11399                         CHECK_STACK (1);
11400                         MONO_INST_NEW (cfg, ins, *ip);
11401                         --sp;
11402                         CHECK_OPSIZE (5);
11403                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11404                         CHECK_TYPELOAD (klass);
11405
11406                         context_used = mini_class_check_context_used (cfg, klass);
11407
11408                         loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
11409                         EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
11410
11411                         if (context_used) {
11412                                 MonoInst *const_ins;
11413                                 int type_reg = alloc_preg (cfg);
11414
11415                                 const_ins = mini_emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
11416                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
11417                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11418                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11419                         } else {
11420                                 int const_reg = alloc_preg (cfg);
11421                                 int type_reg = alloc_preg (cfg);
11422
11423                                 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
11424                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
11425                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
11426                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
11427                         }
11428                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
11429
11430                         EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
11431                         ins->type = STACK_VTYPE;
11432                         ins->klass = mono_defaults.typed_reference_class;
11433                         *sp++ = ins;
11434                         ip += 5;
11435                         break;
11436                 }
11437                 case CEE_LDTOKEN: {
11438                         gpointer handle;
11439                         MonoClass *handle_class;
11440
11441                         CHECK_STACK_OVF (1);
11442
11443                         CHECK_OPSIZE (5);
11444                         n = read32 (ip + 1);
11445
11446                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
11447                                         method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
11448                                 handle = mono_method_get_wrapper_data (method, n);
11449                                 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
11450                                 if (handle_class == mono_defaults.typehandle_class)
11451                                         handle = &((MonoClass*)handle)->byval_arg;
11452                         }
11453                         else {
11454                                 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
11455                                 CHECK_CFG_ERROR;
11456                         }
11457                         if (!handle)
11458                                 LOAD_ERROR;
11459                         mono_class_init (handle_class);
11460                         if (cfg->gshared) {
11461                                 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
11462                                                 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
11463                                         /* This case handles ldtoken
11464                                            of an open type, like for
11465                                            typeof(Gen<>). */
11466                                         context_used = 0;
11467                                 } else if (handle_class == mono_defaults.typehandle_class) {
11468                                         context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
11469                                 } else if (handle_class == mono_defaults.fieldhandle_class)
11470                                         context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
11471                                 else if (handle_class == mono_defaults.methodhandle_class)
11472                                         context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
11473                                 else
11474                                         g_assert_not_reached ();
11475                         }
11476
11477                         if ((cfg->opt & MONO_OPT_SHARED) &&
11478                                         method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
11479                                         method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
11480                                 MonoInst *addr, *vtvar, *iargs [3];
11481                                 int method_context_used;
11482
11483                                 method_context_used = mini_method_check_context_used (cfg, method);
11484
11485                                 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL); 
11486
11487                                 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
11488                                 EMIT_NEW_ICONST (cfg, iargs [1], n);
11489                                 if (method_context_used) {
11490                                         iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
11491                                                 method, MONO_RGCTX_INFO_METHOD);
11492                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
11493                                 } else {
11494                                         EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
11495                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
11496                                 }
11497                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11498
11499                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11500
11501                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11502                         } else {
11503                                 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) && 
11504                                         ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) && 
11505                                         (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
11506                                         (cmethod->klass == mono_defaults.systemtype_class) &&
11507                                         (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
11508                                         MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
11509
11510                                         mono_class_init (tclass);
11511                                         if (context_used) {
11512                                                 ins = mini_emit_get_rgctx_klass (cfg, context_used,
11513                                                         tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
11514                                         } else if (cfg->compile_aot) {
11515                                                 if (method->wrapper_type) {
11516                                                         mono_error_init (&error); //got to do it since there are multiple conditionals below
11517                                                         if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
11518                                                                 /* Special case for static synchronized wrappers */
11519                                                                 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
11520                                                         } else {
11521                                                                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
11522                                                                 /* FIXME: n is not a normal token */
11523                                                                 DISABLE_AOT (cfg);
11524                                                                 EMIT_NEW_PCONST (cfg, ins, NULL);
11525                                                         }
11526                                                 } else {
11527                                                         EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
11528                                                 }
11529                                         } else {
11530                                                 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
11531                                                 CHECK_CFG_ERROR;
11532                                                 EMIT_NEW_PCONST (cfg, ins, rt);
11533                                         }
11534                                         ins->type = STACK_OBJ;
11535                                         ins->klass = cmethod->klass;
11536                                         ip += 5;
11537                                 } else {
11538                                         MonoInst *addr, *vtvar;
11539
11540                                         vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
11541
11542                                         if (context_used) {
11543                                                 if (handle_class == mono_defaults.typehandle_class) {
11544                                                         ins = mini_emit_get_rgctx_klass (cfg, context_used,
11545                                                                         mono_class_from_mono_type ((MonoType *)handle),
11546                                                                         MONO_RGCTX_INFO_TYPE);
11547                                                 } else if (handle_class == mono_defaults.methodhandle_class) {
11548                                                         ins = emit_get_rgctx_method (cfg, context_used,
11549                                                                         (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
11550                                                 } else if (handle_class == mono_defaults.fieldhandle_class) {
11551                                                         ins = emit_get_rgctx_field (cfg, context_used,
11552                                                                         (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
11553                                                 } else {
11554                                                         g_assert_not_reached ();
11555                                                 }
11556                                         } else if (cfg->compile_aot) {
11557                                                 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
11558                                         } else {
11559                                                 EMIT_NEW_PCONST (cfg, ins, handle);
11560                                         }
11561                                         EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
11562                                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
11563                                         EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
11564                                 }
11565                         }
11566
11567                         *sp++ = ins;
11568                         ip += 5;
11569                         break;
11570                 }
11571                 case CEE_THROW:
11572                         CHECK_STACK (1);
11573                         if (sp [-1]->type != STACK_OBJ)
11574                                 UNVERIFIED;
11575
11576                         MONO_INST_NEW (cfg, ins, OP_THROW);
11577                         --sp;
11578                         ins->sreg1 = sp [0]->dreg;
11579                         ip++;
11580                         cfg->cbb->out_of_line = TRUE;
11581                         MONO_ADD_INS (cfg->cbb, ins);
11582                         MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
11583                         MONO_ADD_INS (cfg->cbb, ins);
11584                         sp = stack_start;
11585                         
11586                         link_bblock (cfg, cfg->cbb, end_bblock);
11587                         start_new_bblock = 1;
11588                         /* This can complicate code generation for llvm since the return value might not be defined */
11589                         if (COMPILE_LLVM (cfg))
11590                                 INLINE_FAILURE ("throw");
11591                         break;
11592                 case CEE_ENDFINALLY:
11593                         if (!ip_in_finally_clause (cfg, ip - header->code))
11594                                 UNVERIFIED;
11595                         /* mono_save_seq_point_info () depends on this */
11596                         if (sp != stack_start)
11597                                 emit_seq_point (cfg, method, ip, FALSE, FALSE);
11598                         MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
11599                         MONO_ADD_INS (cfg->cbb, ins);
11600                         ip++;
11601                         start_new_bblock = 1;
11602
11603                         /*
11604                          * Control will leave the method so empty the stack, otherwise
11605                          * the next basic block will start with a nonempty stack.
11606                          */
11607                         while (sp != stack_start) {
11608                                 sp--;
11609                         }
11610                         break;
11611                 case CEE_LEAVE:
11612                 case CEE_LEAVE_S: {
11613                         GList *handlers;
11614
11615                         if (*ip == CEE_LEAVE) {
11616                                 CHECK_OPSIZE (5);
11617                                 target = ip + 5 + (gint32)read32(ip + 1);
11618                         } else {
11619                                 CHECK_OPSIZE (2);
11620                                 target = ip + 2 + (signed char)(ip [1]);
11621                         }
11622
11623                         /* empty the stack */
11624                         while (sp != stack_start) {
11625                                 sp--;
11626                         }
11627
11628                         /* 
11629                          * If this leave statement is in a catch block, check for a
11630                          * pending exception, and rethrow it if necessary.
11631                          * We avoid doing this in runtime invoke wrappers, since those are called
11632                          * by native code which excepts the wrapper to catch all exceptions.
11633                          */
11634                         for (i = 0; i < header->num_clauses; ++i) {
11635                                 MonoExceptionClause *clause = &header->clauses [i];
11636
11637                                 /* 
11638                                  * Use <= in the final comparison to handle clauses with multiple
11639                                  * leave statements, like in bug #78024.
11640                                  * The ordering of the exception clauses guarantees that we find the
11641                                  * innermost clause.
11642                                  */
11643                                 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) {
11644                                         MonoInst *exc_ins;
11645                                         MonoBasicBlock *dont_throw;
11646
11647                                         /*
11648                                           MonoInst *load;
11649
11650                                           NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
11651                                         */
11652
11653                                         exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
11654
11655                                         NEW_BBLOCK (cfg, dont_throw);
11656
11657                                         /*
11658                                          * Currently, we always rethrow the abort exception, despite the 
11659                                          * fact that this is not correct. See thread6.cs for an example. 
11660                                          * But propagating the abort exception is more important than 
11661                                          * getting the sematics right.
11662                                          */
11663                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
11664                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
11665                                         MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
11666
11667                                         MONO_START_BB (cfg, dont_throw);
11668                                 }
11669                         }
11670
11671 #ifdef ENABLE_LLVM
11672                         cfg->cbb->try_end = (intptr_t)(ip - header->code);
11673 #endif
11674
11675                         if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
11676                                 GList *tmp;
11677                                 MonoExceptionClause *clause;
11678
11679                                 for (tmp = handlers; tmp; tmp = tmp->next) {
11680                                         clause = (MonoExceptionClause *)tmp->data;
11681                                         tblock = cfg->cil_offset_to_bb [clause->handler_offset];
11682                                         g_assert (tblock);
11683                                         link_bblock (cfg, cfg->cbb, tblock);
11684                                         MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
11685                                         ins->inst_target_bb = tblock;
11686                                         ins->inst_eh_block = clause;
11687                                         MONO_ADD_INS (cfg->cbb, ins);
11688                                         cfg->cbb->has_call_handler = 1;
11689                                         if (COMPILE_LLVM (cfg)) {
11690                                                 MonoBasicBlock *target_bb;
11691
11692                                                 /* 
11693                                                  * Link the finally bblock with the target, since it will
11694                                                  * conceptually branch there.
11695                                                  */
11696                                                 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
11697                                                 GET_BBLOCK (cfg, target_bb, target);
11698                                                 link_bblock (cfg, tblock, target_bb);
11699                                         }
11700                                 }
11701                                 g_list_free (handlers);
11702                         } 
11703
11704                         MONO_INST_NEW (cfg, ins, OP_BR);
11705                         MONO_ADD_INS (cfg->cbb, ins);
11706                         GET_BBLOCK (cfg, tblock, target);
11707                         link_bblock (cfg, cfg->cbb, tblock);
11708                         ins->inst_target_bb = tblock;
11709
11710                         start_new_bblock = 1;
11711
11712                         if (*ip == CEE_LEAVE)
11713                                 ip += 5;
11714                         else
11715                                 ip += 2;
11716
11717                         break;
11718                 }
11719
11720                         /*
11721                          * Mono specific opcodes
11722                          */
11723                 case MONO_CUSTOM_PREFIX: {
11724
11725                         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
11726
11727                         CHECK_OPSIZE (2);
11728                         switch (ip [1]) {
11729                         case CEE_MONO_ICALL: {
11730                                 gpointer func;
11731                                 MonoJitICallInfo *info;
11732
11733                                 token = read32 (ip + 2);
11734                                 func = mono_method_get_wrapper_data (method, token);
11735                                 info = mono_find_jit_icall_by_addr (func);
11736                                 if (!info)
11737                                         g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
11738                                 g_assert (info);
11739
11740                                 CHECK_STACK (info->sig->param_count);
11741                                 sp -= info->sig->param_count;
11742
11743                                 ins = mono_emit_jit_icall (cfg, info->func, sp);
11744                                 if (!MONO_TYPE_IS_VOID (info->sig->ret))
11745                                         *sp++ = ins;
11746
11747                                 ip += 6;
11748                                 inline_costs += 10 * num_calls++;
11749
11750                                 break;
11751                         }
11752                         case CEE_MONO_LDPTR_CARD_TABLE:
11753                         case CEE_MONO_LDPTR_NURSERY_START:
11754                         case CEE_MONO_LDPTR_NURSERY_BITS:
11755                         case CEE_MONO_LDPTR_INT_REQ_FLAG: {
11756                                 CHECK_STACK_OVF (1);
11757
11758                                 switch (ip [1]) {
11759                                         case CEE_MONO_LDPTR_CARD_TABLE:
11760                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
11761                                                 break;
11762                                         case CEE_MONO_LDPTR_NURSERY_START:
11763                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
11764                                                 break;
11765                                         case CEE_MONO_LDPTR_NURSERY_BITS:
11766                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
11767                                                 break;
11768                                         case CEE_MONO_LDPTR_INT_REQ_FLAG:
11769                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
11770                                                 break;
11771                                 }
11772
11773                                 *sp++ = ins;
11774                                 ip += 2;
11775                                 inline_costs += 10 * num_calls++;
11776                                 break;
11777                         }
11778                         case CEE_MONO_LDPTR: {
11779                                 gpointer ptr;
11780
11781                                 CHECK_STACK_OVF (1);
11782                                 CHECK_OPSIZE (6);
11783                                 token = read32 (ip + 2);
11784
11785                                 ptr = mono_method_get_wrapper_data (method, token);
11786                                 EMIT_NEW_PCONST (cfg, ins, ptr);
11787                                 *sp++ = ins;
11788                                 ip += 6;
11789                                 inline_costs += 10 * num_calls++;
11790                                 /* Can't embed random pointers into AOT code */
11791                                 DISABLE_AOT (cfg);
11792                                 break;
11793                         }
11794                         case CEE_MONO_JIT_ICALL_ADDR: {
11795                                 MonoJitICallInfo *callinfo;
11796                                 gpointer ptr;
11797
11798                                 CHECK_STACK_OVF (1);
11799                                 CHECK_OPSIZE (6);
11800                                 token = read32 (ip + 2);
11801
11802                                 ptr = mono_method_get_wrapper_data (method, token);
11803                                 callinfo = mono_find_jit_icall_by_addr (ptr);
11804                                 g_assert (callinfo);
11805                                 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
11806                                 *sp++ = ins;
11807                                 ip += 6;
11808                                 inline_costs += 10 * num_calls++;
11809                                 break;
11810                         }
11811                         case CEE_MONO_ICALL_ADDR: {
11812                                 MonoMethod *cmethod;
11813                                 gpointer ptr;
11814
11815                                 CHECK_STACK_OVF (1);
11816                                 CHECK_OPSIZE (6);
11817                                 token = read32 (ip + 2);
11818
11819                                 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
11820
11821                                 if (cfg->compile_aot) {
11822                                         if (cfg->direct_pinvoke && ip + 6 < end && (ip [6] == CEE_POP)) {
11823                                                 /*
11824                                                  * This is generated by emit_native_wrapper () to resolve the pinvoke address
11825                                                  * before the call, its not needed when using direct pinvoke.
11826                                                  * This is not an optimization, but its used to avoid looking up pinvokes
11827                                                  * on platforms which don't support dlopen ().
11828                                                  */
11829                                                 EMIT_NEW_PCONST (cfg, ins, NULL);
11830                                         } else {
11831                                                 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
11832                                         }
11833                                 } else {
11834                                         ptr = mono_lookup_internal_call (cmethod);
11835                                         g_assert (ptr);
11836                                         EMIT_NEW_PCONST (cfg, ins, ptr);
11837                                 }
11838                                 *sp++ = ins;
11839                                 ip += 6;
11840                                 break;
11841                         }
11842                         case CEE_MONO_VTADDR: {
11843                                 MonoInst *src_var, *src;
11844
11845                                 CHECK_STACK (1);
11846                                 --sp;
11847
11848                                 // FIXME:
11849                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
11850                                 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
11851                                 *sp++ = src;
11852                                 ip += 2;
11853                                 break;
11854                         }
11855                         case CEE_MONO_NEWOBJ: {
11856                                 MonoInst *iargs [2];
11857
11858                                 CHECK_STACK_OVF (1);
11859                                 CHECK_OPSIZE (6);
11860                                 token = read32 (ip + 2);
11861                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11862                                 mono_class_init (klass);
11863                                 NEW_DOMAINCONST (cfg, iargs [0]);
11864                                 MONO_ADD_INS (cfg->cbb, iargs [0]);
11865                                 NEW_CLASSCONST (cfg, iargs [1], klass);
11866                                 MONO_ADD_INS (cfg->cbb, iargs [1]);
11867                                 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
11868                                 ip += 6;
11869                                 inline_costs += 10 * num_calls++;
11870                                 break;
11871                         }
11872                         case CEE_MONO_OBJADDR:
11873                                 CHECK_STACK (1);
11874                                 --sp;
11875                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
11876                                 ins->dreg = alloc_ireg_mp (cfg);
11877                                 ins->sreg1 = sp [0]->dreg;
11878                                 ins->type = STACK_MP;
11879                                 MONO_ADD_INS (cfg->cbb, ins);
11880                                 *sp++ = ins;
11881                                 ip += 2;
11882                                 break;
11883                         case CEE_MONO_LDNATIVEOBJ:
11884                                 /*
11885                                  * Similar to LDOBJ, but instead load the unmanaged 
11886                                  * representation of the vtype to the stack.
11887                                  */
11888                                 CHECK_STACK (1);
11889                                 CHECK_OPSIZE (6);
11890                                 --sp;
11891                                 token = read32 (ip + 2);
11892                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11893                                 g_assert (klass->valuetype);
11894                                 mono_class_init (klass);
11895
11896                                 {
11897                                         MonoInst *src, *dest, *temp;
11898
11899                                         src = sp [0];
11900                                         temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
11901                                         temp->backend.is_pinvoke = 1;
11902                                         EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
11903                                         mini_emit_stobj (cfg, dest, src, klass, TRUE);
11904
11905                                         EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
11906                                         dest->type = STACK_VTYPE;
11907                                         dest->klass = klass;
11908
11909                                         *sp ++ = dest;
11910                                         ip += 6;
11911                                 }
11912                                 break;
11913                         case CEE_MONO_RETOBJ: {
11914                                 /*
11915                                  * Same as RET, but return the native representation of a vtype
11916                                  * to the caller.
11917                                  */
11918                                 g_assert (cfg->ret);
11919                                 g_assert (mono_method_signature (method)->pinvoke); 
11920                                 CHECK_STACK (1);
11921                                 --sp;
11922                                 
11923                                 CHECK_OPSIZE (6);
11924                                 token = read32 (ip + 2);    
11925                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
11926
11927                                 if (!cfg->vret_addr) {
11928                                         g_assert (cfg->ret_var_is_local);
11929
11930                                         EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
11931                                 } else {
11932                                         EMIT_NEW_RETLOADA (cfg, ins);
11933                                 }
11934                                 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
11935                                 
11936                                 if (sp != stack_start)
11937                                         UNVERIFIED;
11938                                 
11939                                 MONO_INST_NEW (cfg, ins, OP_BR);
11940                                 ins->inst_target_bb = end_bblock;
11941                                 MONO_ADD_INS (cfg->cbb, ins);
11942                                 link_bblock (cfg, cfg->cbb, end_bblock);
11943                                 start_new_bblock = 1;
11944                                 ip += 6;
11945                                 break;
11946                         }
11947                         case CEE_MONO_SAVE_LMF:
11948                         case CEE_MONO_RESTORE_LMF:
11949                                 ip += 2;
11950                                 break;
11951                         case CEE_MONO_CLASSCONST:
11952                                 CHECK_STACK_OVF (1);
11953                                 CHECK_OPSIZE (6);
11954                                 token = read32 (ip + 2);
11955                                 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
11956                                 *sp++ = ins;
11957                                 ip += 6;
11958                                 inline_costs += 10 * num_calls++;
11959                                 break;
11960                         case CEE_MONO_NOT_TAKEN:
11961                                 cfg->cbb->out_of_line = TRUE;
11962                                 ip += 2;
11963                                 break;
11964                         case CEE_MONO_TLS: {
11965                                 MonoTlsKey key;
11966
11967                                 CHECK_STACK_OVF (1);
11968                                 CHECK_OPSIZE (6);
11969                                 key = (MonoTlsKey)read32 (ip + 2);
11970                                 g_assert (key < TLS_KEY_NUM);
11971
11972                                 ins = mono_create_tls_get (cfg, key);
11973                                 g_assert (ins);
11974                                 ins->type = STACK_PTR;
11975                                 *sp++ = ins;
11976                                 ip += 6;
11977                                 break;
11978                         }
11979                         case CEE_MONO_DYN_CALL: {
11980                                 MonoCallInst *call;
11981
11982                                 /* It would be easier to call a trampoline, but that would put an
11983                                  * extra frame on the stack, confusing exception handling. So
11984                                  * implement it inline using an opcode for now.
11985                                  */
11986
11987                                 if (!cfg->dyn_call_var) {
11988                                         cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
11989                                         /* prevent it from being register allocated */
11990                                         cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
11991                                 }
11992
11993                                 /* Has to use a call inst since it local regalloc expects it */
11994                                 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
11995                                 ins = (MonoInst*)call;
11996                                 sp -= 2;
11997                                 ins->sreg1 = sp [0]->dreg;
11998                                 ins->sreg2 = sp [1]->dreg;
11999                                 MONO_ADD_INS (cfg->cbb, ins);
12000
12001                                 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12002
12003                                 ip += 2;
12004                                 inline_costs += 10 * num_calls++;
12005
12006                                 break;
12007                         }
12008                         case CEE_MONO_MEMORY_BARRIER: {
12009                                 CHECK_OPSIZE (6);
12010                                 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12011                                 ip += 6;
12012                                 break;
12013                         }
12014                         case CEE_MONO_ATOMIC_STORE_I4: {
12015                                 g_assert (mono_arch_opcode_supported (OP_ATOMIC_STORE_I4));
12016
12017                                 CHECK_OPSIZE (6);
12018                                 CHECK_STACK (2);
12019                                 sp -= 2;
12020
12021                                 MONO_INST_NEW (cfg, ins, OP_ATOMIC_STORE_I4);
12022                                 ins->dreg = sp [0]->dreg;
12023                                 ins->sreg1 = sp [1]->dreg;
12024                                 ins->backend.memory_barrier_kind = (int) read32 (ip + 2);
12025                                 MONO_ADD_INS (cfg->cbb, ins);
12026
12027                                 ip += 6;
12028                                 break;
12029                         }
12030                         case CEE_MONO_JIT_ATTACH: {
12031                                 MonoInst *args [16], *domain_ins;
12032                                 MonoInst *ad_ins, *jit_tls_ins;
12033                                 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12034
12035                                 g_assert (!mono_threads_is_coop_enabled ());
12036
12037                                 cfg->orig_domain_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12038
12039                                 EMIT_NEW_PCONST (cfg, ins, NULL);
12040                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12041
12042                                 ad_ins = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12043                                 jit_tls_ins = mono_create_tls_get (cfg, TLS_KEY_JIT_TLS);
12044
12045                                 if (ad_ins && jit_tls_ins) {
12046                                         NEW_BBLOCK (cfg, next_bb);
12047                                         NEW_BBLOCK (cfg, call_bb);
12048
12049                                         if (cfg->compile_aot) {
12050                                                 /* AOT code is only used in the root domain */
12051                                                 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12052                                         } else {
12053                                                 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12054                                         }
12055                                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12056                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12057
12058                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12059                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12060
12061                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12062                                         MONO_START_BB (cfg, call_bb);
12063                                 }
12064
12065                                 /* AOT code is only used in the root domain */
12066                                 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12067                                 if (cfg->compile_aot) {
12068                                         MonoInst *addr;
12069
12070                                         /*
12071                                          * This is called on unattached threads, so it cannot go through the trampoline
12072                                          * infrastructure. Use an indirect call through a got slot initialized at load time
12073                                          * instead.
12074                                          */
12075                                         EMIT_NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_JIT_THREAD_ATTACH, NULL);
12076                                         ins = mono_emit_calli (cfg, helper_sig_jit_thread_attach, args, addr, NULL, NULL);
12077                                 } else {
12078                                         ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12079                                 }
12080                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->orig_domain_var->dreg, ins->dreg);
12081
12082                                 if (next_bb)
12083                                         MONO_START_BB (cfg, next_bb);
12084
12085                                 ip += 2;
12086                                 break;
12087                         }
12088                         case CEE_MONO_JIT_DETACH: {
12089                                 MonoInst *args [16];
12090
12091                                 /* Restore the original domain */
12092                                 dreg = alloc_ireg (cfg);
12093                                 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->orig_domain_var->dreg);
12094                                 mono_emit_jit_icall (cfg, mono_jit_set_domain, args);
12095                                 ip += 2;
12096                                 break;
12097                         }
12098                         case CEE_MONO_CALLI_EXTRA_ARG: {
12099                                 MonoInst *addr;
12100                                 MonoMethodSignature *fsig;
12101                                 MonoInst *arg;
12102
12103                                 /*
12104                                  * This is the same as CEE_CALLI, but passes an additional argument
12105                                  * to the called method in llvmonly mode.
12106                                  * This is only used by delegate invoke wrappers to call the
12107                                  * actual delegate method.
12108                                  */
12109                                 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12110
12111                                 CHECK_OPSIZE (6);
12112                                 token = read32 (ip + 2);
12113
12114                                 ins = NULL;
12115
12116                                 cmethod = NULL;
12117                                 CHECK_STACK (1);
12118                                 --sp;
12119                                 addr = *sp;
12120                                 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
12121                                 CHECK_CFG_ERROR;
12122
12123                                 if (cfg->llvm_only)
12124                                         cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12125
12126                                 n = fsig->param_count + fsig->hasthis + 1;
12127
12128                                 CHECK_STACK (n);
12129
12130                                 sp -= n;
12131                                 arg = sp [n - 1];
12132
12133                                 if (cfg->llvm_only) {
12134                                         /*
12135                                          * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12136                                          * cconv. This is set by mono_init_delegate ().
12137                                          */
12138                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12139                                                 MonoInst *callee = addr;
12140                                                 MonoInst *call, *localloc_ins;
12141                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12142                                                 int low_bit_reg = alloc_preg (cfg);
12143
12144                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12145                                                 NEW_BBLOCK (cfg, end_bb);
12146
12147                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12148                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12149                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12150
12151                                                 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12152                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12153                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12154                                                 /*
12155                                                  * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12156                                                  */
12157                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12158                                                 ins->dreg = alloc_preg (cfg);
12159                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12160                                                 MONO_ADD_INS (cfg->cbb, ins);
12161                                                 localloc_ins = ins;
12162                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12163                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12164                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12165
12166                                                 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12167                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12168
12169                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12170                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12171                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12172                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12173                                                 ins->dreg = call->dreg;
12174
12175                                                 MONO_START_BB (cfg, end_bb);
12176                                         } else {
12177                                                 /* Caller uses a normal calling conv */
12178
12179                                                 MonoInst *callee = addr;
12180                                                 MonoInst *call, *localloc_ins;
12181                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12182                                                 int low_bit_reg = alloc_preg (cfg);
12183
12184                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12185                                                 NEW_BBLOCK (cfg, end_bb);
12186
12187                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12188                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12189                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12190
12191                                                 /* Normal case: callee uses a normal cconv, no conversion is needed */
12192                                                 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12193                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12194                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12195                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12196                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12197                                                 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12198                                                 MONO_ADD_INS (cfg->cbb, addr);
12199                                                 /*
12200                                                  * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12201                                                  */
12202                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12203                                                 ins->dreg = alloc_preg (cfg);
12204                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12205                                                 MONO_ADD_INS (cfg->cbb, ins);
12206                                                 localloc_ins = ins;
12207                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12208                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12209                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12210
12211                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12212                                                 ins->dreg = call->dreg;
12213                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12214
12215                                                 MONO_START_BB (cfg, end_bb);
12216                                         }
12217                                 } else {
12218                                         /* Same as CEE_CALLI */
12219                                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12220                                                 /*
12221                                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
12222                                                  */
12223                                                 MonoInst *callee = addr;
12224
12225                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12226                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12227                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12228                                         } else {
12229                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12230                                         }
12231                                 }
12232
12233                                 if (!MONO_TYPE_IS_VOID (fsig->ret))
12234                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12235
12236                                 CHECK_CFG_EXCEPTION;
12237
12238                                 ip += 6;
12239                                 ins_flag = 0;
12240                                 constrained_class = NULL;
12241                                 break;
12242                         }
12243                         case CEE_MONO_LDDOMAIN:
12244                                 CHECK_STACK_OVF (1);
12245                                 EMIT_NEW_PCONST (cfg, ins, cfg->compile_aot ? NULL : cfg->domain);
12246                                 ip += 2;
12247                                 *sp++ = ins;
12248                                 break;
12249                         case CEE_MONO_GET_LAST_ERROR:
12250                                 CHECK_OPSIZE (2);
12251                                 CHECK_STACK_OVF (1);
12252
12253                                 MONO_INST_NEW (cfg, ins, OP_GET_LAST_ERROR);
12254                                 ins->dreg = alloc_dreg (cfg, STACK_I4);
12255                                 ins->type = STACK_I4;
12256                                 MONO_ADD_INS (cfg->cbb, ins);
12257
12258                                 ip += 2;
12259                                 *sp++ = ins;
12260                                 break;
12261                         default:
12262                                 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12263                                 break;
12264                         }
12265                         break;
12266                 }
12267
12268                 case CEE_PREFIX1: {
12269                         CHECK_OPSIZE (2);
12270                         switch (ip [1]) {
12271                         case CEE_ARGLIST: {
12272                                 /* somewhat similar to LDTOKEN */
12273                                 MonoInst *addr, *vtvar;
12274                                 CHECK_STACK_OVF (1);
12275                                 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL); 
12276
12277                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12278                                 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12279
12280                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12281                                 ins->type = STACK_VTYPE;
12282                                 ins->klass = mono_defaults.argumenthandle_class;
12283                                 *sp++ = ins;
12284                                 ip += 2;
12285                                 break;
12286                         }
12287                         case CEE_CEQ:
12288                         case CEE_CGT:
12289                         case CEE_CGT_UN:
12290                         case CEE_CLT:
12291                         case CEE_CLT_UN: {
12292                                 MonoInst *cmp, *arg1, *arg2;
12293
12294                                 CHECK_STACK (2);
12295                                 sp -= 2;
12296                                 arg1 = sp [0];
12297                                 arg2 = sp [1];
12298
12299                                 /*
12300                                  * The following transforms:
12301                                  *    CEE_CEQ    into OP_CEQ
12302                                  *    CEE_CGT    into OP_CGT
12303                                  *    CEE_CGT_UN into OP_CGT_UN
12304                                  *    CEE_CLT    into OP_CLT
12305                                  *    CEE_CLT_UN into OP_CLT_UN
12306                                  */
12307                                 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
12308
12309                                 MONO_INST_NEW (cfg, ins, cmp->opcode);
12310                                 cmp->sreg1 = arg1->dreg;
12311                                 cmp->sreg2 = arg2->dreg;
12312                                 type_from_op (cfg, cmp, arg1, arg2);
12313                                 CHECK_TYPE (cmp);
12314                                 add_widen_op (cfg, cmp, &arg1, &arg2);
12315                                 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
12316                                         cmp->opcode = OP_LCOMPARE;
12317                                 else if (arg1->type == STACK_R4)
12318                                         cmp->opcode = OP_RCOMPARE;
12319                                 else if (arg1->type == STACK_R8)
12320                                         cmp->opcode = OP_FCOMPARE;
12321                                 else
12322                                         cmp->opcode = OP_ICOMPARE;
12323                                 MONO_ADD_INS (cfg->cbb, cmp);
12324                                 ins->type = STACK_I4;
12325                                 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
12326                                 type_from_op (cfg, ins, arg1, arg2);
12327
12328                                 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
12329                                         /*
12330                                          * The backends expect the fceq opcodes to do the
12331                                          * comparison too.
12332                                          */
12333                                         ins->sreg1 = cmp->sreg1;
12334                                         ins->sreg2 = cmp->sreg2;
12335                                         NULLIFY_INS (cmp);
12336                                 }
12337                                 MONO_ADD_INS (cfg->cbb, ins);
12338                                 *sp++ = ins;
12339                                 ip += 2;
12340                                 break;
12341                         }
12342                         case CEE_LDFTN: {
12343                                 MonoInst *argconst;
12344                                 MonoMethod *cil_method;
12345
12346                                 CHECK_STACK_OVF (1);
12347                                 CHECK_OPSIZE (6);
12348                                 n = read32 (ip + 2);
12349                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12350                                 CHECK_CFG_ERROR;
12351
12352                                 mono_class_init (cmethod->klass);
12353
12354                                 mono_save_token_info (cfg, image, n, cmethod);
12355
12356                                 context_used = mini_method_check_context_used (cfg, cmethod);
12357
12358                                 cil_method = cmethod;
12359                                 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
12360                                         emit_method_access_failure (cfg, method, cil_method);
12361
12362                                 if (mono_security_core_clr_enabled ())
12363                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12364
12365                                 /* 
12366                                  * Optimize the common case of ldftn+delegate creation
12367                                  */
12368                                 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
12369                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12370                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12371                                                 MonoInst *target_ins, *handle_ins;
12372                                                 MonoMethod *invoke;
12373                                                 int invoke_context_used;
12374
12375                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
12376                                                 if (!invoke || !mono_method_signature (invoke))
12377                                                         LOAD_ERROR;
12378
12379                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12380
12381                                                 target_ins = sp [-1];
12382
12383                                                 if (mono_security_core_clr_enabled ())
12384                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12385
12386                                                 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
12387                                                         /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
12388                                                         if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
12389                                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
12390                                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
12391                                                         }
12392                                                 }
12393
12394                                                 /* FIXME: SGEN support */
12395                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
12396                                                         ip += 6;
12397                                                         if (cfg->verbose_level > 3)
12398                                                                 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));
12399                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
12400                                                                 sp --;
12401                                                                 *sp = handle_ins;
12402                                                                 CHECK_CFG_EXCEPTION;
12403                                                                 ip += 5;
12404                                                                 sp ++;
12405                                                                 break;
12406                                                         }
12407                                                         ip -= 6;
12408                                                 }
12409                                         }
12410                                 }
12411
12412                                 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
12413                                 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
12414                                 *sp++ = ins;
12415                                 
12416                                 ip += 6;
12417                                 inline_costs += 10 * num_calls++;
12418                                 break;
12419                         }
12420                         case CEE_LDVIRTFTN: {
12421                                 MonoInst *args [2];
12422
12423                                 CHECK_STACK (1);
12424                                 CHECK_OPSIZE (6);
12425                                 n = read32 (ip + 2);
12426                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
12427                                 CHECK_CFG_ERROR;
12428
12429                                 mono_class_init (cmethod->klass);
12430  
12431                                 context_used = mini_method_check_context_used (cfg, cmethod);
12432
12433                                 if (mono_security_core_clr_enabled ())
12434                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
12435
12436                                 /*
12437                                  * Optimize the common case of ldvirtftn+delegate creation
12438                                  */
12439                                 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)) {
12440                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
12441                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
12442                                                 MonoInst *target_ins, *handle_ins;
12443                                                 MonoMethod *invoke;
12444                                                 int invoke_context_used;
12445                                                 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
12446
12447                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
12448                                                 if (!invoke || !mono_method_signature (invoke))
12449                                                         LOAD_ERROR;
12450
12451                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
12452
12453                                                 target_ins = sp [-1];
12454
12455                                                 if (mono_security_core_clr_enabled ())
12456                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
12457
12458                                                 /* FIXME: SGEN support */
12459                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
12460                                                         ip += 6;
12461                                                         if (cfg->verbose_level > 3)
12462                                                                 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));
12463                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
12464                                                                 sp -= 2;
12465                                                                 *sp = handle_ins;
12466                                                                 CHECK_CFG_EXCEPTION;
12467                                                                 ip += 5;
12468                                                                 sp ++;
12469                                                                 break;
12470                                                         }
12471                                                         ip -= 6;
12472                                                 }
12473                                         }
12474                                 }
12475
12476                                 --sp;
12477                                 args [0] = *sp;
12478
12479                                 args [1] = emit_get_rgctx_method (cfg, context_used,
12480                                                                                                   cmethod, MONO_RGCTX_INFO_METHOD);
12481
12482                                 if (context_used)
12483                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
12484                                 else
12485                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
12486
12487                                 ip += 6;
12488                                 inline_costs += 10 * num_calls++;
12489                                 break;
12490                         }
12491                         case CEE_LDARG:
12492                                 CHECK_STACK_OVF (1);
12493                                 CHECK_OPSIZE (4);
12494                                 n = read16 (ip + 2);
12495                                 CHECK_ARG (n);
12496                                 EMIT_NEW_ARGLOAD (cfg, ins, n);
12497                                 *sp++ = ins;
12498                                 ip += 4;
12499                                 break;
12500                         case CEE_LDARGA:
12501                                 CHECK_STACK_OVF (1);
12502                                 CHECK_OPSIZE (4);
12503                                 n = read16 (ip + 2);
12504                                 CHECK_ARG (n);
12505                                 NEW_ARGLOADA (cfg, ins, n);
12506                                 MONO_ADD_INS (cfg->cbb, ins);
12507                                 *sp++ = ins;
12508                                 ip += 4;
12509                                 break;
12510                         case CEE_STARG:
12511                                 CHECK_STACK (1);
12512                                 --sp;
12513                                 CHECK_OPSIZE (4);
12514                                 n = read16 (ip + 2);
12515                                 CHECK_ARG (n);
12516                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
12517                                         UNVERIFIED;
12518                                 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
12519                                 ip += 4;
12520                                 break;
12521                         case CEE_LDLOC:
12522                                 CHECK_STACK_OVF (1);
12523                                 CHECK_OPSIZE (4);
12524                                 n = read16 (ip + 2);
12525                                 CHECK_LOCAL (n);
12526                                 EMIT_NEW_LOCLOAD (cfg, ins, n);
12527                                 *sp++ = ins;
12528                                 ip += 4;
12529                                 break;
12530                         case CEE_LDLOCA: {
12531                                 unsigned char *tmp_ip;
12532                                 CHECK_STACK_OVF (1);
12533                                 CHECK_OPSIZE (4);
12534                                 n = read16 (ip + 2);
12535                                 CHECK_LOCAL (n);
12536
12537                                 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
12538                                         ip = tmp_ip;
12539                                         inline_costs += 1;
12540                                         break;
12541                                 }                       
12542                                 
12543                                 EMIT_NEW_LOCLOADA (cfg, ins, n);
12544                                 *sp++ = ins;
12545                                 ip += 4;
12546                                 break;
12547                         }
12548                         case CEE_STLOC:
12549                                 CHECK_STACK (1);
12550                                 --sp;
12551                                 CHECK_OPSIZE (4);
12552                                 n = read16 (ip + 2);
12553                                 CHECK_LOCAL (n);
12554                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
12555                                         UNVERIFIED;
12556                                 emit_stloc_ir (cfg, sp, header, n);
12557                                 ip += 4;
12558                                 inline_costs += 1;
12559                                 break;
12560                         case CEE_LOCALLOC: {
12561                                 CHECK_STACK (1);
12562                                 MonoBasicBlock *non_zero_bb, *end_bb;
12563                                 int alloc_ptr = alloc_preg (cfg);
12564                                 --sp;
12565                                 if (sp != stack_start) 
12566                                         UNVERIFIED;
12567                                 if (cfg->method != method) 
12568                                         /* 
12569                                          * Inlining this into a loop in a parent could lead to 
12570                                          * stack overflows which is different behavior than the
12571                                          * non-inlined case, thus disable inlining in this case.
12572                                          */
12573                                         INLINE_FAILURE("localloc");
12574
12575                                 NEW_BBLOCK (cfg, non_zero_bb);
12576                                 NEW_BBLOCK (cfg, end_bb);
12577
12578                                 /* if size != zero */
12579                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
12580                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_zero_bb);
12581
12582                                 //size is zero, so result is NULL
12583                                 MONO_EMIT_NEW_PCONST (cfg, alloc_ptr, NULL);
12584                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12585
12586                                 MONO_START_BB (cfg, non_zero_bb);
12587                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
12588                                 ins->dreg = alloc_ptr;
12589                                 ins->sreg1 = sp [0]->dreg;
12590                                 ins->type = STACK_PTR;
12591                                 MONO_ADD_INS (cfg->cbb, ins);
12592
12593                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12594                                 if (init_locals)
12595                                         ins->flags |= MONO_INST_INIT;
12596
12597                                 MONO_START_BB (cfg, end_bb);
12598                                 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, alloc_preg (cfg), alloc_ptr);
12599                                 ins->type = STACK_PTR;
12600
12601                                 *sp++ = ins;
12602                                 ip += 2;
12603                                 break;
12604                         }
12605                         case CEE_ENDFILTER: {
12606                                 MonoExceptionClause *clause, *nearest;
12607                                 int cc;
12608
12609                                 CHECK_STACK (1);
12610                                 --sp;
12611                                 if ((sp != stack_start) || (sp [0]->type != STACK_I4)) 
12612                                         UNVERIFIED;
12613                                 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
12614                                 ins->sreg1 = (*sp)->dreg;
12615                                 MONO_ADD_INS (cfg->cbb, ins);
12616                                 start_new_bblock = 1;
12617                                 ip += 2;
12618
12619                                 nearest = NULL;
12620                                 for (cc = 0; cc < header->num_clauses; ++cc) {
12621                                         clause = &header->clauses [cc];
12622                                         if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
12623                                                 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
12624                                             (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
12625                                                 nearest = clause;
12626                                 }
12627                                 g_assert (nearest);
12628                                 if ((ip - header->code) != nearest->handler_offset)
12629                                         UNVERIFIED;
12630
12631                                 break;
12632                         }
12633                         case CEE_UNALIGNED_:
12634                                 ins_flag |= MONO_INST_UNALIGNED;
12635                                 /* FIXME: record alignment? we can assume 1 for now */
12636                                 CHECK_OPSIZE (3);
12637                                 ip += 3;
12638                                 break;
12639                         case CEE_VOLATILE_:
12640                                 ins_flag |= MONO_INST_VOLATILE;
12641                                 ip += 2;
12642                                 break;
12643                         case CEE_TAIL_:
12644                                 ins_flag   |= MONO_INST_TAILCALL;
12645                                 cfg->flags |= MONO_CFG_HAS_TAIL;
12646                                 /* Can't inline tail calls at this time */
12647                                 inline_costs += 100000;
12648                                 ip += 2;
12649                                 break;
12650                         case CEE_INITOBJ:
12651                                 CHECK_STACK (1);
12652                                 --sp;
12653                                 CHECK_OPSIZE (6);
12654                                 token = read32 (ip + 2);
12655                                 klass = mini_get_class (method, token, generic_context);
12656                                 CHECK_TYPELOAD (klass);
12657                                 if (generic_class_is_reference_type (cfg, klass))
12658                                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
12659                                 else
12660                                         mini_emit_initobj (cfg, *sp, NULL, klass);
12661                                 ip += 6;
12662                                 inline_costs += 1;
12663                                 break;
12664                         case CEE_CONSTRAINED_:
12665                                 CHECK_OPSIZE (6);
12666                                 token = read32 (ip + 2);
12667                                 constrained_class = mini_get_class (method, token, generic_context);
12668                                 CHECK_TYPELOAD (constrained_class);
12669                                 ip += 6;
12670                                 break;
12671                         case CEE_CPBLK:
12672                         case CEE_INITBLK: {
12673                                 MonoInst *iargs [3];
12674                                 CHECK_STACK (3);
12675                                 sp -= 3;
12676
12677                                 /* Skip optimized paths for volatile operations. */
12678                                 if ((ip [1] == CEE_CPBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
12679                                         mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
12680                                 } else if ((ip [1] == CEE_INITBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5) && (sp [1]->opcode == OP_ICONST) && (sp [1]->inst_c0 == 0)) {
12681                                         /* emit_memset only works when val == 0 */
12682                                         mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
12683                                 } else {
12684                                         MonoInst *call;
12685                                         iargs [0] = sp [0];
12686                                         iargs [1] = sp [1];
12687                                         iargs [2] = sp [2];
12688                                         if (ip [1] == CEE_CPBLK) {
12689                                                 /*
12690                                                  * FIXME: It's unclear whether we should be emitting both the acquire
12691                                                  * and release barriers for cpblk. It is technically both a load and
12692                                                  * store operation, so it seems like that's the sensible thing to do.
12693                                                  *
12694                                                  * FIXME: We emit full barriers on both sides of the operation for
12695                                                  * simplicity. We should have a separate atomic memcpy method instead.
12696                                                  */
12697                                                 MonoMethod *memcpy_method = get_memcpy_method ();
12698
12699                                                 if (ins_flag & MONO_INST_VOLATILE)
12700                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12701
12702                                                 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
12703                                                 call->flags |= ins_flag;
12704
12705                                                 if (ins_flag & MONO_INST_VOLATILE)
12706                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
12707                                         } else {
12708                                                 MonoMethod *memset_method = get_memset_method ();
12709                                                 if (ins_flag & MONO_INST_VOLATILE) {
12710                                                         /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
12711                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
12712                                                 }
12713                                                 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
12714                                                 call->flags |= ins_flag;
12715                                         }
12716                                 }
12717                                 ip += 2;
12718                                 ins_flag = 0;
12719                                 inline_costs += 1;
12720                                 break;
12721                         }
12722                         case CEE_NO_:
12723                                 CHECK_OPSIZE (3);
12724                                 if (ip [2] & 0x1)
12725                                         ins_flag |= MONO_INST_NOTYPECHECK;
12726                                 if (ip [2] & 0x2)
12727                                         ins_flag |= MONO_INST_NORANGECHECK;
12728                                 /* we ignore the no-nullcheck for now since we
12729                                  * really do it explicitly only when doing callvirt->call
12730                                  */
12731                                 ip += 3;
12732                                 break;
12733                         case CEE_RETHROW: {
12734                                 MonoInst *load;
12735                                 int handler_offset = -1;
12736
12737                                 for (i = 0; i < header->num_clauses; ++i) {
12738                                         MonoExceptionClause *clause = &header->clauses [i];
12739                                         if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
12740                                                 handler_offset = clause->handler_offset;
12741                                                 break;
12742                                         }
12743                                 }
12744
12745                                 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
12746
12747                                 if (handler_offset == -1)
12748                                         UNVERIFIED;
12749
12750                                 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
12751                                 MONO_INST_NEW (cfg, ins, OP_RETHROW);
12752                                 ins->sreg1 = load->dreg;
12753                                 MONO_ADD_INS (cfg->cbb, ins);
12754
12755                                 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12756                                 MONO_ADD_INS (cfg->cbb, ins);
12757
12758                                 sp = stack_start;
12759                                 link_bblock (cfg, cfg->cbb, end_bblock);
12760                                 start_new_bblock = 1;
12761                                 ip += 2;
12762                                 break;
12763                         }
12764                         case CEE_SIZEOF: {
12765                                 guint32 val;
12766                                 int ialign;
12767
12768                                 CHECK_STACK_OVF (1);
12769                                 CHECK_OPSIZE (6);
12770                                 token = read32 (ip + 2);
12771                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
12772                                         MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
12773                                         CHECK_CFG_ERROR;
12774
12775                                         val = mono_type_size (type, &ialign);
12776                                 } else {
12777                                         MonoClass *klass = mini_get_class (method, token, generic_context);
12778                                         CHECK_TYPELOAD (klass);
12779
12780                                         val = mono_type_size (&klass->byval_arg, &ialign);
12781
12782                                         if (mini_is_gsharedvt_klass (klass))
12783                                                 GSHAREDVT_FAILURE (*ip);
12784                                 }
12785                                 EMIT_NEW_ICONST (cfg, ins, val);
12786                                 *sp++= ins;
12787                                 ip += 6;
12788                                 break;
12789                         }
12790                         case CEE_REFANYTYPE: {
12791                                 MonoInst *src_var, *src;
12792
12793                                 GSHAREDVT_FAILURE (*ip);
12794
12795                                 CHECK_STACK (1);
12796                                 --sp;
12797
12798                                 // FIXME:
12799                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12800                                 if (!src_var)
12801                                         src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12802                                 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12803                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
12804                                 *sp++ = ins;
12805                                 ip += 2;
12806                                 break;
12807                         }
12808                         case CEE_READONLY_:
12809                                 readonly = TRUE;
12810                                 ip += 2;
12811                                 break;
12812
12813                         case CEE_UNUSED56:
12814                         case CEE_UNUSED57:
12815                         case CEE_UNUSED70:
12816                         case CEE_UNUSED:
12817                         case CEE_UNUSED99:
12818                                 UNVERIFIED;
12819                                 
12820                         default:
12821                                 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
12822                                 UNVERIFIED;
12823                         }
12824                         break;
12825                 }
12826                 case CEE_UNUSED58:
12827                 case CEE_UNUSED1:
12828                         UNVERIFIED;
12829
12830                 default:
12831                         g_warning ("opcode 0x%02x not handled", *ip);
12832                         UNVERIFIED;
12833                 }
12834         }
12835         if (start_new_bblock != 1)
12836                 UNVERIFIED;
12837
12838         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
12839         if (cfg->cbb->next_bb) {
12840                 /* This could already be set because of inlining, #693905 */
12841                 MonoBasicBlock *bb = cfg->cbb;
12842
12843                 while (bb->next_bb)
12844                         bb = bb->next_bb;
12845                 bb->next_bb = end_bblock;
12846         } else {
12847                 cfg->cbb->next_bb = end_bblock;
12848         }
12849
12850         if (cfg->method == method && cfg->domainvar) {
12851                 MonoInst *store;
12852                 MonoInst *get_domain;
12853
12854                 cfg->cbb = init_localsbb;
12855
12856                 get_domain = mono_create_tls_get (cfg, TLS_KEY_DOMAIN);
12857                 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
12858                 MONO_ADD_INS (cfg->cbb, store);
12859         }
12860
12861 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
12862         if (cfg->compile_aot)
12863                 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
12864                 mono_get_got_var (cfg);
12865 #endif
12866
12867         if (cfg->method == method && cfg->got_var)
12868                 mono_emit_load_got_addr (cfg);
12869
12870         if (init_localsbb) {
12871                 cfg->cbb = init_localsbb;
12872                 cfg->ip = NULL;
12873                 for (i = 0; i < header->num_locals; ++i) {
12874                         emit_init_local (cfg, i, header->locals [i], init_locals);
12875                 }
12876         }
12877
12878         if (cfg->init_ref_vars && cfg->method == method) {
12879                 /* Emit initialization for ref vars */
12880                 // FIXME: Avoid duplication initialization for IL locals.
12881                 for (i = 0; i < cfg->num_varinfo; ++i) {
12882                         MonoInst *ins = cfg->varinfo [i];
12883
12884                         if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
12885                                 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
12886                 }
12887         }
12888
12889         if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
12890                 cfg->cbb = init_localsbb;
12891                 emit_push_lmf (cfg);
12892         }
12893
12894         cfg->cbb = init_localsbb;
12895         emit_instrumentation_call (cfg, mono_profiler_method_enter);
12896
12897         if (seq_points) {
12898                 MonoBasicBlock *bb;
12899
12900                 /*
12901                  * Make seq points at backward branch targets interruptable.
12902                  */
12903                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12904                         if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
12905                                 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
12906         }
12907
12908         /* Add a sequence point for method entry/exit events */
12909         if (seq_points && cfg->gen_sdb_seq_points) {
12910                 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
12911                 MONO_ADD_INS (init_localsbb, ins);
12912                 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
12913                 MONO_ADD_INS (cfg->bb_exit, ins);
12914         }
12915
12916         /*
12917          * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
12918          * the code they refer to was dead (#11880).
12919          */
12920         if (sym_seq_points) {
12921                 for (i = 0; i < header->code_size; ++i) {
12922                         if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
12923                                 MonoInst *ins;
12924
12925                                 NEW_SEQ_POINT (cfg, ins, i, FALSE);
12926                                 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
12927                         }
12928                 }
12929         }
12930
12931         cfg->ip = NULL;
12932
12933         if (cfg->method == method) {
12934                 MonoBasicBlock *bb;
12935                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12936                         if (bb == cfg->bb_init)
12937                                 bb->region = -1;
12938                         else
12939                                 bb->region = mono_find_block_region (cfg, bb->real_offset);
12940                         if (cfg->spvars)
12941                                 mono_create_spvar_for_region (cfg, bb->region);
12942                         if (cfg->verbose_level > 2)
12943                                 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
12944                 }
12945         } else {
12946                 MonoBasicBlock *bb;
12947                 /* get_most_deep_clause () in mini-llvm.c depends on this for inlined bblocks */
12948                 for (bb = start_bblock; bb != end_bblock; bb  = bb->next_bb) {
12949                         bb->real_offset = inline_offset;
12950                 }
12951         }
12952
12953         if (inline_costs < 0) {
12954                 char *mname;
12955
12956                 /* Method is too large */
12957                 mname = mono_method_full_name (method, TRUE);
12958                 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
12959                 g_free (mname);
12960         }
12961
12962         if ((cfg->verbose_level > 2) && (cfg->method == method)) 
12963                 mono_print_code (cfg, "AFTER METHOD-TO-IR");
12964
12965         goto cleanup;
12966
12967 mono_error_exit:
12968         g_assert (!mono_error_ok (&cfg->error));
12969         goto cleanup;
12970  
12971  exception_exit:
12972         g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
12973         goto cleanup;
12974
12975  unverified:
12976         set_exception_type_from_invalid_il (cfg, method, ip);
12977         goto cleanup;
12978
12979  cleanup:
12980         g_slist_free (class_inits);
12981         mono_basic_block_free (original_bb);
12982         cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
12983         if (cfg->exception_type)
12984                 return -1;
12985         else
12986                 return inline_costs;
12987 }
12988
12989 static int
12990 store_membase_reg_to_store_membase_imm (int opcode)
12991 {
12992         switch (opcode) {
12993         case OP_STORE_MEMBASE_REG:
12994                 return OP_STORE_MEMBASE_IMM;
12995         case OP_STOREI1_MEMBASE_REG:
12996                 return OP_STOREI1_MEMBASE_IMM;
12997         case OP_STOREI2_MEMBASE_REG:
12998                 return OP_STOREI2_MEMBASE_IMM;
12999         case OP_STOREI4_MEMBASE_REG:
13000                 return OP_STOREI4_MEMBASE_IMM;
13001         case OP_STOREI8_MEMBASE_REG:
13002                 return OP_STOREI8_MEMBASE_IMM;
13003         default:
13004                 g_assert_not_reached ();
13005         }
13006
13007         return -1;
13008 }               
13009
13010 int
13011 mono_op_to_op_imm (int opcode)
13012 {
13013         switch (opcode) {
13014         case OP_IADD:
13015                 return OP_IADD_IMM;
13016         case OP_ISUB:
13017                 return OP_ISUB_IMM;
13018         case OP_IDIV:
13019                 return OP_IDIV_IMM;
13020         case OP_IDIV_UN:
13021                 return OP_IDIV_UN_IMM;
13022         case OP_IREM:
13023                 return OP_IREM_IMM;
13024         case OP_IREM_UN:
13025                 return OP_IREM_UN_IMM;
13026         case OP_IMUL:
13027                 return OP_IMUL_IMM;
13028         case OP_IAND:
13029                 return OP_IAND_IMM;
13030         case OP_IOR:
13031                 return OP_IOR_IMM;
13032         case OP_IXOR:
13033                 return OP_IXOR_IMM;
13034         case OP_ISHL:
13035                 return OP_ISHL_IMM;
13036         case OP_ISHR:
13037                 return OP_ISHR_IMM;
13038         case OP_ISHR_UN:
13039                 return OP_ISHR_UN_IMM;
13040
13041         case OP_LADD:
13042                 return OP_LADD_IMM;
13043         case OP_LSUB:
13044                 return OP_LSUB_IMM;
13045         case OP_LAND:
13046                 return OP_LAND_IMM;
13047         case OP_LOR:
13048                 return OP_LOR_IMM;
13049         case OP_LXOR:
13050                 return OP_LXOR_IMM;
13051         case OP_LSHL:
13052                 return OP_LSHL_IMM;
13053         case OP_LSHR:
13054                 return OP_LSHR_IMM;
13055         case OP_LSHR_UN:
13056                 return OP_LSHR_UN_IMM;
13057 #if SIZEOF_REGISTER == 8
13058         case OP_LREM:
13059                 return OP_LREM_IMM;
13060 #endif
13061
13062         case OP_COMPARE:
13063                 return OP_COMPARE_IMM;
13064         case OP_ICOMPARE:
13065                 return OP_ICOMPARE_IMM;
13066         case OP_LCOMPARE:
13067                 return OP_LCOMPARE_IMM;
13068
13069         case OP_STORE_MEMBASE_REG:
13070                 return OP_STORE_MEMBASE_IMM;
13071         case OP_STOREI1_MEMBASE_REG:
13072                 return OP_STOREI1_MEMBASE_IMM;
13073         case OP_STOREI2_MEMBASE_REG:
13074                 return OP_STOREI2_MEMBASE_IMM;
13075         case OP_STOREI4_MEMBASE_REG:
13076                 return OP_STOREI4_MEMBASE_IMM;
13077
13078 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13079         case OP_X86_PUSH:
13080                 return OP_X86_PUSH_IMM;
13081         case OP_X86_COMPARE_MEMBASE_REG:
13082                 return OP_X86_COMPARE_MEMBASE_IMM;
13083 #endif
13084 #if defined(TARGET_AMD64)
13085         case OP_AMD64_ICOMPARE_MEMBASE_REG:
13086                 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13087 #endif
13088         case OP_VOIDCALL_REG:
13089                 return OP_VOIDCALL;
13090         case OP_CALL_REG:
13091                 return OP_CALL;
13092         case OP_LCALL_REG:
13093                 return OP_LCALL;
13094         case OP_FCALL_REG:
13095                 return OP_FCALL;
13096         case OP_LOCALLOC:
13097                 return OP_LOCALLOC_IMM;
13098         }
13099
13100         return -1;
13101 }
13102
13103 static int
13104 ldind_to_load_membase (int opcode)
13105 {
13106         switch (opcode) {
13107         case CEE_LDIND_I1:
13108                 return OP_LOADI1_MEMBASE;
13109         case CEE_LDIND_U1:
13110                 return OP_LOADU1_MEMBASE;
13111         case CEE_LDIND_I2:
13112                 return OP_LOADI2_MEMBASE;
13113         case CEE_LDIND_U2:
13114                 return OP_LOADU2_MEMBASE;
13115         case CEE_LDIND_I4:
13116                 return OP_LOADI4_MEMBASE;
13117         case CEE_LDIND_U4:
13118                 return OP_LOADU4_MEMBASE;
13119         case CEE_LDIND_I:
13120                 return OP_LOAD_MEMBASE;
13121         case CEE_LDIND_REF:
13122                 return OP_LOAD_MEMBASE;
13123         case CEE_LDIND_I8:
13124                 return OP_LOADI8_MEMBASE;
13125         case CEE_LDIND_R4:
13126                 return OP_LOADR4_MEMBASE;
13127         case CEE_LDIND_R8:
13128                 return OP_LOADR8_MEMBASE;
13129         default:
13130                 g_assert_not_reached ();
13131         }
13132
13133         return -1;
13134 }
13135
13136 static int
13137 stind_to_store_membase (int opcode)
13138 {
13139         switch (opcode) {
13140         case CEE_STIND_I1:
13141                 return OP_STOREI1_MEMBASE_REG;
13142         case CEE_STIND_I2:
13143                 return OP_STOREI2_MEMBASE_REG;
13144         case CEE_STIND_I4:
13145                 return OP_STOREI4_MEMBASE_REG;
13146         case CEE_STIND_I:
13147         case CEE_STIND_REF:
13148                 return OP_STORE_MEMBASE_REG;
13149         case CEE_STIND_I8:
13150                 return OP_STOREI8_MEMBASE_REG;
13151         case CEE_STIND_R4:
13152                 return OP_STORER4_MEMBASE_REG;
13153         case CEE_STIND_R8:
13154                 return OP_STORER8_MEMBASE_REG;
13155         default:
13156                 g_assert_not_reached ();
13157         }
13158
13159         return -1;
13160 }
13161
13162 int
13163 mono_load_membase_to_load_mem (int opcode)
13164 {
13165         // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13166 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13167         switch (opcode) {
13168         case OP_LOAD_MEMBASE:
13169                 return OP_LOAD_MEM;
13170         case OP_LOADU1_MEMBASE:
13171                 return OP_LOADU1_MEM;
13172         case OP_LOADU2_MEMBASE:
13173                 return OP_LOADU2_MEM;
13174         case OP_LOADI4_MEMBASE:
13175                 return OP_LOADI4_MEM;
13176         case OP_LOADU4_MEMBASE:
13177                 return OP_LOADU4_MEM;
13178 #if SIZEOF_REGISTER == 8
13179         case OP_LOADI8_MEMBASE:
13180                 return OP_LOADI8_MEM;
13181 #endif
13182         }
13183 #endif
13184
13185         return -1;
13186 }
13187
13188 static inline int
13189 op_to_op_dest_membase (int store_opcode, int opcode)
13190 {
13191 #if defined(TARGET_X86)
13192         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13193                 return -1;
13194
13195         switch (opcode) {
13196         case OP_IADD:
13197                 return OP_X86_ADD_MEMBASE_REG;
13198         case OP_ISUB:
13199                 return OP_X86_SUB_MEMBASE_REG;
13200         case OP_IAND:
13201                 return OP_X86_AND_MEMBASE_REG;
13202         case OP_IOR:
13203                 return OP_X86_OR_MEMBASE_REG;
13204         case OP_IXOR:
13205                 return OP_X86_XOR_MEMBASE_REG;
13206         case OP_ADD_IMM:
13207         case OP_IADD_IMM:
13208                 return OP_X86_ADD_MEMBASE_IMM;
13209         case OP_SUB_IMM:
13210         case OP_ISUB_IMM:
13211                 return OP_X86_SUB_MEMBASE_IMM;
13212         case OP_AND_IMM:
13213         case OP_IAND_IMM:
13214                 return OP_X86_AND_MEMBASE_IMM;
13215         case OP_OR_IMM:
13216         case OP_IOR_IMM:
13217                 return OP_X86_OR_MEMBASE_IMM;
13218         case OP_XOR_IMM:
13219         case OP_IXOR_IMM:
13220                 return OP_X86_XOR_MEMBASE_IMM;
13221         case OP_MOVE:
13222                 return OP_NOP;
13223         }
13224 #endif
13225
13226 #if defined(TARGET_AMD64)
13227         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13228                 return -1;
13229
13230         switch (opcode) {
13231         case OP_IADD:
13232                 return OP_X86_ADD_MEMBASE_REG;
13233         case OP_ISUB:
13234                 return OP_X86_SUB_MEMBASE_REG;
13235         case OP_IAND:
13236                 return OP_X86_AND_MEMBASE_REG;
13237         case OP_IOR:
13238                 return OP_X86_OR_MEMBASE_REG;
13239         case OP_IXOR:
13240                 return OP_X86_XOR_MEMBASE_REG;
13241         case OP_IADD_IMM:
13242                 return OP_X86_ADD_MEMBASE_IMM;
13243         case OP_ISUB_IMM:
13244                 return OP_X86_SUB_MEMBASE_IMM;
13245         case OP_IAND_IMM:
13246                 return OP_X86_AND_MEMBASE_IMM;
13247         case OP_IOR_IMM:
13248                 return OP_X86_OR_MEMBASE_IMM;
13249         case OP_IXOR_IMM:
13250                 return OP_X86_XOR_MEMBASE_IMM;
13251         case OP_LADD:
13252                 return OP_AMD64_ADD_MEMBASE_REG;
13253         case OP_LSUB:
13254                 return OP_AMD64_SUB_MEMBASE_REG;
13255         case OP_LAND:
13256                 return OP_AMD64_AND_MEMBASE_REG;
13257         case OP_LOR:
13258                 return OP_AMD64_OR_MEMBASE_REG;
13259         case OP_LXOR:
13260                 return OP_AMD64_XOR_MEMBASE_REG;
13261         case OP_ADD_IMM:
13262         case OP_LADD_IMM:
13263                 return OP_AMD64_ADD_MEMBASE_IMM;
13264         case OP_SUB_IMM:
13265         case OP_LSUB_IMM:
13266                 return OP_AMD64_SUB_MEMBASE_IMM;
13267         case OP_AND_IMM:
13268         case OP_LAND_IMM:
13269                 return OP_AMD64_AND_MEMBASE_IMM;
13270         case OP_OR_IMM:
13271         case OP_LOR_IMM:
13272                 return OP_AMD64_OR_MEMBASE_IMM;
13273         case OP_XOR_IMM:
13274         case OP_LXOR_IMM:
13275                 return OP_AMD64_XOR_MEMBASE_IMM;
13276         case OP_MOVE:
13277                 return OP_NOP;
13278         }
13279 #endif
13280
13281         return -1;
13282 }
13283
13284 static inline int
13285 op_to_op_store_membase (int store_opcode, int opcode)
13286 {
13287 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13288         switch (opcode) {
13289         case OP_ICEQ:
13290                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13291                         return OP_X86_SETEQ_MEMBASE;
13292         case OP_CNE:
13293                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13294                         return OP_X86_SETNE_MEMBASE;
13295         }
13296 #endif
13297
13298         return -1;
13299 }
13300
13301 static inline int
13302 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13303 {
13304 #ifdef TARGET_X86
13305         /* FIXME: This has sign extension issues */
13306         /*
13307         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13308                 return OP_X86_COMPARE_MEMBASE8_IMM;
13309         */
13310
13311         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13312                 return -1;
13313
13314         switch (opcode) {
13315         case OP_X86_PUSH:
13316                 return OP_X86_PUSH_MEMBASE;
13317         case OP_COMPARE_IMM:
13318         case OP_ICOMPARE_IMM:
13319                 return OP_X86_COMPARE_MEMBASE_IMM;
13320         case OP_COMPARE:
13321         case OP_ICOMPARE:
13322                 return OP_X86_COMPARE_MEMBASE_REG;
13323         }
13324 #endif
13325
13326 #ifdef TARGET_AMD64
13327         /* FIXME: This has sign extension issues */
13328         /*
13329         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13330                 return OP_X86_COMPARE_MEMBASE8_IMM;
13331         */
13332
13333         switch (opcode) {
13334         case OP_X86_PUSH:
13335                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13336                         return OP_X86_PUSH_MEMBASE;
13337                 break;
13338                 /* FIXME: This only works for 32 bit immediates
13339         case OP_COMPARE_IMM:
13340         case OP_LCOMPARE_IMM:
13341                 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
13342                         return OP_AMD64_COMPARE_MEMBASE_IMM;
13343                 */
13344         case OP_ICOMPARE_IMM:
13345                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13346                         return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13347                 break;
13348         case OP_COMPARE:
13349         case OP_LCOMPARE:
13350                 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
13351                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
13352                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
13353                         return OP_AMD64_COMPARE_MEMBASE_REG;
13354                 break;
13355         case OP_ICOMPARE:
13356                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
13357                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
13358                 break;
13359         }
13360 #endif
13361
13362         return -1;
13363 }
13364
13365 static inline int
13366 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
13367 {
13368 #ifdef TARGET_X86
13369         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13370                 return -1;
13371         
13372         switch (opcode) {
13373         case OP_COMPARE:
13374         case OP_ICOMPARE:
13375                 return OP_X86_COMPARE_REG_MEMBASE;
13376         case OP_IADD:
13377                 return OP_X86_ADD_REG_MEMBASE;
13378         case OP_ISUB:
13379                 return OP_X86_SUB_REG_MEMBASE;
13380         case OP_IAND:
13381                 return OP_X86_AND_REG_MEMBASE;
13382         case OP_IOR:
13383                 return OP_X86_OR_REG_MEMBASE;
13384         case OP_IXOR:
13385                 return OP_X86_XOR_REG_MEMBASE;
13386         }
13387 #endif
13388
13389 #ifdef TARGET_AMD64
13390         if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
13391                 switch (opcode) {
13392                 case OP_ICOMPARE:
13393                         return OP_AMD64_ICOMPARE_REG_MEMBASE;
13394                 case OP_IADD:
13395                         return OP_X86_ADD_REG_MEMBASE;
13396                 case OP_ISUB:
13397                         return OP_X86_SUB_REG_MEMBASE;
13398                 case OP_IAND:
13399                         return OP_X86_AND_REG_MEMBASE;
13400                 case OP_IOR:
13401                         return OP_X86_OR_REG_MEMBASE;
13402                 case OP_IXOR:
13403                         return OP_X86_XOR_REG_MEMBASE;
13404                 }
13405         } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
13406                 switch (opcode) {
13407                 case OP_COMPARE:
13408                 case OP_LCOMPARE:
13409                         return OP_AMD64_COMPARE_REG_MEMBASE;
13410                 case OP_LADD:
13411                         return OP_AMD64_ADD_REG_MEMBASE;
13412                 case OP_LSUB:
13413                         return OP_AMD64_SUB_REG_MEMBASE;
13414                 case OP_LAND:
13415                         return OP_AMD64_AND_REG_MEMBASE;
13416                 case OP_LOR:
13417                         return OP_AMD64_OR_REG_MEMBASE;
13418                 case OP_LXOR:
13419                         return OP_AMD64_XOR_REG_MEMBASE;
13420                 }
13421         }
13422 #endif
13423
13424         return -1;
13425 }
13426
13427 int
13428 mono_op_to_op_imm_noemul (int opcode)
13429 {
13430         switch (opcode) {
13431 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
13432         case OP_LSHR:
13433         case OP_LSHL:
13434         case OP_LSHR_UN:
13435                 return -1;
13436 #endif
13437 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
13438         case OP_IDIV:
13439         case OP_IDIV_UN:
13440         case OP_IREM:
13441         case OP_IREM_UN:
13442                 return -1;
13443 #endif
13444 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
13445         case OP_IMUL:
13446                 return -1;
13447 #endif
13448         default:
13449                 return mono_op_to_op_imm (opcode);
13450         }
13451 }
13452
13453 /**
13454  * mono_handle_global_vregs:
13455  *
13456  *   Make vregs used in more than one bblock 'global', i.e. allocate a variable
13457  * for them.
13458  */
13459 void
13460 mono_handle_global_vregs (MonoCompile *cfg)
13461 {
13462         gint32 *vreg_to_bb;
13463         MonoBasicBlock *bb;
13464         int i, pos;
13465
13466         vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
13467
13468 #ifdef MONO_ARCH_SIMD_INTRINSICS
13469         if (cfg->uses_simd_intrinsics)
13470                 mono_simd_simplify_indirection (cfg);
13471 #endif
13472
13473         /* Find local vregs used in more than one bb */
13474         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13475                 MonoInst *ins = bb->code;       
13476                 int block_num = bb->block_num;
13477
13478                 if (cfg->verbose_level > 2)
13479                         printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
13480
13481                 cfg->cbb = bb;
13482                 for (; ins; ins = ins->next) {
13483                         const char *spec = INS_INFO (ins->opcode);
13484                         int regtype = 0, regindex;
13485                         gint32 prev_bb;
13486
13487                         if (G_UNLIKELY (cfg->verbose_level > 2))
13488                                 mono_print_ins (ins);
13489
13490                         g_assert (ins->opcode >= MONO_CEE_LAST);
13491
13492                         for (regindex = 0; regindex < 4; regindex ++) {
13493                                 int vreg = 0;
13494
13495                                 if (regindex == 0) {
13496                                         regtype = spec [MONO_INST_DEST];
13497                                         if (regtype == ' ')
13498                                                 continue;
13499                                         vreg = ins->dreg;
13500                                 } else if (regindex == 1) {
13501                                         regtype = spec [MONO_INST_SRC1];
13502                                         if (regtype == ' ')
13503                                                 continue;
13504                                         vreg = ins->sreg1;
13505                                 } else if (regindex == 2) {
13506                                         regtype = spec [MONO_INST_SRC2];
13507                                         if (regtype == ' ')
13508                                                 continue;
13509                                         vreg = ins->sreg2;
13510                                 } else if (regindex == 3) {
13511                                         regtype = spec [MONO_INST_SRC3];
13512                                         if (regtype == ' ')
13513                                                 continue;
13514                                         vreg = ins->sreg3;
13515                                 }
13516
13517 #if SIZEOF_REGISTER == 4
13518                                 /* In the LLVM case, the long opcodes are not decomposed */
13519                                 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
13520                                         /*
13521                                          * Since some instructions reference the original long vreg,
13522                                          * and some reference the two component vregs, it is quite hard
13523                                          * to determine when it needs to be global. So be conservative.
13524                                          */
13525                                         if (!get_vreg_to_inst (cfg, vreg)) {
13526                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13527
13528                                                 if (cfg->verbose_level > 2)
13529                                                         printf ("LONG VREG R%d made global.\n", vreg);
13530                                         }
13531
13532                                         /*
13533                                          * Make the component vregs volatile since the optimizations can
13534                                          * get confused otherwise.
13535                                          */
13536                                         get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
13537                                         get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
13538                                 }
13539 #endif
13540
13541                                 g_assert (vreg != -1);
13542
13543                                 prev_bb = vreg_to_bb [vreg];
13544                                 if (prev_bb == 0) {
13545                                         /* 0 is a valid block num */
13546                                         vreg_to_bb [vreg] = block_num + 1;
13547                                 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
13548                                         if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
13549                                                 continue;
13550
13551                                         if (!get_vreg_to_inst (cfg, vreg)) {
13552                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
13553                                                         printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
13554
13555                                                 switch (regtype) {
13556                                                 case 'i':
13557                                                         if (vreg_is_ref (cfg, vreg))
13558                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
13559                                                         else
13560                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
13561                                                         break;
13562                                                 case 'l':
13563                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
13564                                                         break;
13565                                                 case 'f':
13566                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
13567                                                         break;
13568                                                 case 'v':
13569                                                 case 'x':
13570                                                         mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
13571                                                         break;
13572                                                 default:
13573                                                         g_assert_not_reached ();
13574                                                 }
13575                                         }
13576
13577                                         /* Flag as having been used in more than one bb */
13578                                         vreg_to_bb [vreg] = -1;
13579                                 }
13580                         }
13581                 }
13582         }
13583
13584         /* If a variable is used in only one bblock, convert it into a local vreg */
13585         for (i = 0; i < cfg->num_varinfo; i++) {
13586                 MonoInst *var = cfg->varinfo [i];
13587                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
13588
13589                 switch (var->type) {
13590                 case STACK_I4:
13591                 case STACK_OBJ:
13592                 case STACK_PTR:
13593                 case STACK_MP:
13594                 case STACK_VTYPE:
13595 #if SIZEOF_REGISTER == 8
13596                 case STACK_I8:
13597 #endif
13598 #if !defined(TARGET_X86)
13599                 /* Enabling this screws up the fp stack on x86 */
13600                 case STACK_R8:
13601 #endif
13602                         if (mono_arch_is_soft_float ())
13603                                 break;
13604
13605                         /*
13606                         if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
13607                                 break;
13608                         */
13609
13610                         /* Arguments are implicitly global */
13611                         /* Putting R4 vars into registers doesn't work currently */
13612                         /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
13613                         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) {
13614                                 /* 
13615                                  * Make that the variable's liveness interval doesn't contain a call, since
13616                                  * that would cause the lvreg to be spilled, making the whole optimization
13617                                  * useless.
13618                                  */
13619                                 /* This is too slow for JIT compilation */
13620 #if 0
13621                                 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
13622                                         MonoInst *ins;
13623                                         int def_index, call_index, ins_index;
13624                                         gboolean spilled = FALSE;
13625
13626                                         def_index = -1;
13627                                         call_index = -1;
13628                                         ins_index = 0;
13629                                         for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
13630                                                 const char *spec = INS_INFO (ins->opcode);
13631
13632                                                 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
13633                                                         def_index = ins_index;
13634
13635                                                 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
13636                                                         ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
13637                                                         if (call_index > def_index) {
13638                                                                 spilled = TRUE;
13639                                                                 break;
13640                                                         }
13641                                                 }
13642
13643                                                 if (MONO_IS_CALL (ins))
13644                                                         call_index = ins_index;
13645
13646                                                 ins_index ++;
13647                                         }
13648
13649                                         if (spilled)
13650                                                 break;
13651                                 }
13652 #endif
13653
13654                                 if (G_UNLIKELY (cfg->verbose_level > 2))
13655                                         printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
13656                                 var->flags |= MONO_INST_IS_DEAD;
13657                                 cfg->vreg_to_inst [var->dreg] = NULL;
13658                         }
13659                         break;
13660                 }
13661         }
13662
13663         /* 
13664          * Compress the varinfo and vars tables so the liveness computation is faster and
13665          * takes up less space.
13666          */
13667         pos = 0;
13668         for (i = 0; i < cfg->num_varinfo; ++i) {
13669                 MonoInst *var = cfg->varinfo [i];
13670                 if (pos < i && cfg->locals_start == i)
13671                         cfg->locals_start = pos;
13672                 if (!(var->flags & MONO_INST_IS_DEAD)) {
13673                         if (pos < i) {
13674                                 cfg->varinfo [pos] = cfg->varinfo [i];
13675                                 cfg->varinfo [pos]->inst_c0 = pos;
13676                                 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
13677                                 cfg->vars [pos].idx = pos;
13678 #if SIZEOF_REGISTER == 4
13679                                 if (cfg->varinfo [pos]->type == STACK_I8) {
13680                                         /* Modify the two component vars too */
13681                                         MonoInst *var1;
13682
13683                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
13684                                         var1->inst_c0 = pos;
13685                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
13686                                         var1->inst_c0 = pos;
13687                                 }
13688 #endif
13689                         }
13690                         pos ++;
13691                 }
13692         }
13693         cfg->num_varinfo = pos;
13694         if (cfg->locals_start > cfg->num_varinfo)
13695                 cfg->locals_start = cfg->num_varinfo;
13696 }
13697
13698 /*
13699  * mono_allocate_gsharedvt_vars:
13700  *
13701  *   Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
13702  * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
13703  */
13704 void
13705 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
13706 {
13707         int i;
13708
13709         cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
13710
13711         for (i = 0; i < cfg->num_varinfo; ++i) {
13712                 MonoInst *ins = cfg->varinfo [i];
13713                 int idx;
13714
13715                 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
13716                         if (i >= cfg->locals_start) {
13717                                 /* Local */
13718                                 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
13719                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
13720                                 ins->opcode = OP_GSHAREDVT_LOCAL;
13721                                 ins->inst_imm = idx;
13722                         } else {
13723                                 /* Arg */
13724                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
13725                                 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
13726                         }
13727                 }
13728         }
13729 }
13730
13731 /**
13732  * mono_spill_global_vars:
13733  *
13734  *   Generate spill code for variables which are not allocated to registers, 
13735  * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
13736  * code is generated which could be optimized by the local optimization passes.
13737  */
13738 void
13739 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
13740 {
13741         MonoBasicBlock *bb;
13742         char spec2 [16];
13743         int orig_next_vreg;
13744         guint32 *vreg_to_lvreg;
13745         guint32 *lvregs;
13746         guint32 i, lvregs_len;
13747         gboolean dest_has_lvreg = FALSE;
13748         MonoStackType stacktypes [128];
13749         MonoInst **live_range_start, **live_range_end;
13750         MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
13751
13752         *need_local_opts = FALSE;
13753
13754         memset (spec2, 0, sizeof (spec2));
13755
13756         /* FIXME: Move this function to mini.c */
13757         stacktypes ['i'] = STACK_PTR;
13758         stacktypes ['l'] = STACK_I8;
13759         stacktypes ['f'] = STACK_R8;
13760 #ifdef MONO_ARCH_SIMD_INTRINSICS
13761         stacktypes ['x'] = STACK_VTYPE;
13762 #endif
13763
13764 #if SIZEOF_REGISTER == 4
13765         /* Create MonoInsts for longs */
13766         for (i = 0; i < cfg->num_varinfo; i++) {
13767                 MonoInst *ins = cfg->varinfo [i];
13768
13769                 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
13770                         switch (ins->type) {
13771                         case STACK_R8:
13772                         case STACK_I8: {
13773                                 MonoInst *tree;
13774
13775                                 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
13776                                         break;
13777
13778                                 g_assert (ins->opcode == OP_REGOFFSET);
13779
13780                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
13781                                 g_assert (tree);
13782                                 tree->opcode = OP_REGOFFSET;
13783                                 tree->inst_basereg = ins->inst_basereg;
13784                                 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
13785
13786                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
13787                                 g_assert (tree);
13788                                 tree->opcode = OP_REGOFFSET;
13789                                 tree->inst_basereg = ins->inst_basereg;
13790                                 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
13791                                 break;
13792                         }
13793                         default:
13794                                 break;
13795                         }
13796                 }
13797         }
13798 #endif
13799
13800         if (cfg->compute_gc_maps) {
13801                 /* registers need liveness info even for !non refs */
13802                 for (i = 0; i < cfg->num_varinfo; i++) {
13803                         MonoInst *ins = cfg->varinfo [i];
13804
13805                         if (ins->opcode == OP_REGVAR)
13806                                 ins->flags |= MONO_INST_GC_TRACK;
13807                 }
13808         }
13809                 
13810         /* FIXME: widening and truncation */
13811
13812         /*
13813          * As an optimization, when a variable allocated to the stack is first loaded into 
13814          * an lvreg, we will remember the lvreg and use it the next time instead of loading
13815          * the variable again.
13816          */
13817         orig_next_vreg = cfg->next_vreg;
13818         vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
13819         lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
13820         lvregs_len = 0;
13821
13822         /* 
13823          * These arrays contain the first and last instructions accessing a given
13824          * variable.
13825          * Since we emit bblocks in the same order we process them here, and we
13826          * don't split live ranges, these will precisely describe the live range of
13827          * the variable, i.e. the instruction range where a valid value can be found
13828          * in the variables location.
13829          * The live range is computed using the liveness info computed by the liveness pass.
13830          * We can't use vmv->range, since that is an abstract live range, and we need
13831          * one which is instruction precise.
13832          * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
13833          */
13834         /* FIXME: Only do this if debugging info is requested */
13835         live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
13836         live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
13837         live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13838         live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
13839         
13840         /* Add spill loads/stores */
13841         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13842                 MonoInst *ins;
13843
13844                 if (cfg->verbose_level > 2)
13845                         printf ("\nSPILL BLOCK %d:\n", bb->block_num);
13846
13847                 /* Clear vreg_to_lvreg array */
13848                 for (i = 0; i < lvregs_len; i++)
13849                         vreg_to_lvreg [lvregs [i]] = 0;
13850                 lvregs_len = 0;
13851
13852                 cfg->cbb = bb;
13853                 MONO_BB_FOR_EACH_INS (bb, ins) {
13854                         const char *spec = INS_INFO (ins->opcode);
13855                         int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
13856                         gboolean store, no_lvreg;
13857                         int sregs [MONO_MAX_SRC_REGS];
13858
13859                         if (G_UNLIKELY (cfg->verbose_level > 2))
13860                                 mono_print_ins (ins);
13861
13862                         if (ins->opcode == OP_NOP)
13863                                 continue;
13864
13865                         /* 
13866                          * We handle LDADDR here as well, since it can only be decomposed
13867                          * when variable addresses are known.
13868                          */
13869                         if (ins->opcode == OP_LDADDR) {
13870                                 MonoInst *var = (MonoInst *)ins->inst_p0;
13871
13872                                 if (var->opcode == OP_VTARG_ADDR) {
13873                                         /* Happens on SPARC/S390 where vtypes are passed by reference */
13874                                         MonoInst *vtaddr = var->inst_left;
13875                                         if (vtaddr->opcode == OP_REGVAR) {
13876                                                 ins->opcode = OP_MOVE;
13877                                                 ins->sreg1 = vtaddr->dreg;
13878                                         }
13879                                         else if (var->inst_left->opcode == OP_REGOFFSET) {
13880                                                 ins->opcode = OP_LOAD_MEMBASE;
13881                                                 ins->inst_basereg = vtaddr->inst_basereg;
13882                                                 ins->inst_offset = vtaddr->inst_offset;
13883                                         } else
13884                                                 NOT_IMPLEMENTED;
13885                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
13886                                         /* gsharedvt arg passed by ref */
13887                                         g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
13888
13889                                         ins->opcode = OP_LOAD_MEMBASE;
13890                                         ins->inst_basereg = var->inst_basereg;
13891                                         ins->inst_offset = var->inst_offset;
13892                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
13893                                         MonoInst *load, *load2, *load3;
13894                                         int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
13895                                         int reg1, reg2, reg3;
13896                                         MonoInst *info_var = cfg->gsharedvt_info_var;
13897                                         MonoInst *locals_var = cfg->gsharedvt_locals_var;
13898
13899                                         /*
13900                                          * gsharedvt local.
13901                                          * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
13902                                          */
13903
13904                                         g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
13905
13906                                         g_assert (info_var);
13907                                         g_assert (locals_var);
13908
13909                                         /* Mark the instruction used to compute the locals var as used */
13910                                         cfg->gsharedvt_locals_var_ins = NULL;
13911
13912                                         /* Load the offset */
13913                                         if (info_var->opcode == OP_REGOFFSET) {
13914                                                 reg1 = alloc_ireg (cfg);
13915                                                 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
13916                                         } else if (info_var->opcode == OP_REGVAR) {
13917                                                 load = NULL;
13918                                                 reg1 = info_var->dreg;
13919                                         } else {
13920                                                 g_assert_not_reached ();
13921                                         }
13922                                         reg2 = alloc_ireg (cfg);
13923                                         NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
13924                                         /* Load the locals area address */
13925                                         reg3 = alloc_ireg (cfg);
13926                                         if (locals_var->opcode == OP_REGOFFSET) {
13927                                                 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
13928                                         } else if (locals_var->opcode == OP_REGVAR) {
13929                                                 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
13930                                         } else {
13931                                                 g_assert_not_reached ();
13932                                         }
13933                                         /* Compute the address */
13934                                         ins->opcode = OP_PADD;
13935                                         ins->sreg1 = reg3;
13936                                         ins->sreg2 = reg2;
13937
13938                                         mono_bblock_insert_before_ins (bb, ins, load3);
13939                                         mono_bblock_insert_before_ins (bb, load3, load2);
13940                                         if (load)
13941                                                 mono_bblock_insert_before_ins (bb, load2, load);
13942                                 } else {
13943                                         g_assert (var->opcode == OP_REGOFFSET);
13944
13945                                         ins->opcode = OP_ADD_IMM;
13946                                         ins->sreg1 = var->inst_basereg;
13947                                         ins->inst_imm = var->inst_offset;
13948                                 }
13949
13950                                 *need_local_opts = TRUE;
13951                                 spec = INS_INFO (ins->opcode);
13952                         }
13953
13954                         if (ins->opcode < MONO_CEE_LAST) {
13955                                 mono_print_ins (ins);
13956                                 g_assert_not_reached ();
13957                         }
13958
13959                         /*
13960                          * Store opcodes have destbasereg in the dreg, but in reality, it is an
13961                          * src register.
13962                          * FIXME:
13963                          */
13964                         if (MONO_IS_STORE_MEMBASE (ins)) {
13965                                 tmp_reg = ins->dreg;
13966                                 ins->dreg = ins->sreg2;
13967                                 ins->sreg2 = tmp_reg;
13968                                 store = TRUE;
13969
13970                                 spec2 [MONO_INST_DEST] = ' ';
13971                                 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
13972                                 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
13973                                 spec2 [MONO_INST_SRC3] = ' ';
13974                                 spec = spec2;
13975                         } else if (MONO_IS_STORE_MEMINDEX (ins))
13976                                 g_assert_not_reached ();
13977                         else
13978                                 store = FALSE;
13979                         no_lvreg = FALSE;
13980
13981                         if (G_UNLIKELY (cfg->verbose_level > 2)) {
13982                                 printf ("\t %.3s %d", spec, ins->dreg);
13983                                 num_sregs = mono_inst_get_src_registers (ins, sregs);
13984                                 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
13985                                         printf (" %d", sregs [srcindex]);
13986                                 printf ("\n");
13987                         }
13988
13989                         /***************/
13990                         /*    DREG     */
13991                         /***************/
13992                         regtype = spec [MONO_INST_DEST];
13993                         g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
13994                         prev_dreg = -1;
13995
13996                         if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
13997                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
13998                                 MonoInst *store_ins;
13999                                 int store_opcode;
14000                                 MonoInst *def_ins = ins;
14001                                 int dreg = ins->dreg; /* The original vreg */
14002
14003                                 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14004
14005                                 if (var->opcode == OP_REGVAR) {
14006                                         ins->dreg = var->dreg;
14007                                 } 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)) {
14008                                         /* 
14009                                          * Instead of emitting a load+store, use a _membase opcode.
14010                                          */
14011                                         g_assert (var->opcode == OP_REGOFFSET);
14012                                         if (ins->opcode == OP_MOVE) {
14013                                                 NULLIFY_INS (ins);
14014                                                 def_ins = NULL;
14015                                         } else {
14016                                                 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14017                                                 ins->inst_basereg = var->inst_basereg;
14018                                                 ins->inst_offset = var->inst_offset;
14019                                                 ins->dreg = -1;
14020                                         }
14021                                         spec = INS_INFO (ins->opcode);
14022                                 } else {
14023                                         guint32 lvreg;
14024
14025                                         g_assert (var->opcode == OP_REGOFFSET);
14026
14027                                         prev_dreg = ins->dreg;
14028
14029                                         /* Invalidate any previous lvreg for this vreg */
14030                                         vreg_to_lvreg [ins->dreg] = 0;
14031
14032                                         lvreg = 0;
14033
14034                                         if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14035                                                 regtype = 'l';
14036                                                 store_opcode = OP_STOREI8_MEMBASE_REG;
14037                                         }
14038
14039                                         ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14040
14041 #if SIZEOF_REGISTER != 8
14042                                         if (regtype == 'l') {
14043                                                 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));
14044                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
14045                                                 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));
14046                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
14047                                                 def_ins = store_ins;
14048                                         }
14049                                         else
14050 #endif
14051                                         {
14052                                                 g_assert (store_opcode != OP_STOREV_MEMBASE);
14053
14054                                                 /* Try to fuse the store into the instruction itself */
14055                                                 /* FIXME: Add more instructions */
14056                                                 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14057                                                         ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14058                                                         ins->inst_imm = ins->inst_c0;
14059                                                         ins->inst_destbasereg = var->inst_basereg;
14060                                                         ins->inst_offset = var->inst_offset;
14061                                                         spec = INS_INFO (ins->opcode);
14062                                                 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14063                                                         ins->opcode = store_opcode;
14064                                                         ins->inst_destbasereg = var->inst_basereg;
14065                                                         ins->inst_offset = var->inst_offset;
14066
14067                                                         no_lvreg = TRUE;
14068
14069                                                         tmp_reg = ins->dreg;
14070                                                         ins->dreg = ins->sreg2;
14071                                                         ins->sreg2 = tmp_reg;
14072                                                         store = TRUE;
14073
14074                                                         spec2 [MONO_INST_DEST] = ' ';
14075                                                         spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14076                                                         spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14077                                                         spec2 [MONO_INST_SRC3] = ' ';
14078                                                         spec = spec2;
14079                                                 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14080                                                         // FIXME: The backends expect the base reg to be in inst_basereg
14081                                                         ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14082                                                         ins->dreg = -1;
14083                                                         ins->inst_basereg = var->inst_basereg;
14084                                                         ins->inst_offset = var->inst_offset;
14085                                                         spec = INS_INFO (ins->opcode);
14086                                                 } else {
14087                                                         /* printf ("INS: "); mono_print_ins (ins); */
14088                                                         /* Create a store instruction */
14089                                                         NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14090
14091                                                         /* Insert it after the instruction */
14092                                                         mono_bblock_insert_after_ins (bb, ins, store_ins);
14093
14094                                                         def_ins = store_ins;
14095
14096                                                         /* 
14097                                                          * We can't assign ins->dreg to var->dreg here, since the
14098                                                          * sregs could use it. So set a flag, and do it after
14099                                                          * the sregs.
14100                                                          */
14101                                                         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)))
14102                                                                 dest_has_lvreg = TRUE;
14103                                                 }
14104                                         }
14105                                 }
14106
14107                                 if (def_ins && !live_range_start [dreg]) {
14108                                         live_range_start [dreg] = def_ins;
14109                                         live_range_start_bb [dreg] = bb;
14110                                 }
14111
14112                                 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14113                                         MonoInst *tmp;
14114
14115                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14116                                         tmp->inst_c1 = dreg;
14117                                         mono_bblock_insert_after_ins (bb, def_ins, tmp);
14118                                 }
14119                         }
14120
14121                         /************/
14122                         /*  SREGS   */
14123                         /************/
14124                         num_sregs = mono_inst_get_src_registers (ins, sregs);
14125                         for (srcindex = 0; srcindex < 3; ++srcindex) {
14126                                 regtype = spec [MONO_INST_SRC1 + srcindex];
14127                                 sreg = sregs [srcindex];
14128
14129                                 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14130                                 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14131                                         MonoInst *var = get_vreg_to_inst (cfg, sreg);
14132                                         MonoInst *use_ins = ins;
14133                                         MonoInst *load_ins;
14134                                         guint32 load_opcode;
14135
14136                                         if (var->opcode == OP_REGVAR) {
14137                                                 sregs [srcindex] = var->dreg;
14138                                                 //mono_inst_set_src_registers (ins, sregs);
14139                                                 live_range_end [sreg] = use_ins;
14140                                                 live_range_end_bb [sreg] = bb;
14141
14142                                                 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14143                                                         MonoInst *tmp;
14144
14145                                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14146                                                         /* var->dreg is a hreg */
14147                                                         tmp->inst_c1 = sreg;
14148                                                         mono_bblock_insert_after_ins (bb, ins, tmp);
14149                                                 }
14150
14151                                                 continue;
14152                                         }
14153
14154                                         g_assert (var->opcode == OP_REGOFFSET);
14155                                                 
14156                                         load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14157
14158                                         g_assert (load_opcode != OP_LOADV_MEMBASE);
14159
14160                                         if (vreg_to_lvreg [sreg]) {
14161                                                 g_assert (vreg_to_lvreg [sreg] != -1);
14162
14163                                                 /* The variable is already loaded to an lvreg */
14164                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14165                                                         printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14166                                                 sregs [srcindex] = vreg_to_lvreg [sreg];
14167                                                 //mono_inst_set_src_registers (ins, sregs);
14168                                                 continue;
14169                                         }
14170
14171                                         /* Try to fuse the load into the instruction */
14172                                         if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14173                                                 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14174                                                 sregs [0] = var->inst_basereg;
14175                                                 //mono_inst_set_src_registers (ins, sregs);
14176                                                 ins->inst_offset = var->inst_offset;
14177                                         } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14178                                                 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14179                                                 sregs [1] = var->inst_basereg;
14180                                                 //mono_inst_set_src_registers (ins, sregs);
14181                                                 ins->inst_offset = var->inst_offset;
14182                                         } else {
14183                                                 if (MONO_IS_REAL_MOVE (ins)) {
14184                                                         ins->opcode = OP_NOP;
14185                                                         sreg = ins->dreg;
14186                                                 } else {
14187                                                         //printf ("%d ", srcindex); mono_print_ins (ins);
14188
14189                                                         sreg = alloc_dreg (cfg, stacktypes [regtype]);
14190
14191                                                         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) {
14192                                                                 if (var->dreg == prev_dreg) {
14193                                                                         /*
14194                                                                          * sreg refers to the value loaded by the load
14195                                                                          * emitted below, but we need to use ins->dreg
14196                                                                          * since it refers to the store emitted earlier.
14197                                                                          */
14198                                                                         sreg = ins->dreg;
14199                                                                 }
14200                                                                 g_assert (sreg != -1);
14201                                                                 vreg_to_lvreg [var->dreg] = sreg;
14202                                                                 g_assert (lvregs_len < 1024);
14203                                                                 lvregs [lvregs_len ++] = var->dreg;
14204                                                         }
14205                                                 }
14206
14207                                                 sregs [srcindex] = sreg;
14208                                                 //mono_inst_set_src_registers (ins, sregs);
14209
14210 #if SIZEOF_REGISTER != 8
14211                                                 if (regtype == 'l') {
14212                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14213                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14214                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14215                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14216                                                         use_ins = load_ins;
14217                                                 }
14218                                                 else
14219 #endif
14220                                                 {
14221 #if SIZEOF_REGISTER == 4
14222                                                         g_assert (load_opcode != OP_LOADI8_MEMBASE);
14223 #endif
14224                                                         NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14225                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14226                                                         use_ins = load_ins;
14227                                                 }
14228                                         }
14229
14230                                         if (var->dreg < orig_next_vreg) {
14231                                                 live_range_end [var->dreg] = use_ins;
14232                                                 live_range_end_bb [var->dreg] = bb;
14233                                         }
14234
14235                                         if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14236                                                 MonoInst *tmp;
14237
14238                                                 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14239                                                 tmp->inst_c1 = var->dreg;
14240                                                 mono_bblock_insert_after_ins (bb, ins, tmp);
14241                                         }
14242                                 }
14243                         }
14244                         mono_inst_set_src_registers (ins, sregs);
14245
14246                         if (dest_has_lvreg) {
14247                                 g_assert (ins->dreg != -1);
14248                                 vreg_to_lvreg [prev_dreg] = ins->dreg;
14249                                 g_assert (lvregs_len < 1024);
14250                                 lvregs [lvregs_len ++] = prev_dreg;
14251                                 dest_has_lvreg = FALSE;
14252                         }
14253
14254                         if (store) {
14255                                 tmp_reg = ins->dreg;
14256                                 ins->dreg = ins->sreg2;
14257                                 ins->sreg2 = tmp_reg;
14258                         }
14259
14260                         if (MONO_IS_CALL (ins)) {
14261                                 /* Clear vreg_to_lvreg array */
14262                                 for (i = 0; i < lvregs_len; i++)
14263                                         vreg_to_lvreg [lvregs [i]] = 0;
14264                                 lvregs_len = 0;
14265                         } else if (ins->opcode == OP_NOP) {
14266                                 ins->dreg = -1;
14267                                 MONO_INST_NULLIFY_SREGS (ins);
14268                         }
14269
14270                         if (cfg->verbose_level > 2)
14271                                 mono_print_ins_index (1, ins);
14272                 }
14273
14274                 /* Extend the live range based on the liveness info */
14275                 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14276                         for (i = 0; i < cfg->num_varinfo; i ++) {
14277                                 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14278
14279                                 if (vreg_is_volatile (cfg, vi->vreg))
14280                                         /* The liveness info is incomplete */
14281                                         continue;
14282
14283                                 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14284                                         /* Live from at least the first ins of this bb */
14285                                         live_range_start [vi->vreg] = bb->code;
14286                                         live_range_start_bb [vi->vreg] = bb;
14287                                 }
14288
14289                                 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14290                                         /* Live at least until the last ins of this bb */
14291                                         live_range_end [vi->vreg] = bb->last_ins;
14292                                         live_range_end_bb [vi->vreg] = bb;
14293                                 }
14294                         }
14295                 }
14296         }
14297         
14298         /*
14299          * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14300          * by storing the current native offset into MonoMethodVar->live_range_start/end.
14301          */
14302         if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14303                 for (i = 0; i < cfg->num_varinfo; ++i) {
14304                         int vreg = MONO_VARINFO (cfg, i)->vreg;
14305                         MonoInst *ins;
14306
14307                         if (live_range_start [vreg]) {
14308                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14309                                 ins->inst_c0 = i;
14310                                 ins->inst_c1 = vreg;
14311                                 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14312                         }
14313                         if (live_range_end [vreg]) {
14314                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14315                                 ins->inst_c0 = i;
14316                                 ins->inst_c1 = vreg;
14317                                 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14318                                         mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14319                                 else
14320                                         mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14321                         }
14322                 }
14323         }
14324
14325         if (cfg->gsharedvt_locals_var_ins) {
14326                 /* Nullify if unused */
14327                 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
14328                 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
14329         }
14330
14331         g_free (live_range_start);
14332         g_free (live_range_end);
14333         g_free (live_range_start_bb);
14334         g_free (live_range_end_bb);
14335 }
14336
14337
14338 /**
14339  * FIXME:
14340  * - use 'iadd' instead of 'int_add'
14341  * - handling ovf opcodes: decompose in method_to_ir.
14342  * - unify iregs/fregs
14343  *   -> partly done, the missing parts are:
14344  *   - a more complete unification would involve unifying the hregs as well, so
14345  *     code wouldn't need if (fp) all over the place. but that would mean the hregs
14346  *     would no longer map to the machine hregs, so the code generators would need to
14347  *     be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
14348  *     wouldn't work any more. Duplicating the code in mono_local_regalloc () into
14349  *     fp/non-fp branches speeds it up by about 15%.
14350  * - use sext/zext opcodes instead of shifts
14351  * - add OP_ICALL
14352  * - get rid of TEMPLOADs if possible and use vregs instead
14353  * - clean up usage of OP_P/OP_ opcodes
14354  * - cleanup usage of DUMMY_USE
14355  * - cleanup the setting of ins->type for MonoInst's which are pushed on the 
14356  *   stack
14357  * - set the stack type and allocate a dreg in the EMIT_NEW macros
14358  * - get rid of all the <foo>2 stuff when the new JIT is ready.
14359  * - make sure handle_stack_args () is called before the branch is emitted
14360  * - when the new IR is done, get rid of all unused stuff
14361  * - COMPARE/BEQ as separate instructions or unify them ?
14362  *   - keeping them separate allows specialized compare instructions like
14363  *     compare_imm, compare_membase
14364  *   - most back ends unify fp compare+branch, fp compare+ceq
14365  * - integrate mono_save_args into inline_method
14366  * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
14367  * - handle long shift opts on 32 bit platforms somehow: they require 
14368  *   3 sregs (2 for arg1 and 1 for arg2)
14369  * - make byref a 'normal' type.
14370  * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
14371  *   variable if needed.
14372  * - do not start a new IL level bblock when cfg->cbb is changed by a function call
14373  *   like inline_method.
14374  * - remove inlining restrictions
14375  * - fix LNEG and enable cfold of INEG
14376  * - generalize x86 optimizations like ldelema as a peephole optimization
14377  * - add store_mem_imm for amd64
14378  * - optimize the loading of the interruption flag in the managed->native wrappers
14379  * - avoid special handling of OP_NOP in passes
14380  * - move code inserting instructions into one function/macro.
14381  * - try a coalescing phase after liveness analysis
14382  * - add float -> vreg conversion + local optimizations on !x86
14383  * - figure out how to handle decomposed branches during optimizations, ie.
14384  *   compare+branch, op_jump_table+op_br etc.
14385  * - promote RuntimeXHandles to vregs
14386  * - vtype cleanups:
14387  *   - add a NEW_VARLOADA_VREG macro
14388  * - the vtype optimizations are blocked by the LDADDR opcodes generated for 
14389  *   accessing vtype fields.
14390  * - get rid of I8CONST on 64 bit platforms
14391  * - dealing with the increase in code size due to branches created during opcode
14392  *   decomposition:
14393  *   - use extended basic blocks
14394  *     - all parts of the JIT
14395  *     - handle_global_vregs () && local regalloc
14396  *   - avoid introducing global vregs during decomposition, like 'vtable' in isinst
14397  * - sources of increase in code size:
14398  *   - vtypes
14399  *   - long compares
14400  *   - isinst and castclass
14401  *   - lvregs not allocated to global registers even if used multiple times
14402  * - call cctors outside the JIT, to make -v output more readable and JIT timings more
14403  *   meaningful.
14404  * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
14405  * - add all micro optimizations from the old JIT
14406  * - put tree optimizations into the deadce pass
14407  * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
14408  *   specific function.
14409  * - unify the float comparison opcodes with the other comparison opcodes, i.e.
14410  *   fcompare + branchCC.
14411  * - create a helper function for allocating a stack slot, taking into account 
14412  *   MONO_CFG_HAS_SPILLUP.
14413  * - merge r68207.
14414  * - merge the ia64 switch changes.
14415  * - optimize mono_regstate2_alloc_int/float.
14416  * - fix the pessimistic handling of variables accessed in exception handler blocks.
14417  * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
14418  *   parts of the tree could be separated by other instructions, killing the tree
14419  *   arguments, or stores killing loads etc. Also, should we fold loads into other
14420  *   instructions if the result of the load is used multiple times ?
14421  * - make the REM_IMM optimization in mini-x86.c arch-independent.
14422  * - LAST MERGE: 108395.
14423  * - when returning vtypes in registers, generate IR and append it to the end of the
14424  *   last bb instead of doing it in the epilog.
14425  * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
14426  */
14427
14428 /*
14429
14430 NOTES
14431 -----
14432
14433 - When to decompose opcodes:
14434   - earlier: this makes some optimizations hard to implement, since the low level IR
14435   no longer contains the neccessary information. But it is easier to do.
14436   - later: harder to implement, enables more optimizations.
14437 - Branches inside bblocks:
14438   - created when decomposing complex opcodes. 
14439     - branches to another bblock: harmless, but not tracked by the branch 
14440       optimizations, so need to branch to a label at the start of the bblock.
14441     - branches to inside the same bblock: very problematic, trips up the local
14442       reg allocator. Can be fixed by spitting the current bblock, but that is a
14443       complex operation, since some local vregs can become global vregs etc.
14444 - Local/global vregs:
14445   - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
14446     local register allocator.
14447   - global vregs: used in more than one bblock. Have an associated MonoMethodVar
14448     structure, created by mono_create_var (). Assigned to hregs or the stack by
14449     the global register allocator.
14450 - When to do optimizations like alu->alu_imm:
14451   - earlier -> saves work later on since the IR will be smaller/simpler
14452   - later -> can work on more instructions
14453 - Handling of valuetypes:
14454   - When a vtype is pushed on the stack, a new temporary is created, an 
14455     instruction computing its address (LDADDR) is emitted and pushed on
14456     the stack. Need to optimize cases when the vtype is used immediately as in
14457     argument passing, stloc etc.
14458 - Instead of the to_end stuff in the old JIT, simply call the function handling
14459   the values on the stack before emitting the last instruction of the bb.
14460 */
14461
14462 #else /* !DISABLE_JIT */
14463
14464 MONO_EMPTY_SOURCE_FILE (method_to_ir);
14465
14466 #endif /* !DISABLE_JIT */