Merge pull request #2237 from xmcclure/container-owner
[mono.git] / mono / mini / mini-llvm.c
1 /*
2  * mini-llvm.c: llvm "Backend" for the mono JIT
3  *
4  * Copyright 2009-2011 Novell Inc (http://www.novell.com)
5  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
6  */
7
8 #include "mini.h"
9 #include <mono/metadata/debug-helpers.h>
10 #include <mono/metadata/debug-mono-symfile.h>
11 #include <mono/metadata/mempool-internals.h>
12 #include <mono/metadata/environment.h>
13 #include <mono/metadata/object-internals.h>
14 #include <mono/utils/mono-tls.h>
15 #include <mono/utils/mono-dl.h>
16 #include <mono/utils/mono-time.h>
17 #include <mono/utils/freebsd-dwarf.h>
18
19 #ifndef __STDC_LIMIT_MACROS
20 #define __STDC_LIMIT_MACROS
21 #endif
22 #ifndef __STDC_CONSTANT_MACROS
23 #define __STDC_CONSTANT_MACROS
24 #endif
25
26 #include "llvm-c/Core.h"
27 #include "llvm-c/ExecutionEngine.h"
28 #include "llvm-c/BitWriter.h"
29 #include "llvm-c/Analysis.h"
30
31 #include "mini-llvm-cpp.h"
32 #include "aot-compiler.h"
33 #include "mini-llvm.h"
34
35 #ifdef __MINGW32__
36
37 #include <stddef.h>
38 extern void *memset(void *, int, size_t);
39 void bzero (void *to, size_t count) { memset (to, 0, count); }
40
41 #endif
42
43 #if LLVM_API_VERSION < 4
44 #error "The version of the mono llvm repository is too old."
45 #endif
46
47  /*
48   * Information associated by mono with LLVM modules.
49   */
50 typedef struct {
51         LLVMModuleRef lmodule;
52         LLVMValueRef throw_icall, rethrow, match_exc, throw_corlib_exception, resume_eh;
53         GHashTable *llvm_types;
54         LLVMValueRef got_var;
55         const char *got_symbol;
56         const char *get_method_symbol;
57         const char *get_unbox_tramp_symbol;
58         GHashTable *plt_entries;
59         GHashTable *plt_entries_ji;
60         GHashTable *method_to_lmethod;
61         GHashTable *direct_callables;
62         char **bb_names;
63         int bb_names_len;
64         GPtrArray *used;
65         LLVMTypeRef ptr_type;
66         GPtrArray *subprogram_mds;
67         MonoEERef *mono_ee;
68         LLVMExecutionEngineRef ee;
69         gboolean external_symbols;
70         gboolean emit_dwarf;
71         int max_got_offset;
72
73         /* For AOT */
74         MonoAssembly *assembly;
75         char *global_prefix;
76         MonoAotFileInfo aot_info;
77         const char *jit_got_symbol;
78         const char *eh_frame_symbol;
79         LLVMValueRef get_method, get_unbox_tramp;
80         LLVMValueRef init_method, init_method_gshared_rgctx, init_method_gshared_this;
81         LLVMValueRef code_start, code_end;
82         LLVMValueRef inited_var;
83         int max_inited_idx, max_method_idx;
84         gboolean has_jitted_code;
85         gboolean static_link;
86         gboolean llvm_only;
87         GHashTable *idx_to_lmethod;
88         GHashTable *idx_to_unbox_tramp;
89         /* Maps a MonoMethod to LLVM instructions representing it */
90         GHashTable *method_to_callers;
91         LLVMContextRef context;
92         LLVMValueRef sentinel_exception;
93 } MonoLLVMModule;
94
95 /*
96  * Information associated by the backend with mono basic blocks.
97  */
98 typedef struct {
99         LLVMBasicBlockRef bblock, end_bblock;
100         LLVMValueRef finally_ind;
101         gboolean added, invoke_target;
102         /* 
103          * If this bblock is the start of a finally clause, this is a list of bblocks it
104          * needs to branch to in ENDFINALLY.
105          */
106         GSList *call_handler_return_bbs;
107         /*
108          * If this bblock is the start of a finally clause, this is the bblock that
109          * CALL_HANDLER needs to branch to.
110          */
111         LLVMBasicBlockRef call_handler_target_bb;
112         /* The list of switch statements generated by ENDFINALLY instructions */
113         GSList *endfinally_switch_ins_list;
114         GSList *phi_nodes;
115 } BBInfo;
116
117 /*
118  * Structure containing emit state
119  */
120 typedef struct {
121         MonoMemPool *mempool;
122
123         /* Maps method names to the corresponding LLVMValueRef */
124         GHashTable *emitted_method_decls;
125
126         MonoCompile *cfg;
127         LLVMValueRef lmethod;
128         MonoLLVMModule *module;
129         LLVMModuleRef lmodule;
130         BBInfo *bblocks;
131         int sindex, default_index, ex_index;
132         LLVMBuilderRef builder;
133         LLVMValueRef *values, *addresses;
134         MonoType **vreg_cli_types;
135         LLVMCallInfo *linfo;
136         MonoMethodSignature *sig;
137         GSList *builders;
138         GHashTable *region_to_handler;
139         GHashTable *clause_to_handler;
140         LLVMBuilderRef alloca_builder;
141         LLVMValueRef last_alloca;
142         LLVMValueRef rgctx_arg;
143         LLVMValueRef this_arg;
144         LLVMTypeRef *vreg_types;
145         LLVMBasicBlockRef init_bb, inited_bb;
146         gboolean *is_dead;
147         gboolean *unreachable;
148         gboolean llvm_only;
149         gboolean has_got_access;
150         int this_arg_pindex, rgctx_arg_pindex;
151         LLVMValueRef imt_rgctx_loc;
152         GHashTable *llvm_types;
153         LLVMValueRef dbg_md;
154         MonoDebugMethodInfo *minfo;
155         char temp_name [32];
156         /* For every clause, the clauses it is nested in */
157         GSList **nested_in;
158         LLVMValueRef ex_var;
159         GHashTable *exc_meta;
160         GHashTable *method_to_callers;
161 } EmitContext;
162
163 typedef struct {
164         MonoBasicBlock *bb;
165         MonoInst *phi;
166         MonoBasicBlock *in_bb;
167         int sreg;
168 } PhiNode;
169
170 /*
171  * Instruction metadata
172  * This is the same as ins_info, but LREG != IREG.
173  */
174 #ifdef MINI_OP
175 #undef MINI_OP
176 #endif
177 #ifdef MINI_OP3
178 #undef MINI_OP3
179 #endif
180 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
181 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
182 #define NONE ' '
183 #define IREG 'i'
184 #define FREG 'f'
185 #define VREG 'v'
186 #define XREG 'x'
187 #define LREG 'l'
188 /* keep in sync with the enum in mini.h */
189 const char
190 llvm_ins_info[] = {
191 #include "mini-ops.h"
192 };
193 #undef MINI_OP
194 #undef MINI_OP3
195
196 #if SIZEOF_VOID_P == 4
197 #define GET_LONG_IMM(ins) (((guint64)(ins)->inst_ms_word << 32) | (guint64)(guint32)(ins)->inst_ls_word)
198 #else
199 #define GET_LONG_IMM(ins) ((ins)->inst_imm)
200 #endif
201
202 #define LLVM_INS_INFO(opcode) (&llvm_ins_info [((opcode) - OP_START - 1) * 4])
203
204 #if 0
205 #define TRACE_FAILURE(msg) do { printf ("%s\n", msg); } while (0)
206 #else
207 #define TRACE_FAILURE(msg)
208 #endif
209
210 #ifdef TARGET_X86
211 #define IS_TARGET_X86 1
212 #else
213 #define IS_TARGET_X86 0
214 #endif
215
216 #ifdef TARGET_AMD64
217 #define IS_TARGET_AMD64 1
218 #else
219 #define IS_TARGET_AMD64 0
220 #endif
221
222 #define LLVM_FAILURE(ctx, reason) do { \
223         TRACE_FAILURE (reason); \
224         (ctx)->cfg->exception_message = g_strdup (reason); \
225         (ctx)->cfg->disable_llvm = TRUE; \
226         goto FAILURE; \
227 } while (0)
228
229 #define CHECK_FAILURE(ctx) do { \
230     if ((ctx)->cfg->disable_llvm) \
231                 goto FAILURE; \
232 } while (0)
233
234 static LLVMIntPredicate cond_to_llvm_cond [] = {
235         LLVMIntEQ,
236         LLVMIntNE,
237         LLVMIntSLE,
238         LLVMIntSGE,
239         LLVMIntSLT,
240         LLVMIntSGT,
241         LLVMIntULE,
242         LLVMIntUGE,
243         LLVMIntULT,
244         LLVMIntUGT,
245 };
246
247 static LLVMRealPredicate fpcond_to_llvm_cond [] = {
248         LLVMRealOEQ,
249         LLVMRealUNE,
250         LLVMRealOLE,
251         LLVMRealOGE,
252         LLVMRealOLT,
253         LLVMRealOGT,
254         LLVMRealULE,
255         LLVMRealUGE,
256         LLVMRealULT,
257         LLVMRealUGT,
258 };
259
260 static MonoNativeTlsKey current_cfg_tls_id;
261
262 static MonoLLVMModule aot_module;
263 static int memset_param_count, memcpy_param_count;
264 static const char *memset_func_name;
265 static const char *memcpy_func_name;
266
267 static void init_jit_module (MonoDomain *domain);
268
269 static void emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code);
270 static LLVMValueRef emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name);
271 static void emit_dbg_info (MonoLLVMModule *module, const char *filename, const char *cu_name);
272
273 /*
274  * IntPtrType:
275  *
276  *   The LLVM type with width == sizeof (gpointer)
277  */
278 static LLVMTypeRef
279 IntPtrType (void)
280 {
281         return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
282 }
283
284 static LLVMTypeRef
285 ObjRefType (void)
286 {
287         return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
288 }
289
290 static LLVMTypeRef
291 ThisType (void)
292 {
293         return sizeof (gpointer) == 8 ? LLVMPointerType (LLVMInt64Type (), 0) : LLVMPointerType (LLVMInt32Type (), 0);
294 }
295
296 /*
297  * get_vtype_size:
298  *
299  *   Return the size of the LLVM representation of the vtype T.
300  */
301 static guint32
302 get_vtype_size (MonoType *t)
303 {
304         int size;
305
306         size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
307
308         /* LLVMArgAsIArgs depends on this since it stores whole words */
309         while (size < 2 * sizeof (gpointer) && mono_is_power_of_two (size) == -1)
310                 size ++;
311
312         return size;
313 }
314
315 /*
316  * simd_class_to_llvm_type:
317  *
318  *   Return the LLVM type corresponding to the Mono.SIMD class KLASS
319  */
320 static LLVMTypeRef
321 simd_class_to_llvm_type (EmitContext *ctx, MonoClass *klass)
322 {
323         if (!strcmp (klass->name, "Vector2d")) {
324                 return LLVMVectorType (LLVMDoubleType (), 2);
325         } else if (!strcmp (klass->name, "Vector2l")) {
326                 return LLVMVectorType (LLVMInt64Type (), 2);
327         } else if (!strcmp (klass->name, "Vector2ul")) {
328                 return LLVMVectorType (LLVMInt64Type (), 2);
329         } else if (!strcmp (klass->name, "Vector4i")) {
330                 return LLVMVectorType (LLVMInt32Type (), 4);
331         } else if (!strcmp (klass->name, "Vector4ui")) {
332                 return LLVMVectorType (LLVMInt32Type (), 4);
333         } else if (!strcmp (klass->name, "Vector4f")) {
334                 return LLVMVectorType (LLVMFloatType (), 4);
335         } else if (!strcmp (klass->name, "Vector8s")) {
336                 return LLVMVectorType (LLVMInt16Type (), 8);
337         } else if (!strcmp (klass->name, "Vector8us")) {
338                 return LLVMVectorType (LLVMInt16Type (), 8);
339         } else if (!strcmp (klass->name, "Vector16sb")) {
340                 return LLVMVectorType (LLVMInt8Type (), 16);
341         } else if (!strcmp (klass->name, "Vector16b")) {
342                 return LLVMVectorType (LLVMInt8Type (), 16);
343         } else {
344                 printf ("%s\n", klass->name);
345                 NOT_IMPLEMENTED;
346                 return NULL;
347         }
348 }
349
350 /* Return the 128 bit SIMD type corresponding to the mono type TYPE */
351 static inline G_GNUC_UNUSED LLVMTypeRef
352 type_to_simd_type (int type)
353 {
354         switch (type) {
355         case MONO_TYPE_I1:
356                 return LLVMVectorType (LLVMInt8Type (), 16);
357         case MONO_TYPE_I2:
358                 return LLVMVectorType (LLVMInt16Type (), 8);
359         case MONO_TYPE_I4:
360                 return LLVMVectorType (LLVMInt32Type (), 4);
361         case MONO_TYPE_I8:
362                 return LLVMVectorType (LLVMInt64Type (), 2);
363         case MONO_TYPE_R8:
364                 return LLVMVectorType (LLVMDoubleType (), 2);
365         case MONO_TYPE_R4:
366                 return LLVMVectorType (LLVMFloatType (), 4);
367         default:
368                 g_assert_not_reached ();
369                 return NULL;
370         }
371 }
372
373 static LLVMTypeRef
374 create_llvm_type_for_type (MonoLLVMModule *module, MonoClass *klass)
375 {
376         int i, size, nfields, esize;
377         LLVMTypeRef *eltypes;
378         char *name;
379         MonoType *t;
380         LLVMTypeRef ltype;
381
382         t = &klass->byval_arg;
383
384         if (mini_type_is_hfa (t, &nfields, &esize)) {
385                 /*
386                  * This is needed on arm64 where HFAs are returned in
387                  * registers.
388                  */
389                 size = nfields;
390                 eltypes = g_new (LLVMTypeRef, size);
391                 for (i = 0; i < size; ++i)
392                         eltypes [i] = esize == 4 ? LLVMFloatType () : LLVMDoubleType ();
393         } else {
394                 size = get_vtype_size (t);
395
396                 eltypes = g_new (LLVMTypeRef, size);
397                 for (i = 0; i < size; ++i)
398                         eltypes [i] = LLVMInt8Type ();
399         }
400
401         name = mono_type_full_name (&klass->byval_arg);
402         ltype = LLVMStructCreateNamed (module->context, name);
403         LLVMStructSetBody (ltype, eltypes, size, FALSE);
404         g_free (eltypes);
405         g_free (name);
406
407         return ltype;
408 }
409
410 /*
411  * type_to_llvm_type:
412  *
413  *   Return the LLVM type corresponding to T.
414  */
415 static LLVMTypeRef
416 type_to_llvm_type (EmitContext *ctx, MonoType *t)
417 {
418         t = mini_get_underlying_type (t);
419
420         switch (t->type) {
421         case MONO_TYPE_VOID:
422                 return LLVMVoidType ();
423         case MONO_TYPE_I1:
424                 return LLVMInt8Type ();
425         case MONO_TYPE_I2:
426                 return LLVMInt16Type ();
427         case MONO_TYPE_I4:
428                 return LLVMInt32Type ();
429         case MONO_TYPE_U1:
430                 return LLVMInt8Type ();
431         case MONO_TYPE_U2:
432                 return LLVMInt16Type ();
433         case MONO_TYPE_U4:
434                 return LLVMInt32Type ();
435         case MONO_TYPE_BOOLEAN:
436                 return LLVMInt8Type ();
437         case MONO_TYPE_I8:
438         case MONO_TYPE_U8:
439                 return LLVMInt64Type ();
440         case MONO_TYPE_CHAR:
441                 return LLVMInt16Type ();
442         case MONO_TYPE_R4:
443                 return LLVMFloatType ();
444         case MONO_TYPE_R8:
445                 return LLVMDoubleType ();
446         case MONO_TYPE_I:
447         case MONO_TYPE_U:
448                 return IntPtrType ();
449         case MONO_TYPE_OBJECT:
450         case MONO_TYPE_CLASS:
451         case MONO_TYPE_ARRAY:
452         case MONO_TYPE_SZARRAY:
453         case MONO_TYPE_STRING:
454         case MONO_TYPE_PTR:
455                 return ObjRefType ();
456         case MONO_TYPE_VAR:
457         case MONO_TYPE_MVAR:
458                 /* Because of generic sharing */
459                 return ObjRefType ();
460         case MONO_TYPE_GENERICINST:
461                 if (!mono_type_generic_inst_is_valuetype (t))
462                         return ObjRefType ();
463                 /* Fall through */
464         case MONO_TYPE_VALUETYPE:
465         case MONO_TYPE_TYPEDBYREF: {
466                 MonoClass *klass;
467                 LLVMTypeRef ltype;
468
469                 klass = mono_class_from_mono_type (t);
470
471                 if (MONO_CLASS_IS_SIMD (ctx->cfg, klass))
472                         return simd_class_to_llvm_type (ctx, klass);
473
474                 if (klass->enumtype)
475                         return type_to_llvm_type (ctx, mono_class_enum_basetype (klass));
476
477                 ltype = (LLVMTypeRef)g_hash_table_lookup (ctx->module->llvm_types, klass);
478                 if (!ltype) {
479                         ltype = create_llvm_type_for_type (ctx->module, klass);
480                         g_hash_table_insert (ctx->module->llvm_types, klass, ltype);
481                 }
482                 return ltype;
483         }
484
485         default:
486                 printf ("X: %d\n", t->type);
487                 ctx->cfg->exception_message = g_strdup_printf ("type %s", mono_type_full_name (t));
488                 ctx->cfg->disable_llvm = TRUE;
489                 return NULL;
490         }
491 }
492
493 /*
494  * type_is_unsigned:
495  *
496  *   Return whenever T is an unsigned int type.
497  */
498 static gboolean
499 type_is_unsigned (EmitContext *ctx, MonoType *t)
500 {
501         if (t->byref)
502                 return FALSE;
503         switch (t->type) {
504         case MONO_TYPE_U1:
505         case MONO_TYPE_U2:
506         case MONO_TYPE_CHAR:
507         case MONO_TYPE_U4:
508         case MONO_TYPE_U8:
509                 return TRUE;
510         default:
511                 return FALSE;
512         }
513 }
514
515 /*
516  * type_to_llvm_arg_type:
517  *
518  *   Same as type_to_llvm_type, but treat i8/i16 as i32.
519  */
520 static LLVMTypeRef
521 type_to_llvm_arg_type (EmitContext *ctx, MonoType *t)
522 {
523         LLVMTypeRef ptype = type_to_llvm_type (ctx, t);
524
525         /*
526          * This works on all abis except arm64/ios which passes multiple
527          * arguments in one stack slot.
528          */
529 #ifndef TARGET_ARM64
530         if (ptype == LLVMInt8Type () || ptype == LLVMInt16Type ()) {
531                 /* 
532                  * LLVM generates code which only sets the lower bits, while JITted
533                  * code expects all the bits to be set.
534                  */
535                 ptype = LLVMInt32Type ();
536         }
537 #endif
538
539         return ptype;
540 }
541
542 /*
543  * llvm_type_to_stack_type:
544  *
545  *   Return the LLVM type which needs to be used when a value of type TYPE is pushed
546  * on the IL stack.
547  */
548 static G_GNUC_UNUSED LLVMTypeRef
549 llvm_type_to_stack_type (MonoCompile *cfg, LLVMTypeRef type)
550 {
551         if (type == NULL)
552                 return NULL;
553         if (type == LLVMInt8Type ())
554                 return LLVMInt32Type ();
555         else if (type == LLVMInt16Type ())
556                 return LLVMInt32Type ();
557         else if (!cfg->r4fp && type == LLVMFloatType ())
558                 return LLVMDoubleType ();
559         else
560                 return type;
561 }
562
563 /*
564  * regtype_to_llvm_type:
565  *
566  *   Return the LLVM type corresponding to the regtype C used in instruction 
567  * descriptions.
568  */
569 static LLVMTypeRef
570 regtype_to_llvm_type (char c)
571 {
572         switch (c) {
573         case 'i':
574                 return LLVMInt32Type ();
575         case 'l':
576                 return LLVMInt64Type ();
577         case 'f':
578                 return LLVMDoubleType ();
579         default:
580                 return NULL;
581         }
582 }
583
584 /*
585  * op_to_llvm_type:
586  *
587  *   Return the LLVM type corresponding to the unary/binary opcode OPCODE.
588  */
589 static LLVMTypeRef
590 op_to_llvm_type (int opcode)
591 {
592         switch (opcode) {
593         case OP_ICONV_TO_I1:
594         case OP_LCONV_TO_I1:
595                 return LLVMInt8Type ();
596         case OP_ICONV_TO_U1:
597         case OP_LCONV_TO_U1:
598                 return LLVMInt8Type ();
599         case OP_ICONV_TO_I2:
600         case OP_LCONV_TO_I2:
601                 return LLVMInt16Type ();
602         case OP_ICONV_TO_U2:
603         case OP_LCONV_TO_U2:
604                 return LLVMInt16Type ();
605         case OP_ICONV_TO_I4:
606         case OP_LCONV_TO_I4:
607                 return LLVMInt32Type ();
608         case OP_ICONV_TO_U4:
609         case OP_LCONV_TO_U4:
610                 return LLVMInt32Type ();
611         case OP_ICONV_TO_I8:
612                 return LLVMInt64Type ();
613         case OP_ICONV_TO_R4:
614                 return LLVMFloatType ();
615         case OP_ICONV_TO_R8:
616                 return LLVMDoubleType ();
617         case OP_ICONV_TO_U8:
618                 return LLVMInt64Type ();
619         case OP_FCONV_TO_I4:
620                 return LLVMInt32Type ();
621         case OP_FCONV_TO_I8:
622                 return LLVMInt64Type ();
623         case OP_FCONV_TO_I1:
624         case OP_FCONV_TO_U1:
625         case OP_RCONV_TO_I1:
626         case OP_RCONV_TO_U1:
627                 return LLVMInt8Type ();
628         case OP_FCONV_TO_I2:
629         case OP_FCONV_TO_U2:
630         case OP_RCONV_TO_I2:
631         case OP_RCONV_TO_U2:
632                 return LLVMInt16Type ();
633         case OP_FCONV_TO_I:
634         case OP_FCONV_TO_U:
635                 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
636         case OP_IADD_OVF:
637         case OP_IADD_OVF_UN:
638         case OP_ISUB_OVF:
639         case OP_ISUB_OVF_UN:
640         case OP_IMUL_OVF:
641         case OP_IMUL_OVF_UN:
642                 return LLVMInt32Type ();
643         case OP_LADD_OVF:
644         case OP_LADD_OVF_UN:
645         case OP_LSUB_OVF:
646         case OP_LSUB_OVF_UN:
647         case OP_LMUL_OVF:
648         case OP_LMUL_OVF_UN:
649                 return LLVMInt64Type ();
650         default:
651                 printf ("%s\n", mono_inst_name (opcode));
652                 g_assert_not_reached ();
653                 return NULL;
654         }
655 }               
656
657 #define CLAUSE_START(clause) ((clause)->try_offset)
658 #define CLAUSE_END(clause) (((clause))->try_offset + ((clause))->try_len)
659
660 /*
661  * load_store_to_llvm_type:
662  *
663  *   Return the size/sign/zero extension corresponding to the load/store opcode
664  * OPCODE.
665  */
666 static LLVMTypeRef
667 load_store_to_llvm_type (int opcode, int *size, gboolean *sext, gboolean *zext)
668 {
669         *sext = FALSE;
670         *zext = FALSE;
671
672         switch (opcode) {
673         case OP_LOADI1_MEMBASE:
674         case OP_STOREI1_MEMBASE_REG:
675         case OP_STOREI1_MEMBASE_IMM:
676         case OP_ATOMIC_LOAD_I1:
677         case OP_ATOMIC_STORE_I1:
678                 *size = 1;
679                 *sext = TRUE;
680                 return LLVMInt8Type ();
681         case OP_LOADU1_MEMBASE:
682         case OP_LOADU1_MEM:
683         case OP_ATOMIC_LOAD_U1:
684         case OP_ATOMIC_STORE_U1:
685                 *size = 1;
686                 *zext = TRUE;
687                 return LLVMInt8Type ();
688         case OP_LOADI2_MEMBASE:
689         case OP_STOREI2_MEMBASE_REG:
690         case OP_STOREI2_MEMBASE_IMM:
691         case OP_ATOMIC_LOAD_I2:
692         case OP_ATOMIC_STORE_I2:
693                 *size = 2;
694                 *sext = TRUE;
695                 return LLVMInt16Type ();
696         case OP_LOADU2_MEMBASE:
697         case OP_LOADU2_MEM:
698         case OP_ATOMIC_LOAD_U2:
699         case OP_ATOMIC_STORE_U2:
700                 *size = 2;
701                 *zext = TRUE;
702                 return LLVMInt16Type ();
703         case OP_LOADI4_MEMBASE:
704         case OP_LOADU4_MEMBASE:
705         case OP_LOADI4_MEM:
706         case OP_LOADU4_MEM:
707         case OP_STOREI4_MEMBASE_REG:
708         case OP_STOREI4_MEMBASE_IMM:
709         case OP_ATOMIC_LOAD_I4:
710         case OP_ATOMIC_STORE_I4:
711         case OP_ATOMIC_LOAD_U4:
712         case OP_ATOMIC_STORE_U4:
713                 *size = 4;
714                 return LLVMInt32Type ();
715         case OP_LOADI8_MEMBASE:
716         case OP_LOADI8_MEM:
717         case OP_STOREI8_MEMBASE_REG:
718         case OP_STOREI8_MEMBASE_IMM:
719         case OP_ATOMIC_LOAD_I8:
720         case OP_ATOMIC_STORE_I8:
721         case OP_ATOMIC_LOAD_U8:
722         case OP_ATOMIC_STORE_U8:
723                 *size = 8;
724                 return LLVMInt64Type ();
725         case OP_LOADR4_MEMBASE:
726         case OP_STORER4_MEMBASE_REG:
727         case OP_ATOMIC_LOAD_R4:
728         case OP_ATOMIC_STORE_R4:
729                 *size = 4;
730                 return LLVMFloatType ();
731         case OP_LOADR8_MEMBASE:
732         case OP_STORER8_MEMBASE_REG:
733         case OP_ATOMIC_LOAD_R8:
734         case OP_ATOMIC_STORE_R8:
735                 *size = 8;
736                 return LLVMDoubleType ();
737         case OP_LOAD_MEMBASE:
738         case OP_LOAD_MEM:
739         case OP_STORE_MEMBASE_REG:
740         case OP_STORE_MEMBASE_IMM:
741                 *size = sizeof (gpointer);
742                 return IntPtrType ();
743         default:
744                 g_assert_not_reached ();
745                 return NULL;
746         }
747 }
748
749 /*
750  * ovf_op_to_intrins:
751  *
752  *   Return the LLVM intrinsics corresponding to the overflow opcode OPCODE.
753  */
754 static const char*
755 ovf_op_to_intrins (int opcode)
756 {
757         switch (opcode) {
758         case OP_IADD_OVF:
759                 return "llvm.sadd.with.overflow.i32";
760         case OP_IADD_OVF_UN:
761                 return "llvm.uadd.with.overflow.i32";
762         case OP_ISUB_OVF:
763                 return "llvm.ssub.with.overflow.i32";
764         case OP_ISUB_OVF_UN:
765                 return "llvm.usub.with.overflow.i32";
766         case OP_IMUL_OVF:
767                 return "llvm.smul.with.overflow.i32";
768         case OP_IMUL_OVF_UN:
769                 return "llvm.umul.with.overflow.i32";
770         case OP_LADD_OVF:
771                 return "llvm.sadd.with.overflow.i64";
772         case OP_LADD_OVF_UN:
773                 return "llvm.uadd.with.overflow.i64";
774         case OP_LSUB_OVF:
775                 return "llvm.ssub.with.overflow.i64";
776         case OP_LSUB_OVF_UN:
777                 return "llvm.usub.with.overflow.i64";
778         case OP_LMUL_OVF:
779                 return "llvm.smul.with.overflow.i64";
780         case OP_LMUL_OVF_UN:
781                 return "llvm.umul.with.overflow.i64";
782         default:
783                 g_assert_not_reached ();
784                 return NULL;
785         }
786 }
787
788 static const char*
789 simd_op_to_intrins (int opcode)
790 {
791         switch (opcode) {
792 #if defined(TARGET_X86) || defined(TARGET_AMD64)
793         case OP_MINPD:
794                 return "llvm.x86.sse2.min.pd";
795         case OP_MINPS:
796                 return "llvm.x86.sse.min.ps";
797         case OP_PMIND_UN:
798                 return "llvm.x86.sse41.pminud";
799         case OP_PMINW_UN:
800                 return "llvm.x86.sse41.pminuw";
801         case OP_PMINB_UN:
802                 return "llvm.x86.sse2.pminu.b";
803         case OP_PMINW:
804                 return "llvm.x86.sse2.pmins.w";
805         case OP_MAXPD:
806                 return "llvm.x86.sse2.max.pd";
807         case OP_MAXPS:
808                 return "llvm.x86.sse.max.ps";
809         case OP_HADDPD:
810                 return "llvm.x86.sse3.hadd.pd";
811         case OP_HADDPS:
812                 return "llvm.x86.sse3.hadd.ps";
813         case OP_HSUBPD:
814                 return "llvm.x86.sse3.hsub.pd";
815         case OP_HSUBPS:
816                 return "llvm.x86.sse3.hsub.ps";
817         case OP_PMAXD_UN:
818                 return "llvm.x86.sse41.pmaxud";
819         case OP_PMAXW_UN:
820                 return "llvm.x86.sse41.pmaxuw";
821         case OP_PMAXB_UN:
822                 return "llvm.x86.sse2.pmaxu.b";
823         case OP_ADDSUBPS:
824                 return "llvm.x86.sse3.addsub.ps";
825         case OP_ADDSUBPD:
826                 return "llvm.x86.sse3.addsub.pd";
827         case OP_EXTRACT_MASK:
828                 return "llvm.x86.sse2.pmovmskb.128";
829         case OP_PSHRW:
830         case OP_PSHRW_REG:
831                 return "llvm.x86.sse2.psrli.w";
832         case OP_PSHRD:
833         case OP_PSHRD_REG:
834                 return "llvm.x86.sse2.psrli.d";
835         case OP_PSHRQ:
836         case OP_PSHRQ_REG:
837                 return "llvm.x86.sse2.psrli.q";
838         case OP_PSHLW:
839         case OP_PSHLW_REG:
840                 return "llvm.x86.sse2.pslli.w";
841         case OP_PSHLD:
842         case OP_PSHLD_REG:
843                 return "llvm.x86.sse2.pslli.d";
844         case OP_PSHLQ:
845         case OP_PSHLQ_REG:
846                 return "llvm.x86.sse2.pslli.q";
847         case OP_PSARW:
848         case OP_PSARW_REG:
849                 return "llvm.x86.sse2.psrai.w";
850         case OP_PSARD:
851         case OP_PSARD_REG:
852                 return "llvm.x86.sse2.psrai.d";
853         case OP_PADDB_SAT:
854                 return "llvm.x86.sse2.padds.b";
855         case OP_PADDW_SAT:
856                 return "llvm.x86.sse2.padds.w";
857         case OP_PSUBB_SAT:
858                 return "llvm.x86.sse2.psubs.b";
859         case OP_PSUBW_SAT:
860                 return "llvm.x86.sse2.psubs.w";
861         case OP_PADDB_SAT_UN:
862                 return "llvm.x86.sse2.paddus.b";
863         case OP_PADDW_SAT_UN:
864                 return "llvm.x86.sse2.paddus.w";
865         case OP_PSUBB_SAT_UN:
866                 return "llvm.x86.sse2.psubus.b";
867         case OP_PSUBW_SAT_UN:
868                 return "llvm.x86.sse2.psubus.w";
869         case OP_PAVGB_UN:
870                 return "llvm.x86.sse2.pavg.b";
871         case OP_PAVGW_UN:
872                 return "llvm.x86.sse2.pavg.w";
873         case OP_SQRTPS:
874                 return "llvm.x86.sse.sqrt.ps";
875         case OP_SQRTPD:
876                 return "llvm.x86.sse2.sqrt.pd";
877         case OP_RSQRTPS:
878                 return "llvm.x86.sse.rsqrt.ps";
879         case OP_RCPPS:
880                 return "llvm.x86.sse.rcp.ps";
881         case OP_CVTDQ2PD:
882                 return "llvm.x86.sse2.cvtdq2pd";
883         case OP_CVTDQ2PS:
884                 return "llvm.x86.sse2.cvtdq2ps";
885         case OP_CVTPD2DQ:
886                 return "llvm.x86.sse2.cvtpd2dq";
887         case OP_CVTPS2DQ:
888                 return "llvm.x86.sse2.cvtps2dq";
889         case OP_CVTPD2PS:
890                 return "llvm.x86.sse2.cvtpd2ps";
891         case OP_CVTPS2PD:
892                 return "llvm.x86.sse2.cvtps2pd";
893         case OP_CVTTPD2DQ:
894                 return "llvm.x86.sse2.cvttpd2dq";
895         case OP_CVTTPS2DQ:
896                 return "llvm.x86.sse2.cvttps2dq";
897         case OP_COMPPS:
898                 return "llvm.x86.sse.cmp.ps";
899         case OP_COMPPD:
900                 return "llvm.x86.sse2.cmp.pd";
901         case OP_PACKW:
902                 return "llvm.x86.sse2.packsswb.128";
903         case OP_PACKD:
904                 return "llvm.x86.sse2.packssdw.128";
905         case OP_PACKW_UN:
906                 return "llvm.x86.sse2.packuswb.128";
907         case OP_PACKD_UN:
908                 return "llvm.x86.sse41.packusdw";
909         case OP_PMULW_HIGH:
910                 return "llvm.x86.sse2.pmulh.w";
911         case OP_PMULW_HIGH_UN:
912                 return "llvm.x86.sse2.pmulhu.w";
913 #endif
914         default:
915                 g_assert_not_reached ();
916                 return NULL;
917         }
918 }
919
920 static LLVMTypeRef
921 simd_op_to_llvm_type (int opcode)
922 {
923 #if defined(TARGET_X86) || defined(TARGET_AMD64)
924         switch (opcode) {
925         case OP_EXTRACT_R8:
926         case OP_EXPAND_R8:
927                 return type_to_simd_type (MONO_TYPE_R8);
928         case OP_EXTRACT_I8:
929         case OP_EXPAND_I8:
930                 return type_to_simd_type (MONO_TYPE_I8);
931         case OP_EXTRACT_I4:
932         case OP_EXPAND_I4:
933                 return type_to_simd_type (MONO_TYPE_I4);
934         case OP_EXTRACT_I2:
935         case OP_EXTRACT_U2:
936         case OP_EXTRACTX_U2:
937         case OP_EXPAND_I2:
938                 return type_to_simd_type (MONO_TYPE_I2);
939         case OP_EXTRACT_I1:
940         case OP_EXTRACT_U1:
941         case OP_EXPAND_I1:
942                 return type_to_simd_type (MONO_TYPE_I1);
943         case OP_EXPAND_R4:
944                 return type_to_simd_type (MONO_TYPE_R4);
945         case OP_CVTDQ2PD:
946         case OP_CVTDQ2PS:
947                 return type_to_simd_type (MONO_TYPE_I4);
948         case OP_CVTPD2DQ:
949         case OP_CVTPD2PS:
950         case OP_CVTTPD2DQ:
951                 return type_to_simd_type (MONO_TYPE_R8);
952         case OP_CVTPS2DQ:
953         case OP_CVTPS2PD:
954         case OP_CVTTPS2DQ:
955                 return type_to_simd_type (MONO_TYPE_R4);
956         case OP_EXTRACT_MASK:
957                 return type_to_simd_type (MONO_TYPE_I1);
958         case OP_SQRTPS:
959         case OP_RSQRTPS:
960         case OP_RCPPS:
961         case OP_DUPPS_LOW:
962         case OP_DUPPS_HIGH:
963                 return type_to_simd_type (MONO_TYPE_R4);
964         case OP_SQRTPD:
965         case OP_DUPPD:
966                 return type_to_simd_type (MONO_TYPE_R8);
967         default:
968                 g_assert_not_reached ();
969                 return NULL;
970         }
971 #else
972         return NULL;
973 #endif
974 }
975
976 /*
977  * get_bb:
978  *
979  *   Return the LLVM basic block corresponding to BB.
980  */
981 static LLVMBasicBlockRef
982 get_bb (EmitContext *ctx, MonoBasicBlock *bb)
983 {
984         char bb_name_buf [128];
985         char *bb_name;
986
987         if (ctx->bblocks [bb->block_num].bblock == NULL) {
988                 if (bb->flags & BB_EXCEPTION_HANDLER) {
989                         int clause_index = (mono_get_block_region_notry (ctx->cfg, bb->region) >> 8) - 1;
990                         sprintf (bb_name_buf, "EH_CLAUSE%d_BB%d", clause_index, bb->block_num);
991                         bb_name = bb_name_buf;
992                 } else if (bb->block_num < 256) {
993                         if (!ctx->module->bb_names) {
994                                 ctx->module->bb_names_len = 256;
995                                 ctx->module->bb_names = g_new0 (char*, ctx->module->bb_names_len);
996                         }
997                         if (!ctx->module->bb_names [bb->block_num]) {
998                                 char *n;
999
1000                                 n = g_strdup_printf ("BB%d", bb->block_num);
1001                                 mono_memory_barrier ();
1002                                 ctx->module->bb_names [bb->block_num] = n;
1003                         }
1004                         bb_name = ctx->module->bb_names [bb->block_num];
1005                 } else {
1006                         sprintf (bb_name_buf, "BB%d", bb->block_num);
1007                         bb_name = bb_name_buf;
1008                 }
1009
1010                 ctx->bblocks [bb->block_num].bblock = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
1011                 ctx->bblocks [bb->block_num].end_bblock = ctx->bblocks [bb->block_num].bblock;
1012         }
1013
1014         return ctx->bblocks [bb->block_num].bblock;
1015 }
1016
1017 /* 
1018  * get_end_bb:
1019  *
1020  *   Return the last LLVM bblock corresponding to BB.
1021  * This might not be equal to the bb returned by get_bb () since we need to generate
1022  * multiple LLVM bblocks for a mono bblock to handle throwing exceptions.
1023  */
1024 static LLVMBasicBlockRef
1025 get_end_bb (EmitContext *ctx, MonoBasicBlock *bb)
1026 {
1027         get_bb (ctx, bb);
1028         return ctx->bblocks [bb->block_num].end_bblock;
1029 }
1030
1031 static LLVMBasicBlockRef
1032 gen_bb (EmitContext *ctx, const char *prefix)
1033 {
1034         char bb_name [128];
1035
1036         sprintf (bb_name, "%s%d", prefix, ++ ctx->ex_index);
1037         return LLVMAppendBasicBlock (ctx->lmethod, bb_name);
1038 }
1039
1040 /*
1041  * resolve_patch:
1042  *
1043  *   Return the target of the patch identified by TYPE and TARGET.
1044  */
1045 static gpointer
1046 resolve_patch (MonoCompile *cfg, MonoJumpInfoType type, gconstpointer target)
1047 {
1048         MonoJumpInfo ji;
1049
1050         memset (&ji, 0, sizeof (ji));
1051         ji.type = type;
1052         ji.data.target = target;
1053
1054         return mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
1055 }
1056
1057 /*
1058  * convert_full:
1059  *
1060  *   Emit code to convert the LLVM value V to DTYPE.
1061  */
1062 static LLVMValueRef
1063 convert_full (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype, gboolean is_unsigned)
1064 {
1065         LLVMTypeRef stype = LLVMTypeOf (v);
1066
1067         if (stype != dtype) {
1068                 gboolean ext = FALSE;
1069
1070                 /* Extend */
1071                 if (dtype == LLVMInt64Type () && (stype == LLVMInt32Type () || stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1072                         ext = TRUE;
1073                 else if (dtype == LLVMInt32Type () && (stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
1074                         ext = TRUE;
1075                 else if (dtype == LLVMInt16Type () && (stype == LLVMInt8Type ()))
1076                         ext = TRUE;
1077
1078                 if (ext)
1079                         return is_unsigned ? LLVMBuildZExt (ctx->builder, v, dtype, "") : LLVMBuildSExt (ctx->builder, v, dtype, "");
1080
1081                 if (dtype == LLVMDoubleType () && stype == LLVMFloatType ())
1082                         return LLVMBuildFPExt (ctx->builder, v, dtype, "");
1083
1084                 /* Trunc */
1085                 if (stype == LLVMInt64Type () && (dtype == LLVMInt32Type () || dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1086                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1087                 if (stype == LLVMInt32Type () && (dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
1088                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1089                 if (stype == LLVMInt16Type () && dtype == LLVMInt8Type ())
1090                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
1091                 if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
1092                         return LLVMBuildFPTrunc (ctx->builder, v, dtype, "");
1093
1094                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1095                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1096                 if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
1097                         return LLVMBuildIntToPtr (ctx->builder, v, dtype, "");
1098                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
1099                         return LLVMBuildPtrToInt (ctx->builder, v, dtype, "");
1100
1101                 if (mono_arch_is_soft_float ()) {
1102                         if (stype == LLVMInt32Type () && dtype == LLVMFloatType ())
1103                                 return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1104                         if (stype == LLVMInt32Type () && dtype == LLVMDoubleType ())
1105                                 return LLVMBuildBitCast (ctx->builder, LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), ""), dtype, "");
1106                 }
1107
1108                 if (LLVMGetTypeKind (stype) == LLVMVectorTypeKind && LLVMGetTypeKind (dtype) == LLVMVectorTypeKind)
1109                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
1110
1111                 LLVMDumpValue (v);
1112                 LLVMDumpValue (LLVMConstNull (dtype));
1113                 g_assert_not_reached ();
1114                 return NULL;
1115         } else {
1116                 return v;
1117         }
1118 }
1119
1120 static LLVMValueRef
1121 convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
1122 {
1123         return convert_full (ctx, v, dtype, FALSE);
1124 }
1125
1126 /*
1127  * emit_volatile_load:
1128  *
1129  *   If vreg is volatile, emit a load from its address.
1130  */
1131 static LLVMValueRef
1132 emit_volatile_load (EmitContext *ctx, int vreg)
1133 {
1134         MonoType *t;
1135
1136         LLVMValueRef v = LLVMBuildLoad (ctx->builder, ctx->addresses [vreg], "");
1137         t = ctx->vreg_cli_types [vreg];
1138         if (t && !t->byref) {
1139                 /* 
1140                  * Might have to zero extend since llvm doesn't have 
1141                  * unsigned types.
1142                  */
1143                 if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_U2 || t->type == MONO_TYPE_CHAR || t->type == MONO_TYPE_BOOLEAN)
1144                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt32Type (), "");
1145                 else if (t->type == MONO_TYPE_I1 || t->type == MONO_TYPE_I2)
1146                         v = LLVMBuildSExt (ctx->builder, v, LLVMInt32Type (), "");
1147                 else if (t->type == MONO_TYPE_U8)
1148                         v = LLVMBuildZExt (ctx->builder, v, LLVMInt64Type (), "");
1149         }
1150
1151         return v;
1152 }
1153
1154 /*
1155  * emit_volatile_store:
1156  *
1157  *   If VREG is volatile, emit a store from its value to its address.
1158  */
1159 static void
1160 emit_volatile_store (EmitContext *ctx, int vreg)
1161 {
1162         MonoInst *var = get_vreg_to_inst (ctx->cfg, vreg);
1163
1164         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1165                 g_assert (ctx->addresses [vreg]);
1166                 LLVMBuildStore (ctx->builder, convert (ctx, ctx->values [vreg], type_to_llvm_type (ctx, var->inst_vtype)), ctx->addresses [vreg]);
1167         }
1168 }
1169
1170 static LLVMTypeRef
1171 sig_to_llvm_sig_no_cinfo (EmitContext *ctx, MonoMethodSignature *sig)
1172 {
1173         LLVMTypeRef ret_type;
1174         LLVMTypeRef *param_types = NULL;
1175         LLVMTypeRef res;
1176         int i, pindex;
1177         MonoType *rtype;
1178
1179         rtype = mini_get_underlying_type (sig->ret);
1180         ret_type = type_to_llvm_type (ctx, rtype);
1181         CHECK_FAILURE (ctx);
1182
1183         param_types = g_new0 (LLVMTypeRef, (sig->param_count * 8) + 3);
1184         pindex = 0;
1185
1186         if (sig->hasthis)
1187                 param_types [pindex ++] = ThisType ();
1188         for (i = 0; i < sig->param_count; ++i)
1189                 param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
1190
1191         CHECK_FAILURE (ctx);
1192
1193         res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
1194         g_free (param_types);
1195
1196         return res;
1197
1198  FAILURE:
1199         g_free (param_types);
1200
1201         return NULL;
1202 }
1203
1204 /*
1205  * sig_to_llvm_sig_full:
1206  *
1207  *   Return the LLVM signature corresponding to the mono signature SIG using the
1208  * calling convention information in CINFO. Fill out the parameter mapping information in CINFO.
1209  */
1210 static LLVMTypeRef
1211 sig_to_llvm_sig_full (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *cinfo)
1212 {
1213         LLVMTypeRef ret_type;
1214         LLVMTypeRef *param_types = NULL;
1215         LLVMTypeRef res;
1216         int i, j, pindex, vret_arg_pindex = 0;
1217         gboolean vretaddr = FALSE;
1218         MonoType *rtype;
1219
1220         if (!cinfo)
1221                 return sig_to_llvm_sig_no_cinfo (ctx, sig);
1222
1223         rtype = mini_get_underlying_type (sig->ret);
1224         ret_type = type_to_llvm_type (ctx, rtype);
1225         CHECK_FAILURE (ctx);
1226
1227         switch (cinfo->ret.storage) {
1228         case LLVMArgVtypeInReg:
1229                 /* LLVM models this by returning an aggregate value */
1230                 if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgNone) {
1231                         LLVMTypeRef members [2];
1232
1233                         members [0] = IntPtrType ();
1234                         ret_type = LLVMStructType (members, 1, FALSE);
1235                 } else if (cinfo->ret.pair_storage [0] == LLVMArgNone && cinfo->ret.pair_storage [1] == LLVMArgNone) {
1236                         /* Empty struct */
1237                         ret_type = LLVMVoidType ();
1238                 } else if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgInIReg) {
1239                         LLVMTypeRef members [2];
1240
1241                         members [0] = IntPtrType ();
1242                         members [1] = IntPtrType ();
1243                         ret_type = LLVMStructType (members, 2, FALSE);
1244                 } else {
1245                         g_assert_not_reached ();
1246                 }
1247                 break;
1248         case LLVMArgVtypeByVal:
1249                 /* Vtype returned normally by val */
1250                 break;
1251         case LLVMArgVtypeAsScalar:
1252                 /* LLVM models this by returning an int */
1253                 g_assert (cinfo->ret.nslots == 1 || cinfo->ret.nslots == 2);
1254                 ret_type = LLVMIntType (cinfo->ret.nslots * sizeof (mgreg_t) * 8);
1255                 break;
1256         case LLVMArgFpStruct: {
1257                 /* Vtype returned as a fp struct */
1258                 LLVMTypeRef members [16];
1259
1260                 /* Have to create our own structure since we don't map fp structures to LLVM fp structures yet */
1261                 for (i = 0; i < cinfo->ret.nslots; ++i)
1262                         members [i] = cinfo->ret.esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1263                 ret_type = LLVMStructType (members, cinfo->ret.nslots, FALSE);
1264                 break;
1265         }
1266         case LLVMArgVtypeByRef:
1267                 /* Vtype returned using a hidden argument */
1268                 ret_type = LLVMVoidType ();
1269                 break;
1270         case LLVMArgVtypeRetAddr:
1271         case LLVMArgScalarRetAddr:
1272                 vretaddr = TRUE;
1273                 ret_type = LLVMVoidType ();
1274                 break;
1275         default:
1276                 break;
1277         }
1278
1279         param_types = g_new0 (LLVMTypeRef, (sig->param_count * 8) + 3);
1280         pindex = 0;
1281         if (cinfo->ret.storage == LLVMArgVtypeByRef) {
1282                 /*
1283                  * Has to be the first argument because of the sret argument attribute
1284                  * FIXME: This might conflict with passing 'this' as the first argument, but
1285                  * this is only used on arm64 which has a dedicated struct return register.
1286                  */
1287                 cinfo->vret_arg_pindex = pindex;
1288                 param_types [pindex] = type_to_llvm_arg_type (ctx, sig->ret);
1289                 CHECK_FAILURE (ctx);
1290                 param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1291                 pindex ++;
1292         }
1293         if (!ctx->llvm_only && cinfo->rgctx_arg) {
1294                 cinfo->rgctx_arg_pindex = pindex;
1295                 param_types [pindex] = ctx->module->ptr_type;
1296                 pindex ++;
1297         }
1298         if (cinfo->imt_arg) {
1299                 cinfo->imt_arg_pindex = pindex;
1300                 param_types [pindex] = ctx->module->ptr_type;
1301                 pindex ++;
1302         }
1303         if (vretaddr) {
1304                 /* Compute the index in the LLVM signature where the vret arg needs to be passed */
1305                 vret_arg_pindex = pindex;
1306                 if (cinfo->vret_arg_index == 1) {
1307                         /* Add the slots consumed by the first argument */
1308                         LLVMArgInfo *ainfo = &cinfo->args [0];
1309                         switch (ainfo->storage) {
1310                         case LLVMArgVtypeInReg:
1311                                 for (j = 0; j < 2; ++j) {
1312                                         if (ainfo->pair_storage [j] == LLVMArgInIReg)
1313                                                 vret_arg_pindex ++;
1314                                 }
1315                                 break;
1316                         default:
1317                                 vret_arg_pindex ++;
1318                         }
1319                 }
1320
1321                 cinfo->vret_arg_pindex = vret_arg_pindex;
1322         }                               
1323
1324         if (vretaddr && vret_arg_pindex == pindex)
1325                 param_types [pindex ++] = IntPtrType ();
1326         if (sig->hasthis) {
1327                 cinfo->this_arg_pindex = pindex;
1328                 param_types [pindex ++] = ThisType ();
1329                 cinfo->args [0].pindex = cinfo->this_arg_pindex;
1330         }
1331         if (vretaddr && vret_arg_pindex == pindex)
1332                 param_types [pindex ++] = IntPtrType ();
1333         for (i = 0; i < sig->param_count; ++i) {
1334                 LLVMArgInfo *ainfo = &cinfo->args [i + sig->hasthis];
1335
1336                 if (vretaddr && vret_arg_pindex == pindex)
1337                         param_types [pindex ++] = IntPtrType ();
1338                 ainfo->pindex = pindex;
1339
1340                 switch (ainfo->storage) {
1341                 case LLVMArgVtypeInReg:
1342                         for (j = 0; j < 2; ++j) {
1343                                 switch (ainfo->pair_storage [j]) {
1344                                 case LLVMArgInIReg:
1345                                         param_types [pindex ++] = LLVMIntType (sizeof (gpointer) * 8);
1346                                         break;
1347                                 case LLVMArgNone:
1348                                         break;
1349                                 default:
1350                                         g_assert_not_reached ();
1351                                 }
1352                         }
1353                         break;
1354                 case LLVMArgVtypeByVal:
1355                         param_types [pindex] = type_to_llvm_arg_type (ctx, ainfo->type);
1356                         CHECK_FAILURE (ctx);
1357                         param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1358                         pindex ++;
1359                         break;
1360                 case LLVMArgAsIArgs:
1361                         param_types [pindex] = LLVMArrayType (IntPtrType (), ainfo->nslots);
1362                         pindex ++;
1363                         break;
1364                 case LLVMArgVtypeByRef:
1365                 case LLVMArgScalarByRef:
1366                         param_types [pindex] = type_to_llvm_arg_type (ctx, ainfo->type);
1367                         CHECK_FAILURE (ctx);
1368                         param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
1369                         pindex ++;
1370                         break;
1371                 case LLVMArgAsFpArgs: {
1372                         int j;
1373
1374                         for (j = 0; j < ainfo->nslots; ++j)
1375                                 param_types [pindex + j] = ainfo->esize == 8 ? LLVMDoubleType () : LLVMFloatType ();
1376                         pindex += ainfo->nslots;
1377                         break;
1378                 }
1379                 case LLVMArgVtypeAsScalar:
1380                         g_assert_not_reached ();
1381                         break;
1382                 default:
1383                         param_types [pindex ++] = type_to_llvm_arg_type (ctx, ainfo->type);
1384                         break;
1385                 }
1386         }
1387         if (vretaddr && vret_arg_pindex == pindex)
1388                 param_types [pindex ++] = IntPtrType ();
1389         if (ctx->llvm_only && cinfo->rgctx_arg) {
1390                 /* Pass the rgctx as the last argument */
1391                 cinfo->rgctx_arg_pindex = pindex;
1392                 param_types [pindex] = ctx->module->ptr_type;
1393                 pindex ++;
1394         }
1395
1396         CHECK_FAILURE (ctx);
1397
1398         res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
1399         g_free (param_types);
1400
1401         return res;
1402
1403  FAILURE:
1404         g_free (param_types);
1405
1406         return NULL;
1407 }
1408
1409 static LLVMTypeRef
1410 sig_to_llvm_sig (EmitContext *ctx, MonoMethodSignature *sig)
1411 {
1412         return sig_to_llvm_sig_full (ctx, sig, NULL);
1413 }
1414
1415 /*
1416  * LLVMFunctionType1:
1417  *
1418  *   Create an LLVM function type from the arguments.
1419  */
1420 static G_GNUC_UNUSED LLVMTypeRef
1421 LLVMFunctionType0 (LLVMTypeRef ReturnType,
1422                                    int IsVarArg)
1423 {
1424         return LLVMFunctionType (ReturnType, NULL, 0, IsVarArg);
1425 }
1426
1427 /*
1428  * LLVMFunctionType1:
1429  *
1430  *   Create an LLVM function type from the arguments.
1431  */
1432 static G_GNUC_UNUSED LLVMTypeRef 
1433 LLVMFunctionType1 (LLVMTypeRef ReturnType,
1434                                    LLVMTypeRef ParamType1,
1435                                    int IsVarArg)
1436 {
1437         LLVMTypeRef param_types [1];
1438
1439         param_types [0] = ParamType1;
1440
1441         return LLVMFunctionType (ReturnType, param_types, 1, IsVarArg);
1442 }
1443
1444 /*
1445  * LLVMFunctionType2:
1446  *
1447  *   Create an LLVM function type from the arguments.
1448  */
1449 static G_GNUC_UNUSED LLVMTypeRef
1450 LLVMFunctionType2 (LLVMTypeRef ReturnType,
1451                                    LLVMTypeRef ParamType1,
1452                                    LLVMTypeRef ParamType2,
1453                                    int IsVarArg)
1454 {
1455         LLVMTypeRef param_types [2];
1456
1457         param_types [0] = ParamType1;
1458         param_types [1] = ParamType2;
1459
1460         return LLVMFunctionType (ReturnType, param_types, 2, IsVarArg);
1461 }
1462
1463 /*
1464  * LLVMFunctionType3:
1465  *
1466  *   Create an LLVM function type from the arguments.
1467  */
1468 static G_GNUC_UNUSED LLVMTypeRef
1469 LLVMFunctionType3 (LLVMTypeRef ReturnType,
1470                                    LLVMTypeRef ParamType1,
1471                                    LLVMTypeRef ParamType2,
1472                                    LLVMTypeRef ParamType3,
1473                                    int IsVarArg)
1474 {
1475         LLVMTypeRef param_types [3];
1476
1477         param_types [0] = ParamType1;
1478         param_types [1] = ParamType2;
1479         param_types [2] = ParamType3;
1480
1481         return LLVMFunctionType (ReturnType, param_types, 3, IsVarArg);
1482 }
1483
1484 /*
1485  * create_builder:
1486  *
1487  *   Create an LLVM builder and remember it so it can be freed later.
1488  */
1489 static LLVMBuilderRef
1490 create_builder (EmitContext *ctx)
1491 {
1492         LLVMBuilderRef builder = LLVMCreateBuilder ();
1493
1494         ctx->builders = g_slist_prepend_mempool (ctx->cfg->mempool, ctx->builders, builder);
1495
1496         return builder;
1497 }
1498
1499 static char*
1500 get_aotconst_name (MonoJumpInfoType type, gconstpointer data, int got_offset)
1501 {
1502         char *name;
1503
1504         switch (type) {
1505         case MONO_PATCH_INFO_INTERNAL_METHOD:
1506                 name = g_strdup_printf ("jit_icall_%s", data);
1507                 break;
1508         default:
1509                 name = g_strdup_printf ("%s_%d", mono_ji_type_to_string (type), got_offset);
1510                 break;
1511         }
1512
1513         return name;
1514 }
1515
1516 static LLVMValueRef
1517 get_aotconst (EmitContext *ctx, MonoJumpInfoType type, gconstpointer data)
1518 {
1519         MonoCompile *cfg;
1520         guint32 got_offset;
1521         LLVMValueRef indexes [2];
1522         MonoJumpInfo *ji;
1523         LLVMValueRef got_entry_addr, load;
1524         LLVMBuilderRef builder = ctx->builder;
1525         char *name = NULL;
1526
1527         cfg = ctx->cfg;
1528
1529         ji = g_new0 (MonoJumpInfo, 1);
1530         ji->type = type;
1531         ji->data.target = data;
1532
1533         ji = mono_aot_patch_info_dup (ji);
1534
1535         ji->next = cfg->patch_info;
1536         cfg->patch_info = ji;
1537
1538         got_offset = mono_aot_get_got_offset (cfg->patch_info);
1539         ctx->module->max_got_offset = MAX (ctx->module->max_got_offset, got_offset);
1540         /* 
1541          * If the got slot is shared, it means its initialized when the aot image is loaded, so we don't need to
1542          * explicitly initialize it.
1543          */
1544         if (!mono_aot_is_shared_got_offset (got_offset)) {
1545                 //mono_print_ji (ji);
1546                 //printf ("\n");
1547                 ctx->has_got_access = TRUE;
1548         }
1549
1550         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1551         indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
1552         got_entry_addr = LLVMBuildGEP (builder, ctx->module->got_var, indexes, 2, "");
1553
1554         name = get_aotconst_name (type, data, got_offset);
1555         load = LLVMBuildLoad (builder, got_entry_addr, name ? name : "");
1556         g_free (name);
1557         //set_invariant_load_flag (load);
1558
1559         return load;
1560 }
1561
1562 static LLVMValueRef
1563 get_callee (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gconstpointer data)
1564 {
1565         LLVMValueRef callee;
1566         char *callee_name;
1567         if (ctx->llvm_only) {
1568                 LLVMValueRef load;
1569
1570                 callee_name = mono_aot_get_direct_call_symbol (type, data);
1571                 if (callee_name) {
1572                         /* Directly callable */
1573                         // FIXME: Locking
1574                         callee = (LLVMValueRef)g_hash_table_lookup (ctx->module->direct_callables, callee_name);
1575                         if (!callee) {
1576                                 callee = LLVMAddFunction (ctx->lmodule, callee_name, llvm_sig);
1577
1578                                 LLVMSetVisibility (callee, LLVMHiddenVisibility);
1579
1580                                 g_hash_table_insert (ctx->module->direct_callables, (char*)callee_name, callee);
1581                         } else {
1582                                 g_free (callee_name);
1583                         }
1584                         return callee;
1585                 }
1586
1587                 /*
1588                  * Calls are made through the GOT.
1589                  */
1590                 load = get_aotconst (ctx, type, data);
1591
1592                 return convert (ctx, load, LLVMPointerType (llvm_sig, 0));
1593         } else {
1594                 MonoJumpInfo *ji = NULL;
1595
1596                 callee_name = mono_aot_get_plt_symbol (type, data);
1597                 if (!callee_name)
1598                         return NULL;
1599
1600                 if (ctx->cfg->compile_aot)
1601                         /* Add a patch so referenced wrappers can be compiled in full aot mode */
1602                         mono_add_patch_info (ctx->cfg, 0, type, data);
1603
1604                 // FIXME: Locking
1605                 callee = (LLVMValueRef)g_hash_table_lookup (ctx->module->plt_entries, callee_name);
1606                 if (!callee) {
1607                         callee = LLVMAddFunction (ctx->lmodule, callee_name, llvm_sig);
1608
1609                         LLVMSetVisibility (callee, LLVMHiddenVisibility);
1610
1611                         g_hash_table_insert (ctx->module->plt_entries, (char*)callee_name, callee);
1612                 }
1613
1614                 if (ctx->cfg->compile_aot) {
1615                         ji = g_new0 (MonoJumpInfo, 1);
1616                         ji->type = type;
1617                         ji->data.target = data;
1618
1619                         g_hash_table_insert (ctx->module->plt_entries_ji, ji, callee);
1620                 }
1621
1622                 return callee;
1623         }
1624 }
1625
1626 static int
1627 get_handler_clause (MonoCompile *cfg, MonoBasicBlock *bb)
1628 {
1629         MonoMethodHeader *header = cfg->header;
1630         MonoExceptionClause *clause;
1631         int i;
1632
1633         /* Directly */
1634         if (bb->region != -1 && MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
1635                 return (bb->region >> 8) - 1;
1636
1637         /* Indirectly */
1638         for (i = 0; i < header->num_clauses; ++i) {
1639                 clause = &header->clauses [i];
1640                            
1641                 if (MONO_OFFSET_IN_CLAUSE (clause, bb->real_offset) && clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1642                         return i;
1643         }
1644
1645         return -1;
1646 }
1647
1648 static MonoExceptionClause *
1649 get_most_deep_clause (MonoCompile *cfg, EmitContext *ctx, MonoBasicBlock *bb)
1650 {
1651         // Since they're sorted by nesting we just need
1652         // the first one that the bb is a member of
1653         MonoExceptionClause *last = NULL;
1654
1655         for (int i = 0; i < cfg->header->num_clauses; i++) {
1656                 MonoExceptionClause *curr = &cfg->header->clauses [i];
1657
1658                 if (MONO_OFFSET_IN_CLAUSE (curr, bb->real_offset))
1659                         return curr;
1660                 /*
1661                 if (MONO_OFFSET_IN_CLAUSE (curr, bb->real_offset)) {
1662                         if (last && CLAUSE_END(last) > CLAUSE_END(curr))
1663                                 last = curr;
1664                         else
1665                                 last = curr;
1666                 } else if(last) {
1667                         break;
1668                 }
1669                 */
1670         }
1671
1672         return last;
1673 }
1674         
1675 static void
1676 set_metadata_flag (LLVMValueRef v, const char *flag_name)
1677 {
1678         LLVMValueRef md_arg;
1679         int md_kind;
1680
1681         md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1682         md_arg = LLVMMDString ("mono", 4);
1683         LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1684 }
1685
1686 static void
1687 set_invariant_load_flag (LLVMValueRef v)
1688 {
1689         LLVMValueRef md_arg;
1690         int md_kind;
1691         const char *flag_name;
1692
1693         // FIXME: Cache this
1694         flag_name = "invariant.load";
1695         md_kind = LLVMGetMDKindID (flag_name, strlen (flag_name));
1696         md_arg = LLVMMDString ("<index>", strlen ("<index>"));
1697         LLVMSetMetadata (v, md_kind, LLVMMDNode (&md_arg, 1));
1698 }
1699
1700 /*
1701  * emit_call:
1702  *
1703  *   Emit an LLVM call or invoke instruction depending on whenever the call is inside
1704  * a try region.
1705  */
1706 static LLVMValueRef
1707 emit_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, LLVMValueRef callee, LLVMValueRef *args, int pindex)
1708 {
1709         MonoCompile *cfg = ctx->cfg;
1710         LLVMValueRef lcall = NULL;
1711         LLVMBuilderRef builder = *builder_ref;
1712         MonoExceptionClause *clause;
1713
1714         if (ctx->llvm_only) {
1715                 clause = get_most_deep_clause (cfg, ctx, bb);
1716
1717                 if (clause) {
1718                         g_assert (clause->flags == MONO_EXCEPTION_CLAUSE_NONE || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
1719
1720                         /*
1721                          * Have to use an invoke instead of a call, branching to the
1722                          * handler bblock of the clause containing this bblock.
1723                          */
1724                         intptr_t key = CLAUSE_END(clause);
1725
1726                         LLVMBasicBlockRef lpad_bb = (LLVMBasicBlockRef)g_hash_table_lookup (ctx->exc_meta, (gconstpointer)key);
1727
1728                         // FIXME: Find the one that has the lowest end bound for the right start address
1729                         // FIXME: Finally + nesting
1730
1731                         if (lpad_bb) {
1732                                 LLVMBasicBlockRef noex_bb = gen_bb (ctx, "CALL_NOEX_BB");
1733
1734                                 /* Use an invoke */
1735                                 lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, lpad_bb, "");
1736
1737                                 builder = ctx->builder = create_builder (ctx);
1738                                 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1739
1740                                 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1741                         }
1742                 }
1743         } else {
1744                 int clause_index = get_handler_clause (cfg, bb);
1745
1746                 if (clause_index != -1) {
1747                         MonoMethodHeader *header = cfg->header;
1748                         MonoExceptionClause *ec = &header->clauses [clause_index];
1749                         MonoBasicBlock *tblock;
1750                         LLVMBasicBlockRef ex_bb, noex_bb;
1751
1752                         /*
1753                          * Have to use an invoke instead of a call, branching to the
1754                          * handler bblock of the clause containing this bblock.
1755                          */
1756
1757                         g_assert (ec->flags == MONO_EXCEPTION_CLAUSE_NONE || ec->flags == MONO_EXCEPTION_CLAUSE_FINALLY);
1758
1759                         tblock = cfg->cil_offset_to_bb [ec->handler_offset];
1760                         g_assert (tblock);
1761
1762                         ctx->bblocks [tblock->block_num].invoke_target = TRUE;
1763
1764                         ex_bb = get_bb (ctx, tblock);
1765
1766                         noex_bb = gen_bb (ctx, "NOEX_BB");
1767
1768                         /* Use an invoke */
1769                         lcall = LLVMBuildInvoke (builder, callee, args, pindex, noex_bb, ex_bb, "");
1770
1771                         builder = ctx->builder = create_builder (ctx);
1772                         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1773
1774                         ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1775                 }
1776         }
1777         
1778         if (!lcall) {
1779                 lcall = LLVMBuildCall (builder, callee, args, pindex, "");
1780                 ctx->builder = builder;
1781         }
1782
1783         if (builder_ref)
1784                 *builder_ref = ctx->builder;
1785
1786         return lcall;
1787 }
1788
1789 static LLVMValueRef
1790 emit_load_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting, BarrierKind barrier)
1791 {
1792         const char *intrins_name;
1793         LLVMValueRef args [16], res;
1794         LLVMTypeRef addr_type;
1795
1796         if (is_faulting && bb->region != -1 && !ctx->cfg->llvm_only) {
1797                 LLVMAtomicOrdering ordering;
1798
1799                 switch (barrier) {
1800                 case LLVM_BARRIER_NONE:
1801                         ordering = LLVMAtomicOrderingNotAtomic;
1802                         break;
1803                 case LLVM_BARRIER_ACQ:
1804                         ordering = LLVMAtomicOrderingAcquire;
1805                         break;
1806                 case LLVM_BARRIER_SEQ:
1807                         ordering = LLVMAtomicOrderingSequentiallyConsistent;
1808                         break;
1809                 default:
1810                         g_assert_not_reached ();
1811                         break;
1812                 }
1813
1814                 /*
1815                  * We handle loads which can fault by calling a mono specific intrinsic
1816                  * using an invoke, so they are handled properly inside try blocks.
1817                  * We can't use this outside clauses, since LLVM optimizes intrinsics which
1818                  * are marked with IntrReadArgMem.
1819                  */
1820                 switch (size) {
1821                 case 1:
1822                         intrins_name = "llvm.mono.load.i8.p0i8";
1823                         break;
1824                 case 2:
1825                         intrins_name = "llvm.mono.load.i16.p0i16";
1826                         break;
1827                 case 4:
1828                         intrins_name = "llvm.mono.load.i32.p0i32";
1829                         break;
1830                 case 8:
1831                         intrins_name = "llvm.mono.load.i64.p0i64";
1832                         break;
1833                 default:
1834                         g_assert_not_reached ();
1835                 }
1836
1837                 addr_type = LLVMTypeOf (addr);
1838                 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0) || addr_type == LLVMPointerType (LLVMFloatType (), 0))
1839                         addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1840
1841                 args [0] = addr;
1842                 args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1843                 args [2] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1844                 args [3] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
1845                 res = emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->lmodule, intrins_name), args, 4);
1846
1847                 if (addr_type == LLVMPointerType (LLVMDoubleType (), 0))
1848                         res = LLVMBuildBitCast (*builder_ref, res, LLVMDoubleType (), "");
1849                 else if (addr_type == LLVMPointerType (LLVMFloatType (), 0))
1850                         res = LLVMBuildBitCast (*builder_ref, res, LLVMFloatType (), "");
1851                 
1852                 return res;
1853         } else {
1854                 LLVMValueRef res;
1855
1856                 /* 
1857                  * We emit volatile loads for loads which can fault, because otherwise
1858                  * LLVM will generate invalid code when encountering a load from a
1859                  * NULL address.
1860                  */
1861                  res = mono_llvm_build_load (*builder_ref, addr, name, is_faulting, barrier);
1862
1863                  /* Mark it with a custom metadata */
1864                  /*
1865                  if (is_faulting)
1866                          set_metadata_flag (res, "mono.faulting.load");
1867                  */
1868
1869                  return res;
1870         }
1871 }
1872
1873 static LLVMValueRef
1874 emit_load (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef addr, const char *name, gboolean is_faulting)
1875 {
1876         return emit_load_general (ctx, bb, builder_ref, size, addr, name, is_faulting, LLVM_BARRIER_NONE);
1877 }
1878
1879 static void
1880 emit_store_general (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, gboolean is_faulting, BarrierKind barrier)
1881 {
1882         const char *intrins_name;
1883         LLVMValueRef args [16];
1884
1885         if (is_faulting && bb->region != -1 && !ctx->cfg->llvm_only) {
1886                 LLVMAtomicOrdering ordering;
1887
1888                 switch (barrier) {
1889                 case LLVM_BARRIER_NONE:
1890                         ordering = LLVMAtomicOrderingNotAtomic;
1891                         break;
1892                 case LLVM_BARRIER_REL:
1893                         ordering = LLVMAtomicOrderingRelease;
1894                         break;
1895                 case LLVM_BARRIER_SEQ:
1896                         ordering = LLVMAtomicOrderingSequentiallyConsistent;
1897                         break;
1898                 default:
1899                         g_assert_not_reached ();
1900                         break;
1901                 }
1902
1903                 switch (size) {
1904                 case 1:
1905                         intrins_name = "llvm.mono.store.i8.p0i8";
1906                         break;
1907                 case 2:
1908                         intrins_name = "llvm.mono.store.i16.p0i16";
1909                         break;
1910                 case 4:
1911                         intrins_name = "llvm.mono.store.i32.p0i32";
1912                         break;
1913                 case 8:
1914                         intrins_name = "llvm.mono.store.i64.p0i64";
1915                         break;
1916                 default:
1917                         g_assert_not_reached ();
1918                 }
1919
1920                 if (LLVMTypeOf (value) == LLVMDoubleType () || LLVMTypeOf (value) == LLVMFloatType ()) {
1921                         value = LLVMBuildBitCast (*builder_ref, value, LLVMIntType (size * 8), "");
1922                         addr = LLVMBuildBitCast (*builder_ref, addr, LLVMPointerType (LLVMIntType (size * 8), 0), "");
1923                 }
1924
1925                 args [0] = value;
1926                 args [1] = addr;
1927                 args [2] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
1928                 args [3] = LLVMConstInt (LLVMInt1Type (), TRUE, FALSE);
1929                 args [4] = LLVMConstInt (LLVMInt32Type (), ordering, FALSE);
1930                 emit_call (ctx, bb, builder_ref, LLVMGetNamedFunction (ctx->lmodule, intrins_name), args, 5);
1931         } else {
1932                 mono_llvm_build_store (*builder_ref, value, addr, is_faulting, barrier);
1933         }
1934 }
1935
1936 static void
1937 emit_store (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, int size, LLVMValueRef value, LLVMValueRef addr, gboolean is_faulting)
1938 {
1939         emit_store_general (ctx, bb, builder_ref, size, value, addr, is_faulting, LLVM_BARRIER_NONE);
1940 }
1941
1942 /*
1943  * emit_cond_system_exception:
1944  *
1945  *   Emit code to throw the exception EXC_TYPE if the condition CMP is false.
1946  * Might set the ctx exception.
1947  */
1948 static void
1949 emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp)
1950 {
1951         LLVMBasicBlockRef ex_bb, ex2_bb = NULL, noex_bb;
1952         LLVMBuilderRef builder;
1953         MonoClass *exc_class;
1954         LLVMValueRef args [2];
1955         LLVMValueRef callee;
1956         
1957         ex_bb = gen_bb (ctx, "EX_BB");
1958         if (ctx->llvm_only)
1959                 ex2_bb = gen_bb (ctx, "EX2_BB");
1960         noex_bb = gen_bb (ctx, "NOEX_BB");
1961
1962         LLVMBuildCondBr (ctx->builder, cmp, ex_bb, noex_bb);
1963
1964         exc_class = mono_class_from_name (mono_get_corlib (), "System", exc_type);
1965         g_assert (exc_class);
1966
1967         /* Emit exception throwing code */
1968         ctx->builder = builder = create_builder (ctx);
1969         LLVMPositionBuilderAtEnd (builder, ex_bb);
1970
1971         if (ctx->cfg->llvm_only) {
1972                 static LLVMTypeRef sig;
1973
1974                 if (!sig)
1975                         sig = LLVMFunctionType1 (LLVMVoidType (), LLVMInt32Type (), FALSE);
1976                 callee = get_callee (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_llvm_throw_corlib_exception");
1977
1978                 LLVMBuildBr (builder, ex2_bb);
1979
1980                 ctx->builder = builder = create_builder (ctx);
1981                 LLVMPositionBuilderAtEnd (ctx->builder, ex2_bb);
1982
1983                 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
1984                 emit_call (ctx, bb, &builder, callee, args, 1);
1985                 LLVMBuildUnreachable (builder);
1986
1987                 ctx->builder = builder = create_builder (ctx);
1988                 LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
1989
1990                 ctx->bblocks [bb->block_num].end_bblock = noex_bb;
1991
1992                 ctx->ex_index ++;
1993                 return;
1994         }
1995
1996         callee = ctx->module->throw_corlib_exception;
1997         if (!callee) {
1998                 LLVMTypeRef sig;
1999                 const char *icall_name;
2000
2001                 sig = LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), LLVMPointerType (LLVMInt8Type (), 0), FALSE);
2002                 icall_name = "llvm_throw_corlib_exception_abs_trampoline";
2003
2004                 if (ctx->cfg->compile_aot) {
2005                         callee = get_callee (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
2006                 } else {
2007                         callee = LLVMAddFunction (ctx->lmodule, "llvm_throw_corlib_exception_trampoline", sig);
2008
2009                         /*
2010                          * Differences between the LLVM/non-LLVM throw corlib exception trampoline:
2011                          * - On x86, LLVM generated code doesn't push the arguments
2012                          * - The trampoline takes the throw address as an arguments, not a pc offset.
2013                          */
2014                         LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
2015
2016                         mono_memory_barrier ();
2017                         ctx->module->throw_corlib_exception = callee;
2018                 }
2019         }
2020
2021         if (IS_TARGET_X86 || IS_TARGET_AMD64)
2022                 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token - MONO_TOKEN_TYPE_DEF, FALSE);
2023         else
2024                 args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token, FALSE);
2025
2026         /*
2027          * The LLVM mono branch contains changes so a block address can be passed as an
2028          * argument to a call.
2029          */
2030         args [1] = LLVMBlockAddress (ctx->lmethod, ex_bb);
2031         emit_call (ctx, bb, &builder, callee, args, 2);
2032
2033         LLVMBuildUnreachable (builder);
2034
2035         ctx->builder = builder = create_builder (ctx);
2036         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
2037
2038         ctx->bblocks [bb->block_num].end_bblock = noex_bb;
2039
2040         ctx->ex_index ++;
2041         return;
2042 }
2043
2044 /*
2045  * emit_args_to_vtype:
2046  *
2047  *   Emit code to store the vtype in the arguments args to the address ADDRESS.
2048  */
2049 static void
2050 emit_args_to_vtype (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args)
2051 {
2052         int j, size, nslots;
2053
2054         size = get_vtype_size (t);
2055
2056         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
2057                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
2058         }
2059
2060         if (ainfo->storage == LLVMArgAsFpArgs)
2061                 nslots = ainfo->nslots;
2062         else
2063                 nslots = 2;
2064
2065         for (j = 0; j < nslots; ++j) {
2066                 LLVMValueRef index [2], addr, daddr;
2067                 int part_size = size > sizeof (gpointer) ? sizeof (gpointer) : size;
2068                 LLVMTypeRef part_type;
2069
2070                 if (ainfo->pair_storage [j] == LLVMArgNone)
2071                         continue;
2072
2073                 switch (ainfo->pair_storage [j]) {
2074                 case LLVMArgInIReg: {
2075                         part_type = LLVMIntType (part_size * 8);
2076                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
2077                                 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
2078                                 addr = LLVMBuildGEP (builder, address, index, 1, "");
2079                         } else {
2080                                 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
2081                                 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2082                                 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2083                         }
2084                         LLVMBuildStore (builder, convert (ctx, args [j], part_type), LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (part_type, 0), ""));
2085                         break;
2086                 }
2087                 case LLVMArgInFPReg: {
2088                         LLVMTypeRef arg_type;
2089
2090                         if (ainfo->esize == 8)
2091                                 arg_type = LLVMDoubleType ();
2092                         else
2093                                 arg_type = LLVMFloatType ();
2094
2095                         index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2096                         daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
2097                         addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2098                         LLVMBuildStore (builder, args [j], addr);
2099                         break;
2100                 }
2101                 case LLVMArgNone:
2102                         break;
2103                 default:
2104                         g_assert_not_reached ();
2105                 }
2106
2107                 size -= sizeof (gpointer);
2108         }
2109 }
2110
2111 /*
2112  * emit_vtype_to_args:
2113  *
2114  *   Emit code to load a vtype at address ADDRESS into scalar arguments. Store the arguments
2115  * into ARGS, and the number of arguments into NARGS.
2116  */
2117 static void
2118 emit_vtype_to_args (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *args, guint32 *nargs)
2119 {
2120         int pindex = 0;
2121         int j, size, nslots;
2122         LLVMTypeRef arg_type;
2123
2124         size = get_vtype_size (t);
2125
2126         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t)))
2127                 address = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (LLVMInt8Type (), 0), "");
2128
2129         if (ainfo->storage == LLVMArgAsFpArgs)
2130                 nslots = ainfo->nslots;
2131         else
2132                 nslots = 2;
2133         for (j = 0; j < nslots; ++j) {
2134                 LLVMValueRef index [2], addr, daddr;
2135                 int partsize = size > sizeof (gpointer) ? sizeof (gpointer) : size;
2136
2137                 if (ainfo->pair_storage [j] == LLVMArgNone)
2138                         continue;
2139
2140                 switch (ainfo->pair_storage [j]) {
2141                 case LLVMArgInIReg:
2142                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (t))) {
2143                                 index [0] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
2144                                 addr = LLVMBuildGEP (builder, address, index, 1, "");
2145                         } else {
2146                                 daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (IntPtrType (), 0), "");
2147                                 index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2148                                 addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2149                         }
2150                         args [pindex ++] = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (LLVMIntType (partsize * 8), 0), ""), ""), IntPtrType ());
2151                         break;
2152                 case LLVMArgInFPReg:
2153                         if (ainfo->esize == 8)
2154                                 arg_type = LLVMDoubleType ();
2155                         else
2156                                 arg_type = LLVMFloatType ();
2157                         daddr = LLVMBuildBitCast (ctx->builder, address, LLVMPointerType (arg_type, 0), "");
2158                         index [0] = LLVMConstInt (LLVMInt32Type (), j, FALSE);
2159                         addr = LLVMBuildGEP (builder, daddr, index, 1, "");
2160                         args [pindex ++] = LLVMBuildLoad (builder, addr, "");
2161                         break;
2162                 case LLVMArgNone:
2163                         break;
2164                 default:
2165                         g_assert_not_reached ();
2166                 }
2167                 size -= sizeof (gpointer);
2168         }
2169
2170         *nargs = pindex;
2171 }
2172
2173 static LLVMValueRef
2174 build_alloca_llvm_type_name (EmitContext *ctx, LLVMTypeRef t, int align, const char *name)
2175 {
2176         /*
2177          * Have to place all alloca's at the end of the entry bb, since otherwise they would
2178          * get executed every time control reaches them.
2179          */
2180         LLVMPositionBuilder (ctx->alloca_builder, get_bb (ctx, ctx->cfg->bb_entry), ctx->last_alloca);
2181
2182         ctx->last_alloca = mono_llvm_build_alloca (ctx->alloca_builder, t, NULL, align, name);
2183         return ctx->last_alloca;
2184 }
2185
2186 static LLVMValueRef
2187 build_alloca_llvm_type (EmitContext *ctx, LLVMTypeRef t, int align)
2188 {
2189         return build_alloca_llvm_type_name (ctx, t, align, "");
2190 }
2191
2192 static LLVMValueRef
2193 build_alloca (EmitContext *ctx, MonoType *t)
2194 {
2195         MonoClass *k = mono_class_from_mono_type (t);
2196         int align;
2197
2198         if (MONO_CLASS_IS_SIMD (ctx->cfg, k))
2199                 align = 16;
2200         else
2201                 align = mono_class_min_align (k);
2202
2203         /* Sometimes align is not a power of 2 */
2204         while (mono_is_power_of_two (align) == -1)
2205                 align ++;
2206
2207         return build_alloca_llvm_type (ctx, type_to_llvm_type (ctx, t), align);
2208 }
2209
2210 /*
2211  * Put the global into the 'llvm.used' array to prevent it from being optimized away.
2212  */
2213 static void
2214 mark_as_used (MonoLLVMModule *module, LLVMValueRef global)
2215 {
2216         if (!module->used)
2217                 module->used = g_ptr_array_sized_new (16);
2218         g_ptr_array_add (module->used, global);
2219 }
2220
2221 static void
2222 emit_llvm_used (MonoLLVMModule *module)
2223 {
2224         LLVMModuleRef lmodule = module->lmodule;
2225         LLVMTypeRef used_type;
2226         LLVMValueRef used, *used_elem;
2227         int i;
2228                 
2229         if (!module->used)
2230                 return;
2231
2232         used_type = LLVMArrayType (LLVMPointerType (LLVMInt8Type (), 0), module->used->len);
2233         used = LLVMAddGlobal (lmodule, used_type, "llvm.used");
2234         used_elem = g_new0 (LLVMValueRef, module->used->len);
2235         for (i = 0; i < module->used->len; ++i)
2236                 used_elem [i] = LLVMConstBitCast ((LLVMValueRef)g_ptr_array_index (module->used, i), LLVMPointerType (LLVMInt8Type (), 0));
2237         LLVMSetInitializer (used, LLVMConstArray (LLVMPointerType (LLVMInt8Type (), 0), used_elem, module->used->len));
2238         LLVMSetLinkage (used, LLVMAppendingLinkage);
2239         LLVMSetSection (used, "llvm.metadata");
2240 }
2241
2242 /*
2243  * emit_get_method:
2244  *
2245  *   Emit a function mapping method indexes to their code
2246  */
2247 static void
2248 emit_get_method (MonoLLVMModule *module)
2249 {
2250         LLVMModuleRef lmodule = module->lmodule;
2251         LLVMValueRef func, switch_ins, m;
2252         LLVMBasicBlockRef entry_bb, fail_bb, bb, code_start_bb, code_end_bb;
2253         LLVMBasicBlockRef *bbs;
2254         LLVMTypeRef rtype;
2255         LLVMBuilderRef builder;
2256         char *name;
2257         int i;
2258
2259         /*
2260          * Emit a switch statement. Emitting a table of function addresses is smaller/faster,
2261          * but generating code seems safer.
2262          */
2263         rtype = LLVMPointerType (LLVMInt8Type (), 0);
2264         func = LLVMAddFunction (lmodule, module->get_method_symbol, LLVMFunctionType1 (rtype, LLVMInt32Type (), FALSE));
2265         LLVMSetLinkage (func, LLVMExternalLinkage);
2266         LLVMSetVisibility (func, LLVMHiddenVisibility);
2267         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2268         module->get_method = func;
2269
2270         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2271
2272         /*
2273          * Return llvm_code_start/llvm_code_end when called with -1/-2.
2274          * Hopefully, the toolchain doesn't reorder these functions. If it does,
2275          * then we will have to find another solution.
2276          */
2277
2278         name = g_strdup_printf ("BB_CODE_START");
2279         code_start_bb = LLVMAppendBasicBlock (func, name);
2280         g_free (name);
2281         builder = LLVMCreateBuilder ();
2282         LLVMPositionBuilderAtEnd (builder, code_start_bb);
2283         LLVMBuildRet (builder, LLVMBuildBitCast (builder, module->code_start, rtype, ""));
2284
2285         name = g_strdup_printf ("BB_CODE_END");
2286         code_end_bb = LLVMAppendBasicBlock (func, name);
2287         g_free (name);
2288         builder = LLVMCreateBuilder ();
2289         LLVMPositionBuilderAtEnd (builder, code_end_bb);
2290         LLVMBuildRet (builder, LLVMBuildBitCast (builder, module->code_end, rtype, ""));
2291
2292         bbs = g_new0 (LLVMBasicBlockRef, module->max_method_idx + 1);
2293         for (i = 0; i < module->max_method_idx + 1; ++i) {
2294                 name = g_strdup_printf ("BB_%d", i);
2295                 bb = LLVMAppendBasicBlock (func, name);
2296                 g_free (name);
2297                 bbs [i] = bb;
2298
2299                 builder = LLVMCreateBuilder ();
2300                 LLVMPositionBuilderAtEnd (builder, bb);
2301
2302                 m = (LLVMValueRef)g_hash_table_lookup (module->idx_to_lmethod, GINT_TO_POINTER (i));
2303                 if (m)
2304                         LLVMBuildRet (builder, LLVMBuildBitCast (builder, m, rtype, ""));
2305                 else
2306                         LLVMBuildRet (builder, LLVMConstNull (rtype));
2307         }
2308
2309         fail_bb = LLVMAppendBasicBlock (func, "FAIL");
2310         builder = LLVMCreateBuilder ();
2311         LLVMPositionBuilderAtEnd (builder, fail_bb);
2312         LLVMBuildRet (builder, LLVMConstNull (rtype));
2313
2314         builder = LLVMCreateBuilder ();
2315         LLVMPositionBuilderAtEnd (builder, entry_bb);
2316
2317         switch_ins = LLVMBuildSwitch (builder, LLVMGetParam (func, 0), fail_bb, 0);
2318         LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), -1, FALSE), code_start_bb);
2319         LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), -2, FALSE), code_end_bb);
2320         for (i = 0; i < module->max_method_idx + 1; ++i) {
2321                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i, FALSE), bbs [i]);
2322         }
2323
2324         mark_as_used (module, func);
2325 }
2326
2327 /*
2328  * emit_get_unbox_tramp:
2329  *
2330  *   Emit a function mapping method indexes to their unbox trampoline
2331  */
2332 static void
2333 emit_get_unbox_tramp (MonoLLVMModule *module)
2334 {
2335         LLVMModuleRef lmodule = module->lmodule;
2336         LLVMValueRef func, switch_ins, m;
2337         LLVMBasicBlockRef entry_bb, fail_bb, bb;
2338         LLVMBasicBlockRef *bbs;
2339         LLVMTypeRef rtype;
2340         LLVMBuilderRef builder;
2341         char *name;
2342         int i;
2343
2344         /* Similar to emit_get_method () */
2345
2346         rtype = LLVMPointerType (LLVMInt8Type (), 0);
2347         func = LLVMAddFunction (lmodule, module->get_unbox_tramp_symbol, LLVMFunctionType1 (rtype, LLVMInt32Type (), FALSE));
2348         LLVMSetLinkage (func, LLVMExternalLinkage);
2349         LLVMSetVisibility (func, LLVMHiddenVisibility);
2350         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2351         module->get_unbox_tramp = func;
2352
2353         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2354
2355         bbs = g_new0 (LLVMBasicBlockRef, module->max_method_idx + 1);
2356         for (i = 0; i < module->max_method_idx + 1; ++i) {
2357                 m = (LLVMValueRef)g_hash_table_lookup (module->idx_to_unbox_tramp, GINT_TO_POINTER (i));
2358                 if (!m)
2359                         continue;
2360
2361                 name = g_strdup_printf ("BB_%d", i);
2362                 bb = LLVMAppendBasicBlock (func, name);
2363                 g_free (name);
2364                 bbs [i] = bb;
2365
2366                 builder = LLVMCreateBuilder ();
2367                 LLVMPositionBuilderAtEnd (builder, bb);
2368
2369                 LLVMBuildRet (builder, LLVMBuildBitCast (builder, m, rtype, ""));
2370         }
2371
2372         fail_bb = LLVMAppendBasicBlock (func, "FAIL");
2373         builder = LLVMCreateBuilder ();
2374         LLVMPositionBuilderAtEnd (builder, fail_bb);
2375         LLVMBuildRet (builder, LLVMConstNull (rtype));
2376
2377         builder = LLVMCreateBuilder ();
2378         LLVMPositionBuilderAtEnd (builder, entry_bb);
2379
2380         switch_ins = LLVMBuildSwitch (builder, LLVMGetParam (func, 0), fail_bb, 0);
2381         for (i = 0; i < module->max_method_idx + 1; ++i) {
2382                 m = (LLVMValueRef)g_hash_table_lookup (module->idx_to_unbox_tramp, GINT_TO_POINTER (i));
2383                 if (!m)
2384                         continue;
2385
2386                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i, FALSE), bbs [i]);
2387         }
2388
2389         mark_as_used (module, func);
2390 }
2391
2392 /* Add a function to mark the beginning of LLVM code */
2393 static void
2394 emit_llvm_code_start (MonoLLVMModule *module)
2395 {
2396         LLVMModuleRef lmodule = module->lmodule;
2397         LLVMValueRef func;
2398         LLVMBasicBlockRef entry_bb;
2399         LLVMBuilderRef builder;
2400
2401         func = LLVMAddFunction (lmodule, "llvm_code_start", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
2402         LLVMSetLinkage (func, LLVMInternalLinkage);
2403         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2404         module->code_start = func;
2405         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2406         builder = LLVMCreateBuilder ();
2407         LLVMPositionBuilderAtEnd (builder, entry_bb);
2408         LLVMBuildRetVoid (builder);
2409 }
2410
2411 static LLVMValueRef
2412 emit_init_icall_wrapper (MonoLLVMModule *module, const char *name, const char *icall_name, int subtype)
2413 {
2414         LLVMModuleRef lmodule = module->lmodule;
2415         LLVMValueRef func, indexes [2], got_entry_addr, args [16], callee;
2416         LLVMBasicBlockRef entry_bb;
2417         LLVMBuilderRef builder;
2418         LLVMTypeRef sig;
2419         MonoJumpInfo *ji;
2420         int got_offset;
2421
2422         switch (subtype) {
2423         case 0:
2424                 func = LLVMAddFunction (lmodule, name, LLVMFunctionType1 (LLVMVoidType (), LLVMInt32Type (), FALSE));
2425                 sig = LLVMFunctionType2 (LLVMVoidType (), IntPtrType (), LLVMInt32Type (), FALSE);
2426                 break;
2427         case 1:
2428                 func = LLVMAddFunction (lmodule, name, LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), IntPtrType (), FALSE));
2429                 sig = LLVMFunctionType3 (LLVMVoidType (), IntPtrType (), LLVMInt32Type (), IntPtrType (), FALSE);
2430                 break;
2431         case 2:
2432                 func = LLVMAddFunction (lmodule, name, LLVMFunctionType2 (LLVMVoidType (), LLVMInt32Type (), ObjRefType (), FALSE));
2433                 sig = LLVMFunctionType3 (LLVMVoidType (), IntPtrType (), LLVMInt32Type (), ObjRefType (), FALSE);
2434                 break;
2435         default:
2436                 g_assert_not_reached ();
2437         }
2438         LLVMSetLinkage (func, LLVMInternalLinkage);
2439         LLVMAddFunctionAttr (func, LLVMNoInlineAttribute);
2440         mono_llvm_set_preserveall_cc (func);
2441         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2442         builder = LLVMCreateBuilder ();
2443         LLVMPositionBuilderAtEnd (builder, entry_bb);
2444
2445         /* get_aotconst */
2446         ji = g_new0 (MonoJumpInfo, 1);
2447         ji->type = MONO_PATCH_INFO_AOT_MODULE;
2448         ji = mono_aot_patch_info_dup (ji);
2449         got_offset = mono_aot_get_got_offset (ji);
2450         module->max_got_offset = MAX (module->max_got_offset, got_offset);
2451         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2452         indexes [1] = LLVMConstInt (LLVMInt32Type (), got_offset, FALSE);
2453         got_entry_addr = LLVMBuildGEP (builder, module->got_var, indexes, 2, "");
2454         args [0] = LLVMBuildPtrToInt (builder, LLVMBuildLoad (builder, got_entry_addr, ""), IntPtrType (), "");
2455         args [1] = LLVMGetParam (func, 0);
2456         if (subtype)
2457                 args [2] = LLVMGetParam (func, 1);
2458
2459         ji = g_new0 (MonoJumpInfo, 1);
2460         ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2461         ji->data.name = icall_name;
2462         ji = mono_aot_patch_info_dup (ji);
2463         got_offset = mono_aot_get_got_offset (ji);
2464         module->max_got_offset = MAX (module->max_got_offset, got_offset);
2465         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2466         indexes [1] = LLVMConstInt (LLVMInt32Type (), got_offset, FALSE);
2467         got_entry_addr = LLVMBuildGEP (builder, module->got_var, indexes, 2, "");
2468         callee = LLVMBuildLoad (builder, got_entry_addr, "");
2469         callee = LLVMBuildBitCast (builder, callee, LLVMPointerType (sig, 0), "");
2470         LLVMBuildCall (builder, callee, args, LLVMCountParamTypes (sig), "");
2471
2472         // Set the inited flag
2473         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2474         indexes [1] = LLVMGetParam (func, 0);
2475         LLVMBuildStore (builder, LLVMConstInt (LLVMInt8Type (), 1, FALSE), LLVMBuildGEP (builder, module->inited_var, indexes, 2, ""));
2476
2477         LLVMBuildRetVoid (builder);
2478
2479         LLVMVerifyFunction(func, 0);
2480         return func;
2481 }
2482
2483 /*
2484  * Emit wrappers around the C icalls used to initialize llvm methods, to
2485  * make the calling code smaller and to enable usage of the llvm
2486  * PreserveAll calling convention.
2487  */
2488 static void
2489 emit_init_icall_wrappers (MonoLLVMModule *module)
2490 {
2491         module->init_method = emit_init_icall_wrapper (module, "init_method", "mono_aot_init_llvm_method", 0);
2492         module->init_method_gshared_rgctx = emit_init_icall_wrapper (module, "init_method_gshared_rgctx", "mono_aot_init_gshared_method_rgctx", 1);
2493         module->init_method_gshared_this = emit_init_icall_wrapper (module, "init_method_gshared_this", "mono_aot_init_gshared_method_this", 2);
2494 }
2495
2496 static void
2497 emit_llvm_code_end (MonoLLVMModule *module)
2498 {
2499         LLVMModuleRef lmodule = module->lmodule;
2500         LLVMValueRef func;
2501         LLVMBasicBlockRef entry_bb;
2502         LLVMBuilderRef builder;
2503
2504         func = LLVMAddFunction (lmodule, "llvm_code_end", LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE));
2505         LLVMSetLinkage (func, LLVMInternalLinkage);
2506         LLVMAddFunctionAttr (func, LLVMNoUnwindAttribute);
2507         module->code_end = func;
2508         entry_bb = LLVMAppendBasicBlock (func, "ENTRY");
2509         builder = LLVMCreateBuilder ();
2510         LLVMPositionBuilderAtEnd (builder, entry_bb);
2511         LLVMBuildRetVoid (builder);
2512 }
2513
2514 static void
2515 emit_div_check (EmitContext *ctx, LLVMBuilderRef builder, MonoBasicBlock *bb, MonoInst *ins, LLVMValueRef lhs, LLVMValueRef rhs)
2516 {
2517         gboolean need_div_check = ctx->cfg->backend->need_div_check;
2518
2519         if (bb->region)
2520                 /* LLVM doesn't know that these can throw an exception since they are not called through an intrinsic */
2521                 need_div_check = TRUE;
2522
2523         if (!need_div_check)
2524                 return;
2525
2526         switch (ins->opcode) {
2527         case OP_IDIV:
2528         case OP_LDIV:
2529         case OP_IREM:
2530         case OP_LREM:
2531         case OP_IDIV_UN:
2532         case OP_LDIV_UN:
2533         case OP_IREM_UN:
2534         case OP_LREM_UN:
2535         case OP_IDIV_IMM:
2536         case OP_LDIV_IMM:
2537         case OP_IREM_IMM:
2538         case OP_LREM_IMM:
2539         case OP_IDIV_UN_IMM:
2540         case OP_LDIV_UN_IMM:
2541         case OP_IREM_UN_IMM:
2542         case OP_LREM_UN_IMM: {
2543                 LLVMValueRef cmp;
2544                 gboolean is_signed = (ins->opcode == OP_IDIV || ins->opcode == OP_LDIV || ins->opcode == OP_IREM || ins->opcode == OP_LREM ||
2545                                                           ins->opcode == OP_IDIV_IMM || ins->opcode == OP_LDIV_IMM || ins->opcode == OP_IREM_IMM || ins->opcode == OP_LREM_IMM);
2546
2547                 cmp = LLVMBuildICmp (builder, LLVMIntEQ, rhs, LLVMConstInt (LLVMTypeOf (rhs), 0, FALSE), "");
2548                 emit_cond_system_exception (ctx, bb, "DivideByZeroException", cmp);
2549                 CHECK_FAILURE (ctx);
2550                 builder = ctx->builder;
2551
2552                 /* b == -1 && a == 0x80000000 */
2553                 if (is_signed) {
2554                         LLVMValueRef c = (LLVMTypeOf (lhs) == LLVMInt32Type ()) ? LLVMConstInt (LLVMTypeOf (lhs), 0x80000000, FALSE) : LLVMConstInt (LLVMTypeOf (lhs), 0x8000000000000000LL, FALSE);
2555                         LLVMValueRef cond1 = LLVMBuildICmp (builder, LLVMIntEQ, rhs, LLVMConstInt (LLVMTypeOf (rhs), -1, FALSE), "");
2556                         LLVMValueRef cond2 = LLVMBuildICmp (builder, LLVMIntEQ, lhs, c, "");
2557
2558                         cmp = LLVMBuildICmp (builder, LLVMIntEQ, LLVMBuildAnd (builder, cond1, cond2, ""), LLVMConstInt (LLVMInt1Type (), 1, FALSE), "");
2559                         emit_cond_system_exception (ctx, bb, "OverflowException", cmp);
2560                         CHECK_FAILURE (ctx);
2561                         builder = ctx->builder;
2562                 }
2563                 break;
2564         }
2565         default:
2566                 break;
2567         }
2568
2569  FAILURE:
2570         return;
2571 }
2572
2573 /*
2574  * emit_init_method:
2575  *
2576  *   Emit code to initialize the GOT slots used by the method.
2577  */
2578 static void
2579 emit_init_method (EmitContext *ctx)
2580 {
2581         LLVMValueRef indexes [16], args [16], callee;
2582         LLVMValueRef inited_var, cmp, call;
2583         LLVMBasicBlockRef inited_bb, notinited_bb;
2584         LLVMBuilderRef builder = ctx->builder;
2585         MonoCompile *cfg = ctx->cfg;
2586
2587         ctx->module->max_inited_idx = MAX (ctx->module->max_inited_idx, cfg->method_index);
2588         ctx->module->max_method_idx = MAX (ctx->module->max_method_idx, cfg->method_index);
2589
2590         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2591         indexes [1] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, FALSE);
2592         inited_var = LLVMBuildLoad (builder, LLVMBuildGEP (builder, ctx->module->inited_var, indexes, 2, ""), "is_inited");
2593
2594         args [0] = inited_var;
2595         args [1] = LLVMConstInt (LLVMInt8Type (), 1, FALSE);
2596         inited_var = LLVMBuildCall (ctx->builder, LLVMGetNamedFunction (ctx->lmodule, "llvm.expect.i8"), args, 2, "");
2597
2598         cmp = LLVMBuildICmp (builder, LLVMIntEQ, inited_var, LLVMConstInt (LLVMTypeOf (inited_var), 0, FALSE), "");
2599
2600         inited_bb = ctx->inited_bb;
2601         notinited_bb = gen_bb (ctx, "NOTINITED_BB");
2602
2603         LLVMBuildCondBr (ctx->builder, cmp, notinited_bb, inited_bb);
2604
2605         builder = ctx->builder = create_builder (ctx);
2606         LLVMPositionBuilderAtEnd (ctx->builder, notinited_bb);
2607
2608         // FIXME: Cache
2609         if (ctx->rgctx_arg) {
2610                 args [0] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, 0);
2611                 args [1] = convert (ctx, ctx->rgctx_arg, IntPtrType ());
2612                 callee = ctx->module->init_method_gshared_rgctx;
2613                 call = LLVMBuildCall (builder, callee, args, 2, "");
2614         } else if (cfg->gshared) {
2615                 args [0] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, 0);
2616                 args [1] = convert (ctx, ctx->this_arg, ObjRefType ());
2617                 callee = ctx->module->init_method_gshared_this;
2618                 call = LLVMBuildCall (builder, callee, args, 2, "");
2619         } else {
2620                 args [0] = LLVMConstInt (LLVMInt32Type (), cfg->method_index, 0);
2621                 callee = ctx->module->init_method;
2622                 call = LLVMBuildCall (builder, callee, args, 1, "");
2623         }
2624
2625         /*
2626          * This enables llvm to keep arguments in their original registers/
2627          * scratch registers, since the call will not clobber them.
2628          */
2629         mono_llvm_set_call_preserveall_cc (call);
2630
2631         LLVMBuildBr (builder, inited_bb);
2632         ctx->bblocks [cfg->bb_entry->block_num].end_bblock = inited_bb;
2633
2634         builder = ctx->builder = create_builder (ctx);
2635         LLVMPositionBuilderAtEnd (ctx->builder, inited_bb);
2636 }
2637
2638 static void
2639 emit_unbox_tramp (EmitContext *ctx, const char *method_name, LLVMTypeRef method_type, LLVMValueRef method, int method_index)
2640 {
2641         /*
2642          * Emit unbox trampoline using a tail call
2643          */
2644         LLVMValueRef tramp, call, *args;
2645         LLVMBuilderRef builder;
2646         LLVMBasicBlockRef lbb;
2647         char *tramp_name;
2648         int i, nargs;
2649
2650         tramp_name = g_strdup_printf ("ut_%s", method_name);
2651         tramp = LLVMAddFunction (ctx->module->lmodule, tramp_name, method_type);
2652         LLVMSetLinkage (tramp, LLVMInternalLinkage);
2653         LLVMAddFunctionAttr (tramp, LLVMNoUnwindAttribute);
2654         if (!ctx->llvm_only && ctx->rgctx_arg_pindex != -1)
2655                 LLVMAddAttribute (LLVMGetParam (tramp, ctx->rgctx_arg_pindex), LLVMInRegAttribute);
2656
2657         lbb = LLVMAppendBasicBlock (tramp, "");
2658         builder = LLVMCreateBuilder ();
2659         LLVMPositionBuilderAtEnd (builder, lbb);
2660
2661         nargs = LLVMCountParamTypes (method_type);
2662         args = g_new0 (LLVMValueRef, nargs);
2663         for (i = 0; i < nargs; ++i) {
2664                 args [i] = LLVMGetParam (tramp, i);
2665                 if (i == ctx->this_arg_pindex) {
2666                         LLVMTypeRef arg_type = LLVMTypeOf (args [i]);
2667
2668                         args [i] = LLVMBuildPtrToInt (builder, args [i], IntPtrType (), "");
2669                         args [i] = LLVMBuildAdd (builder, args [i], LLVMConstInt (IntPtrType (), sizeof (MonoObject), FALSE), "");
2670                         args [i] = LLVMBuildIntToPtr (builder, args [i], arg_type, "");
2671                 }
2672         }
2673         call = LLVMBuildCall (builder, method, args, nargs, "");
2674         if (!ctx->llvm_only && ctx->rgctx_arg_pindex != -1)
2675                 LLVMAddInstrAttribute (call, 1 + ctx->rgctx_arg_pindex, LLVMInRegAttribute);
2676         mono_llvm_set_must_tail (call);
2677         if (LLVMGetReturnType (method_type) == LLVMVoidType ())
2678                 LLVMBuildRetVoid (builder);
2679         else
2680                 LLVMBuildRet (builder, call);
2681
2682         g_hash_table_insert (ctx->module->idx_to_unbox_tramp, GINT_TO_POINTER (method_index), tramp);
2683 }
2684
2685 /*
2686  * emit_entry_bb:
2687  *
2688  *   Emit code to load/convert arguments.
2689  */
2690 static void
2691 emit_entry_bb (EmitContext *ctx, LLVMBuilderRef builder)
2692 {
2693         int i, j, pindex;
2694         MonoCompile *cfg = ctx->cfg;
2695         MonoMethodSignature *sig = ctx->sig;
2696         LLVMCallInfo *linfo = ctx->linfo;
2697         MonoBasicBlock *bb;
2698         char **names;
2699
2700         LLVMBuilderRef old_builder = ctx->builder;
2701         ctx->builder = builder;
2702
2703         ctx->alloca_builder = create_builder (ctx);
2704
2705         /*
2706          * Handle indirect/volatile variables by allocating memory for them
2707          * using 'alloca', and storing their address in a temporary.
2708          */
2709         for (i = 0; i < cfg->num_varinfo; ++i) {
2710                 MonoInst *var = cfg->varinfo [i];
2711                 LLVMTypeRef vtype;
2712
2713                 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (mini_type_is_vtype (var->inst_vtype) && !MONO_CLASS_IS_SIMD (ctx->cfg, var->klass))) {
2714                         vtype = type_to_llvm_type (ctx, var->inst_vtype);
2715                         CHECK_FAILURE (ctx);
2716                         /* Could be already created by an OP_VPHI */
2717                         if (!ctx->addresses [var->dreg])
2718                                 ctx->addresses [var->dreg] = build_alloca (ctx, var->inst_vtype);
2719                         ctx->vreg_cli_types [var->dreg] = var->inst_vtype;
2720                 }
2721         }
2722
2723         names = g_new (char *, sig->param_count);
2724         mono_method_get_param_names (cfg->method, (const char **) names);
2725
2726         for (i = 0; i < sig->param_count; ++i) {
2727                 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
2728                 int reg = cfg->args [i + sig->hasthis]->dreg;
2729                 char *name;
2730
2731                 pindex = ainfo->pindex;
2732
2733                 switch (ainfo->storage) {
2734                 case LLVMArgVtypeInReg:
2735                 case LLVMArgAsFpArgs: {
2736                         LLVMValueRef args [8];
2737                         int j;
2738
2739                         /* The argument is received as a set of int/fp arguments, store them into the real argument */
2740                         memset (args, 0, sizeof (args));
2741                         if (ainfo->storage == LLVMArgVtypeInReg) {
2742                                 args [0] = LLVMGetParam (ctx->lmethod, pindex);
2743                                 if (ainfo->pair_storage [1] != LLVMArgNone)
2744                                         args [1] = LLVMGetParam (ctx->lmethod, pindex + 1);
2745                         } else {
2746                                 g_assert (ainfo->nslots <= 8);
2747                                 for (j = 0; j < ainfo->nslots; ++j)
2748                                         args [j] = LLVMGetParam (ctx->lmethod, pindex + j);
2749                         }
2750                         ctx->addresses [reg] = build_alloca (ctx, ainfo->type);
2751
2752                         emit_args_to_vtype (ctx, builder, ainfo->type, ctx->addresses [reg], ainfo, args);
2753
2754                         if (ainfo->storage == LLVMArgVtypeInReg && MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (ainfo->type))) {
2755                                 /* Treat these as normal values */
2756                                 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2757                         }
2758                         break;
2759                 }
2760                 case LLVMArgVtypeByVal: {
2761                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, pindex);
2762
2763                         if (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (ainfo->type))) {
2764                                 /* Treat these as normal values */
2765                                 ctx->values [reg] = LLVMBuildLoad (builder, ctx->addresses [reg], "");
2766                         }
2767                         break;
2768                 }
2769                 case LLVMArgVtypeByRef: {
2770                         /* The argument is passed by ref */
2771                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, pindex);
2772                         break;
2773                 }
2774                 case LLVMArgScalarByRef: {
2775                         if (names [i])
2776                                 name = g_strdup_printf ("arg_%s", names [i]);
2777                         else
2778                                 name = g_strdup_printf ("arg_%d", i);
2779                         ctx->values [reg] = LLVMBuildLoad (builder, LLVMGetParam (ctx->lmethod, pindex), name);
2780                         g_free (name);
2781                         break;
2782                 }
2783                 case LLVMArgAsIArgs: {
2784                         LLVMValueRef arg = LLVMGetParam (ctx->lmethod, pindex);
2785
2786                         ctx->addresses [reg] = build_alloca (ctx, ainfo->type);
2787
2788                         /* The argument is received as an array of ints, store it into the real argument */
2789                         LLVMBuildStore (ctx->builder, arg, convert (ctx, ctx->addresses [reg], LLVMPointerType (LLVMTypeOf (arg), 0)));
2790                         break;
2791                 }
2792                 case LLVMArgVtypeAsScalar:
2793                         g_assert_not_reached ();
2794                         break;
2795                 default:
2796                         ctx->values [reg] = convert_full (ctx, ctx->values [reg], llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, ainfo->type)), type_is_unsigned (ctx, ainfo->type));
2797                         break;
2798                 }
2799         }
2800         g_free (names);
2801
2802         if (cfg->vret_addr)
2803                 emit_volatile_store (ctx, cfg->vret_addr->dreg);
2804         if (sig->hasthis)
2805                 emit_volatile_store (ctx, cfg->args [0]->dreg);
2806         for (i = 0; i < sig->param_count; ++i)
2807                 if (!mini_type_is_vtype (sig->params [i]))
2808                         emit_volatile_store (ctx, cfg->args [i + sig->hasthis]->dreg);
2809
2810         if (sig->hasthis && !cfg->rgctx_var && cfg->gshared) {
2811                 LLVMValueRef this_alloc;
2812
2813                 /*
2814                  * The exception handling code needs the location where the this argument was
2815                  * stored for gshared methods. We create a separate alloca to hold it, and mark it
2816                  * with the "mono.this" custom metadata to tell llvm that it needs to save its
2817                  * location into the LSDA.
2818                  */
2819                 this_alloc = mono_llvm_build_alloca (builder, ThisType (), LLVMConstInt (LLVMInt32Type (), 1, FALSE), 0, "");
2820                 /* This volatile store will keep the alloca alive */
2821                 mono_llvm_build_store (builder, ctx->values [cfg->args [0]->dreg], this_alloc, TRUE, LLVM_BARRIER_NONE);
2822
2823                 set_metadata_flag (this_alloc, "mono.this");
2824         }
2825
2826         if (cfg->rgctx_var) {
2827                 LLVMValueRef rgctx_alloc, store;
2828
2829                 /*
2830                  * We handle the rgctx arg similarly to the this pointer.
2831                  */
2832                 g_assert (ctx->addresses [cfg->rgctx_var->dreg]);
2833                 rgctx_alloc = ctx->addresses [cfg->rgctx_var->dreg];
2834                 /* This volatile store will keep the alloca alive */
2835                 store = mono_llvm_build_store (builder, convert (ctx, ctx->rgctx_arg, IntPtrType ()), rgctx_alloc, TRUE, LLVM_BARRIER_NONE);
2836
2837                 set_metadata_flag (rgctx_alloc, "mono.this");
2838         }
2839
2840         /* Initialize the method if needed */
2841         if (cfg->compile_aot && ctx->llvm_only) {
2842                 /* Emit a location for the initialization code */
2843                 ctx->init_bb = gen_bb (ctx, "INIT_BB");
2844                 ctx->inited_bb = gen_bb (ctx, "INITED_BB");
2845
2846                 LLVMBuildBr (ctx->builder, ctx->init_bb);
2847                 builder = ctx->builder = create_builder (ctx);
2848                 LLVMPositionBuilderAtEnd (ctx->builder, ctx->inited_bb);
2849                 ctx->bblocks [cfg->bb_entry->block_num].end_bblock = ctx->inited_bb;
2850         }
2851
2852         /* Compute nesting between clauses */
2853         ctx->nested_in = mono_mempool_alloc0 (cfg->mempool, sizeof (GSList*) * cfg->header->num_clauses);
2854         for (i = 0; i < cfg->header->num_clauses; ++i) {
2855                 for (j = 0; j < cfg->header->num_clauses; ++j) {
2856                         MonoExceptionClause *clause1 = &cfg->header->clauses [i];
2857                         MonoExceptionClause *clause2 = &cfg->header->clauses [j];
2858
2859                         if (i != j && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset)
2860                                 ctx->nested_in [i] = g_slist_prepend_mempool (cfg->mempool, ctx->nested_in [i], GINT_TO_POINTER (j));
2861                 }
2862         }
2863
2864         /*
2865          * For finally clauses, create an indicator variable telling OP_ENDFINALLY whenever
2866          * it needs to continue normally, or return back to the exception handling system.
2867          */
2868         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2869                 int clause_index;
2870                 char name [128];
2871
2872                 if (!(bb->region != -1 && (bb->flags & BB_EXCEPTION_HANDLER)))
2873                         continue;
2874
2875                 clause_index = MONO_REGION_CLAUSE_INDEX (bb->region);
2876                 g_hash_table_insert (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)), bb);
2877                 g_hash_table_insert (ctx->clause_to_handler, GINT_TO_POINTER (clause_index), bb);
2878
2879                 if (bb->in_scount == 0) {
2880                         LLVMValueRef val;
2881
2882                         sprintf (name, "finally_ind_bb%d", bb->block_num);
2883                         val = LLVMBuildAlloca (builder, LLVMInt32Type (), name);
2884                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), val);
2885
2886                         ctx->bblocks [bb->block_num].finally_ind = val;
2887                 } else {
2888                         /* Create a variable to hold the exception var */
2889                         if (!ctx->ex_var)
2890                                 ctx->ex_var = LLVMBuildAlloca (builder, ObjRefType (), "exvar");
2891                 }
2892
2893                 /*
2894                  * Create a new bblock which CALL_HANDLER/landing pads can branch to, because branching to the
2895                  * LLVM bblock containing a landing pad causes problems for the
2896                  * LLVM optimizer passes.
2897                  */
2898                 sprintf (name, "BB%d_CALL_HANDLER_TARGET", bb->block_num);
2899                 ctx->bblocks [bb->block_num].call_handler_target_bb = LLVMAppendBasicBlock (ctx->lmethod, name);
2900         }
2901
2902         FAILURE:
2903                 goto CLEANUP;
2904
2905         CLEANUP:
2906                 ctx->builder = old_builder;
2907 }
2908
2909 static void
2910 process_call (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef *builder_ref, MonoInst *ins)
2911 {
2912         MonoCompile *cfg = ctx->cfg;
2913         LLVMModuleRef lmodule = ctx->lmodule;
2914         LLVMValueRef *values = ctx->values;
2915         LLVMValueRef *addresses = ctx->addresses;
2916         MonoCallInst *call = (MonoCallInst*)ins;
2917         MonoMethodSignature *sig = call->signature;
2918         LLVMValueRef callee = NULL, lcall;
2919         LLVMValueRef *args;
2920         LLVMCallInfo *cinfo;
2921         GSList *l;
2922         int i, len, nargs;
2923         gboolean vretaddr;
2924         LLVMTypeRef llvm_sig;
2925         gpointer target;
2926         gboolean is_virtual, calli;
2927         LLVMBuilderRef builder = *builder_ref;
2928
2929         if (call->signature->call_convention != MONO_CALL_DEFAULT)
2930                 LLVM_FAILURE (ctx, "non-default callconv");
2931
2932         cinfo = call->cinfo;
2933         g_assert (cinfo);
2934         if (call->rgctx_arg_reg)
2935                 cinfo->rgctx_arg = TRUE;
2936         if (call->imt_arg_reg)
2937                 cinfo->imt_arg = TRUE;
2938
2939         vretaddr = (cinfo->ret.storage == LLVMArgVtypeRetAddr || cinfo->ret.storage == LLVMArgVtypeByRef || cinfo->ret.storage == LLVMArgScalarRetAddr);
2940
2941         llvm_sig = sig_to_llvm_sig_full (ctx, sig, cinfo);
2942         CHECK_FAILURE (ctx);
2943
2944         is_virtual = (ins->opcode == OP_VOIDCALL_MEMBASE || ins->opcode == OP_CALL_MEMBASE || ins->opcode == OP_VCALL_MEMBASE || ins->opcode == OP_LCALL_MEMBASE || ins->opcode == OP_FCALL_MEMBASE || ins->opcode == OP_RCALL_MEMBASE);
2945         calli = !call->fptr_is_patch && (ins->opcode == OP_VOIDCALL_REG || ins->opcode == OP_CALL_REG || ins->opcode == OP_VCALL_REG || ins->opcode == OP_LCALL_REG || ins->opcode == OP_FCALL_REG || ins->opcode == OP_RCALL_REG);
2946
2947         /* FIXME: Avoid creating duplicate methods */
2948
2949         if (ins->flags & MONO_INST_HAS_METHOD) {
2950                 if (is_virtual) {
2951                         callee = NULL;
2952                 } else {
2953                         if (cfg->compile_aot) {
2954                                 callee = get_callee (ctx, llvm_sig, MONO_PATCH_INFO_METHOD, call->method);
2955                                 if (!callee)
2956                                         LLVM_FAILURE (ctx, "can't encode patch");
2957
2958                                 if (cfg->llvm_only && call->method->klass->image->assembly == ctx->module->assembly) {
2959                                         /*
2960                                          * Collect instructions representing the callee into a hash so they can be replaced
2961                                          * by the llvm method for the callee if the callee turns out to be direct
2962                                          * callable. Currently this only requires it to not fail llvm compilation.
2963                                          */
2964                                         GSList *l = g_hash_table_lookup (ctx->method_to_callers, call->method);
2965                                         l = g_slist_prepend (l, callee);
2966                                         g_hash_table_insert (ctx->method_to_callers, call->method, l);
2967                                 }
2968                         } else {
2969                                 callee = LLVMAddFunction (lmodule, "", llvm_sig);
2970  
2971                                 target =
2972                                         mono_create_jit_trampoline_in_domain (mono_domain_get (),
2973                                                                                                                   call->method);
2974                                 LLVMAddGlobalMapping (ctx->module->ee, callee, target);
2975                         }
2976                 }
2977
2978                 if (call->method && strstr (call->method->klass->name, "AsyncVoidMethodBuilder"))
2979                         /* LLVM miscompiles async methods */
2980                         LLVM_FAILURE (ctx, "#13734");
2981         } else if (calli) {
2982         } else {
2983                 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
2984
2985                 if (info) {
2986                         /*
2987                           MonoJumpInfo ji;
2988
2989                           memset (&ji, 0, sizeof (ji));
2990                           ji.type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
2991                           ji.data.target = info->name;
2992
2993                           target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
2994                         */
2995                         if (cfg->compile_aot) {
2996                                 callee = get_callee (ctx, llvm_sig, MONO_PATCH_INFO_INTERNAL_METHOD, (char*)info->name);
2997                                 if (!callee)
2998                                         LLVM_FAILURE (ctx, "can't encode patch");
2999                         } else {
3000                                 callee = LLVMAddFunction (lmodule, "", llvm_sig);
3001                                 target = (gpointer)mono_icall_get_wrapper (info);
3002                                 LLVMAddGlobalMapping (ctx->module->ee, callee, target);
3003                         }
3004                 } else {
3005                         if (cfg->compile_aot) {
3006                                 callee = NULL;
3007                                 if (cfg->abs_patches) {
3008                                         MonoJumpInfo *abs_ji = (MonoJumpInfo*)g_hash_table_lookup (cfg->abs_patches, call->fptr);
3009                                         if (abs_ji) {
3010                                                 callee = get_callee (ctx, llvm_sig, abs_ji->type, abs_ji->data.target);
3011                                                 if (!callee)
3012                                                         LLVM_FAILURE (ctx, "can't encode patch");
3013                                         }
3014                                 }
3015                                 if (!callee)
3016                                         LLVM_FAILURE (ctx, "aot");
3017                         } else {
3018                                 callee = LLVMAddFunction (lmodule, "", llvm_sig);
3019                                 target = NULL;
3020                                 if (cfg->abs_patches) {
3021                                         MonoJumpInfo *abs_ji = (MonoJumpInfo*)g_hash_table_lookup (cfg->abs_patches, call->fptr);
3022                                         if (abs_ji) {
3023                                                 /*
3024                                                  * FIXME: Some trampolines might have
3025                                                  * their own calling convention on some platforms.
3026                                                  */
3027 #ifndef TARGET_AMD64
3028                                                 if (abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER || abs_ji->type == MONO_PATCH_INFO_MONITOR_ENTER_V4 ||
3029                                                                 abs_ji->type == MONO_PATCH_INFO_MONITOR_EXIT)
3030                                                         LLVM_FAILURE (ctx, "trampoline with own cconv");
3031 #endif
3032                                                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE);
3033                                                 LLVMAddGlobalMapping (ctx->module->ee, callee, target);
3034                                         }
3035                                 }
3036                                 if (!target)
3037                                         LLVMAddGlobalMapping (ctx->module->ee, callee, (gpointer)call->fptr);
3038                         }
3039                 }
3040         }
3041
3042         if (is_virtual) {
3043                 int size = sizeof (gpointer);
3044                 LLVMValueRef index;
3045
3046                 g_assert (ins->inst_offset % size == 0);
3047                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
3048
3049                 callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMPointerType (IntPtrType (), 0), 0)), &index, 1, ""), ""), LLVMPointerType (llvm_sig, 0));
3050         } else if (calli) {
3051                 callee = convert (ctx, values [ins->sreg1], LLVMPointerType (llvm_sig, 0));
3052         } else {
3053                 if (ins->flags & MONO_INST_HAS_METHOD) {
3054                 }
3055         }
3056
3057         /* 
3058          * Collect and convert arguments
3059          */
3060         nargs = (sig->param_count * 16) + sig->hasthis + vretaddr + call->rgctx_reg + call->imt_arg_reg;
3061         len = sizeof (LLVMValueRef) * nargs;
3062         args = (LLVMValueRef*)alloca (len);
3063         memset (args, 0, len);
3064         l = call->out_ireg_args;
3065
3066         if (call->rgctx_arg_reg) {
3067                 g_assert (values [call->rgctx_arg_reg]);
3068                 g_assert (cinfo->rgctx_arg_pindex < nargs);
3069                 /*
3070                  * On ARM, the imt/rgctx argument is passed in a caller save register, but some of our trampolines etc. clobber it, leading to
3071                  * problems is LLVM moves the arg assignment earlier. To work around this, save the argument into a stack slot and load
3072                  * it using a volatile load.
3073                  */
3074 #ifdef TARGET_ARM
3075                 if (!ctx->imt_rgctx_loc)
3076                         ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->module->ptr_type, sizeof (gpointer));
3077                 LLVMBuildStore (builder, convert (ctx, ctx->values [call->rgctx_arg_reg], ctx->module->ptr_type), ctx->imt_rgctx_loc);
3078                 args [cinfo->rgctx_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE, LLVM_BARRIER_NONE);
3079 #else
3080                 args [cinfo->rgctx_arg_pindex] = convert (ctx, values [call->rgctx_arg_reg], ctx->module->ptr_type);
3081 #endif
3082         }
3083         if (call->imt_arg_reg) {
3084                 g_assert (!ctx->llvm_only);
3085                 g_assert (values [call->imt_arg_reg]);
3086                 g_assert (cinfo->imt_arg_pindex < nargs);
3087 #ifdef TARGET_ARM
3088                 if (!ctx->imt_rgctx_loc)
3089                         ctx->imt_rgctx_loc = build_alloca_llvm_type (ctx, ctx->module->ptr_type, sizeof (gpointer));
3090                 LLVMBuildStore (builder, convert (ctx, ctx->values [call->imt_arg_reg], ctx->module->ptr_type), ctx->imt_rgctx_loc);
3091                 args [cinfo->imt_arg_pindex] = mono_llvm_build_load (builder, ctx->imt_rgctx_loc, "", TRUE, LLVM_BARRIER_NONE);
3092 #else
3093                 args [cinfo->imt_arg_pindex] = convert (ctx, values [call->imt_arg_reg], ctx->module->ptr_type);
3094 #endif
3095         }
3096         if (vretaddr) {
3097                 if (!addresses [call->inst.dreg])
3098                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3099                 g_assert (cinfo->vret_arg_pindex < nargs);
3100                 if (cinfo->ret.storage == LLVMArgVtypeByRef)
3101                         args [cinfo->vret_arg_pindex] = addresses [call->inst.dreg];
3102                 else
3103                         args [cinfo->vret_arg_pindex] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
3104         }
3105
3106         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3107                 guint32 regpair;
3108                 int reg, pindex;
3109                 LLVMArgInfo *ainfo = &call->cinfo->args [i];
3110
3111                 pindex = ainfo->pindex;
3112
3113                 regpair = (guint32)(gssize)(l->data);
3114                 reg = regpair & 0xffffff;
3115                 args [pindex] = values [reg];
3116                 switch (ainfo->storage) {
3117                 case LLVMArgVtypeInReg:
3118                 case LLVMArgAsFpArgs: {
3119                         guint32 nargs;
3120
3121                         g_assert (addresses [reg]);
3122                         emit_vtype_to_args (ctx, builder, ainfo->type, addresses [reg], ainfo, args + pindex, &nargs);
3123                         pindex += nargs;
3124
3125                         // FIXME: alignment
3126                         // FIXME: Get rid of the VMOVE
3127                         break;
3128                 }
3129                 case LLVMArgVtypeByVal:
3130                         g_assert (addresses [reg]);
3131                         args [pindex] = addresses [reg];
3132                         break;
3133                 case LLVMArgVtypeByRef:
3134                 case LLVMArgScalarByRef: {
3135                         g_assert (addresses [reg]);
3136                         args [pindex] = convert (ctx, addresses [reg], LLVMPointerType (type_to_llvm_arg_type (ctx, ainfo->type), 0));
3137                         break;
3138                 }
3139                 case LLVMArgAsIArgs:
3140                         g_assert (addresses [reg]);
3141                         args [pindex] = LLVMBuildLoad (ctx->builder, convert (ctx, addresses [reg], LLVMPointerType (LLVMArrayType (IntPtrType (), ainfo->nslots), 0)), "");
3142                         break;
3143                 case LLVMArgVtypeAsScalar:
3144                         g_assert_not_reached ();
3145                         break;
3146                 default:
3147                         g_assert (args [pindex]);
3148                         if (i == 0 && sig->hasthis)
3149                                 args [pindex] = convert (ctx, args [pindex], ThisType ());
3150                         else
3151                                 args [pindex] = convert (ctx, args [pindex], type_to_llvm_arg_type (ctx, ainfo->type));
3152                         break;
3153                 }
3154                 g_assert (pindex <= nargs);
3155
3156                 l = l->next;
3157         }
3158
3159         // FIXME: Align call sites
3160
3161         /*
3162          * Emit the call
3163          */
3164
3165         lcall = emit_call (ctx, bb, &builder, callee, args, LLVMCountParamTypes (llvm_sig));
3166
3167         /*
3168          * Modify cconv and parameter attributes to pass rgctx/imt correctly.
3169          */
3170 #if defined(MONO_ARCH_IMT_REG) && defined(MONO_ARCH_RGCTX_REG)
3171         g_assert (MONO_ARCH_IMT_REG == MONO_ARCH_RGCTX_REG);
3172 #endif
3173         /* The two can't be used together, so use only one LLVM calling conv to pass them */
3174         g_assert (!(call->rgctx_arg_reg && call->imt_arg_reg));
3175         if (!sig->pinvoke && !cfg->llvm_only)
3176                 LLVMSetInstructionCallConv (lcall, LLVMMono1CallConv);
3177
3178         if (cinfo->ret.storage == LLVMArgVtypeByRef)
3179                 LLVMAddInstrAttribute (lcall, 1 + cinfo->vret_arg_pindex, LLVMStructRetAttribute);
3180         if (!ctx->llvm_only && call->rgctx_arg_reg)
3181                 LLVMAddInstrAttribute (lcall, 1 + cinfo->rgctx_arg_pindex, LLVMInRegAttribute);
3182         if (call->imt_arg_reg)
3183                 LLVMAddInstrAttribute (lcall, 1 + cinfo->imt_arg_pindex, LLVMInRegAttribute);
3184
3185         /* Add byval attributes if needed */
3186         for (i = 0; i < sig->param_count; ++i) {
3187                 LLVMArgInfo *ainfo = &call->cinfo->args [i + sig->hasthis];
3188
3189                 if (ainfo && ainfo->storage == LLVMArgVtypeByVal)
3190                         LLVMAddInstrAttribute (lcall, 1 + ainfo->pindex, LLVMByValAttribute);
3191         }
3192
3193         /*
3194          * Convert the result
3195          */
3196         switch (cinfo->ret.storage) {
3197         case LLVMArgVtypeInReg: {
3198                 LLVMValueRef regs [2];
3199
3200                 if (LLVMTypeOf (lcall) == LLVMVoidType ())
3201                         /* Empty struct */
3202                         break;
3203
3204                 if (!addresses [ins->dreg])
3205                         addresses [ins->dreg] = build_alloca (ctx, sig->ret);
3206
3207                 regs [0] = LLVMBuildExtractValue (builder, lcall, 0, "");
3208                 if (cinfo->ret.pair_storage [1] != LLVMArgNone)
3209                         regs [1] = LLVMBuildExtractValue (builder, lcall, 1, "");
3210                 emit_args_to_vtype (ctx, builder, sig->ret, addresses [ins->dreg], &cinfo->ret, regs);
3211                 break;
3212         }
3213         case LLVMArgVtypeByVal:
3214                 if (!addresses [call->inst.dreg])
3215                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3216                 LLVMBuildStore (builder, lcall, addresses [call->inst.dreg]);
3217                 break;
3218         case LLVMArgFpStruct:
3219                 if (!addresses [call->inst.dreg])
3220                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3221                 LLVMBuildStore (builder, lcall, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (LLVMTypeOf (lcall), 0), FALSE));
3222                 break;
3223         case LLVMArgVtypeAsScalar:
3224                 if (!addresses [call->inst.dreg])
3225                         addresses [call->inst.dreg] = build_alloca (ctx, sig->ret);
3226                 LLVMBuildStore (builder, lcall, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (LLVMTypeOf (lcall), 0), FALSE));
3227                 break;
3228         case LLVMArgVtypeRetAddr:
3229         case LLVMArgVtypeByRef:
3230                 break;
3231         case LLVMArgScalarRetAddr:
3232                 /* Normal scalar returned using a vtype return argument */
3233                 values [ins->dreg] = LLVMBuildLoad (builder, convert_full (ctx, addresses [call->inst.dreg], LLVMPointerType (type_to_llvm_type (ctx, sig->ret), 0), FALSE), "");
3234                 break;
3235         default:
3236                 if (sig->ret->type != MONO_TYPE_VOID)
3237                         /* If the method returns an unsigned value, need to zext it */
3238                         values [ins->dreg] = convert_full (ctx, lcall, llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, sig->ret)), type_is_unsigned (ctx, sig->ret));
3239                 break;
3240         }
3241
3242         *builder_ref = ctx->builder;
3243
3244         return;
3245  FAILURE:
3246         return;
3247 }
3248
3249 static void
3250 emit_llvmonly_throw (EmitContext *ctx, MonoBasicBlock *bb, gboolean rethrow, LLVMValueRef exc)
3251 {
3252         const char *icall_name = rethrow ? "mono_llvm_rethrow_exception" : "mono_llvm_throw_exception";
3253         LLVMValueRef callee = rethrow ? ctx->module->rethrow : ctx->module->throw_icall;
3254
3255         LLVMTypeRef exc_type = type_to_llvm_type (ctx, &mono_get_exception_class ()->byval_arg);
3256
3257         if (!callee) {
3258                 LLVMTypeRef fun_sig = LLVMFunctionType1 (LLVMVoidType (), exc_type, FALSE);
3259
3260                 if (ctx->cfg->compile_aot) {
3261                         callee = get_callee (ctx, fun_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3262                 } else {
3263                         callee = LLVMAddFunction (ctx->lmodule, icall_name, fun_sig);
3264                         LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3265                         mono_memory_barrier ();
3266
3267                         if (rethrow)
3268                                 ctx->module->rethrow = callee;
3269                         else
3270                                 ctx->module->throw_icall = callee;
3271                 }
3272         }
3273
3274         LLVMValueRef args [2];
3275
3276         args [0] = convert (ctx, exc, exc_type);
3277         emit_call (ctx, bb, &ctx->builder, callee, args, 1);
3278
3279         LLVMBuildUnreachable (ctx->builder);
3280
3281         ctx->builder = create_builder (ctx);
3282 }
3283
3284 static void
3285 emit_throw (EmitContext *ctx, MonoBasicBlock *bb, gboolean rethrow, LLVMValueRef exc)
3286 {
3287         MonoMethodSignature *throw_sig;
3288         LLVMValueRef callee, arg;
3289         const char *icall_name;
3290                                 
3291         callee = rethrow ? ctx->module->rethrow : ctx->module->throw_icall;
3292         icall_name = rethrow ? "mono_arch_rethrow_exception" : "mono_arch_throw_exception";
3293
3294         if (!callee) {
3295                 throw_sig = mono_metadata_signature_alloc (mono_get_corlib (), 1);
3296                 throw_sig->ret = &mono_get_void_class ()->byval_arg;
3297                 throw_sig->params [0] = &mono_get_object_class ()->byval_arg;
3298                 if (ctx->cfg->compile_aot) {
3299                         callee = get_callee (ctx, sig_to_llvm_sig (ctx, throw_sig), MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3300                 } else {
3301                         callee = LLVMAddFunction (ctx->lmodule, icall_name, sig_to_llvm_sig (ctx, throw_sig));
3302
3303 #ifdef TARGET_X86
3304                         /* 
3305                          * LLVM doesn't push the exception argument, so we need a different
3306                          * trampoline.
3307                          */
3308                         LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, rethrow ? "llvm_rethrow_exception_trampoline" : "llvm_throw_exception_trampoline"));
3309 #else
3310                         LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3311 #endif
3312                 }
3313
3314                 mono_memory_barrier ();
3315                 if (rethrow)
3316                         ctx->module->rethrow = callee;
3317                 else
3318                         ctx->module->throw_icall = callee;
3319         }
3320         arg = convert (ctx, exc, type_to_llvm_type (ctx, &mono_get_object_class ()->byval_arg));
3321         emit_call (ctx, bb, &ctx->builder, callee, &arg, 1);
3322 }
3323
3324 static void
3325 emit_resume_eh (EmitContext *ctx, MonoBasicBlock *bb)
3326 {
3327         const char *icall_name = "mono_llvm_resume_exception";
3328         LLVMValueRef callee = ctx->module->resume_eh;
3329
3330         LLVMTypeRef fun_sig = LLVMFunctionType0 (LLVMVoidType (), FALSE);
3331
3332         if (!callee) {
3333                 if (ctx->cfg->compile_aot) {
3334                         callee = get_callee (ctx, fun_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3335                 } else {
3336                         callee = LLVMAddFunction (ctx->lmodule, icall_name, fun_sig);
3337                         LLVMAddGlobalMapping (ctx->module->ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3338                         mono_memory_barrier ();
3339
3340                         ctx->module->resume_eh = callee;
3341                 }
3342         }
3343
3344         emit_call (ctx, bb, &ctx->builder, callee, NULL, 0);
3345
3346         LLVMBuildUnreachable (ctx->builder);
3347
3348         ctx->builder = create_builder (ctx);
3349 }
3350
3351 static LLVMValueRef
3352 mono_llvm_emit_clear_exception_call (EmitContext *ctx, LLVMBuilderRef builder)
3353 {
3354         const char *icall_name = "mono_llvm_clear_exception";
3355
3356         LLVMTypeRef call_sig = LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE);
3357         LLVMValueRef callee = NULL;
3358
3359         if (!callee) {
3360                 if (ctx->cfg->compile_aot) {
3361                         callee = get_callee (ctx, call_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3362                 } else {
3363                         // FIXME: This is broken.
3364                         callee = LLVMAddFunction (ctx->lmodule, icall_name, call_sig);
3365                 }
3366         }
3367
3368         g_assert (builder && callee);
3369
3370         return LLVMBuildCall (builder, callee, NULL, 0, "");
3371 }
3372
3373 static LLVMValueRef
3374 mono_llvm_emit_load_exception_call (EmitContext *ctx, LLVMBuilderRef builder)
3375 {
3376         const char *icall_name = "mono_llvm_load_exception";
3377
3378         LLVMTypeRef call_sig = LLVMFunctionType (ObjRefType (), NULL, 0, FALSE);
3379         LLVMValueRef callee = NULL;
3380
3381         if (!callee) {
3382                 if (ctx->cfg->compile_aot) {
3383                         callee = get_callee (ctx, call_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3384                 } else {
3385                         // FIXME: This is broken.
3386                         callee = LLVMAddFunction (ctx->lmodule, icall_name, call_sig);
3387                 }
3388         }
3389
3390         g_assert (builder && callee);
3391
3392         return LLVMBuildCall (builder, callee, NULL, 0, icall_name);
3393 }
3394
3395
3396 static LLVMValueRef
3397 mono_llvm_emit_match_exception_call (EmitContext *ctx, LLVMBuilderRef builder, gint32 region_start, gint32 region_end)
3398 {
3399         const char *icall_name = "mono_llvm_match_exception";
3400
3401         ctx->builder = builder;
3402
3403         const int num_args = 3;
3404         LLVMValueRef args [num_args];
3405         args [0] = convert (ctx, get_aotconst (ctx, MONO_PATCH_INFO_AOT_JIT_INFO, GINT_TO_POINTER (ctx->cfg->method_index)), IntPtrType ());
3406         args [1] = LLVMConstInt (LLVMInt32Type (), region_start, 0);
3407         args [2] = LLVMConstInt (LLVMInt32Type (), region_end, 0);
3408
3409         LLVMTypeRef match_sig = LLVMFunctionType3 (LLVMInt32Type (), IntPtrType (), LLVMInt32Type (), LLVMInt32Type (), FALSE);
3410         LLVMValueRef callee = ctx->module->match_exc;
3411
3412         if (!callee) {
3413                 if (ctx->cfg->compile_aot) {
3414                         ctx->builder = builder;
3415                         // get_callee expects ctx->builder to be the emitting builder
3416                         callee = get_callee (ctx, match_sig, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name);
3417                 } else {
3418                         callee = ctx->module->match_exc = LLVMAddFunction (ctx->lmodule, icall_name, match_sig);
3419                         LLVMAddGlobalMapping (ctx->module->ee, ctx->module->match_exc, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, icall_name));
3420                         ctx->module->match_exc = callee;
3421                         mono_memory_barrier ();
3422                 }
3423         }
3424
3425         g_assert (builder && callee);
3426
3427         g_assert (ctx->ex_var);
3428
3429         return LLVMBuildCall (builder, callee, args, num_args, icall_name);
3430 }
3431
3432 // FIXME: This won't work because the code-finding makes this
3433 // not a constant.
3434 /*#define MONO_PERSONALITY_DEBUG*/
3435
3436 #ifdef MONO_PERSONALITY_DEBUG
3437 static const gboolean use_debug_personality = TRUE;
3438 static const char *default_personality_name = "mono_debug_personality";
3439 #else
3440 static const gboolean use_debug_personality = FALSE;
3441 static const char *default_personality_name = "__gxx_personality_v0";
3442 #endif
3443
3444 static LLVMTypeRef
3445 default_cpp_lpad_exc_signature (void)
3446 {
3447         static gboolean inited = FALSE;
3448         static LLVMTypeRef sig;
3449
3450         if (!sig) {
3451                 LLVMTypeRef signature [2];
3452                 signature [0] = LLVMPointerType (LLVMInt8Type (), 0);
3453                 signature [1] = LLVMInt32Type ();
3454                 sig = LLVMStructType (signature, 2, FALSE);
3455                 inited = TRUE;
3456         }
3457
3458         return sig;
3459 }
3460
3461 static LLVMValueRef
3462 get_mono_personality (EmitContext *ctx)
3463 {
3464         LLVMValueRef personality = NULL;
3465         static gint32 mapping_inited = FALSE;
3466         LLVMTypeRef personality_type = LLVMFunctionType (LLVMInt32Type (), NULL, 0, TRUE);
3467
3468         if (!use_debug_personality) {
3469                 if (ctx->cfg->compile_aot) {
3470                                 personality = LLVMGetNamedFunction (ctx->lmodule, default_personality_name);
3471                 } else if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0) {
3472                                 personality = LLVMAddFunction (ctx->lmodule, default_personality_name, personality_type);
3473                                 LLVMAddGlobalMapping (ctx->module->ee, personality, personality);
3474                 }
3475         } else {
3476                 if (ctx->cfg->compile_aot) {
3477                         personality = get_callee (ctx, personality_type, MONO_PATCH_INFO_INTERNAL_METHOD, default_personality_name);
3478                 } else {
3479                         personality = LLVMAddFunction (ctx->lmodule, default_personality_name, personality_type);
3480                         LLVMAddGlobalMapping (ctx->module->ee, personality, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, default_personality_name));
3481                         mono_memory_barrier ();
3482                 }
3483         }
3484
3485         g_assert (personality);
3486         return personality;
3487 }
3488
3489 static LLVMBasicBlockRef
3490 emit_landing_pad (EmitContext *ctx, int group_index, int group_size)
3491 {
3492         MonoCompile *cfg = ctx->cfg;
3493         LLVMBuilderRef old_builder = ctx->builder;
3494         MonoExceptionClause *group_start = cfg->header->clauses + group_index;
3495
3496         LLVMBuilderRef lpadBuilder = create_builder (ctx);
3497         ctx->builder = lpadBuilder;
3498
3499         MonoBasicBlock *handler_bb = cfg->cil_offset_to_bb [CLAUSE_START (group_start)];
3500         g_assert (handler_bb);
3501
3502         // <resultval> = landingpad <somety> personality <type> <pers_fn> <clause>+
3503         LLVMValueRef personality = get_mono_personality (ctx);
3504         g_assert (personality);
3505
3506         char *bb_name = g_strdup_printf ("LPAD%d_BB", group_index);
3507         LLVMBasicBlockRef lpad_bb = gen_bb (ctx, bb_name);
3508         g_free (bb_name);
3509         LLVMPositionBuilderAtEnd (lpadBuilder, lpad_bb);
3510         LLVMValueRef landing_pad = LLVMBuildLandingPad (lpadBuilder, default_cpp_lpad_exc_signature (), personality, 0, "");
3511         g_assert (landing_pad);
3512
3513         LLVMValueRef cast = LLVMBuildBitCast (lpadBuilder, ctx->module->sentinel_exception, LLVMPointerType (LLVMInt8Type (), 0), "int8TypeInfo");
3514         LLVMAddClause (landing_pad, cast);
3515
3516         LLVMBasicBlockRef resume_bb = gen_bb (ctx, "RESUME_BB");
3517         LLVMBuilderRef resume_builder = create_builder (ctx);
3518         ctx->builder = resume_builder;
3519         LLVMPositionBuilderAtEnd (resume_builder, resume_bb);
3520
3521         emit_resume_eh (ctx, handler_bb);
3522
3523         // Build match
3524         ctx->builder = lpadBuilder;
3525         LLVMPositionBuilderAtEnd (lpadBuilder, lpad_bb);
3526
3527         gboolean finally_only = TRUE;
3528
3529         MonoExceptionClause *group_cursor = group_start;
3530
3531         for (int i = 0; i < group_size; i ++) {
3532                 if (!(group_cursor->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
3533                         finally_only = FALSE;
3534
3535                 group_cursor++;
3536         }
3537
3538         // FIXME:
3539         // Handle landing pad inlining
3540
3541         if (!finally_only) {
3542                 // So at each level of the exception stack we will match the exception again.
3543                 // During that match, we need to compare against the handler types for the current
3544                 // protected region. We send the try start and end so that we can only check against
3545                 // handlers for this lexical protected region.
3546                 LLVMValueRef match = mono_llvm_emit_match_exception_call (ctx, lpadBuilder, group_start->try_offset, group_start->try_offset + group_start->try_len);
3547
3548                 // if returns -1, resume
3549                 LLVMValueRef switch_ins = LLVMBuildSwitch (lpadBuilder, match, resume_bb, group_size);
3550
3551                 // else move to that target bb
3552                 for (int i=0; i < group_size; i++) {
3553                         MonoExceptionClause *clause = group_start + i;
3554                         int clause_index = clause - cfg->header->clauses;
3555                         MonoBasicBlock *handler_bb = (MonoBasicBlock*)g_hash_table_lookup (ctx->clause_to_handler, GINT_TO_POINTER (clause_index));
3556                         g_assert (handler_bb);
3557                         g_assert (ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
3558                         LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE), ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
3559                 }
3560         } else {
3561                 int clause_index = group_start - cfg->header->clauses;
3562                 MonoBasicBlock *finally_bb = (MonoBasicBlock*)g_hash_table_lookup (ctx->clause_to_handler, GINT_TO_POINTER (clause_index));
3563                 g_assert (finally_bb);
3564
3565                 LLVMBuildBr (ctx->builder, ctx->bblocks [finally_bb->block_num].call_handler_target_bb);
3566         }
3567
3568         ctx->builder = old_builder;
3569
3570         return lpad_bb;
3571 }
3572
3573
3574 static void
3575 emit_llvmonly_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBasicBlockRef cbb)
3576 {
3577         int clause_index = MONO_REGION_CLAUSE_INDEX (bb->region);
3578         MonoExceptionClause *clause = &ctx->cfg->header->clauses [clause_index];
3579
3580         // Make exception available to catch blocks
3581         if (!(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
3582                 LLVMValueRef mono_exc = mono_llvm_emit_load_exception_call (ctx, ctx->builder);
3583
3584                 g_assert (ctx->ex_var);
3585                 LLVMBuildStore (ctx->builder, LLVMBuildBitCast (ctx->builder, mono_exc, ObjRefType (), ""), ctx->ex_var);
3586
3587                 if (bb->in_scount == 1) {
3588                         MonoInst *exvar = bb->in_stack [0];
3589                         g_assert (!ctx->values [exvar->dreg]);
3590                         g_assert (ctx->ex_var);
3591                         ctx->values [exvar->dreg] = LLVMBuildLoad (ctx->builder, ctx->ex_var, "save_exception");
3592                         emit_volatile_store (ctx, exvar->dreg);
3593                 }
3594
3595                 mono_llvm_emit_clear_exception_call (ctx, ctx->builder);
3596         }
3597
3598         LLVMBuilderRef handler_builder = create_builder (ctx);
3599         LLVMBasicBlockRef target_bb = ctx->bblocks [bb->block_num].call_handler_target_bb;
3600         LLVMPositionBuilderAtEnd (handler_builder, target_bb);
3601
3602         // Make the handler code end with a jump to cbb
3603         LLVMBuildBr (handler_builder, cbb);
3604 }
3605
3606 static void
3607 emit_handler_start (EmitContext *ctx, MonoBasicBlock *bb, LLVMBuilderRef builder)
3608 {
3609         MonoCompile *cfg = ctx->cfg;
3610         LLVMValueRef *values = ctx->values;
3611         LLVMModuleRef lmodule = ctx->lmodule;
3612         BBInfo *bblocks = ctx->bblocks;
3613         LLVMTypeRef i8ptr;
3614         LLVMValueRef personality;
3615         LLVMValueRef landing_pad;
3616         LLVMBasicBlockRef target_bb;
3617         MonoInst *exvar;
3618         static gint32 mapping_inited;
3619         static int ti_generator;
3620         char ti_name [128];
3621         MonoClass **ti;
3622         LLVMValueRef type_info;
3623         int clause_index;
3624         GSList *l;
3625
3626         // <resultval> = landingpad <somety> personality <type> <pers_fn> <clause>+
3627
3628         if (cfg->compile_aot) {
3629                 /* Use a dummy personality function */
3630                 personality = LLVMGetNamedFunction (lmodule, "mono_personality");
3631                 g_assert (personality);
3632         } else {
3633                 personality = LLVMGetNamedFunction (lmodule, "mono_personality");
3634                 if (InterlockedCompareExchange (&mapping_inited, 1, 0) == 0)
3635                         LLVMAddGlobalMapping (ctx->module->ee, personality, mono_personality);
3636         }
3637
3638         i8ptr = LLVMPointerType (LLVMInt8Type (), 0);
3639
3640         clause_index = (mono_get_block_region_notry (cfg, bb->region) >> 8) - 1;
3641
3642         /*
3643          * Create the type info
3644          */
3645         sprintf (ti_name, "type_info_%d", ti_generator);
3646         ti_generator ++;
3647
3648         if (cfg->compile_aot) {
3649                 /* decode_eh_frame () in aot-runtime.c will decode this */
3650                 type_info = LLVMAddGlobal (lmodule, LLVMInt32Type (), ti_name);
3651                 LLVMSetInitializer (type_info, LLVMConstInt (LLVMInt32Type (), clause_index, FALSE));
3652
3653                 /*
3654                  * These symbols are not really used, the clause_index is embedded into the EH tables generated by DwarfMonoException in LLVM.
3655                  */
3656                 LLVMSetLinkage (type_info, LLVMInternalLinkage);
3657         } else {
3658                 /*
3659                  * After the cfg mempool is freed, the type info will point to stale memory,
3660                  * but this is not a problem, since we decode it once in exception_cb during
3661                  * compilation.
3662                  */
3663                 ti = mono_mempool_alloc (cfg->mempool, sizeof (gint32));
3664                 *(gint32*)ti = clause_index;
3665
3666                 type_info = LLVMAddGlobal (lmodule, i8ptr, ti_name);
3667
3668                 LLVMAddGlobalMapping (ctx->module->ee, type_info, ti);
3669         }
3670
3671         {
3672                 LLVMTypeRef members [2], ret_type;
3673
3674                 members [0] = i8ptr;
3675                 members [1] = LLVMInt32Type ();
3676                 ret_type = LLVMStructType (members, 2, FALSE);
3677
3678                 landing_pad = LLVMBuildLandingPad (builder, ret_type, personality, 1, "");
3679                 LLVMAddClause (landing_pad, type_info);
3680
3681                 /* Store the exception into the exvar */
3682                 if (ctx->ex_var)
3683                         LLVMBuildStore (builder, convert (ctx, LLVMBuildExtractValue (builder, landing_pad, 0, "ex_obj"), ObjRefType ()), ctx->ex_var);
3684         }
3685
3686         /*
3687          * LLVM throw sites are associated with a one landing pad, and LLVM generated
3688          * code expects control to be transferred to this landing pad even in the
3689          * presence of nested clauses. The landing pad needs to branch to the landing
3690          * pads belonging to nested clauses based on the selector value returned by
3691          * the landing pad instruction, which is passed to the landing pad in a
3692          * register by the EH code.
3693          */
3694         target_bb = bblocks [bb->block_num].call_handler_target_bb;
3695         g_assert (target_bb);
3696
3697         /*
3698          * Branch to the correct landing pad
3699          */
3700         LLVMValueRef ex_selector = LLVMBuildExtractValue (builder, landing_pad, 1, "ex_selector");
3701         LLVMValueRef switch_ins = LLVMBuildSwitch (builder, ex_selector, target_bb, 0);
3702
3703         for (l = ctx->nested_in [clause_index]; l; l = l->next) {
3704                 int nesting_clause_index = GPOINTER_TO_INT (l->data);
3705                 MonoBasicBlock *handler_bb;
3706
3707                 handler_bb = g_hash_table_lookup (ctx->clause_to_handler, GINT_TO_POINTER (nesting_clause_index));
3708                 g_assert (handler_bb);
3709
3710                 g_assert (ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
3711                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), nesting_clause_index, FALSE), ctx->bblocks [handler_bb->block_num].call_handler_target_bb);
3712         }
3713
3714         /* Start a new bblock which CALL_HANDLER can branch to */
3715         target_bb = bblocks [bb->block_num].call_handler_target_bb;
3716         if (target_bb) {
3717                 ctx->builder = builder = create_builder (ctx);
3718                 LLVMPositionBuilderAtEnd (ctx->builder, target_bb);
3719
3720                 ctx->bblocks [bb->block_num].end_bblock = target_bb;
3721
3722                 /* Store the exception into the IL level exvar */
3723                 if (bb->in_scount == 1) {
3724                         g_assert (bb->in_scount == 1);
3725                         exvar = bb->in_stack [0];
3726
3727                         // FIXME: This is shared with filter clauses ?
3728                         g_assert (!values [exvar->dreg]);
3729
3730                         g_assert (ctx->ex_var);
3731                         values [exvar->dreg] = LLVMBuildLoad (builder, ctx->ex_var, "");
3732                         emit_volatile_store (ctx, exvar->dreg);
3733                 }
3734         }
3735 }
3736
3737 static void
3738 process_bb (EmitContext *ctx, MonoBasicBlock *bb)
3739 {
3740         MonoCompile *cfg = ctx->cfg;
3741         MonoMethodSignature *sig = ctx->sig;
3742         LLVMValueRef method = ctx->lmethod;
3743         LLVMValueRef *values = ctx->values;
3744         LLVMValueRef *addresses = ctx->addresses;
3745         LLVMCallInfo *linfo = ctx->linfo;
3746         LLVMModuleRef lmodule = ctx->lmodule;
3747         BBInfo *bblocks = ctx->bblocks;
3748         MonoInst *ins;
3749         LLVMBasicBlockRef cbb;
3750         LLVMBuilderRef builder, starting_builder;
3751         gboolean has_terminator;
3752         LLVMValueRef v;
3753         LLVMValueRef lhs, rhs;
3754         int nins = 0;
3755
3756         cbb = get_end_bb (ctx, bb);
3757
3758         builder = create_builder (ctx);
3759         ctx->builder = builder;
3760         LLVMPositionBuilderAtEnd (builder, cbb);
3761
3762         CHECK_FAILURE (ctx);
3763
3764         if (bb->flags & BB_EXCEPTION_HANDLER) {
3765                 if (!ctx->llvm_only && !bblocks [bb->block_num].invoke_target) {
3766                         LLVM_FAILURE (ctx, "handler without invokes");
3767                 }
3768
3769                 if (ctx->llvm_only)
3770                         emit_llvmonly_handler_start (ctx, bb, cbb);
3771                 else
3772                         emit_handler_start (ctx, bb, builder);
3773                 CHECK_FAILURE (ctx);
3774                 builder = ctx->builder;
3775         }
3776
3777         has_terminator = FALSE;
3778         starting_builder = builder;
3779         for (ins = bb->code; ins; ins = ins->next) {
3780                 const char *spec = LLVM_INS_INFO (ins->opcode);
3781                 char *dname = NULL;
3782                 char dname_buf [128];
3783
3784                 emit_dbg_loc (ctx, builder, ins->cil_code);
3785
3786                 nins ++;
3787                 if (nins > 3000 && builder == starting_builder) {
3788                         /* some steps in llc are non-linear in the size of basic blocks, see #5714 */
3789                         LLVM_FAILURE (ctx, "basic block too long");
3790                 }
3791
3792                 if (has_terminator)
3793                         /* There could be instructions after a terminator, skip them */
3794                         break;
3795
3796                 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins)) {
3797                         sprintf (dname_buf, "t%d", ins->dreg);
3798                         dname = dname_buf;
3799                 }
3800
3801                 if (spec [MONO_INST_SRC1] != ' ' && spec [MONO_INST_SRC1] != 'v') {
3802                         MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
3803
3804                         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
3805                                 lhs = emit_volatile_load (ctx, ins->sreg1);
3806                         } else {
3807                                 /* It is ok for SETRET to have an uninitialized argument */
3808                                 if (!values [ins->sreg1] && ins->opcode != OP_SETRET)
3809                                         LLVM_FAILURE (ctx, "sreg1");
3810                                 lhs = values [ins->sreg1];
3811                         }
3812                 } else {
3813                         lhs = NULL;
3814                 }
3815
3816                 if (spec [MONO_INST_SRC2] != ' ' && spec [MONO_INST_SRC2] != ' ') {
3817                         MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
3818                         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
3819                                 rhs = emit_volatile_load (ctx, ins->sreg2);
3820                         } else {
3821                                 if (!values [ins->sreg2])
3822                                         LLVM_FAILURE (ctx, "sreg2");
3823                                 rhs = values [ins->sreg2];
3824                         }
3825                 } else {
3826                         rhs = NULL;
3827                 }
3828
3829                 //mono_print_ins (ins);
3830                 switch (ins->opcode) {
3831                 case OP_NOP:
3832                 case OP_NOT_NULL:
3833                 case OP_LIVERANGE_START:
3834                 case OP_LIVERANGE_END:
3835                         break;
3836                 case OP_ICONST:
3837                         values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE);
3838                         break;
3839                 case OP_I8CONST:
3840 #if SIZEOF_VOID_P == 4
3841                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
3842 #else
3843                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), (gint64)ins->inst_c0, FALSE);
3844 #endif
3845                         break;
3846                 case OP_R8CONST:
3847                         values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), *(double*)ins->inst_p0);
3848                         break;
3849                 case OP_R4CONST:
3850                         if (cfg->r4fp)
3851                                 values [ins->dreg] = LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0);
3852                         else
3853                                 values [ins->dreg] = LLVMConstFPExt (LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0), LLVMDoubleType ());
3854                         break;
3855                 case OP_DUMMY_ICONST:
3856                         values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3857                         break;
3858                 case OP_DUMMY_I8CONST:
3859                         values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
3860                         break;
3861                 case OP_DUMMY_R8CONST:
3862                         values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), 0.0f);
3863                         break;
3864                 case OP_BR: {
3865                         LLVMBasicBlockRef target_bb = get_bb (ctx, ins->inst_target_bb);
3866                         LLVMBuildBr (builder, target_bb);
3867                         has_terminator = TRUE;
3868                         break;
3869                 }
3870                 case OP_SWITCH: {
3871                         int i;
3872                         LLVMValueRef v;
3873                         char bb_name [128];
3874                         LLVMBasicBlockRef new_bb;
3875                         LLVMBuilderRef new_builder;
3876
3877                         // The default branch is already handled
3878                         // FIXME: Handle it here
3879
3880                         /* Start new bblock */
3881                         sprintf (bb_name, "SWITCH_DEFAULT_BB%d", ctx->default_index ++);
3882                         new_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
3883
3884                         lhs = convert (ctx, lhs, LLVMInt32Type ());
3885                         v = LLVMBuildSwitch (builder, lhs, new_bb, GPOINTER_TO_UINT (ins->klass));
3886                         for (i = 0; i < GPOINTER_TO_UINT (ins->klass); ++i) {
3887                                 MonoBasicBlock *target_bb = ins->inst_many_bb [i];
3888
3889                                 LLVMAddCase (v, LLVMConstInt (LLVMInt32Type (), i, FALSE), get_bb (ctx, target_bb));
3890                         }
3891
3892                         new_builder = create_builder (ctx);
3893                         LLVMPositionBuilderAtEnd (new_builder, new_bb);
3894                         LLVMBuildUnreachable (new_builder);
3895
3896                         has_terminator = TRUE;
3897                         g_assert (!ins->next);
3898                                 
3899                         break;
3900                 }
3901
3902                 case OP_SETRET:
3903                         switch (linfo->ret.storage) {
3904                         case LLVMArgVtypeInReg: {
3905                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
3906                                 LLVMValueRef val, addr, retval;
3907                                 int i;
3908
3909                                 retval = LLVMGetUndef (ret_type);
3910
3911                                 if (!addresses [ins->sreg1]) {
3912                                         /*
3913                                          * The return type is an LLVM vector type, have to convert between it and the
3914                                          * real return type which is a struct type.
3915                                          */
3916                                         g_assert (MONO_CLASS_IS_SIMD (ctx->cfg, mono_class_from_mono_type (sig->ret)));
3917                                         /* Convert to 2xi64 first */
3918                                         val = LLVMBuildBitCast (builder, values [ins->sreg1], LLVMVectorType (IntPtrType (), 2), "");
3919
3920                                         for (i = 0; i < 2; ++i) {
3921                                                 if (linfo->ret.pair_storage [i] == LLVMArgInIReg) {
3922                                                         retval = LLVMBuildInsertValue (builder, retval, LLVMBuildExtractElement (builder, val, LLVMConstInt (LLVMInt32Type (), i, FALSE), ""), i, "");
3923                                                 } else {
3924                                                         g_assert (linfo->ret.pair_storage [i] == LLVMArgNone);
3925                                                 }
3926                                         }
3927                                 } else {
3928                                         addr = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (ret_type, 0), "");
3929                                         for (i = 0; i < 2; ++i) {
3930                                                 if (linfo->ret.pair_storage [i] == LLVMArgInIReg) {
3931                                                         LLVMValueRef indexes [2], part_addr;
3932
3933                                                         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
3934                                                         indexes [1] = LLVMConstInt (LLVMInt32Type (), i, FALSE);
3935                                                         part_addr = LLVMBuildGEP (builder, addr, indexes, 2, "");
3936
3937                                                         retval = LLVMBuildInsertValue (builder, retval, LLVMBuildLoad (builder, part_addr, ""), i, "");
3938                                                 } else {
3939                                                         g_assert (linfo->ret.pair_storage [i] == LLVMArgNone);
3940                                                 }
3941                                         }
3942                                 }
3943                                 LLVMBuildRet (builder, retval);
3944                                 break;
3945                         }
3946                         case LLVMArgVtypeAsScalar: {
3947                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
3948                                 LLVMValueRef retval;
3949                                 int size;
3950
3951                                 size = get_vtype_size (sig->ret);
3952
3953                                 g_assert (addresses [ins->sreg1]);
3954
3955                                 retval = LLVMBuildLoad (builder, LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (ret_type, 0), ""), "");
3956                                 LLVMBuildRet (builder, retval);
3957                                 break;
3958                         }
3959                         case LLVMArgVtypeByVal: {
3960                                 LLVMValueRef retval;
3961
3962                                 g_assert (addresses [ins->sreg1]);
3963                                 retval = LLVMBuildLoad (builder, addresses [ins->sreg1], "");
3964                                 LLVMBuildRet (builder, retval);
3965                                 break;
3966                         }
3967                         case LLVMArgVtypeByRef: {
3968                                 LLVMBuildRetVoid (builder);
3969                                 break;
3970                         }
3971                         case LLVMArgVtypeRetAddr: {
3972                                 LLVMBuildRetVoid (builder);
3973                                 break;
3974                         }
3975                         case LLVMArgScalarRetAddr: {
3976                                 LLVMTypeRef ret_type = type_to_llvm_type (ctx, sig->ret);
3977                                 LLVMValueRef param = LLVMGetParam (method, linfo->vret_arg_pindex);
3978
3979                                 /* sreg1 might not be set */
3980                                 if (lhs)
3981                                         LLVMBuildStore (builder, convert (ctx, lhs, ret_type), convert (ctx, param, LLVMPointerType (ret_type, 0)));
3982                                 LLVMBuildRetVoid (builder);
3983                                 break;
3984                         }
3985                         case LLVMArgFpStruct: {
3986                                 LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
3987                                 LLVMValueRef retval;
3988
3989                                 g_assert (addresses [ins->sreg1]);
3990                                 retval = LLVMBuildLoad (builder, convert (ctx, addresses [ins->sreg1], LLVMPointerType (ret_type, 0)), "");
3991                                 LLVMBuildRet (builder, retval);
3992                                 break;
3993                         }
3994                         case LLVMArgNone:
3995                         case LLVMArgNormal: {
3996                                 if (!lhs || ctx->is_dead [ins->sreg1]) {
3997                                         /*
3998                                          * The method did not set its return value, probably because it
3999                                          * ends with a throw.
4000                                          */
4001                                         if (cfg->vret_addr)
4002                                                 LLVMBuildRetVoid (builder);
4003                                         else
4004                                                 LLVMBuildRet (builder, LLVMConstNull (type_to_llvm_type (ctx, sig->ret)));
4005                                 } else {
4006                                         LLVMBuildRet (builder, convert (ctx, lhs, type_to_llvm_type (ctx, sig->ret)));
4007                                 }
4008                                 has_terminator = TRUE;
4009                                 break;
4010                         }
4011                         default:
4012                                 g_assert_not_reached ();
4013                                 break;
4014                         }
4015                         break;
4016                 case OP_ICOMPARE:
4017                 case OP_FCOMPARE:
4018                 case OP_RCOMPARE:
4019                 case OP_LCOMPARE:
4020                 case OP_COMPARE:
4021                 case OP_ICOMPARE_IMM:
4022                 case OP_LCOMPARE_IMM:
4023                 case OP_COMPARE_IMM: {
4024                         CompRelation rel;
4025                         LLVMValueRef cmp;
4026
4027                         if (ins->next->opcode == OP_NOP)
4028                                 break;
4029
4030                         if (ins->next->opcode == OP_BR)
4031                                 /* The comparison result is not needed */
4032                                 continue;
4033
4034                         rel = mono_opcode_to_cond (ins->next->opcode);
4035
4036                         if (ins->opcode == OP_ICOMPARE_IMM) {
4037                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
4038                                 rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4039                         }
4040                         if (ins->opcode == OP_LCOMPARE_IMM) {
4041                                 lhs = convert (ctx, lhs, LLVMInt64Type ());
4042                                 rhs = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
4043                         }
4044                         if (ins->opcode == OP_LCOMPARE) {
4045                                 lhs = convert (ctx, lhs, LLVMInt64Type ());
4046                                 rhs = convert (ctx, rhs, LLVMInt64Type ());
4047                         }
4048                         if (ins->opcode == OP_ICOMPARE) {
4049                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
4050                                 rhs = convert (ctx, rhs, LLVMInt32Type ());
4051                         }
4052
4053                         if (lhs && rhs) {
4054                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
4055                                         rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
4056                                 else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
4057                                         lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
4058                         }
4059
4060                         /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
4061                         if (ins->opcode == OP_FCOMPARE) {
4062                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
4063                         } else if (ins->opcode == OP_RCOMPARE) {
4064                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
4065                         } else if (ins->opcode == OP_COMPARE_IMM) {
4066                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && ins->inst_imm == 0)
4067                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, LLVMConstNull (LLVMTypeOf (lhs)), "");
4068                                 else
4069                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), "");
4070                         } else if (ins->opcode == OP_LCOMPARE_IMM) {
4071                                 if (SIZEOF_REGISTER == 4 && COMPILE_LLVM (cfg))  {
4072                                         /* The immediate is encoded in two fields */
4073                                         guint64 l = ((guint64)(guint32)ins->inst_offset << 32) | ((guint32)ins->inst_imm);
4074                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), l, FALSE), "");
4075                                 } else {
4076                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, LLVMInt64Type ()), LLVMConstInt (LLVMInt64Type (), ins->inst_imm, FALSE), "");
4077                                 }
4078                         }
4079                         else if (ins->opcode == OP_COMPARE) {
4080                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind && LLVMTypeOf (lhs) == LLVMTypeOf (rhs))
4081                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
4082                                 else
4083                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), convert (ctx, rhs, IntPtrType ()), "");
4084                         } else
4085                                 cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
4086
4087                         if (MONO_IS_COND_BRANCH_OP (ins->next)) {
4088                                 if (ins->next->inst_true_bb == ins->next->inst_false_bb) {
4089                                         /*
4090                                          * If the target bb contains PHI instructions, LLVM requires
4091                                          * two PHI entries for this bblock, while we only generate one.
4092                                          * So convert this to an unconditional bblock. (bxc #171).
4093                                          */
4094                                         LLVMBuildBr (builder, get_bb (ctx, ins->next->inst_true_bb));
4095                                 } else {
4096                                         LLVMBuildCondBr (builder, cmp, get_bb (ctx, ins->next->inst_true_bb), get_bb (ctx, ins->next->inst_false_bb));
4097                                 }
4098                                 has_terminator = TRUE;
4099                         } else if (MONO_IS_SETCC (ins->next)) {
4100                                 sprintf (dname_buf, "t%d", ins->next->dreg);
4101                                 dname = dname_buf;
4102                                 values [ins->next->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
4103
4104                                 /* Add stores for volatile variables */
4105                                 emit_volatile_store (ctx, ins->next->dreg);
4106                         } else if (MONO_IS_COND_EXC (ins->next)) {
4107                                 emit_cond_system_exception (ctx, bb, (const char*)ins->next->inst_p1, cmp);
4108                                 CHECK_FAILURE (ctx);
4109                                 builder = ctx->builder;
4110                         } else {
4111                                 LLVM_FAILURE (ctx, "next");
4112                         }
4113
4114                         ins = ins->next;
4115                         break;
4116                 }
4117                 case OP_FCEQ:
4118                 case OP_FCLT:
4119                 case OP_FCLT_UN:
4120                 case OP_FCGT:
4121                 case OP_FCGT_UN: {
4122                         CompRelation rel;
4123                         LLVMValueRef cmp;
4124
4125                         rel = mono_opcode_to_cond (ins->opcode);
4126
4127                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
4128                         values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
4129                         break;
4130                 }
4131                 case OP_RCEQ:
4132                 case OP_RCLT:
4133                 case OP_RCLT_UN:
4134                 case OP_RCGT:
4135                 case OP_RCGT_UN: {
4136                         CompRelation rel;
4137                         LLVMValueRef cmp;
4138
4139                         rel = mono_opcode_to_cond (ins->opcode);
4140
4141                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMFloatType ()), convert (ctx, rhs, LLVMFloatType ()), "");
4142                         values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
4143                         break;
4144                 }
4145                 case OP_PHI:
4146                 case OP_FPHI:
4147                 case OP_VPHI:
4148                 case OP_XPHI: {
4149                         int i;
4150                         gboolean empty = TRUE;
4151
4152                         /* Check that all input bblocks really branch to us */
4153                         for (i = 0; i < bb->in_count; ++i) {
4154                                 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_NOT_REACHED)
4155                                         ins->inst_phi_args [i + 1] = -1;
4156                                 else
4157                                         empty = FALSE;
4158                         }
4159
4160                         if (empty) {
4161                                 /* LLVM doesn't like phi instructions with zero operands */
4162                                 ctx->is_dead [ins->dreg] = TRUE;
4163                                 break;
4164                         }                                       
4165
4166                         /* Created earlier, insert it now */
4167                         LLVMInsertIntoBuilder (builder, values [ins->dreg]);
4168
4169                         for (i = 0; i < ins->inst_phi_args [0]; i++) {
4170                                 int sreg1 = ins->inst_phi_args [i + 1];
4171                                 int count, j;
4172
4173                                 /* 
4174                                  * Count the number of times the incoming bblock branches to us,
4175                                  * since llvm requires a separate entry for each.
4176                                  */
4177                                 if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_SWITCH) {
4178                                         MonoInst *switch_ins = bb->in_bb [i]->last_ins;
4179
4180                                         count = 0;
4181                                         for (j = 0; j < GPOINTER_TO_UINT (switch_ins->klass); ++j) {
4182                                                 if (switch_ins->inst_many_bb [j] == bb)
4183                                                         count ++;
4184                                         }
4185                                 } else {
4186                                         count = 1;
4187                                 }
4188
4189                                 /* Remember for later */
4190                                 for (j = 0; j < count; ++j) {
4191                                         PhiNode *node = (PhiNode*)mono_mempool_alloc0 (ctx->mempool, sizeof (PhiNode));
4192                                         node->bb = bb;
4193                                         node->phi = ins;
4194                                         node->in_bb = bb->in_bb [i];
4195                                         node->sreg = sreg1;
4196                                         bblocks [bb->in_bb [i]->block_num].phi_nodes = g_slist_prepend_mempool (ctx->mempool, bblocks [bb->in_bb [i]->block_num].phi_nodes, node);
4197                                 }
4198                         }
4199                         break;
4200                 }
4201                 case OP_MOVE:
4202                 case OP_LMOVE:
4203                 case OP_XMOVE:
4204                 case OP_SETFRET:
4205                         g_assert (lhs);
4206                         values [ins->dreg] = lhs;
4207                         break;
4208                 case OP_FMOVE:
4209                 case OP_RMOVE: {
4210                         MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
4211                                 
4212                         g_assert (lhs);
4213                         values [ins->dreg] = lhs;
4214
4215                         if (var && var->klass->byval_arg.type == MONO_TYPE_R4) {
4216                                 /* 
4217                                  * This is added by the spilling pass in case of the JIT,
4218                                  * but we have to do it ourselves.
4219                                  */
4220                                 values [ins->dreg] = convert (ctx, values [ins->dreg], LLVMFloatType ());
4221                         }
4222                         break;
4223                 }
4224                 case OP_MOVE_F_TO_I4: {
4225                         values [ins->dreg] = LLVMBuildBitCast (builder, LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), ""), LLVMInt32Type (), "");
4226                         break;
4227                 }
4228                 case OP_MOVE_I4_TO_F: {
4229                         values [ins->dreg] = LLVMBuildFPExt (builder, LLVMBuildBitCast (builder, lhs, LLVMFloatType (), ""), LLVMDoubleType (), "");
4230                         break;
4231                 }
4232                 case OP_MOVE_F_TO_I8: {
4233                         values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMInt64Type (), "");
4234                         break;
4235                 }
4236                 case OP_MOVE_I8_TO_F: {
4237                         values [ins->dreg] = LLVMBuildBitCast (builder, lhs, LLVMDoubleType (), "");
4238                         break;
4239                 }
4240                 case OP_IADD:
4241                 case OP_ISUB:
4242                 case OP_IAND:
4243                 case OP_IMUL:
4244                 case OP_IDIV:
4245                 case OP_IDIV_UN:
4246                 case OP_IREM:
4247                 case OP_IREM_UN:
4248                 case OP_IOR:
4249                 case OP_IXOR:
4250                 case OP_ISHL:
4251                 case OP_ISHR:
4252                 case OP_ISHR_UN:
4253                 case OP_FADD:
4254                 case OP_FSUB:
4255                 case OP_FMUL:
4256                 case OP_FDIV:
4257                 case OP_LADD:
4258                 case OP_LSUB:
4259                 case OP_LMUL:
4260                 case OP_LDIV:
4261                 case OP_LDIV_UN:
4262                 case OP_LREM:
4263                 case OP_LREM_UN:
4264                 case OP_LAND:
4265                 case OP_LOR:
4266                 case OP_LXOR:
4267                 case OP_LSHL:
4268                 case OP_LSHR:
4269                 case OP_LSHR_UN:
4270                         lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
4271                         rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
4272
4273                         emit_div_check (ctx, builder, bb, ins, lhs, rhs);
4274                         CHECK_FAILURE (ctx);
4275                         builder = ctx->builder;
4276
4277                         switch (ins->opcode) {
4278                         case OP_IADD:
4279                         case OP_LADD:
4280                                 values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, dname);
4281                                 break;
4282                         case OP_ISUB:
4283                         case OP_LSUB:
4284                                 values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, dname);
4285                                 break;
4286                         case OP_IMUL:
4287                         case OP_LMUL:
4288                                 values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, dname);
4289                                 break;
4290                         case OP_IREM:
4291                         case OP_LREM:
4292                                 values [ins->dreg] = LLVMBuildSRem (builder, lhs, rhs, dname);
4293                                 break;
4294                         case OP_IREM_UN:
4295                         case OP_LREM_UN:
4296                                 values [ins->dreg] = LLVMBuildURem (builder, lhs, rhs, dname);
4297                                 break;
4298                         case OP_IDIV:
4299                         case OP_LDIV:
4300                                 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, rhs, dname);
4301                                 break;
4302                         case OP_IDIV_UN:
4303                         case OP_LDIV_UN:
4304                                 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, rhs, dname);
4305                                 break;
4306                         case OP_FDIV:
4307                         case OP_RDIV:
4308                                 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
4309                                 break;
4310                         case OP_IAND:
4311                         case OP_LAND:
4312                                 values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, dname);
4313                                 break;
4314                         case OP_IOR:
4315                         case OP_LOR:
4316                                 values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, dname);
4317                                 break;
4318                         case OP_IXOR:
4319                         case OP_LXOR:
4320                                 values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, dname);
4321                                 break;
4322                         case OP_ISHL:
4323                         case OP_LSHL:
4324                                 values [ins->dreg] = LLVMBuildShl (builder, lhs, rhs, dname);
4325                                 break;
4326                         case OP_ISHR:
4327                         case OP_LSHR:
4328                                 values [ins->dreg] = LLVMBuildAShr (builder, lhs, rhs, dname);
4329                                 break;
4330                         case OP_ISHR_UN:
4331                         case OP_LSHR_UN:
4332                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, rhs, dname);
4333                                 break;
4334
4335                         case OP_FADD:
4336                                 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
4337                                 break;
4338                         case OP_FSUB:
4339                                 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
4340                                 break;
4341                         case OP_FMUL:
4342                                 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
4343                                 break;
4344
4345                         default:
4346                                 g_assert_not_reached ();
4347                         }
4348                         break;
4349                 case OP_RADD:
4350                 case OP_RSUB:
4351                 case OP_RMUL:
4352                 case OP_RDIV: {
4353                         lhs = convert (ctx, lhs, LLVMFloatType ());
4354                         rhs = convert (ctx, rhs, LLVMFloatType ());
4355                         switch (ins->opcode) {
4356                         case OP_RADD:
4357                                 values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, dname);
4358                                 break;
4359                         case OP_RSUB:
4360                                 values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, dname);
4361                                 break;
4362                         case OP_RMUL:
4363                                 values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, dname);
4364                                 break;
4365                         case OP_RDIV:
4366                                 values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
4367                                 break;
4368                         default:
4369                                 g_assert_not_reached ();
4370                                 break;
4371                         }
4372                         break;
4373                 }
4374                 case OP_IADD_IMM:
4375                 case OP_ISUB_IMM:
4376                 case OP_IMUL_IMM:
4377                 case OP_IREM_IMM:
4378                 case OP_IREM_UN_IMM:
4379                 case OP_IDIV_IMM:
4380                 case OP_IDIV_UN_IMM:
4381                 case OP_IAND_IMM:
4382                 case OP_IOR_IMM:
4383                 case OP_IXOR_IMM:
4384                 case OP_ISHL_IMM:
4385                 case OP_ISHR_IMM:
4386                 case OP_ISHR_UN_IMM:
4387                 case OP_LADD_IMM:
4388                 case OP_LSUB_IMM:
4389                 case OP_LREM_IMM:
4390                 case OP_LAND_IMM:
4391                 case OP_LOR_IMM:
4392                 case OP_LXOR_IMM:
4393                 case OP_LSHL_IMM:
4394                 case OP_LSHR_IMM:
4395                 case OP_LSHR_UN_IMM:
4396                 case OP_ADD_IMM:
4397                 case OP_AND_IMM:
4398                 case OP_MUL_IMM:
4399                 case OP_SHL_IMM:
4400                 case OP_SHR_IMM:
4401                 case OP_SHR_UN_IMM: {
4402                         LLVMValueRef imm;
4403
4404                         if (spec [MONO_INST_SRC1] == 'l') {
4405                                 imm = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
4406                         } else {
4407                                 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4408                         }
4409
4410                         emit_div_check (ctx, builder, bb, ins, lhs, imm);
4411                         CHECK_FAILURE (ctx);
4412                         builder = ctx->builder;
4413
4414 #if SIZEOF_VOID_P == 4
4415                         if (ins->opcode == OP_LSHL_IMM || ins->opcode == OP_LSHR_IMM || ins->opcode == OP_LSHR_UN_IMM)
4416                                 imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
4417 #endif
4418
4419                         if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
4420                                 lhs = convert (ctx, lhs, IntPtrType ());
4421                         imm = convert (ctx, imm, LLVMTypeOf (lhs));
4422                         switch (ins->opcode) {
4423                         case OP_IADD_IMM:
4424                         case OP_LADD_IMM:
4425                         case OP_ADD_IMM:
4426                                 values [ins->dreg] = LLVMBuildAdd (builder, lhs, imm, dname);
4427                                 break;
4428                         case OP_ISUB_IMM:
4429                         case OP_LSUB_IMM:
4430                                 values [ins->dreg] = LLVMBuildSub (builder, lhs, imm, dname);
4431                                 break;
4432                         case OP_IMUL_IMM:
4433                         case OP_MUL_IMM:
4434                                 values [ins->dreg] = LLVMBuildMul (builder, lhs, imm, dname);
4435                                 break;
4436                         case OP_IDIV_IMM:
4437                         case OP_LDIV_IMM:
4438                                 values [ins->dreg] = LLVMBuildSDiv (builder, lhs, imm, dname);
4439                                 break;
4440                         case OP_IDIV_UN_IMM:
4441                         case OP_LDIV_UN_IMM:
4442                                 values [ins->dreg] = LLVMBuildUDiv (builder, lhs, imm, dname);
4443                                 break;
4444                         case OP_IREM_IMM:
4445                         case OP_LREM_IMM:
4446                                 values [ins->dreg] = LLVMBuildSRem (builder, lhs, imm, dname);
4447                                 break;
4448                         case OP_IREM_UN_IMM:
4449                                 values [ins->dreg] = LLVMBuildURem (builder, lhs, imm, dname);
4450                                 break;
4451                         case OP_IAND_IMM:
4452                         case OP_LAND_IMM:
4453                         case OP_AND_IMM:
4454                                 values [ins->dreg] = LLVMBuildAnd (builder, lhs, imm, dname);
4455                                 break;
4456                         case OP_IOR_IMM:
4457                         case OP_LOR_IMM:
4458                                 values [ins->dreg] = LLVMBuildOr (builder, lhs, imm, dname);
4459                                 break;
4460                         case OP_IXOR_IMM:
4461                         case OP_LXOR_IMM:
4462                                 values [ins->dreg] = LLVMBuildXor (builder, lhs, imm, dname);
4463                                 break;
4464                         case OP_ISHL_IMM:
4465                         case OP_LSHL_IMM:
4466                         case OP_SHL_IMM:
4467                                 values [ins->dreg] = LLVMBuildShl (builder, lhs, imm, dname);
4468                                 break;
4469                         case OP_ISHR_IMM:
4470                         case OP_LSHR_IMM:
4471                         case OP_SHR_IMM:
4472                                 values [ins->dreg] = LLVMBuildAShr (builder, lhs, imm, dname);
4473                                 break;
4474                         case OP_ISHR_UN_IMM:
4475                                 /* This is used to implement conv.u4, so the lhs could be an i8 */
4476                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
4477                                 imm = convert (ctx, imm, LLVMInt32Type ());
4478                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
4479                                 break;
4480                         case OP_LSHR_UN_IMM:
4481                         case OP_SHR_UN_IMM:
4482                                 values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
4483                                 break;
4484                         default:
4485                                 g_assert_not_reached ();
4486                         }
4487                         break;
4488                 }
4489                 case OP_INEG:
4490                         values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
4491                         break;
4492                 case OP_LNEG:
4493                         values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt64Type (), 0, FALSE), lhs, dname);
4494                         break;
4495                 case OP_FNEG:
4496                         lhs = convert (ctx, lhs, LLVMDoubleType ());
4497                         values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMDoubleType (), 0.0), lhs, dname);
4498                         break;
4499                 case OP_RNEG:
4500                         lhs = convert (ctx, lhs, LLVMFloatType ());
4501                         values [ins->dreg] = LLVMBuildFSub (builder, LLVMConstReal (LLVMFloatType (), 0.0), lhs, dname);
4502                         break;
4503                 case OP_INOT: {
4504                         guint32 v = 0xffffffff;
4505                         values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt32Type (), v, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
4506                         break;
4507                 }
4508                 case OP_LNOT: {
4509                         guint64 v = 0xffffffffffffffffLL;
4510                         values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt64Type (), v, FALSE), lhs, dname);
4511                         break;
4512                 }
4513 #if defined(TARGET_X86) || defined(TARGET_AMD64)
4514                 case OP_X86_LEA: {
4515                         LLVMValueRef v1, v2;
4516
4517                         v1 = LLVMBuildMul (builder, convert (ctx, rhs, IntPtrType ()), LLVMConstInt (IntPtrType (), (1 << ins->backend.shift_amount), FALSE), "");
4518                         v2 = LLVMBuildAdd (builder, convert (ctx, lhs, IntPtrType ()), v1, "");
4519                         values [ins->dreg] = LLVMBuildAdd (builder, v2, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), dname);
4520                         break;
4521                 }
4522 #endif
4523
4524                 case OP_ICONV_TO_I1:
4525                 case OP_ICONV_TO_I2:
4526                 case OP_ICONV_TO_I4:
4527                 case OP_ICONV_TO_U1:
4528                 case OP_ICONV_TO_U2:
4529                 case OP_ICONV_TO_U4:
4530                 case OP_LCONV_TO_I1:
4531                 case OP_LCONV_TO_I2:
4532                 case OP_LCONV_TO_U1:
4533                 case OP_LCONV_TO_U2:
4534                 case OP_LCONV_TO_U4: {
4535                         gboolean sign;
4536
4537                         sign = (ins->opcode == OP_ICONV_TO_I1) || (ins->opcode == OP_ICONV_TO_I2) || (ins->opcode == OP_ICONV_TO_I4) || (ins->opcode == OP_LCONV_TO_I1) || (ins->opcode == OP_LCONV_TO_I2);
4538
4539                         /* Have to do two casts since our vregs have type int */
4540                         v = LLVMBuildTrunc (builder, lhs, op_to_llvm_type (ins->opcode), "");
4541                         if (sign)
4542                                 values [ins->dreg] = LLVMBuildSExt (builder, v, LLVMInt32Type (), dname);
4543                         else
4544                                 values [ins->dreg] = LLVMBuildZExt (builder, v, LLVMInt32Type (), dname);
4545                         break;
4546                 }
4547                 case OP_ICONV_TO_I8:
4548                         values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
4549                         break;
4550                 case OP_ICONV_TO_U8:
4551                         values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
4552                         break;
4553                 case OP_FCONV_TO_I4:
4554                 case OP_RCONV_TO_I4:
4555                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt32Type (), dname);
4556                         break;
4557                 case OP_FCONV_TO_I1:
4558                 case OP_RCONV_TO_I1:
4559                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
4560                         break;
4561                 case OP_FCONV_TO_U1:
4562                 case OP_RCONV_TO_U1:
4563                         values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
4564                         break;
4565                 case OP_FCONV_TO_I2:
4566                 case OP_RCONV_TO_I2:
4567                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
4568                         break;
4569                 case OP_FCONV_TO_U2:
4570                 case OP_RCONV_TO_U2:
4571                         values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
4572                         break;
4573                 case OP_FCONV_TO_I8:
4574                 case OP_RCONV_TO_I8:
4575                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt64Type (), dname);
4576                         break;
4577                 case OP_FCONV_TO_I:
4578                         values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, IntPtrType (), dname);
4579                         break;
4580                 case OP_ICONV_TO_R8:
4581                 case OP_LCONV_TO_R8:
4582                         values [ins->dreg] = LLVMBuildSIToFP (builder, lhs, LLVMDoubleType (), dname);
4583                         break;
4584                 case OP_ICONV_TO_R_UN:
4585                 case OP_LCONV_TO_R_UN:
4586                         values [ins->dreg] = LLVMBuildUIToFP (builder, lhs, LLVMDoubleType (), dname);
4587                         break;
4588 #if SIZEOF_VOID_P == 4
4589                 case OP_LCONV_TO_U:
4590 #endif
4591                 case OP_LCONV_TO_I4:
4592                         values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
4593                         break;
4594                 case OP_ICONV_TO_R4:
4595                 case OP_LCONV_TO_R4:
4596                         v = LLVMBuildSIToFP (builder, lhs, LLVMFloatType (), "");
4597                         if (cfg->r4fp)
4598                                 values [ins->dreg] = v;
4599                         else
4600                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
4601                         break;
4602                 case OP_FCONV_TO_R4:
4603                         v = LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), "");
4604                         if (cfg->r4fp)
4605                                 values [ins->dreg] = v;
4606                         else
4607                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
4608                         break;
4609                 case OP_RCONV_TO_R8:
4610                         values [ins->dreg] = LLVMBuildFPExt (builder, lhs, LLVMDoubleType (), dname);
4611                         break;
4612                 case OP_RCONV_TO_R4:
4613                         values [ins->dreg] = lhs;
4614                         break;
4615                 case OP_SEXT_I4:
4616                         values [ins->dreg] = LLVMBuildSExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
4617                         break;
4618                 case OP_ZEXT_I4:
4619                         values [ins->dreg] = LLVMBuildZExt (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMInt64Type (), dname);
4620                         break;
4621                 case OP_TRUNC_I4:
4622                         values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
4623                         break;
4624                 case OP_LOCALLOC_IMM: {
4625                         LLVMValueRef v;
4626
4627                         guint32 size = ins->inst_imm;
4628                         size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4629
4630                         v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, "");
4631
4632                         if (ins->flags & MONO_INST_INIT) {
4633                                 LLVMValueRef args [5];
4634
4635                                 args [0] = v;
4636                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
4637                                 args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
4638                                 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
4639                                 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
4640                                 LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memset_func_name), args, memset_param_count, "");
4641                         }
4642
4643                         values [ins->dreg] = v;
4644                         break;
4645                 }
4646                 case OP_LOCALLOC: {
4647                         LLVMValueRef v, size;
4648                                 
4649                         size = LLVMBuildAnd (builder, LLVMBuildAdd (builder, convert (ctx, lhs, LLVMInt32Type ()), LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT - 1, FALSE), ""), LLVMConstInt (LLVMInt32Type (), ~ (MONO_ARCH_FRAME_ALIGNMENT - 1), FALSE), "");
4650
4651                         v = mono_llvm_build_alloca (builder, LLVMInt8Type (), size, MONO_ARCH_FRAME_ALIGNMENT, "");
4652
4653                         if (ins->flags & MONO_INST_INIT) {
4654                                 LLVMValueRef args [5];
4655
4656                                 args [0] = v;
4657                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
4658                                 args [2] = size;
4659                                 args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
4660                                 args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
4661                                 LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memset_func_name), args, memset_param_count, "");
4662                         }
4663                         values [ins->dreg] = v;
4664                         break;
4665                 }
4666
4667                 case OP_LOADI1_MEMBASE:
4668                 case OP_LOADU1_MEMBASE:
4669                 case OP_LOADI2_MEMBASE:
4670                 case OP_LOADU2_MEMBASE:
4671                 case OP_LOADI4_MEMBASE:
4672                 case OP_LOADU4_MEMBASE:
4673                 case OP_LOADI8_MEMBASE:
4674                 case OP_LOADR4_MEMBASE:
4675                 case OP_LOADR8_MEMBASE:
4676                 case OP_LOAD_MEMBASE:
4677                 case OP_LOADI8_MEM:
4678                 case OP_LOADU1_MEM:
4679                 case OP_LOADU2_MEM:
4680                 case OP_LOADI4_MEM:
4681                 case OP_LOADU4_MEM:
4682                 case OP_LOAD_MEM: {
4683                         int size = 8;
4684                         LLVMValueRef base, index, addr;
4685                         LLVMTypeRef t;
4686                         gboolean sext = FALSE, zext = FALSE;
4687                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
4688
4689                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
4690
4691                         if (sext || zext)
4692                                 dname = (char*)"";
4693
4694                         if ((ins->opcode == OP_LOADI8_MEM) || (ins->opcode == OP_LOAD_MEM) || (ins->opcode == OP_LOADI4_MEM) || (ins->opcode == OP_LOADU4_MEM) || (ins->opcode == OP_LOADU1_MEM) || (ins->opcode == OP_LOADU2_MEM)) {
4695                                 addr = LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE);
4696                         } else {
4697                                 /* _MEMBASE */
4698                                 base = lhs;
4699
4700                                 if (ins->inst_offset == 0) {
4701                                         addr = base;
4702                                 } else if (ins->inst_offset % size != 0) {
4703                                         /* Unaligned load */
4704                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
4705                                         addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
4706                                 } else {
4707                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
4708                                         addr = LLVMBuildGEP (builder, convert (ctx, base, LLVMPointerType (t, 0)), &index, 1, "");
4709                                 }
4710                         }
4711
4712                         addr = convert (ctx, addr, LLVMPointerType (t, 0));
4713
4714                         values [ins->dreg] = emit_load (ctx, bb, &builder, size, addr, dname, is_volatile);
4715
4716                         if (!is_volatile && (ins->flags & MONO_INST_INVARIANT_LOAD)) {
4717                                 /*
4718                                  * These will signal LLVM that these loads do not alias any stores, and
4719                                  * they can't fail, allowing them to be hoisted out of loops.
4720                                  */
4721                                 set_invariant_load_flag (values [ins->dreg]);
4722                                 set_metadata_flag (values [ins->dreg], "mono.nofail.load");
4723                         }
4724
4725                         if (sext)
4726                                 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
4727                         else if (zext)
4728                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
4729                         else if (!cfg->r4fp && ins->opcode == OP_LOADR4_MEMBASE)
4730                                 values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
4731                         break;
4732                 }
4733                                 
4734                 case OP_STOREI1_MEMBASE_REG:
4735                 case OP_STOREI2_MEMBASE_REG:
4736                 case OP_STOREI4_MEMBASE_REG:
4737                 case OP_STOREI8_MEMBASE_REG:
4738                 case OP_STORER4_MEMBASE_REG:
4739                 case OP_STORER8_MEMBASE_REG:
4740                 case OP_STORE_MEMBASE_REG: {
4741                         int size = 8;
4742                         LLVMValueRef index, addr;
4743                         LLVMTypeRef t;
4744                         gboolean sext = FALSE, zext = FALSE;
4745                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
4746
4747                         if (!values [ins->inst_destbasereg])
4748                                 LLVM_FAILURE (ctx, "inst_destbasereg");
4749
4750                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
4751
4752                         if (ins->inst_offset % size != 0) {
4753                                 /* Unaligned store */
4754                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
4755                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
4756                         } else {
4757                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
4758                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
4759                         }
4760                         emit_store (ctx, bb, &builder, size, convert (ctx, values [ins->sreg1], t), convert (ctx, addr, LLVMPointerType (t, 0)), is_volatile);
4761                         break;
4762                 }
4763
4764                 case OP_STOREI1_MEMBASE_IMM:
4765                 case OP_STOREI2_MEMBASE_IMM:
4766                 case OP_STOREI4_MEMBASE_IMM:
4767                 case OP_STOREI8_MEMBASE_IMM:
4768                 case OP_STORE_MEMBASE_IMM: {
4769                         int size = 8;
4770                         LLVMValueRef index, addr;
4771                         LLVMTypeRef t;
4772                         gboolean sext = FALSE, zext = FALSE;
4773                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
4774
4775                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
4776
4777                         if (ins->inst_offset % size != 0) {
4778                                 /* Unaligned store */
4779                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset, FALSE);
4780                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (LLVMInt8Type (), 0)), &index, 1, "");
4781                         } else {
4782                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
4783                                 addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
4784                         }
4785                         emit_store (ctx, bb, &builder, size, convert (ctx, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), t), convert (ctx, addr, LLVMPointerType (t, 0)), is_volatile);
4786                         break;
4787                 }
4788
4789                 case OP_CHECK_THIS:
4790                         emit_load (ctx, bb, &builder, sizeof (gpointer), convert (ctx, lhs, LLVMPointerType (IntPtrType (), 0)), "", TRUE);
4791                         break;
4792                 case OP_OUTARG_VTRETADDR:
4793                         break;
4794                 case OP_VOIDCALL:
4795                 case OP_CALL:
4796                 case OP_LCALL:
4797                 case OP_FCALL:
4798                 case OP_RCALL:
4799                 case OP_VCALL:
4800                 case OP_VOIDCALL_MEMBASE:
4801                 case OP_CALL_MEMBASE:
4802                 case OP_LCALL_MEMBASE:
4803                 case OP_FCALL_MEMBASE:
4804                 case OP_RCALL_MEMBASE:
4805                 case OP_VCALL_MEMBASE:
4806                 case OP_VOIDCALL_REG:
4807                 case OP_CALL_REG:
4808                 case OP_LCALL_REG:
4809                 case OP_FCALL_REG:
4810                 case OP_RCALL_REG:
4811                 case OP_VCALL_REG: {
4812                         process_call (ctx, bb, &builder, ins);
4813                         CHECK_FAILURE (ctx);
4814                         break;
4815                 }
4816                 case OP_AOTCONST: {
4817                         guint32 got_offset;
4818                         LLVMValueRef indexes [2];
4819                         MonoJumpInfo *tmp_ji, *ji;
4820                         LLVMValueRef got_entry_addr;
4821                         char *name;
4822
4823                         /* 
4824                          * FIXME: Can't allocate from the cfg mempool since that is freed if
4825                          * the LLVM compile fails.
4826                          */
4827                         tmp_ji = g_new0 (MonoJumpInfo, 1);
4828                         tmp_ji->type = (MonoJumpInfoType)ins->inst_c1;
4829                         tmp_ji->data.target = ins->inst_p0;
4830
4831                         ji = mono_aot_patch_info_dup (tmp_ji);
4832                         g_free (tmp_ji);
4833
4834                         ji->next = cfg->patch_info;
4835                         cfg->patch_info = ji;
4836                                    
4837                         //mono_add_patch_info (cfg, 0, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4838                         got_offset = mono_aot_get_got_offset (cfg->patch_info);
4839                         ctx->module->max_got_offset = MAX (ctx->module->max_got_offset, got_offset);
4840                         if (!mono_aot_is_shared_got_offset (got_offset)) {
4841                                 //mono_print_ji (ji);
4842                                 //printf ("\n");
4843                                 ctx->has_got_access = TRUE;
4844                         }
4845  
4846                         indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
4847                         indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)got_offset, FALSE);
4848                         got_entry_addr = LLVMBuildGEP (builder, ctx->module->got_var, indexes, 2, "");
4849
4850                         name = get_aotconst_name (ji->type, ji->data.target, got_offset);
4851                         values [ins->dreg] = LLVMBuildLoad (builder, got_entry_addr, name);
4852                         g_free (name);
4853                         set_invariant_load_flag (values [ins->dreg]);
4854                         break;
4855                 }
4856                 case OP_NOT_REACHED:
4857                         LLVMBuildUnreachable (builder);
4858                         has_terminator = TRUE;
4859                         g_assert (bb->block_num < cfg->max_block_num);
4860                         ctx->unreachable [bb->block_num] = TRUE;
4861                         /* Might have instructions after this */
4862                         while (ins->next) {
4863                                 MonoInst *next = ins->next;
4864                                 /* 
4865                                  * FIXME: If later code uses the regs defined by these instructions,
4866                                  * compilation will fail.
4867                                  */
4868                                 MONO_DELETE_INS (bb, next);
4869                         }                               
4870                         break;
4871                 case OP_LDADDR: {
4872                         MonoInst *var = ins->inst_i0;
4873
4874                         if (var->opcode == OP_VTARG_ADDR) {
4875                                 /* The variable contains the vtype address */
4876                                 values [ins->dreg] = values [var->dreg];
4877                         } else {
4878                                 values [ins->dreg] = addresses [var->dreg];
4879                         }
4880                         break;
4881                 }
4882                 case OP_SIN: {
4883                         LLVMValueRef args [1];
4884
4885                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
4886                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "llvm.sin.f64"), args, 1, dname);
4887                         break;
4888                 }
4889                 case OP_COS: {
4890                         LLVMValueRef args [1];
4891
4892                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
4893                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "llvm.cos.f64"), args, 1, dname);
4894                         break;
4895                 }
4896                 case OP_SQRT: {
4897                         LLVMValueRef args [1];
4898
4899 #if 0
4900                         /* This no longer seems to happen */
4901                         /*
4902                          * LLVM optimizes sqrt(nan) into undefined in
4903                          * lib/Analysis/ConstantFolding.cpp
4904                          * Also, sqrt(NegativeInfinity) is optimized into 0.
4905                          */
4906                         LLVM_FAILURE (ctx, "sqrt");
4907 #endif
4908                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
4909                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "llvm.sqrt.f64"), args, 1, dname);
4910                         break;
4911                 }
4912                 case OP_ABS: {
4913                         LLVMValueRef args [1];
4914
4915                         args [0] = convert (ctx, lhs, LLVMDoubleType ());
4916                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, "fabs"), args, 1, dname);
4917                         break;
4918                 }
4919
4920                 case OP_IMIN:
4921                 case OP_LMIN:
4922                 case OP_IMAX:
4923                 case OP_LMAX:
4924                 case OP_IMIN_UN:
4925                 case OP_LMIN_UN:
4926                 case OP_IMAX_UN:
4927                 case OP_LMAX_UN: {
4928                         LLVMValueRef v;
4929
4930                         lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
4931                         rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
4932
4933                         switch (ins->opcode) {
4934                         case OP_IMIN:
4935                         case OP_LMIN:
4936                                 v = LLVMBuildICmp (builder, LLVMIntSLE, lhs, rhs, "");
4937                                 break;
4938                         case OP_IMAX:
4939                         case OP_LMAX:
4940                                 v = LLVMBuildICmp (builder, LLVMIntSGE, lhs, rhs, "");
4941                                 break;
4942                         case OP_IMIN_UN:
4943                         case OP_LMIN_UN:
4944                                 v = LLVMBuildICmp (builder, LLVMIntULE, lhs, rhs, "");
4945                                 break;
4946                         case OP_IMAX_UN:
4947                         case OP_LMAX_UN:
4948                                 v = LLVMBuildICmp (builder, LLVMIntUGE, lhs, rhs, "");
4949                                 break;
4950                         default:
4951                                 g_assert_not_reached ();
4952                                 break;
4953                         }
4954                         values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
4955                         break;
4956                 }
4957                 case OP_ATOMIC_EXCHANGE_I4:
4958                 case OP_ATOMIC_EXCHANGE_I8: {
4959                         LLVMValueRef args [2];
4960                         LLVMTypeRef t;
4961                                 
4962                         if (ins->opcode == OP_ATOMIC_EXCHANGE_I4)
4963                                 t = LLVMInt32Type ();
4964                         else
4965                                 t = LLVMInt64Type ();
4966
4967                         g_assert (ins->inst_offset == 0);
4968
4969                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
4970                         args [1] = convert (ctx, rhs, t);
4971
4972                         values [ins->dreg] = mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_XCHG, args [0], args [1]);
4973                         break;
4974                 }
4975                 case OP_ATOMIC_ADD_I4:
4976                 case OP_ATOMIC_ADD_I8: {
4977                         LLVMValueRef args [2];
4978                         LLVMTypeRef t;
4979                                 
4980                         if (ins->opcode == OP_ATOMIC_ADD_I4)
4981                                 t = LLVMInt32Type ();
4982                         else
4983                                 t = LLVMInt64Type ();
4984
4985                         g_assert (ins->inst_offset == 0);
4986
4987                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
4988                         args [1] = convert (ctx, rhs, t);
4989                         values [ins->dreg] = LLVMBuildAdd (builder, mono_llvm_build_atomic_rmw (builder, LLVM_ATOMICRMW_OP_ADD, args [0], args [1]), args [1], dname);
4990                         break;
4991                 }
4992                 case OP_ATOMIC_CAS_I4:
4993                 case OP_ATOMIC_CAS_I8: {
4994                         LLVMValueRef args [3], val;
4995                         LLVMTypeRef t;
4996                                 
4997                         if (ins->opcode == OP_ATOMIC_CAS_I4)
4998                                 t = LLVMInt32Type ();
4999                         else
5000                                 t = LLVMInt64Type ();
5001
5002                         args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
5003                         /* comparand */
5004                         args [1] = convert (ctx, values [ins->sreg3], t);
5005                         /* new value */
5006                         args [2] = convert (ctx, values [ins->sreg2], t);
5007                         val = mono_llvm_build_cmpxchg (builder, args [0], args [1], args [2]);
5008                         /* cmpxchg returns a pair */
5009                         values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, "");
5010                         break;
5011                 }
5012                 case OP_MEMORY_BARRIER: {
5013                         mono_llvm_build_fence (builder, (BarrierKind) ins->backend.memory_barrier_kind);
5014                         break;
5015                 }
5016                 case OP_ATOMIC_LOAD_I1:
5017                 case OP_ATOMIC_LOAD_I2:
5018                 case OP_ATOMIC_LOAD_I4:
5019                 case OP_ATOMIC_LOAD_I8:
5020                 case OP_ATOMIC_LOAD_U1:
5021                 case OP_ATOMIC_LOAD_U2:
5022                 case OP_ATOMIC_LOAD_U4:
5023                 case OP_ATOMIC_LOAD_U8:
5024                 case OP_ATOMIC_LOAD_R4:
5025                 case OP_ATOMIC_LOAD_R8: {
5026                         LLVM_FAILURE (ctx, "atomic mono.load intrinsic");
5027
5028                         int size;
5029                         gboolean sext, zext;
5030                         LLVMTypeRef t;
5031                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
5032                         BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
5033                         LLVMValueRef index, addr;
5034
5035                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
5036
5037                         if (sext || zext)
5038                                 dname = (char *)"";
5039
5040                         if (ins->inst_offset != 0) {
5041                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
5042                                 addr = LLVMBuildGEP (builder, convert (ctx, lhs, LLVMPointerType (t, 0)), &index, 1, "");
5043                         } else {
5044                                 addr = lhs;
5045                         }
5046
5047                         addr = convert (ctx, addr, LLVMPointerType (t, 0));
5048
5049                         values [ins->dreg] = emit_load_general (ctx, bb, &builder, size, addr, dname, is_volatile, barrier);
5050
5051                         if (sext)
5052                                 values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
5053                         else if (zext)
5054                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
5055                         break;
5056                 }
5057                 case OP_ATOMIC_STORE_I1:
5058                 case OP_ATOMIC_STORE_I2:
5059                 case OP_ATOMIC_STORE_I4:
5060                 case OP_ATOMIC_STORE_I8:
5061                 case OP_ATOMIC_STORE_U1:
5062                 case OP_ATOMIC_STORE_U2:
5063                 case OP_ATOMIC_STORE_U4:
5064                 case OP_ATOMIC_STORE_U8:
5065                 case OP_ATOMIC_STORE_R4:
5066                 case OP_ATOMIC_STORE_R8: {
5067                         LLVM_FAILURE (ctx, "atomic mono.store intrinsic");
5068
5069                         int size;
5070                         gboolean sext, zext;
5071                         LLVMTypeRef t;
5072                         gboolean is_volatile = (ins->flags & MONO_INST_FAULT);
5073                         BarrierKind barrier = (BarrierKind) ins->backend.memory_barrier_kind;
5074                         LLVMValueRef index, addr, value;
5075
5076                         if (!values [ins->inst_destbasereg])
5077                                 LLVM_FAILURE (ctx, "inst_destbasereg");
5078
5079                         t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
5080
5081                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);
5082                         addr = LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, "");
5083                         value = convert (ctx, values [ins->sreg1], t);
5084
5085                         emit_store_general (ctx, bb, &builder, size, value, addr, is_volatile, barrier);
5086                         break;
5087                 }
5088                 case OP_RELAXED_NOP: {
5089 #if defined(TARGET_AMD64) || defined(TARGET_X86)
5090                         emit_call (ctx, bb, &builder, LLVMGetNamedFunction (ctx->lmodule, "llvm.x86.sse2.pause"), NULL, 0);
5091                         break;
5092 #else
5093                         break;
5094 #endif
5095                 }
5096                 case OP_TLS_GET: {
5097 #if (defined(TARGET_AMD64) || defined(TARGET_X86)) && defined(__linux__)
5098 #ifdef TARGET_AMD64
5099                         // 257 == FS segment register
5100                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 257);
5101 #else
5102                         // 256 == GS segment register
5103                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
5104 #endif
5105                         // FIXME: XEN
5106                         values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), ins->inst_offset, TRUE), ptrtype, ""), "");
5107 #elif defined(TARGET_AMD64) && defined(TARGET_OSX)
5108                         /* See mono_amd64_emit_tls_get () */
5109                         int offset = mono_amd64_get_tls_gs_offset () + (ins->inst_offset * 8);
5110
5111                         // 256 == GS segment register
5112                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
5113                         values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), offset, TRUE), ptrtype, ""), "");
5114 #else
5115                         LLVM_FAILURE (ctx, "opcode tls-get");
5116 #endif
5117
5118                         break;
5119                 }
5120                 case OP_TLS_GET_REG: {
5121 #if defined(TARGET_AMD64) && defined(TARGET_OSX)
5122                         /* See emit_tls_get_reg () */
5123                         // 256 == GS segment register
5124                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
5125                         values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, convert (ctx, lhs, LLVMInt32Type ()), ptrtype, ""), "");
5126 #else
5127                         LLVM_FAILURE (ctx, "opcode tls-get");
5128 #endif
5129                         break;
5130                 }
5131
5132                 case OP_TLS_SET_REG: {
5133 #if defined(TARGET_AMD64) && defined(TARGET_OSX)
5134                         /* See emit_tls_get_reg () */
5135                         // 256 == GS segment register
5136                         LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
5137                         LLVMBuildStore (builder, convert (ctx, lhs, IntPtrType ()), LLVMBuildIntToPtr (builder, convert (ctx, rhs, LLVMInt32Type ()), ptrtype, ""));
5138 #else
5139                         LLVM_FAILURE (ctx, "opcode tls-set-reg");
5140 #endif
5141                         break;
5142                 }
5143
5144                         /*
5145                          * Overflow opcodes.
5146                          */
5147                 case OP_IADD_OVF:
5148                 case OP_IADD_OVF_UN:
5149                 case OP_ISUB_OVF:
5150                 case OP_ISUB_OVF_UN:
5151                 case OP_IMUL_OVF:
5152                 case OP_IMUL_OVF_UN:
5153 #if SIZEOF_VOID_P == 8
5154                 case OP_LADD_OVF:
5155                 case OP_LADD_OVF_UN:
5156                 case OP_LSUB_OVF:
5157                 case OP_LSUB_OVF_UN:
5158                 case OP_LMUL_OVF:
5159                 case OP_LMUL_OVF_UN:
5160 #endif
5161                         {
5162                                 LLVMValueRef args [2], val, ovf, func;
5163
5164                                 args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
5165                                 args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
5166                                 func = LLVMGetNamedFunction (lmodule, ovf_op_to_intrins (ins->opcode));
5167                                 g_assert (func);
5168                                 val = LLVMBuildCall (builder, func, args, 2, "");
5169                                 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
5170                                 ovf = LLVMBuildExtractValue (builder, val, 1, "");
5171                                 emit_cond_system_exception (ctx, bb, "OverflowException", ovf);
5172                                 CHECK_FAILURE (ctx);
5173                                 builder = ctx->builder;
5174                                 break;
5175                         }
5176
5177                         /* 
5178                          * Valuetypes.
5179                          *   We currently model them using arrays. Promotion to local vregs is 
5180                          * disabled for them in mono_handle_global_vregs () in the LLVM case, 
5181                          * so we always have an entry in cfg->varinfo for them.
5182                          * FIXME: Is this needed ?
5183                          */
5184                 case OP_VZERO: {
5185                         MonoClass *klass = ins->klass;
5186                         LLVMValueRef args [5];
5187
5188                         if (!klass) {
5189                                 // FIXME:
5190                                 LLVM_FAILURE (ctx, "!klass");
5191                                 break;
5192                         }
5193
5194                         if (!addresses [ins->dreg])
5195                                 addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
5196                         args [0] = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
5197                         args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
5198                         args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
5199                         // FIXME: Alignment
5200                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5201                         args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
5202                         LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memset_func_name), args, memset_param_count, "");
5203                         break;
5204                 }
5205                 case OP_DUMMY_VZERO:
5206                         break;
5207
5208                 case OP_STOREV_MEMBASE:
5209                 case OP_LOADV_MEMBASE:
5210                 case OP_VMOVE: {
5211                         MonoClass *klass = ins->klass;
5212                         LLVMValueRef src = NULL, dst, args [5];
5213                         gboolean done = FALSE;
5214
5215                         if (!klass) {
5216                                 // FIXME:
5217                                 LLVM_FAILURE (ctx, "!klass");
5218                                 break;
5219                         }
5220
5221                         if (mini_is_gsharedvt_klass (klass)) {
5222                                 // FIXME:
5223                                 LLVM_FAILURE (ctx, "gsharedvt");
5224                                 break;
5225                         }
5226
5227                         switch (ins->opcode) {
5228                         case OP_STOREV_MEMBASE:
5229                                 if (cfg->gen_write_barriers && klass->has_references && ins->inst_destbasereg != cfg->frame_reg &&
5230                                         LLVMGetInstructionOpcode (values [ins->inst_destbasereg]) != LLVMAlloca) {
5231                                         /* Decomposed earlier */
5232                                         g_assert_not_reached ();
5233                                         break;
5234                                 }
5235                                 if (!addresses [ins->sreg1]) {
5236                                         /* SIMD */
5237                                         g_assert (values [ins->sreg1]);
5238                                         dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (type_to_llvm_type (ctx, &klass->byval_arg), 0));
5239                                         LLVMBuildStore (builder, values [ins->sreg1], dst);
5240                                         done = TRUE;
5241                                 } else {
5242                                         src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
5243                                         dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
5244                                 }
5245                                 break;
5246                         case OP_LOADV_MEMBASE:
5247                                 if (!addresses [ins->dreg])
5248                                         addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
5249                                 src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
5250                                 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
5251                                 break;
5252                         case OP_VMOVE:
5253                                 if (!addresses [ins->sreg1])
5254                                         addresses [ins->sreg1] = build_alloca (ctx, &klass->byval_arg);
5255                                 if (!addresses [ins->dreg])
5256                                         addresses [ins->dreg] = build_alloca (ctx, &klass->byval_arg);
5257                                 src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
5258                                 dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
5259                                 break;
5260                         default:
5261                                 g_assert_not_reached ();
5262                         }
5263                         CHECK_FAILURE (ctx);
5264
5265                         if (done)
5266                                 break;
5267
5268                         args [0] = dst;
5269                         args [1] = src;
5270                         args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
5271                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5272                         // FIXME: Alignment
5273                         args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5274                         args [4] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
5275                         LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, memcpy_func_name), args, memcpy_param_count, "");
5276                         break;
5277                 }
5278                 case OP_LLVM_OUTARG_VT: {
5279                         LLVMArgInfo *ainfo = (LLVMArgInfo*)ins->inst_p0;
5280                         MonoType *t = mini_get_underlying_type (ins->inst_vtype);
5281
5282                         if (ainfo->storage == LLVMArgScalarByRef) {
5283                                 LLVMTypeRef argtype;
5284                                 LLVMValueRef loc, v;
5285
5286                                 argtype = type_to_llvm_arg_type (ctx, t);
5287                                 loc = build_alloca_llvm_type (ctx, argtype, 0);
5288                                 v = convert (ctx, values [ins->sreg1], argtype);
5289                                 LLVMBuildStore (ctx->builder, v, loc);
5290                                 addresses [ins->dreg] = loc;
5291                         } else {
5292                                 if (!addresses [ins->sreg1]) {
5293                                         addresses [ins->sreg1] = build_alloca (ctx, t);
5294                                         g_assert (values [ins->sreg1]);
5295                                         LLVMBuildStore (builder, values [ins->sreg1], addresses [ins->sreg1]);
5296                                 }
5297                                 addresses [ins->dreg] = addresses [ins->sreg1];
5298                         }
5299                         break;
5300                 }
5301
5302                         /* 
5303                          * SIMD
5304                          */
5305 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5306                 case OP_XZERO: {
5307                         values [ins->dreg] = LLVMConstNull (type_to_llvm_type (ctx, &ins->klass->byval_arg));
5308                         break;
5309                 }
5310                 case OP_LOADX_MEMBASE: {
5311                         LLVMTypeRef t = type_to_llvm_type (ctx, &ins->klass->byval_arg);
5312                         LLVMValueRef src;
5313
5314                         src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
5315                         values [ins->dreg] = mono_llvm_build_aligned_load (builder, src, "", FALSE, 1);
5316                         break;
5317                 }
5318                 case OP_STOREX_MEMBASE: {
5319                         LLVMTypeRef t = LLVMTypeOf (values [ins->sreg1]);
5320                         LLVMValueRef dest;
5321
5322                         dest = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (t, 0));
5323                         mono_llvm_build_aligned_store (builder, values [ins->sreg1], dest, FALSE, 1);
5324                         break;
5325                 }
5326                 case OP_PADDB:
5327                 case OP_PADDW:
5328                 case OP_PADDD:
5329                 case OP_PADDQ:
5330                         values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, "");
5331                         break;
5332                 case OP_ADDPD:
5333                 case OP_ADDPS:
5334                         values [ins->dreg] = LLVMBuildFAdd (builder, lhs, rhs, "");
5335                         break;
5336                 case OP_PSUBB:
5337                 case OP_PSUBW:
5338                 case OP_PSUBD:
5339                 case OP_PSUBQ:
5340                         values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, "");
5341                         break;
5342                 case OP_SUBPD:
5343                 case OP_SUBPS:
5344                         values [ins->dreg] = LLVMBuildFSub (builder, lhs, rhs, "");
5345                         break;
5346                 case OP_MULPD:
5347                 case OP_MULPS:
5348                         values [ins->dreg] = LLVMBuildFMul (builder, lhs, rhs, "");
5349                         break;
5350                 case OP_DIVPD:
5351                 case OP_DIVPS:
5352                         values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, "");
5353                         break;
5354                 case OP_PAND:
5355                         values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, "");
5356                         break;
5357                 case OP_POR:
5358                         values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, "");
5359                         break;
5360                 case OP_PXOR:
5361                         values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, "");
5362                         break;
5363                 case OP_PMULW:
5364                 case OP_PMULD:
5365                         values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, "");
5366                         break;
5367                 case OP_ANDPS:
5368                 case OP_ANDNPS:
5369                 case OP_ORPS:
5370                 case OP_XORPS:
5371                 case OP_ANDPD:
5372                 case OP_ANDNPD:
5373                 case OP_ORPD:
5374                 case OP_XORPD: {
5375                         LLVMTypeRef t, rt;
5376                         LLVMValueRef v = NULL;
5377
5378                         switch (ins->opcode) {
5379                         case OP_ANDPS:
5380                         case OP_ANDNPS:
5381                         case OP_ORPS:
5382                         case OP_XORPS:
5383                                 t = LLVMVectorType (LLVMInt32Type (), 4);
5384                                 rt = LLVMVectorType (LLVMFloatType (), 4);
5385                                 break;
5386                         case OP_ANDPD:
5387                         case OP_ANDNPD:
5388                         case OP_ORPD:
5389                         case OP_XORPD:
5390                                 t = LLVMVectorType (LLVMInt64Type (), 2);
5391                                 rt = LLVMVectorType (LLVMDoubleType (), 2);
5392                                 break;
5393                         default:
5394                                 t = LLVMInt32Type ();
5395                                 rt = LLVMInt32Type ();
5396                                 g_assert_not_reached ();
5397                         }
5398
5399                         lhs = LLVMBuildBitCast (builder, lhs, t, "");
5400                         rhs = LLVMBuildBitCast (builder, rhs, t, "");
5401                         switch (ins->opcode) {
5402                         case OP_ANDPS:
5403                         case OP_ANDPD:
5404                                 v = LLVMBuildAnd (builder, lhs, rhs, "");
5405                                 break;
5406                         case OP_ORPS:
5407                         case OP_ORPD:
5408                                 v = LLVMBuildOr (builder, lhs, rhs, "");
5409                                 break;
5410                         case OP_XORPS:
5411                         case OP_XORPD:
5412                                 v = LLVMBuildXor (builder, lhs, rhs, "");
5413                                 break;
5414                         case OP_ANDNPS:
5415                         case OP_ANDNPD:
5416                                 v = LLVMBuildAnd (builder, rhs, LLVMBuildNot (builder, lhs, ""), "");
5417                                 break;
5418                         }
5419                         values [ins->dreg] = LLVMBuildBitCast (builder, v, rt, "");
5420                         break;
5421                 }
5422                 case OP_MINPD:
5423                 case OP_MINPS:
5424                 case OP_MAXPD:
5425                 case OP_MAXPS:
5426                 case OP_ADDSUBPD:
5427                 case OP_ADDSUBPS:
5428                 case OP_PMIND_UN:
5429                 case OP_PMINW_UN:
5430                 case OP_PMINB_UN:
5431                 case OP_PMINW:
5432                 case OP_PMAXD_UN:
5433                 case OP_PMAXW_UN:
5434                 case OP_PMAXB_UN:
5435                 case OP_HADDPD:
5436                 case OP_HADDPS:
5437                 case OP_HSUBPD:
5438                 case OP_HSUBPS:
5439                 case OP_PADDB_SAT:
5440                 case OP_PADDW_SAT:
5441                 case OP_PSUBB_SAT:
5442                 case OP_PSUBW_SAT:
5443                 case OP_PADDB_SAT_UN:
5444                 case OP_PADDW_SAT_UN:
5445                 case OP_PSUBB_SAT_UN:
5446                 case OP_PSUBW_SAT_UN:
5447                 case OP_PAVGB_UN:
5448                 case OP_PAVGW_UN:
5449                 case OP_PACKW:
5450                 case OP_PACKD:
5451                 case OP_PACKW_UN:
5452                 case OP_PACKD_UN:
5453                 case OP_PMULW_HIGH:
5454                 case OP_PMULW_HIGH_UN: {
5455                         LLVMValueRef args [2];
5456
5457                         args [0] = lhs;
5458                         args [1] = rhs;
5459
5460                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 2, dname);
5461                         break;
5462                 }
5463                 case OP_PCMPEQB:
5464                 case OP_PCMPEQW:
5465                 case OP_PCMPEQD:
5466                 case OP_PCMPEQQ: {
5467                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntEQ, lhs, rhs, ""), LLVMTypeOf (lhs), "");
5468                         break;
5469                 }
5470                 case OP_PCMPGTB: {
5471                         values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildICmp (builder, LLVMIntSGT, lhs, rhs, ""), LLVMTypeOf (lhs), "");
5472                         break;
5473                 }
5474                 case OP_EXTRACT_R8:
5475                 case OP_EXTRACT_I8:
5476                 case OP_EXTRACT_I4:
5477                 case OP_EXTRACT_I2:
5478                 case OP_EXTRACT_U2:
5479                 case OP_EXTRACTX_U2:
5480                 case OP_EXTRACT_I1:
5481                 case OP_EXTRACT_U1: {
5482                         LLVMTypeRef t;
5483                         gboolean zext = FALSE;
5484
5485                         t = simd_op_to_llvm_type (ins->opcode);
5486
5487                         switch (ins->opcode) {
5488                         case OP_EXTRACT_R8:
5489                         case OP_EXTRACT_I8:
5490                         case OP_EXTRACT_I4:
5491                         case OP_EXTRACT_I2:
5492                         case OP_EXTRACT_I1:
5493                                 break;
5494                         case OP_EXTRACT_U2:
5495                         case OP_EXTRACTX_U2:
5496                         case OP_EXTRACT_U1:
5497                                 zext = TRUE;
5498                                 break;
5499                         default:
5500                                 t = LLVMInt32Type ();
5501                                 g_assert_not_reached ();
5502                         }
5503
5504                         lhs = LLVMBuildBitCast (builder, lhs, t, "");
5505                         values [ins->dreg] = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), "");
5506                         if (zext)
5507                                 values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), "");
5508                         break;
5509                 }
5510
5511                 case OP_EXPAND_I1:
5512                 case OP_EXPAND_I2:
5513                 case OP_EXPAND_I4:
5514                 case OP_EXPAND_I8:
5515                 case OP_EXPAND_R4:
5516                 case OP_EXPAND_R8: {
5517                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
5518                         LLVMValueRef mask [16], v;
5519                         int i;
5520
5521                         for (i = 0; i < 16; ++i)
5522                                 mask [i] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
5523
5524                         v = convert (ctx, values [ins->sreg1], LLVMGetElementType (t));
5525
5526                         values [ins->dreg] = LLVMBuildInsertElement (builder, LLVMConstNull (t), v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
5527                         values [ins->dreg] = LLVMBuildShuffleVector (builder, values [ins->dreg], LLVMGetUndef (t), LLVMConstVector (mask, LLVMGetVectorSize (t)), "");
5528                         break;
5529                 }
5530
5531                 case OP_INSERT_I1:
5532                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt8Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
5533                         break;
5534                 case OP_INSERT_I2:
5535                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt16Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
5536                         break;
5537                 case OP_INSERT_I4:
5538                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt32Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
5539                         break;
5540                 case OP_INSERT_I8:
5541                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMInt64Type ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
5542                         break;
5543                 case OP_INSERT_R4:
5544                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMFloatType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
5545                         break;
5546                 case OP_INSERT_R8:
5547                         values [ins->dreg] = LLVMBuildInsertElement (builder, values [ins->sreg1], convert (ctx, values [ins->sreg2], LLVMDoubleType ()), LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE), dname);
5548                         break;
5549
5550                 case OP_CVTDQ2PD:
5551                 case OP_CVTDQ2PS:
5552                 case OP_CVTPD2DQ:
5553                 case OP_CVTPS2DQ:
5554                 case OP_CVTPD2PS:
5555                 case OP_CVTPS2PD:
5556                 case OP_CVTTPD2DQ:
5557                 case OP_CVTTPS2DQ:
5558                 case OP_EXTRACT_MASK:
5559                 case OP_SQRTPS:
5560                 case OP_SQRTPD:
5561                 case OP_RSQRTPS:
5562                 case OP_RCPPS: {
5563                         LLVMValueRef v;
5564
5565                         v = convert (ctx, values [ins->sreg1], simd_op_to_llvm_type (ins->opcode));
5566
5567                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), &v, 1, dname);
5568                         break;
5569                 }
5570
5571                 case OP_COMPPS:
5572                 case OP_COMPPD: {
5573                         LLVMValueRef args [3];
5574
5575                         args [0] = lhs;
5576                         args [1] = rhs;
5577                         args [2] = LLVMConstInt (LLVMInt8Type (), ins->inst_c0, FALSE);
5578
5579                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 3, dname);
5580                         break;
5581                 }
5582
5583                 case OP_ICONV_TO_X:
5584                         /* This is only used for implementing shifts by non-immediate */
5585                         values [ins->dreg] = lhs;
5586                         break;
5587
5588                 case OP_PSHRW:
5589                 case OP_PSHRD:
5590                 case OP_PSHRQ:
5591                 case OP_PSARW:
5592                 case OP_PSARD:
5593                 case OP_PSHLW:
5594                 case OP_PSHLD:
5595                 case OP_PSHLQ: {
5596                         LLVMValueRef args [3];
5597
5598                         args [0] = lhs;
5599                         args [1] = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
5600
5601                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 2, dname);
5602                         break;
5603                 }
5604
5605                 case OP_PSHRW_REG:
5606                 case OP_PSHRD_REG:
5607                 case OP_PSHRQ_REG:
5608                 case OP_PSARW_REG:
5609                 case OP_PSARD_REG:
5610                 case OP_PSHLW_REG:
5611                 case OP_PSHLD_REG:
5612                 case OP_PSHLQ_REG: {
5613                         LLVMValueRef args [3];
5614
5615                         args [0] = lhs;
5616                         args [1] = values [ins->sreg2];
5617
5618                         values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (lmodule, simd_op_to_intrins (ins->opcode)), args, 2, dname);
5619                         break;
5620                 }
5621
5622                 case OP_SHUFPS:
5623                 case OP_SHUFPD:
5624                 case OP_PSHUFLED:
5625                 case OP_PSHUFLEW_LOW:
5626                 case OP_PSHUFLEW_HIGH: {
5627                         int mask [16];
5628                         LLVMValueRef v1 = NULL, v2 = NULL, mask_values [16];
5629                         int i, mask_size = 0;
5630                         int imask = ins->inst_c0;
5631         
5632                         /* Convert the x86 shuffle mask to LLVM's */
5633                         switch (ins->opcode) {
5634                         case OP_SHUFPS:
5635                                 mask_size = 4;
5636                                 mask [0] = ((imask >> 0) & 3);
5637                                 mask [1] = ((imask >> 2) & 3);
5638                                 mask [2] = ((imask >> 4) & 3) + 4;
5639                                 mask [3] = ((imask >> 6) & 3) + 4;
5640                                 v1 = values [ins->sreg1];
5641                                 v2 = values [ins->sreg2];
5642                                 break;
5643                         case OP_SHUFPD:
5644                                 mask_size = 2;
5645                                 mask [0] = ((imask >> 0) & 1);
5646                                 mask [1] = ((imask >> 1) & 1) + 2;
5647                                 v1 = values [ins->sreg1];
5648                                 v2 = values [ins->sreg2];
5649                                 break;
5650                         case OP_PSHUFLEW_LOW:
5651                                 mask_size = 8;
5652                                 mask [0] = ((imask >> 0) & 3);
5653                                 mask [1] = ((imask >> 2) & 3);
5654                                 mask [2] = ((imask >> 4) & 3);
5655                                 mask [3] = ((imask >> 6) & 3);
5656                                 mask [4] = 4 + 0;
5657                                 mask [5] = 4 + 1;
5658                                 mask [6] = 4 + 2;
5659                                 mask [7] = 4 + 3;
5660                                 v1 = values [ins->sreg1];
5661                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
5662                                 break;
5663                         case OP_PSHUFLEW_HIGH:
5664                                 mask_size = 8;
5665                                 mask [0] = 0;
5666                                 mask [1] = 1;
5667                                 mask [2] = 2;
5668                                 mask [3] = 3;
5669                                 mask [4] = 4 + ((imask >> 0) & 3);
5670                                 mask [5] = 4 + ((imask >> 2) & 3);
5671                                 mask [6] = 4 + ((imask >> 4) & 3);
5672                                 mask [7] = 4 + ((imask >> 6) & 3);
5673                                 v1 = values [ins->sreg1];
5674                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
5675                                 break;
5676                         case OP_PSHUFLED:
5677                                 mask_size = 4;
5678                                 mask [0] = ((imask >> 0) & 3);
5679                                 mask [1] = ((imask >> 2) & 3);
5680                                 mask [2] = ((imask >> 4) & 3);
5681                                 mask [3] = ((imask >> 6) & 3);
5682                                 v1 = values [ins->sreg1];
5683                                 v2 = LLVMGetUndef (LLVMTypeOf (v1));
5684                                 break;
5685                         default:
5686                                 g_assert_not_reached ();
5687                         }
5688                         for (i = 0; i < mask_size; ++i)
5689                                 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
5690
5691                         values [ins->dreg] =
5692                                 LLVMBuildShuffleVector (builder, v1, v2,
5693                                                                                 LLVMConstVector (mask_values, mask_size), dname);
5694                         break;
5695                 }
5696
5697                 case OP_UNPACK_LOWB:
5698                 case OP_UNPACK_LOWW:
5699                 case OP_UNPACK_LOWD:
5700                 case OP_UNPACK_LOWQ:
5701                 case OP_UNPACK_LOWPS:
5702                 case OP_UNPACK_LOWPD:
5703                 case OP_UNPACK_HIGHB:
5704                 case OP_UNPACK_HIGHW:
5705                 case OP_UNPACK_HIGHD:
5706                 case OP_UNPACK_HIGHQ:
5707                 case OP_UNPACK_HIGHPS:
5708                 case OP_UNPACK_HIGHPD: {
5709                         int mask [16];
5710                         LLVMValueRef mask_values [16];
5711                         int i, mask_size = 0;
5712                         gboolean low = FALSE;
5713
5714                         switch (ins->opcode) {
5715                         case OP_UNPACK_LOWB:
5716                                 mask_size = 16;
5717                                 low = TRUE;
5718                                 break;
5719                         case OP_UNPACK_LOWW:
5720                                 mask_size = 8;
5721                                 low = TRUE;
5722                                 break;
5723                         case OP_UNPACK_LOWD:
5724                         case OP_UNPACK_LOWPS:
5725                                 mask_size = 4;
5726                                 low = TRUE;
5727                                 break;
5728                         case OP_UNPACK_LOWQ:
5729                         case OP_UNPACK_LOWPD:
5730                                 mask_size = 2;
5731                                 low = TRUE;
5732                                 break;
5733                         case OP_UNPACK_HIGHB:
5734                                 mask_size = 16;
5735                                 break;
5736                         case OP_UNPACK_HIGHW:
5737                                 mask_size = 8;
5738                                 break;
5739                         case OP_UNPACK_HIGHD:
5740                         case OP_UNPACK_HIGHPS:
5741                                 mask_size = 4;
5742                                 break;
5743                         case OP_UNPACK_HIGHQ:
5744                         case OP_UNPACK_HIGHPD:
5745                                 mask_size = 2;
5746                                 break;
5747                         default:
5748                                 g_assert_not_reached ();
5749                         }
5750
5751                         if (low) {
5752                                 for (i = 0; i < (mask_size / 2); ++i) {
5753                                         mask [(i * 2)] = i;
5754                                         mask [(i * 2) + 1] = mask_size + i;
5755                                 }
5756                         } else {
5757                                 for (i = 0; i < (mask_size / 2); ++i) {
5758                                         mask [(i * 2)] = (mask_size / 2) + i;
5759                                         mask [(i * 2) + 1] = mask_size + (mask_size / 2) + i;
5760                                 }
5761                         }
5762
5763                         for (i = 0; i < mask_size; ++i)
5764                                 mask_values [i] = LLVMConstInt (LLVMInt32Type (), mask [i], FALSE);
5765                         
5766                         values [ins->dreg] =
5767                                 LLVMBuildShuffleVector (builder, values [ins->sreg1], values [ins->sreg2],
5768                                                                                 LLVMConstVector (mask_values, mask_size), dname);
5769                         break;
5770                 }
5771
5772                 case OP_DUPPD: {
5773                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
5774                         LLVMValueRef v, val;
5775
5776                         v = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
5777                         val = LLVMConstNull (t);
5778                         val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
5779                         val = LLVMBuildInsertElement (builder, val, v, LLVMConstInt (LLVMInt32Type (), 1, FALSE), dname);
5780
5781                         values [ins->dreg] = val;
5782                         break;
5783                 }
5784                 case OP_DUPPS_LOW:
5785                 case OP_DUPPS_HIGH: {
5786                         LLVMTypeRef t = simd_op_to_llvm_type (ins->opcode);
5787                         LLVMValueRef v1, v2, val;
5788                         
5789
5790                         if (ins->opcode == OP_DUPPS_LOW) {
5791                                 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
5792                                 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
5793                         } else {
5794                                 v1 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
5795                                 v2 = LLVMBuildExtractElement (builder, lhs, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
5796                         }
5797                         val = LLVMConstNull (t);
5798                         val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 0, FALSE), "");
5799                         val = LLVMBuildInsertElement (builder, val, v1, LLVMConstInt (LLVMInt32Type (), 1, FALSE), "");
5800                         val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 2, FALSE), "");
5801                         val = LLVMBuildInsertElement (builder, val, v2, LLVMConstInt (LLVMInt32Type (), 3, FALSE), "");
5802                         
5803                         values [ins->dreg] = val;
5804                         break;
5805                 }
5806
5807 #endif /* SIMD */
5808
5809                 case OP_DUMMY_USE:
5810                         break;
5811
5812                         /*
5813                          * EXCEPTION HANDLING
5814                          */
5815                 case OP_IMPLICIT_EXCEPTION:
5816                         /* This marks a place where an implicit exception can happen */
5817                         if (bb->region != -1)
5818                                 LLVM_FAILURE (ctx, "implicit-exception");
5819                         break;
5820                 case OP_THROW:
5821                 case OP_RETHROW: {
5822                         gboolean rethrow = (ins->opcode == OP_RETHROW);
5823                         if (ctx->llvm_only) {
5824                                 emit_llvmonly_throw (ctx, bb, rethrow, lhs);
5825                                 has_terminator = TRUE;
5826                                 ctx->unreachable [bb->block_num] = TRUE;
5827                         } else {
5828                                 emit_throw (ctx, bb, rethrow, lhs);
5829                                 builder = ctx->builder;
5830                         }
5831                         break;
5832                 }
5833                 case OP_CALL_HANDLER: {
5834                         /* 
5835                          * We don't 'call' handlers, but instead simply branch to them.
5836                          * The code generated by ENDFINALLY will branch back to us.
5837                          */
5838                         LLVMBasicBlockRef noex_bb;
5839                         GSList *bb_list;
5840                         BBInfo *info = &bblocks [ins->inst_target_bb->block_num];
5841
5842                         bb_list = info->call_handler_return_bbs;
5843
5844                         /* 
5845                          * Set the indicator variable for the finally clause.
5846                          */
5847                         lhs = info->finally_ind;
5848                         g_assert (lhs);
5849                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), g_slist_length (bb_list) + 1, FALSE), lhs);
5850                                 
5851                         /* Branch to the finally clause */
5852                         LLVMBuildBr (builder, info->call_handler_target_bb);
5853
5854                         noex_bb = gen_bb (ctx, "CALL_HANDLER_CONT_BB");
5855                         info->call_handler_return_bbs = g_slist_append_mempool (cfg->mempool, info->call_handler_return_bbs, noex_bb);
5856
5857                         builder = ctx->builder = create_builder (ctx);
5858                         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
5859
5860                         bblocks [bb->block_num].end_bblock = noex_bb;
5861                         break;
5862                 }
5863                 case OP_START_HANDLER: {
5864                         break;
5865                 }
5866                 case OP_ENDFINALLY: {
5867                         LLVMBasicBlockRef resume_bb;
5868                         MonoBasicBlock *handler_bb;
5869                         LLVMValueRef val, switch_ins, callee;
5870                         GSList *bb_list;
5871                         BBInfo *info;
5872
5873                         handler_bb = (MonoBasicBlock*)g_hash_table_lookup (ctx->region_to_handler, GUINT_TO_POINTER (mono_get_block_region_notry (cfg, bb->region)));
5874                         g_assert (handler_bb);
5875                         info = &bblocks [handler_bb->block_num];
5876                         lhs = info->finally_ind;
5877                         g_assert (lhs);
5878
5879                         bb_list = info->call_handler_return_bbs;
5880
5881                         resume_bb = gen_bb (ctx, "ENDFINALLY_RESUME_BB");
5882
5883                         /* Load the finally variable */
5884                         val = LLVMBuildLoad (builder, lhs, "");
5885
5886                         /* Reset the variable */
5887                         LLVMBuildStore (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), lhs);
5888
5889                         /* Branch to either resume_bb, or to the bblocks in bb_list */
5890                         switch_ins = LLVMBuildSwitch (builder, val, resume_bb, g_slist_length (bb_list));
5891                         /* 
5892                          * The other targets are added at the end to handle OP_CALL_HANDLER
5893                          * opcodes processed later.
5894                          */
5895                         info->endfinally_switch_ins_list = g_slist_append_mempool (cfg->mempool, info->endfinally_switch_ins_list, switch_ins);
5896
5897                         builder = ctx->builder = create_builder (ctx);
5898                         LLVMPositionBuilderAtEnd (ctx->builder, resume_bb);
5899
5900                         if (ctx->llvm_only) {
5901                                 emit_resume_eh (ctx, bb);
5902                         } else {
5903                                 if (ctx->cfg->compile_aot) {
5904                                         callee = get_callee (ctx, LLVMFunctionType (LLVMVoidType (), NULL, 0, FALSE), MONO_PATCH_INFO_INTERNAL_METHOD, "llvm_resume_unwind_trampoline");
5905                                 } else {
5906                                         callee = LLVMGetNamedFunction (lmodule, "llvm_resume_unwind_trampoline");
5907                                 }
5908                                 LLVMBuildCall (builder, callee, NULL, 0, "");
5909                                 LLVMBuildUnreachable (builder);
5910                         }
5911
5912                         has_terminator = TRUE;
5913                         break;
5914                 }
5915                 default: {
5916                         char reason [128];
5917
5918                         sprintf (reason, "opcode %s", mono_inst_name (ins->opcode));
5919                         LLVM_FAILURE (ctx, reason);
5920                         break;
5921                 }
5922                 }
5923
5924                 /* Convert the value to the type required by phi nodes */
5925                 if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins) && ctx->vreg_types [ins->dreg]) {
5926                         if (!values [ins->dreg])
5927                                 /* vtypes */
5928                                 values [ins->dreg] = addresses [ins->dreg];
5929                         else
5930                                 values [ins->dreg] = convert (ctx, values [ins->dreg], ctx->vreg_types [ins->dreg]);
5931                 }
5932
5933                 /* Add stores for volatile variables */
5934                 if (spec [MONO_INST_DEST] != ' ' && spec [MONO_INST_DEST] != 'v' && !MONO_IS_STORE_MEMBASE (ins))
5935                         emit_volatile_store (ctx, ins->dreg);
5936         }
5937
5938         if (!has_terminator && bb->next_bb && (bb == cfg->bb_entry || bb->in_count > 0)) {
5939                 LLVMBuildBr (builder, get_bb (ctx, bb->next_bb));
5940         }
5941
5942         if (bb == cfg->bb_exit && sig->ret->type == MONO_TYPE_VOID) {
5943                 emit_dbg_loc (ctx, builder, cfg->header->code + cfg->header->code_size - 1);
5944                 LLVMBuildRetVoid (builder);
5945         }
5946
5947         if (bb == cfg->bb_entry)
5948                 ctx->last_alloca = LLVMGetLastInstruction (get_bb (ctx, cfg->bb_entry));
5949
5950         return;
5951
5952  FAILURE:
5953         return;
5954 }
5955
5956 /*
5957  * mono_llvm_check_method_supported:
5958  *
5959  *   Do some quick checks to decide whenever cfg->method can be compiled by LLVM, to avoid
5960  * compiling a method twice.
5961  */
5962 void
5963 mono_llvm_check_method_supported (MonoCompile *cfg)
5964 {
5965         int i, j;
5966
5967         if (cfg->llvm_only)
5968                 return;
5969
5970         if (cfg->method->save_lmf) {
5971                 cfg->exception_message = g_strdup ("lmf");
5972                 cfg->disable_llvm = TRUE;
5973         }
5974         if (cfg->disable_llvm)
5975                 return;
5976
5977         /*
5978          * Nested clauses where one of the clauses is a finally clause is
5979          * not supported, because LLVM can't figure out the control flow,
5980          * probably because we resume exception handling by calling our
5981          * own function instead of using the 'resume' llvm instruction.
5982          */
5983         for (i = 0; i < cfg->header->num_clauses; ++i) {
5984                 for (j = 0; j < cfg->header->num_clauses; ++j) {
5985                         MonoExceptionClause *clause1 = &cfg->header->clauses [i];
5986                         MonoExceptionClause *clause2 = &cfg->header->clauses [j];
5987
5988                         if (i != j && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset &&
5989                                 (clause1->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause2->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
5990                                 cfg->exception_message = g_strdup ("nested clauses");
5991                                 cfg->disable_llvm = TRUE;
5992                                 break;
5993                         }
5994                 }
5995         }
5996         if (cfg->disable_llvm)
5997                 return;
5998
5999         /* FIXME: */
6000         if (cfg->method->dynamic) {
6001                 cfg->exception_message = g_strdup ("dynamic.");
6002                 cfg->disable_llvm = TRUE;
6003         }
6004         if (cfg->disable_llvm)
6005                 return;
6006 }
6007
6008 static LLVMCallInfo*
6009 get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
6010 {
6011         LLVMCallInfo *linfo;
6012         int i;
6013
6014         linfo = mono_arch_get_llvm_call_info (cfg, sig);
6015         for (i = 0; i < sig->param_count; ++i)
6016                 linfo->args [i + sig->hasthis].type = sig->params [i];
6017
6018         return linfo;
6019 }
6020
6021 /*
6022  * mono_llvm_emit_method:
6023  *
6024  *   Emit LLVM IL from the mono IL, and compile it to native code using LLVM.
6025  */
6026 void
6027 mono_llvm_emit_method (MonoCompile *cfg)
6028 {
6029         EmitContext *ctx;
6030         MonoMethodSignature *sig;
6031         MonoBasicBlock *bb;
6032         LLVMTypeRef method_type;
6033         LLVMValueRef method = NULL;
6034         char *method_name;
6035         LLVMValueRef *values;
6036         int i, max_block_num, bb_index;
6037         gboolean last = FALSE;
6038         GPtrArray *phi_values;
6039         LLVMCallInfo *linfo;
6040         GSList *l;
6041         LLVMModuleRef lmodule;
6042         BBInfo *bblocks;
6043         GPtrArray *bblock_list;
6044         MonoMethodHeader *header;
6045         MonoExceptionClause *clause;
6046         char **names;
6047
6048         /* The code below might acquire the loader lock, so use it for global locking */
6049         mono_loader_lock ();
6050
6051         /* Used to communicate with the callbacks */
6052         mono_native_tls_set_value (current_cfg_tls_id, cfg);
6053
6054         ctx = g_new0 (EmitContext, 1);
6055         ctx->cfg = cfg;
6056         ctx->mempool = cfg->mempool;
6057
6058         /*
6059          * This maps vregs to the LLVM instruction defining them
6060          */
6061         values = g_new0 (LLVMValueRef, cfg->next_vreg);
6062         /*
6063          * This maps vregs for volatile variables to the LLVM instruction defining their
6064          * address.
6065          */
6066         ctx->addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
6067         ctx->vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
6068         ctx->vreg_cli_types = g_new0 (MonoType*, cfg->next_vreg);
6069         phi_values = g_ptr_array_sized_new (256);
6070         /* 
6071          * This signals whenever the vreg was defined by a phi node with no input vars
6072          * (i.e. all its input bblocks end with NOT_REACHABLE).
6073          */
6074         ctx->is_dead = g_new0 (gboolean, cfg->next_vreg);
6075         /* Whenever the bblock is unreachable */
6076         ctx->unreachable = g_new0 (gboolean, cfg->max_block_num);
6077         bblock_list = g_ptr_array_sized_new (256);
6078
6079         ctx->values = values;
6080         ctx->region_to_handler = g_hash_table_new (NULL, NULL);
6081         ctx->clause_to_handler = g_hash_table_new (NULL, NULL);
6082         ctx->method_to_callers = g_hash_table_new (NULL, NULL);
6083  
6084         if (cfg->compile_aot) {
6085                 ctx->module = &aot_module;
6086                 method_name = mono_aot_get_method_name (cfg);
6087                 cfg->llvm_method_name = g_strdup (method_name);
6088         } else {
6089                 init_jit_module (cfg->domain);
6090                 ctx->module = (MonoLLVMModule*)domain_jit_info (cfg->domain)->llvm_module;
6091                 method_name = mono_method_full_name (cfg->method, TRUE);
6092         }
6093
6094         lmodule = ctx->lmodule = ctx->module->lmodule;
6095         ctx->llvm_only = ctx->module->llvm_only;
6096
6097         if (cfg->gsharedvt && !cfg->llvm_only)
6098                 LLVM_FAILURE (ctx, "gsharedvt");
6099
6100 #if 1
6101         {
6102                 static int count = 0;
6103                 count ++;
6104
6105                 if (g_getenv ("LLVM_COUNT")) {
6106                         if (count == atoi (g_getenv ("LLVM_COUNT"))) {
6107                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
6108                                 fflush (stdout);
6109                                 last = TRUE;
6110                         }
6111                         if (count > atoi (g_getenv ("LLVM_COUNT")))
6112                                 LLVM_FAILURE (ctx, "");
6113                 }
6114         }
6115 #endif
6116
6117         sig = mono_method_signature (cfg->method);
6118         ctx->sig = sig;
6119
6120         linfo = get_llvm_call_info (cfg, sig);
6121         ctx->linfo = linfo;
6122         CHECK_FAILURE (ctx);
6123
6124         if (cfg->rgctx_var)
6125                 linfo->rgctx_arg = TRUE;
6126         method_type = sig_to_llvm_sig_full (ctx, sig, linfo);
6127         CHECK_FAILURE (ctx);
6128
6129         method = LLVMAddFunction (lmodule, method_name, method_type);
6130         ctx->lmethod = method;
6131
6132         if (!cfg->llvm_only)
6133                 LLVMSetFunctionCallConv (method, LLVMMono1CallConv);
6134         LLVMSetLinkage (method, LLVMPrivateLinkage);
6135
6136         LLVMAddFunctionAttr (method, LLVMUWTable);
6137
6138         if (cfg->compile_aot) {
6139                 LLVMSetLinkage (method, LLVMInternalLinkage);
6140                 if (ctx->module->external_symbols) {
6141                         LLVMSetLinkage (method, LLVMExternalLinkage);
6142                         LLVMSetVisibility (method, LLVMHiddenVisibility);
6143                 }
6144         } else {
6145                 LLVMSetLinkage (method, LLVMPrivateLinkage);
6146         }
6147
6148         if (cfg->method->save_lmf && !cfg->llvm_only)
6149                 LLVM_FAILURE (ctx, "lmf");
6150
6151         if (sig->pinvoke && cfg->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && !cfg->llvm_only)
6152                 LLVM_FAILURE (ctx, "pinvoke signature");
6153
6154         header = cfg->header;
6155         for (i = 0; i < header->num_clauses; ++i) {
6156                 clause = &header->clauses [i];
6157                 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_NONE)
6158                         LLVM_FAILURE (ctx, "non-finally/catch clause.");
6159         }
6160         if (header->num_clauses || (cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING))
6161                 /* We can't handle inlined methods with clauses */
6162                 LLVMAddFunctionAttr (method, LLVMNoInlineAttribute);
6163
6164         if (linfo->rgctx_arg) {
6165                 ctx->rgctx_arg = LLVMGetParam (method, linfo->rgctx_arg_pindex);
6166                 ctx->rgctx_arg_pindex = linfo->rgctx_arg_pindex;
6167                 /*
6168                  * We mark the rgctx parameter with the inreg attribute, which is mapped to
6169                  * MONO_ARCH_RGCTX_REG in the Mono calling convention in llvm, i.e.
6170                  * CC_X86_64_Mono in X86CallingConv.td.
6171                  */
6172                 if (!ctx->llvm_only)
6173                         LLVMAddAttribute (ctx->rgctx_arg, LLVMInRegAttribute);
6174                 LLVMSetValueName (ctx->rgctx_arg, "rgctx");
6175         } else {
6176                 ctx->rgctx_arg_pindex = -1;
6177         }
6178         if (cfg->vret_addr) {
6179                 values [cfg->vret_addr->dreg] = LLVMGetParam (method, linfo->vret_arg_pindex);
6180                 LLVMSetValueName (values [cfg->vret_addr->dreg], "vret");
6181                 if (linfo->ret.storage == LLVMArgVtypeByRef) {
6182                         LLVMAddAttribute (LLVMGetParam (method, linfo->vret_arg_pindex), LLVMStructRetAttribute);
6183                         LLVMAddAttribute (LLVMGetParam (method, linfo->vret_arg_pindex), LLVMNoAliasAttribute);
6184                 }
6185         } else if (linfo->ret.storage == LLVMArgScalarRetAddr) {
6186                 LLVMValueRef param = LLVMGetParam (method, linfo->vret_arg_pindex);
6187                 LLVMSetValueName (param, "vret");
6188         }
6189
6190         if (sig->hasthis) {
6191                 ctx->this_arg_pindex = linfo->this_arg_pindex;
6192                 ctx->this_arg = LLVMGetParam (method, linfo->this_arg_pindex);
6193                 values [cfg->args [0]->dreg] = ctx->this_arg;
6194                 LLVMSetValueName (values [cfg->args [0]->dreg], "this");
6195         }
6196
6197         names = g_new (char *, sig->param_count);
6198         mono_method_get_param_names (cfg->method, (const char **) names);
6199
6200         for (i = 0; i < sig->param_count; ++i) {
6201                 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
6202                 char *name;
6203
6204                 values [cfg->args [i + sig->hasthis]->dreg] = LLVMGetParam (method, ainfo->pindex);
6205                 if (ainfo->storage == LLVMArgScalarByRef) {
6206                         if (names [i] && names [i][0] != '\0')
6207                                 name = g_strdup_printf ("p_arg_%s", names [i]);
6208                         else
6209                                 name = g_strdup_printf ("p_arg_%d", i);
6210                 } else {
6211                         if (names [i] && names [i][0] != '\0')
6212                                 name = g_strdup_printf ("arg_%s", names [i]);
6213                         else
6214                                 name = g_strdup_printf ("arg_%d", i);
6215                 }
6216                 LLVMSetValueName (values [cfg->args [i + sig->hasthis]->dreg], name);
6217                 g_free (name);
6218                 if (ainfo->storage == LLVMArgVtypeByVal)
6219                         LLVMAddAttribute (LLVMGetParam (method, ainfo->pindex), LLVMByValAttribute);
6220
6221                 if (ainfo->storage == LLVMArgVtypeByRef) {
6222                         /* For OP_LDADDR */
6223                         cfg->args [i + sig->hasthis]->opcode = OP_VTARG_ADDR;
6224                 }
6225         }
6226         g_free (names);
6227
6228         if (ctx->module->emit_dwarf && cfg->compile_aot && mono_debug_enabled ()) {
6229                 ctx->minfo = mono_debug_lookup_method (cfg->method);
6230                 ctx->dbg_md = emit_dbg_subprogram (ctx, cfg, method, method_name);
6231         }
6232
6233         max_block_num = 0;
6234         for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
6235                 max_block_num = MAX (max_block_num, bb->block_num);
6236         ctx->bblocks = bblocks = g_new0 (BBInfo, max_block_num + 1);
6237
6238         /* Add branches between non-consecutive bblocks */
6239         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6240                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
6241                         bb->next_bb != bb->last_ins->inst_false_bb) {
6242                         
6243                         MonoInst *inst = (MonoInst*)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6244                         inst->opcode = OP_BR;
6245                         inst->inst_target_bb = bb->last_ins->inst_false_bb;
6246                         mono_bblock_add_inst (bb, inst);
6247                 }
6248         }
6249
6250         /*
6251          * The INDIRECT flag added by OP_LDADDR inhibits optimizations, even if the LDADDR
6252          * was later optimized away, so clear these flags, and add them back for the still
6253          * present OP_LDADDR instructions.
6254          */
6255         for (i = 0; i < cfg->next_vreg; ++i) {
6256                 MonoInst *ins;
6257
6258                 ins = get_vreg_to_inst (cfg, i);
6259                 if (ins && ins != cfg->rgctx_var)
6260                         ins->flags &= ~MONO_INST_INDIRECT;
6261         }
6262
6263         /*
6264          * Make a first pass over the code to precreate PHI nodes/set INDIRECT flags.
6265          */
6266         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6267                 MonoInst *ins;
6268                 LLVMBuilderRef builder;
6269                 char *dname;
6270                 char dname_buf[128];
6271
6272                 builder = create_builder (ctx);
6273
6274                 for (ins = bb->code; ins; ins = ins->next) {
6275                         switch (ins->opcode) {
6276                         case OP_PHI:
6277                         case OP_FPHI:
6278                         case OP_VPHI:
6279                         case OP_XPHI: {
6280                                 LLVMTypeRef phi_type = llvm_type_to_stack_type (cfg, type_to_llvm_type (ctx, &ins->klass->byval_arg));
6281
6282                                 CHECK_FAILURE (ctx);
6283
6284                                 if (ins->opcode == OP_VPHI) {
6285                                         /* Treat valuetype PHI nodes as operating on the address itself */
6286                                         g_assert (ins->klass);
6287                                         phi_type = LLVMPointerType (type_to_llvm_type (ctx, &ins->klass->byval_arg), 0);
6288                                 }
6289
6290                                 /* 
6291                                  * Have to precreate these, as they can be referenced by
6292                                  * earlier instructions.
6293                                  */
6294                                 sprintf (dname_buf, "t%d", ins->dreg);
6295                                 dname = dname_buf;
6296                                 values [ins->dreg] = LLVMBuildPhi (builder, phi_type, dname);
6297
6298                                 if (ins->opcode == OP_VPHI)
6299                                         ctx->addresses [ins->dreg] = values [ins->dreg];
6300
6301                                 g_ptr_array_add (phi_values, values [ins->dreg]);
6302
6303                                 /* 
6304                                  * Set the expected type of the incoming arguments since these have
6305                                  * to have the same type.
6306                                  */
6307                                 for (i = 0; i < ins->inst_phi_args [0]; i++) {
6308                                         int sreg1 = ins->inst_phi_args [i + 1];
6309                                         
6310                                         if (sreg1 != -1)
6311                                                 ctx->vreg_types [sreg1] = phi_type;
6312                                 }
6313                                 break;
6314                                 }
6315                         case OP_LDADDR:
6316                                 ((MonoInst*)ins->inst_p0)->flags |= MONO_INST_INDIRECT;
6317                                 break;
6318                         default:
6319                                 break;
6320                         }
6321                 }
6322         }
6323
6324         /* 
6325          * Create an ordering for bblocks, use the depth first order first, then
6326          * put the exception handling bblocks last.
6327          */
6328         for (bb_index = 0; bb_index < cfg->num_bblocks; ++bb_index) {
6329                 bb = cfg->bblocks [bb_index];
6330                 if (!(bb->region != -1 && !MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))) {
6331                         g_ptr_array_add (bblock_list, bb);
6332                         bblocks [bb->block_num].added = TRUE;
6333                 }
6334         }
6335
6336         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6337                 if (!bblocks [bb->block_num].added)
6338                         g_ptr_array_add (bblock_list, bb);
6339         }
6340
6341         /*
6342          * Second pass: generate code.
6343          */
6344         // Emit entry point
6345         LLVMBuilderRef entry_builder = create_builder (ctx);
6346         LLVMBasicBlockRef entry_bb = get_bb (ctx, cfg->bb_entry);
6347         LLVMPositionBuilderAtEnd (entry_builder, entry_bb);
6348         emit_entry_bb (ctx, entry_builder);
6349
6350         // Make landing pads first
6351         ctx->exc_meta = g_hash_table_new_full (NULL, NULL, NULL, NULL);
6352
6353         if (ctx->llvm_only) {
6354                 size_t group_index = 0;
6355                 while (group_index < cfg->header->num_clauses) {
6356                         int count = 0;
6357                         size_t cursor = group_index;
6358                         while (cursor < cfg->header->num_clauses &&
6359                                    CLAUSE_START (&cfg->header->clauses [cursor]) == CLAUSE_START (&cfg->header->clauses [group_index]) &&
6360                                    CLAUSE_END (&cfg->header->clauses [cursor]) == CLAUSE_END (&cfg->header->clauses [group_index])) {
6361                                 count++;
6362                                 cursor++;
6363                         }
6364
6365                         LLVMBasicBlockRef lpad_bb = emit_landing_pad (ctx, group_index, count);
6366                         intptr_t key = CLAUSE_END (&cfg->header->clauses [group_index]);
6367                         g_hash_table_insert (ctx->exc_meta, (gpointer)key, lpad_bb);
6368
6369                         group_index = cursor;
6370                 }
6371         }
6372
6373         for (bb_index = 0; bb_index < bblock_list->len; ++bb_index) {
6374                 bb = (MonoBasicBlock*)g_ptr_array_index (bblock_list, bb_index);
6375
6376                 // Prune unreachable mono BBs.
6377                 if (!(bb == cfg->bb_entry || bb->in_count > 0))
6378                         continue;
6379
6380                 process_bb (ctx, bb);
6381                 CHECK_FAILURE (ctx);
6382         }
6383         g_hash_table_destroy (ctx->exc_meta);
6384
6385         mono_memory_barrier ();
6386
6387         /* Add incoming phi values */
6388         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6389                 GSList *l, *ins_list;
6390
6391                 ins_list = bblocks [bb->block_num].phi_nodes;
6392
6393                 for (l = ins_list; l; l = l->next) {
6394                         PhiNode *node = (PhiNode*)l->data;
6395                         MonoInst *phi = node->phi;
6396                         int sreg1 = node->sreg;
6397                         LLVMBasicBlockRef in_bb;
6398
6399                         if (sreg1 == -1)
6400                                 continue;
6401
6402                         in_bb = get_end_bb (ctx, node->in_bb);
6403
6404                         if (ctx->unreachable [node->in_bb->block_num])
6405                                 continue;
6406
6407                         if (!values [sreg1])
6408                                 /* Can happen with values in EH clauses */
6409                                 LLVM_FAILURE (ctx, "incoming phi sreg1");
6410
6411                         if (phi->opcode == OP_VPHI) {
6412                                 g_assert (LLVMTypeOf (ctx->addresses [sreg1]) == LLVMTypeOf (values [phi->dreg]));
6413                                 LLVMAddIncoming (values [phi->dreg], &ctx->addresses [sreg1], &in_bb, 1);
6414                         } else {
6415                                 if (LLVMTypeOf (values [sreg1]) != LLVMTypeOf (values [phi->dreg]))
6416                                         // FIXME:
6417                                         LLVM_FAILURE (ctx, "incoming phi arg type mismatch");
6418                                 g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [phi->dreg]));
6419                                 LLVMAddIncoming (values [phi->dreg], &values [sreg1], &in_bb, 1);
6420                         }
6421                 }
6422         }
6423
6424         /* Nullify empty phi instructions */
6425         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6426                 GSList *l, *ins_list;
6427
6428                 ins_list = bblocks [bb->block_num].phi_nodes;
6429
6430                 for (l = ins_list; l; l = l->next) {
6431                         PhiNode *node = (PhiNode*)l->data;
6432                         MonoInst *phi = node->phi;
6433                         LLVMValueRef phi_ins = values [phi->dreg];
6434
6435                         if (!phi_ins)
6436                                 /* Already removed */
6437                                 continue;
6438
6439                         if (LLVMCountIncoming (phi_ins) == 0) {
6440                                 mono_llvm_replace_uses_of (phi_ins, LLVMConstNull (LLVMTypeOf (phi_ins)));
6441                                 LLVMInstructionEraseFromParent (phi_ins);
6442                                 values [phi->dreg] = NULL;
6443                         }
6444                 }
6445         }
6446
6447         /* Create the SWITCH statements for ENDFINALLY instructions */
6448         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6449                 BBInfo *info = &bblocks [bb->block_num];
6450                 GSList *l;
6451                 for (l = info->endfinally_switch_ins_list; l; l = l->next) {
6452                         LLVMValueRef switch_ins = (LLVMValueRef)l->data;
6453                         GSList *bb_list = info->call_handler_return_bbs;
6454
6455                         for (i = 0; i < g_slist_length (bb_list); ++i)
6456                                 LLVMAddCase (switch_ins, LLVMConstInt (LLVMInt32Type (), i + 1, FALSE), g_slist_nth (bb_list, i)->data);
6457                 }
6458         }
6459
6460         /* Initialize the method if needed */
6461         if (cfg->compile_aot && ctx->llvm_only) {
6462                 // FIXME: Add more shared got entries
6463                 ctx->builder = create_builder (ctx);
6464                 LLVMPositionBuilderAtEnd (ctx->builder, ctx->init_bb);
6465
6466                 // FIXME: beforefieldinit
6467                 if (ctx->has_got_access || mono_class_get_cctor (cfg->method->klass)) {
6468                         emit_init_method (ctx);
6469                 } else {
6470                         LLVMBuildBr (ctx->builder, ctx->inited_bb);
6471                 }
6472         }
6473
6474         if (cfg->llvm_only) {
6475                 GHashTableIter iter;
6476                 MonoMethod *method;
6477                 GSList *callers, *l, *l2;
6478
6479                 /*
6480                  * Add the contents of ctx->method_to_callers to module->method_to_callers.
6481                  * We can't do this earlier, as it contains llvm instructions which can be
6482                  * freed if compilation fails.
6483                  * FIXME: Get rid of this when all methods can be llvm compiled.
6484                  */
6485                 g_hash_table_iter_init (&iter, ctx->method_to_callers);
6486                 while (g_hash_table_iter_next (&iter, (void**)&method, (void**)&callers)) {
6487                         for (l = callers; l; l = l->next) {
6488                                 l2 = g_hash_table_lookup (ctx->module->method_to_callers, method);
6489                                 l2 = g_slist_prepend (l2, l->data);
6490                                 g_hash_table_insert (ctx->module->method_to_callers, method, l2);
6491                         }
6492                 }
6493         }
6494
6495         if (cfg->verbose_level > 1)
6496                 mono_llvm_dump_value (method);
6497
6498         if (cfg->compile_aot && !cfg->llvm_only)
6499                 mark_as_used (ctx->module, method);
6500
6501         if (cfg->compile_aot) {
6502                 LLVMValueRef md_args [16];
6503                 LLVMValueRef md_node;
6504                 int method_index;
6505
6506                 method_index = mono_aot_get_method_index (cfg->orig_method);
6507                 md_args [0] = LLVMMDString (method_name, strlen (method_name));
6508                 md_args [1] = LLVMConstInt (LLVMInt32Type (), method_index, FALSE);
6509                 md_node = LLVMMDNode (md_args, 2);
6510                 LLVMAddNamedMetadataOperand (lmodule, "mono.function_indexes", md_node);
6511                 //LLVMSetMetadata (method, md_kind, LLVMMDNode (&md_arg, 1));
6512         }
6513
6514         if (cfg->compile_aot) {
6515                 /* Don't generate native code, keep the LLVM IR */
6516                 if (cfg->verbose_level)
6517                         printf ("%s emitted as %s\n", mono_method_full_name (cfg->method, TRUE), method_name);
6518
6519                 int err = LLVMVerifyFunction(method,   LLVMPrintMessageAction);
6520                 g_assert (err == 0);
6521         } else {
6522                 //LLVMVerifyFunction(method, 0);
6523                 mono_llvm_optimize_method (ctx->module->mono_ee, method);
6524
6525                 if (cfg->verbose_level > 1)
6526                         mono_llvm_dump_value (method);
6527
6528                 cfg->native_code = LLVMGetPointerToGlobal (ctx->module->ee, method);
6529
6530                 /* Set by emit_cb */
6531                 g_assert (cfg->code_len);
6532
6533                 /* FIXME: Free the LLVM IL for the function */
6534         }
6535
6536         if (ctx->module->method_to_lmethod)
6537                 g_hash_table_insert (ctx->module->method_to_lmethod, cfg->method, method);
6538         if (ctx->module->idx_to_lmethod)
6539                 g_hash_table_insert (ctx->module->idx_to_lmethod, GINT_TO_POINTER (cfg->method_index), method);
6540
6541         if (ctx->llvm_only && cfg->orig_method->klass->valuetype && !(cfg->orig_method->flags & METHOD_ATTRIBUTE_STATIC))
6542                 emit_unbox_tramp (ctx, method_name, method_type, method, cfg->method_index);
6543
6544         goto CLEANUP;
6545
6546  FAILURE:
6547
6548         if (method) {
6549                 /* Need to add unused phi nodes as they can be referenced by other values */
6550                 LLVMBasicBlockRef phi_bb = LLVMAppendBasicBlock (method, "PHI_BB");
6551                 LLVMBuilderRef builder;
6552
6553                 builder = create_builder (ctx);
6554                 LLVMPositionBuilderAtEnd (builder, phi_bb);
6555
6556                 for (i = 0; i < phi_values->len; ++i) {
6557                         LLVMValueRef v = g_ptr_array_index (phi_values, i);
6558                         if (LLVMGetInstructionParent (v) == NULL)
6559                                 LLVMInsertIntoBuilder (builder, v);
6560                 }
6561                 
6562                 LLVMDeleteFunction (method);
6563         }
6564
6565  CLEANUP:
6566         g_free (values);
6567         g_free (ctx->addresses);
6568         g_free (ctx->vreg_types);
6569         g_free (ctx->vreg_cli_types);
6570         g_free (ctx->is_dead);
6571         g_free (ctx->unreachable);
6572         g_ptr_array_free (phi_values, TRUE);
6573         g_free (ctx->bblocks);
6574         g_hash_table_destroy (ctx->region_to_handler);
6575         g_hash_table_destroy (ctx->clause_to_handler);
6576         g_free (method_name);
6577         g_ptr_array_free (bblock_list, TRUE);
6578
6579         for (l = ctx->builders; l; l = l->next) {
6580                 LLVMBuilderRef builder = l->data;
6581                 LLVMDisposeBuilder (builder);
6582         }
6583
6584         g_free (ctx);
6585
6586         mono_native_tls_set_value (current_cfg_tls_id, NULL);
6587
6588         mono_loader_unlock ();
6589 }
6590
6591 /*
6592  * mono_llvm_emit_call:
6593  *
6594  *   Same as mono_arch_emit_call () for LLVM.
6595  */
6596 void
6597 mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
6598 {
6599         MonoInst *in;
6600         MonoMethodSignature *sig;
6601         int i, n, stack_size;
6602         LLVMArgInfo *ainfo;
6603
6604         stack_size = 0;
6605
6606         sig = call->signature;
6607         n = sig->param_count + sig->hasthis;
6608
6609         call->cinfo = get_llvm_call_info (cfg, sig);
6610
6611         if (cfg->disable_llvm)
6612                 return;
6613
6614         if (sig->call_convention == MONO_CALL_VARARG) {
6615                 cfg->exception_message = g_strdup ("varargs");
6616                 cfg->disable_llvm = TRUE;
6617         }
6618
6619         for (i = 0; i < n; ++i) {
6620                 MonoInst *ins;
6621
6622                 ainfo = call->cinfo->args + i;
6623
6624                 in = call->args [i];
6625                         
6626                 /* Simply remember the arguments */
6627                 switch (ainfo->storage) {
6628                 case LLVMArgNormal: {
6629                         MonoType *t = (sig->hasthis && i == 0) ? &mono_get_intptr_class ()->byval_arg : ainfo->type;
6630                         int opcode;
6631
6632                         opcode = mono_type_to_regmove (cfg, t);
6633                         if (opcode == OP_FMOVE) {
6634                                 MONO_INST_NEW (cfg, ins, OP_FMOVE);
6635                                 ins->dreg = mono_alloc_freg (cfg);
6636                         } else if (opcode == OP_LMOVE) {
6637                                 MONO_INST_NEW (cfg, ins, OP_LMOVE);
6638                                 ins->dreg = mono_alloc_lreg (cfg);
6639                         } else if (opcode == OP_RMOVE) {
6640                                 MONO_INST_NEW (cfg, ins, OP_RMOVE);
6641                                 ins->dreg = mono_alloc_freg (cfg);
6642                         } else {
6643                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
6644                                 ins->dreg = mono_alloc_ireg (cfg);
6645                         }
6646                         ins->sreg1 = in->dreg;
6647                         break;
6648                 }
6649                 case LLVMArgVtypeByVal:
6650                 case LLVMArgVtypeByRef:
6651                 case LLVMArgVtypeInReg:
6652                 case LLVMArgVtypeAsScalar:
6653                 case LLVMArgScalarByRef:
6654                 case LLVMArgAsIArgs:
6655                 case LLVMArgAsFpArgs:
6656                         MONO_INST_NEW (cfg, ins, OP_LLVM_OUTARG_VT);
6657                         ins->dreg = mono_alloc_ireg (cfg);
6658                         ins->sreg1 = in->dreg;
6659                         ins->inst_p0 = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMArgInfo));
6660                         memcpy (ins->inst_p0, ainfo, sizeof (LLVMArgInfo));
6661                         ins->inst_vtype = ainfo->type;
6662                         ins->klass = mono_class_from_mono_type (ainfo->type);
6663                         break;
6664                 default:
6665                         cfg->exception_message = g_strdup ("ainfo->storage");
6666                         cfg->disable_llvm = TRUE;
6667                         return;
6668                 }
6669
6670                 if (!cfg->disable_llvm) {
6671                         MONO_ADD_INS (cfg->cbb, ins);
6672                         mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
6673                 }
6674         }
6675 }
6676
6677 static unsigned char*
6678 alloc_cb (LLVMValueRef function, int size)
6679 {
6680         MonoCompile *cfg;
6681
6682         cfg = mono_native_tls_get_value (current_cfg_tls_id);
6683
6684         if (cfg) {
6685                 // FIXME: dynamic
6686                 return mono_domain_code_reserve (cfg->domain, size);
6687         } else {
6688                 return mono_domain_code_reserve (mono_domain_get (), size);
6689         }
6690 }
6691
6692 static void
6693 emitted_cb (LLVMValueRef function, void *start, void *end)
6694 {
6695         MonoCompile *cfg;
6696
6697         cfg = mono_native_tls_get_value (current_cfg_tls_id);
6698         g_assert (cfg);
6699         cfg->code_len = (guint8*)end - (guint8*)start;
6700 }
6701
6702 static void
6703 exception_cb (void *data)
6704 {
6705         MonoCompile *cfg;
6706         MonoJitExceptionInfo *ei;
6707         guint32 ei_len, i, j, nested_len, nindex;
6708         gpointer *type_info;
6709         int this_reg, this_offset;
6710
6711         cfg = mono_native_tls_get_value (current_cfg_tls_id);
6712         g_assert (cfg);
6713
6714         /*
6715          * data points to a DWARF FDE structure, convert it to our unwind format and
6716          * save it.
6717          * An alternative would be to save it directly, and modify our unwinder to work
6718          * with it.
6719          */
6720         cfg->encoded_unwind_ops = mono_unwind_decode_fde ((guint8*)data, &cfg->encoded_unwind_ops_len, NULL, &ei, &ei_len, &type_info, &this_reg, &this_offset);
6721         if (cfg->verbose_level > 1)
6722                 mono_print_unwind_info (cfg->encoded_unwind_ops, cfg->encoded_unwind_ops_len);
6723
6724         /* Count nested clauses */
6725         nested_len = 0;
6726         for (i = 0; i < ei_len; ++i) {
6727                 gint32 cindex1 = *(gint32*)type_info [i];
6728                 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
6729
6730                 for (j = 0; j < cfg->header->num_clauses; ++j) {
6731                         int cindex2 = j;
6732                         MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
6733
6734                         if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
6735                                 nested_len ++;
6736                         }
6737                 }
6738         }
6739
6740         cfg->llvm_ex_info = mono_mempool_alloc0 (cfg->mempool, (ei_len + nested_len) * sizeof (MonoJitExceptionInfo));
6741         cfg->llvm_ex_info_len = ei_len + nested_len;
6742         memcpy (cfg->llvm_ex_info, ei, ei_len * sizeof (MonoJitExceptionInfo));
6743         /* Fill the rest of the information from the type info */
6744         for (i = 0; i < ei_len; ++i) {
6745                 gint32 clause_index = *(gint32*)type_info [i];
6746                 MonoExceptionClause *clause = &cfg->header->clauses [clause_index];
6747
6748                 cfg->llvm_ex_info [i].flags = clause->flags;
6749                 cfg->llvm_ex_info [i].data.catch_class = clause->data.catch_class;
6750                 cfg->llvm_ex_info [i].clause_index = clause_index;
6751         }
6752
6753         /*
6754          * For nested clauses, the LLVM produced exception info associates the try interval with
6755          * the innermost handler, while mono expects it to be associated with all nesting clauses.
6756          * So add new clauses which use the IL info (catch class etc.) from the nesting clause,
6757          * and everything else from the nested clause.
6758          */
6759         nindex = ei_len;
6760         for (i = 0; i < ei_len; ++i) {
6761                 gint32 cindex1 = *(gint32*)type_info [i];
6762                 MonoExceptionClause *clause1 = &cfg->header->clauses [cindex1];
6763
6764                 for (j = 0; j < cfg->header->num_clauses; ++j) {
6765                         int cindex2 = j;
6766                         MonoExceptionClause *clause2 = &cfg->header->clauses [cindex2];
6767                         MonoJitExceptionInfo *nesting_ei, *nested_ei;
6768
6769                         if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset) {
6770                                 /* clause1 is the nested clause */
6771                                 nested_ei = &cfg->llvm_ex_info [i];
6772                                 nesting_ei = &cfg->llvm_ex_info [nindex];
6773                                 nindex ++;
6774
6775                                 memcpy (nesting_ei, nested_ei, sizeof (MonoJitExceptionInfo));
6776
6777                                 nesting_ei->flags = clause2->flags;
6778                                 nesting_ei->data.catch_class = clause2->data.catch_class;
6779                                 nesting_ei->clause_index = cindex2;
6780                         }
6781                 }
6782         }
6783         g_assert (nindex == ei_len + nested_len);
6784         cfg->llvm_this_reg = this_reg;
6785         cfg->llvm_this_offset = this_offset;
6786
6787         /* type_info [i] is cfg mempool allocated, no need to free it */
6788
6789         g_free (ei);
6790         g_free (type_info);
6791 }
6792
6793 static char*
6794 dlsym_cb (const char *name, void **symbol)
6795 {
6796         MonoDl *current;
6797         char *err;
6798
6799         err = NULL;
6800         if (!strcmp (name, "__bzero")) {
6801                 *symbol = (void*)bzero;
6802         } else {
6803                 current = mono_dl_open (NULL, 0, NULL);
6804                 g_assert (current);
6805
6806                 err = mono_dl_symbol (current, name, symbol);
6807
6808                 mono_dl_close (current);
6809         }
6810 #ifdef MONO_ARCH_HAVE_CREATE_LLVM_NATIVE_THUNK
6811         *symbol = (char*)mono_arch_create_llvm_native_thunk (mono_domain_get (), (guint8*)(*symbol));
6812 #endif
6813         return err;
6814 }
6815
6816 static inline void
6817 AddFunc (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef *param_types, int nparams)
6818 {
6819         LLVMAddFunction (module, name, LLVMFunctionType (ret_type, param_types, nparams, FALSE));
6820 }
6821
6822 static inline void
6823 AddFunc2 (LLVMModuleRef module, const char *name, LLVMTypeRef ret_type, LLVMTypeRef param_type1, LLVMTypeRef param_type2)
6824 {
6825         LLVMTypeRef param_types [4];
6826
6827         param_types [0] = param_type1;
6828         param_types [1] = param_type2;
6829
6830         AddFunc (module, name, ret_type, param_types, 2);
6831 }
6832
6833 static void
6834 add_intrinsics (LLVMModuleRef module)
6835 {
6836         /* Emit declarations of instrinsics */
6837         /*
6838          * It would be nicer to emit only the intrinsics actually used, but LLVM's Module
6839          * type doesn't seem to do any locking.
6840          */
6841         {
6842                 LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
6843
6844                 memset_param_count = 5;
6845                 memset_func_name = "llvm.memset.p0i8.i32";
6846
6847                 AddFunc (module, memset_func_name, LLVMVoidType (), params, memset_param_count);
6848         }
6849
6850         {
6851                 LLVMTypeRef params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type (), LLVMInt1Type () };
6852
6853                 memcpy_param_count = 5;
6854                 memcpy_func_name = "llvm.memcpy.p0i8.p0i8.i32";
6855
6856                 AddFunc (module, memcpy_func_name, LLVMVoidType (), params, memcpy_param_count);
6857         }
6858
6859         {
6860                 LLVMTypeRef params [] = { LLVMDoubleType () };
6861
6862                 AddFunc (module, "llvm.sin.f64", LLVMDoubleType (), params, 1);
6863                 AddFunc (module, "llvm.cos.f64", LLVMDoubleType (), params, 1);
6864                 AddFunc (module, "llvm.sqrt.f64", LLVMDoubleType (), params, 1);
6865
6866                 /* This isn't an intrinsic, instead llvm seems to special case it by name */
6867                 AddFunc (module, "fabs", LLVMDoubleType (), params, 1);
6868         }
6869
6870         {
6871                 LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
6872                 LLVMTypeRef params [] = { LLVMInt32Type (), LLVMInt32Type () };
6873                 LLVMTypeRef ret_type = LLVMStructType (ovf_res_i32, 2, FALSE);
6874
6875                 AddFunc (module, "llvm.sadd.with.overflow.i32", ret_type, params, 2);
6876                 AddFunc (module, "llvm.sadd.with.overflow.i32", ret_type, params, 2);
6877                 AddFunc (module, "llvm.uadd.with.overflow.i32", ret_type, params, 2);
6878                 AddFunc (module, "llvm.ssub.with.overflow.i32", ret_type, params, 2);
6879                 AddFunc (module, "llvm.usub.with.overflow.i32", ret_type, params, 2);
6880                 AddFunc (module, "llvm.smul.with.overflow.i32", ret_type, params, 2);
6881                 AddFunc (module, "llvm.umul.with.overflow.i32", ret_type, params, 2);
6882         }
6883
6884         {
6885                 LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
6886                 LLVMTypeRef params [] = { LLVMInt64Type (), LLVMInt64Type () };
6887                 LLVMTypeRef ret_type = LLVMStructType (ovf_res_i64, 2, FALSE);
6888
6889                 AddFunc (module, "llvm.sadd.with.overflow.i64", ret_type, params, 2);
6890                 AddFunc (module, "llvm.uadd.with.overflow.i64", ret_type, params, 2);
6891                 AddFunc (module, "llvm.ssub.with.overflow.i64", ret_type, params, 2);
6892                 AddFunc (module, "llvm.usub.with.overflow.i64", ret_type, params, 2);
6893                 AddFunc (module, "llvm.smul.with.overflow.i64", ret_type, params, 2);
6894                 AddFunc (module, "llvm.umul.with.overflow.i64", ret_type, params, 2);
6895         }
6896
6897         AddFunc2 (module, "llvm.expect.i8", LLVMInt8Type (), LLVMInt8Type (), LLVMInt8Type ());
6898
6899         /* EH intrinsics */
6900         {
6901                 AddFunc (module, "mono_personality", LLVMVoidType (), NULL, 0);
6902
6903                 AddFunc (module, "llvm_resume_unwind_trampoline", LLVMVoidType (), NULL, 0);
6904         }
6905
6906         /* SSE intrinsics */
6907 #if defined(TARGET_X86) || defined(TARGET_AMD64)
6908         {
6909                 LLVMTypeRef ret_type, arg_types [16];
6910
6911                 /* Binary ops */
6912                 ret_type = type_to_simd_type (MONO_TYPE_I4);
6913                 arg_types [0] = ret_type;
6914                 arg_types [1] = ret_type;
6915                 AddFunc (module, "llvm.x86.sse41.pminud", ret_type, arg_types, 2);
6916                 AddFunc (module, "llvm.x86.sse41.pmaxud", ret_type, arg_types, 2);
6917
6918                 ret_type = type_to_simd_type (MONO_TYPE_I2);
6919                 arg_types [0] = ret_type;
6920                 arg_types [1] = ret_type;
6921                 AddFunc (module, "llvm.x86.sse41.pminuw", ret_type, arg_types, 2);
6922                 AddFunc (module, "llvm.x86.sse2.pmins.w", ret_type, arg_types, 2);
6923                 AddFunc (module, "llvm.x86.sse41.pmaxuw", ret_type, arg_types, 2);
6924                 AddFunc (module, "llvm.x86.sse2.padds.w", ret_type, arg_types, 2);
6925                 AddFunc (module, "llvm.x86.sse2.psubs.w", ret_type, arg_types, 2);
6926                 AddFunc (module, "llvm.x86.sse2.paddus.w", ret_type, arg_types, 2);
6927                 AddFunc (module, "llvm.x86.sse2.psubus.w", ret_type, arg_types, 2);
6928                 AddFunc (module, "llvm.x86.sse2.pavg.w", ret_type, arg_types, 2);
6929                 AddFunc (module, "llvm.x86.sse2.pmulh.w", ret_type, arg_types, 2);
6930                 AddFunc (module, "llvm.x86.sse2.pmulhu.w", ret_type, arg_types, 2);
6931
6932                 ret_type = type_to_simd_type (MONO_TYPE_I1);
6933                 arg_types [0] = ret_type;
6934                 arg_types [1] = ret_type;
6935                 AddFunc (module, "llvm.x86.sse2.pminu.b", ret_type, arg_types, 2);
6936                 AddFunc (module, "llvm.x86.sse2.pmaxu.b", ret_type, arg_types, 2);
6937                 AddFunc (module, "llvm.x86.sse2.padds.b", ret_type, arg_types, 2);
6938                 AddFunc (module, "llvm.x86.sse2.psubs.b", ret_type, arg_types, 2);
6939                 AddFunc (module, "llvm.x86.sse2.paddus.b", ret_type, arg_types, 2);
6940                 AddFunc (module, "llvm.x86.sse2.psubus.b", ret_type, arg_types, 2);
6941                 AddFunc (module, "llvm.x86.sse2.pavg.b", ret_type, arg_types, 2);
6942
6943                 ret_type = type_to_simd_type (MONO_TYPE_R8);
6944                 arg_types [0] = ret_type;
6945                 arg_types [1] = ret_type;
6946                 AddFunc (module, "llvm.x86.sse2.min.pd", ret_type, arg_types, 2);
6947                 AddFunc (module, "llvm.x86.sse2.max.pd", ret_type, arg_types, 2);
6948                 AddFunc (module, "llvm.x86.sse3.hadd.pd", ret_type, arg_types, 2);
6949                 AddFunc (module, "llvm.x86.sse3.hsub.pd", ret_type, arg_types, 2);
6950                 AddFunc (module, "llvm.x86.sse3.addsub.pd", ret_type, arg_types, 2);
6951
6952                 ret_type = type_to_simd_type (MONO_TYPE_R4);
6953                 arg_types [0] = ret_type;
6954                 arg_types [1] = ret_type;
6955                 AddFunc (module, "llvm.x86.sse.min.ps", ret_type, arg_types, 2);
6956                 AddFunc (module, "llvm.x86.sse.max.ps", ret_type, arg_types, 2);
6957                 AddFunc (module, "llvm.x86.sse3.hadd.ps", ret_type, arg_types, 2);
6958                 AddFunc (module, "llvm.x86.sse3.hsub.ps", ret_type, arg_types, 2);
6959                 AddFunc (module, "llvm.x86.sse3.addsub.ps", ret_type, arg_types, 2);
6960
6961                 /* pack */
6962                 ret_type = type_to_simd_type (MONO_TYPE_I1);
6963                 arg_types [0] = type_to_simd_type (MONO_TYPE_I2);
6964                 arg_types [1] = type_to_simd_type (MONO_TYPE_I2);
6965                 AddFunc (module, "llvm.x86.sse2.packsswb.128", ret_type, arg_types, 2);
6966                 AddFunc (module, "llvm.x86.sse2.packuswb.128", ret_type, arg_types, 2);
6967                 ret_type = type_to_simd_type (MONO_TYPE_I2);
6968                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
6969                 arg_types [1] = type_to_simd_type (MONO_TYPE_I4);
6970                 AddFunc (module, "llvm.x86.sse2.packssdw.128", ret_type, arg_types, 2);
6971                 AddFunc (module, "llvm.x86.sse41.packusdw", ret_type, arg_types, 2);
6972
6973                 /* cmp pd/ps */
6974                 ret_type = type_to_simd_type (MONO_TYPE_R8);
6975                 arg_types [0] = ret_type;
6976                 arg_types [1] = ret_type;
6977                 arg_types [2] = LLVMInt8Type ();
6978                 AddFunc (module, "llvm.x86.sse2.cmp.pd", ret_type, arg_types, 3);
6979                 ret_type = type_to_simd_type (MONO_TYPE_R4);
6980                 arg_types [0] = ret_type;
6981                 arg_types [1] = ret_type;
6982                 arg_types [2] = LLVMInt8Type ();
6983                 AddFunc (module, "llvm.x86.sse.cmp.ps", ret_type, arg_types, 3);
6984
6985                 /* Conversion ops */
6986                 ret_type = type_to_simd_type (MONO_TYPE_R8);
6987                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
6988                 AddFunc (module, "llvm.x86.sse2.cvtdq2pd", ret_type, arg_types, 1);
6989                 ret_type = type_to_simd_type (MONO_TYPE_R4);
6990                 arg_types [0] = type_to_simd_type (MONO_TYPE_I4);
6991                 AddFunc (module, "llvm.x86.sse2.cvtdq2ps", ret_type, arg_types, 1);
6992                 ret_type = type_to_simd_type (MONO_TYPE_I4);
6993                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
6994                 AddFunc (module, "llvm.x86.sse2.cvtpd2dq", ret_type, arg_types, 1);
6995                 ret_type = type_to_simd_type (MONO_TYPE_I4);
6996                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
6997                 AddFunc (module, "llvm.x86.sse2.cvtps2dq", ret_type, arg_types, 1);
6998                 ret_type = type_to_simd_type (MONO_TYPE_R4);
6999                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
7000                 AddFunc (module, "llvm.x86.sse2.cvtpd2ps", ret_type, arg_types, 1);
7001                 ret_type = type_to_simd_type (MONO_TYPE_R8);
7002                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
7003                 AddFunc (module, "llvm.x86.sse2.cvtps2pd", ret_type, arg_types, 1);
7004
7005                 ret_type = type_to_simd_type (MONO_TYPE_I4);
7006                 arg_types [0] = type_to_simd_type (MONO_TYPE_R8);
7007                 AddFunc (module, "llvm.x86.sse2.cvttpd2dq", ret_type, arg_types, 1);
7008                 ret_type = type_to_simd_type (MONO_TYPE_I4);
7009                 arg_types [0] = type_to_simd_type (MONO_TYPE_R4);
7010                 AddFunc (module, "llvm.x86.sse2.cvttps2dq", ret_type, arg_types, 1);
7011
7012                 /* Unary ops */
7013                 ret_type = type_to_simd_type (MONO_TYPE_R8);
7014                 arg_types [0] = ret_type;
7015                 AddFunc (module, "llvm.x86.sse2.sqrt.pd", ret_type, arg_types, 1);
7016                 ret_type = type_to_simd_type (MONO_TYPE_R4);
7017                 arg_types [0] = ret_type;
7018                 AddFunc (module, "llvm.x86.sse.sqrt.ps", ret_type, arg_types, 1);
7019                 ret_type = type_to_simd_type (MONO_TYPE_R4);
7020                 arg_types [0] = ret_type;
7021                 AddFunc (module, "llvm.x86.sse.rsqrt.ps", ret_type, arg_types, 1);
7022                 ret_type = type_to_simd_type (MONO_TYPE_R4);
7023                 arg_types [0] = ret_type;
7024                 AddFunc (module, "llvm.x86.sse.rcp.ps", ret_type, arg_types, 1);
7025
7026                 /* shifts */
7027                 ret_type = type_to_simd_type (MONO_TYPE_I2);
7028                 arg_types [0] = ret_type;
7029                 arg_types [1] = LLVMInt32Type ();
7030                 AddFunc (module, "llvm.x86.sse2.psrli.w", ret_type, arg_types, 2);
7031                 AddFunc (module, "llvm.x86.sse2.psrai.w", ret_type, arg_types, 2);
7032                 AddFunc (module, "llvm.x86.sse2.pslli.w", ret_type, arg_types, 2);
7033                 ret_type = type_to_simd_type (MONO_TYPE_I4);
7034                 arg_types [0] = ret_type;
7035                 arg_types [1] = LLVMInt32Type ();
7036                 AddFunc (module, "llvm.x86.sse2.psrli.d", ret_type, arg_types, 2);
7037                 AddFunc (module, "llvm.x86.sse2.psrai.d", ret_type, arg_types, 2);
7038                 AddFunc (module, "llvm.x86.sse2.pslli.d", ret_type, arg_types, 2);
7039                 ret_type = type_to_simd_type (MONO_TYPE_I8);
7040                 arg_types [0] = ret_type;
7041                 arg_types [1] = LLVMInt32Type ();
7042                 AddFunc (module, "llvm.x86.sse2.psrli.q", ret_type, arg_types, 2);
7043                 AddFunc (module, "llvm.x86.sse2.pslli.q", ret_type, arg_types, 2);
7044
7045                 /* pmovmskb */
7046                 ret_type = LLVMInt32Type ();
7047                 arg_types [0] = type_to_simd_type (MONO_TYPE_I1);
7048                 AddFunc (module, "llvm.x86.sse2.pmovmskb.128", ret_type, arg_types, 1);
7049         }
7050
7051         AddFunc (module, "llvm.x86.sse2.pause", LLVMVoidType (), NULL, 0);
7052 #endif
7053
7054         /* Load/Store intrinsics */
7055         {
7056                 LLVMTypeRef arg_types [5];
7057                 int i;
7058                 char name [128];
7059
7060                 for (i = 1; i <= 8; i *= 2) {
7061                         arg_types [0] = LLVMPointerType (LLVMIntType (i * 8), 0);
7062                         arg_types [1] = LLVMInt32Type ();
7063                         arg_types [2] = LLVMInt1Type ();
7064                         arg_types [3] = LLVMInt32Type ();
7065                         sprintf (name, "llvm.mono.load.i%d.p0i%d", i * 8, i * 8);
7066                         AddFunc (module, name, LLVMIntType (i * 8), arg_types, 4);
7067
7068                         arg_types [0] = LLVMIntType (i * 8);
7069                         arg_types [1] = LLVMPointerType (LLVMIntType (i * 8), 0);
7070                         arg_types [2] = LLVMInt32Type ();
7071                         arg_types [3] = LLVMInt1Type ();
7072                         arg_types [4] = LLVMInt32Type ();
7073                         sprintf (name, "llvm.mono.store.i%d.p0i%d", i * 8, i * 8);
7074                         AddFunc (module, name, LLVMVoidType (), arg_types, 5);
7075                 }
7076         }
7077 }
7078
7079 static void
7080 add_types (MonoLLVMModule *module)
7081 {
7082         module->ptr_type = LLVMPointerType (sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type (), 0);
7083 }
7084
7085 void
7086 mono_llvm_init (void)
7087 {
7088         mono_native_tls_alloc (&current_cfg_tls_id, NULL);
7089 }
7090
7091 static void
7092 init_jit_module (MonoDomain *domain)
7093 {
7094         MonoJitICallInfo *info;
7095         MonoJitDomainInfo *dinfo;
7096         MonoLLVMModule *module;
7097         char *name;
7098
7099         dinfo = domain_jit_info (domain);
7100         if (dinfo->llvm_module)
7101                 return;
7102
7103         mono_loader_lock ();
7104
7105         if (dinfo->llvm_module) {
7106                 mono_loader_unlock ();
7107                 return;
7108         }
7109
7110         module = g_new0 (MonoLLVMModule, 1);
7111
7112         name = g_strdup_printf ("mono-%s", domain->friendly_name);
7113         module->lmodule = LLVMModuleCreateWithName (name);
7114         module->context = LLVMGetGlobalContext ();
7115
7116         module->mono_ee = mono_llvm_create_ee (LLVMCreateModuleProviderForExistingModule (module->lmodule), alloc_cb, emitted_cb, exception_cb, dlsym_cb, &module->ee);
7117
7118         add_intrinsics (module->lmodule);
7119         add_types (module);
7120
7121         module->llvm_types = g_hash_table_new (NULL, NULL);
7122
7123         info = mono_find_jit_icall_by_name ("llvm_resume_unwind_trampoline");
7124         g_assert (info);
7125         LLVMAddGlobalMapping (module->ee, LLVMGetNamedFunction (module->lmodule, "llvm_resume_unwind_trampoline"), (void*)info->func);
7126
7127         mono_memory_barrier ();
7128
7129         dinfo->llvm_module = module;
7130
7131         mono_loader_unlock ();
7132 }
7133
7134 void
7135 mono_llvm_cleanup (void)
7136 {
7137         MonoLLVMModule *module = &aot_module;
7138
7139         if (module->lmodule)
7140                 LLVMDisposeModule (module->lmodule);
7141
7142         if (module->context)
7143                 LLVMContextDispose (module->context);
7144 }
7145
7146 void
7147 mono_llvm_free_domain_info (MonoDomain *domain)
7148 {
7149         MonoJitDomainInfo *info = domain_jit_info (domain);
7150         MonoLLVMModule *module = info->llvm_module;
7151         int i;
7152
7153         if (!module)
7154                 return;
7155
7156         if (module->llvm_types)
7157                 g_hash_table_destroy (module->llvm_types);
7158
7159         mono_llvm_dispose_ee (module->mono_ee);
7160
7161         if (module->bb_names) {
7162                 for (i = 0; i < module->bb_names_len; ++i)
7163                         g_free (module->bb_names [i]);
7164                 g_free (module->bb_names);
7165         }
7166         //LLVMDisposeModule (module->module);
7167
7168         g_free (module);
7169
7170         info->llvm_module = NULL;
7171 }
7172
7173 void
7174 mono_llvm_create_aot_module (MonoAssembly *assembly, const char *global_prefix, gboolean emit_dwarf, gboolean static_link, gboolean llvm_only)
7175 {
7176         MonoLLVMModule *module = &aot_module;
7177
7178         /* Delete previous module */
7179         if (module->plt_entries)
7180                 g_hash_table_destroy (module->plt_entries);
7181         if (module->lmodule)
7182                 LLVMDisposeModule (module->lmodule);
7183
7184         memset (module, 0, sizeof (aot_module));
7185
7186         module->lmodule = LLVMModuleCreateWithName ("aot");
7187         module->assembly = assembly;
7188         module->global_prefix = g_strdup (global_prefix);
7189         module->got_symbol = g_strdup_printf ("%s_llvm_got", global_prefix);
7190         module->eh_frame_symbol = g_strdup_printf ("%s_eh_frame", global_prefix);
7191         module->get_method_symbol = g_strdup_printf ("%s_get_method", global_prefix);
7192         module->get_unbox_tramp_symbol = g_strdup_printf ("%s_get_unbox_tramp", global_prefix);
7193         module->external_symbols = TRUE;
7194         module->emit_dwarf = emit_dwarf;
7195         module->static_link = static_link;
7196         module->llvm_only = llvm_only;
7197         /* The first few entries are reserved */
7198         module->max_got_offset = 16;
7199         module->context = LLVMContextCreate ();
7200
7201         add_intrinsics (module->lmodule);
7202         add_types (module);
7203
7204         /* Add GOT */
7205         /*
7206          * We couldn't compute the type of the LLVM global representing the got because
7207          * its size is only known after all the methods have been emitted. So create
7208          * a dummy variable, and replace all uses it with the real got variable when
7209          * its size is known in mono_llvm_emit_aot_module ().
7210          */
7211         {
7212                 LLVMTypeRef got_type = LLVMArrayType (module->ptr_type, 0);
7213
7214                 module->got_var = LLVMAddGlobal (module->lmodule, got_type, "mono_dummy_got");
7215                 LLVMSetInitializer (module->got_var, LLVMConstNull (got_type));
7216         }
7217
7218         /* Add initialization array */
7219         if (llvm_only) {
7220                 LLVMTypeRef inited_type = LLVMArrayType (LLVMInt8Type (), 0);
7221
7222                 module->inited_var = LLVMAddGlobal (aot_module.lmodule, inited_type, "mono_inited_tmp");
7223                 LLVMSetInitializer (module->inited_var, LLVMConstNull (inited_type));
7224         }
7225
7226         if (llvm_only)
7227                 emit_init_icall_wrappers (module);
7228
7229         emit_llvm_code_start (module);
7230
7231         /* Add a dummy personality function */
7232         if (!use_debug_personality) {
7233                 LLVMValueRef personality = LLVMAddFunction (module->lmodule, default_personality_name, LLVMFunctionType (LLVMInt32Type (), NULL, 0, TRUE));
7234                 LLVMSetLinkage (personality, LLVMExternalLinkage);
7235                 mark_as_used (module, personality);
7236         }
7237
7238         /* Add a reference to the c++ exception we throw/catch */
7239         {
7240                 LLVMTypeRef exc = LLVMPointerType (LLVMInt8Type (), 0);
7241                 module->sentinel_exception = LLVMAddGlobal (module->lmodule, exc, "_ZTIPi");
7242                 LLVMSetLinkage (module->sentinel_exception, LLVMExternalLinkage);
7243                 mono_llvm_set_is_constant (module->sentinel_exception);
7244         }
7245
7246         module->llvm_types = g_hash_table_new (NULL, NULL);
7247         module->plt_entries = g_hash_table_new (g_str_hash, g_str_equal);
7248         module->plt_entries_ji = g_hash_table_new (NULL, NULL);
7249         module->direct_callables = g_hash_table_new (g_str_hash, g_str_equal);
7250         module->method_to_lmethod = g_hash_table_new (NULL, NULL);
7251         module->idx_to_lmethod = g_hash_table_new (NULL, NULL);
7252         module->idx_to_unbox_tramp = g_hash_table_new (NULL, NULL);
7253         module->method_to_callers = g_hash_table_new (NULL, NULL);
7254 }
7255
7256 static LLVMValueRef
7257 llvm_array_from_uints (LLVMTypeRef el_type, guint32 *values, int nvalues)
7258 {
7259         int i;
7260         LLVMValueRef res, *vals;
7261
7262         vals = g_new0 (LLVMValueRef, nvalues);
7263         for (i = 0; i < nvalues; ++i)
7264                 vals [i] = LLVMConstInt (LLVMInt32Type (), values [i], FALSE);
7265         res = LLVMConstArray (LLVMInt32Type (), vals, nvalues);
7266         g_free (vals);
7267         return res;
7268 }
7269
7270 /*
7271  * mono_llvm_emit_aot_file_info:
7272  *
7273  *   Emit the MonoAotFileInfo structure.
7274  * Same as emit_aot_file_info () in aot-compiler.c.
7275  */
7276 void
7277 mono_llvm_emit_aot_file_info (MonoAotFileInfo *info, gboolean has_jitted_code)
7278 {
7279         MonoLLVMModule *module = &aot_module;
7280
7281         /* Save these for later */
7282         memcpy (&module->aot_info, info, sizeof (MonoAotFileInfo));
7283         module->has_jitted_code = has_jitted_code;
7284 }
7285
7286 /*
7287  * mono_llvm_emit_aot_data:
7288  *
7289  *   Emit the binary data DATA pointed to by symbol SYMBOL.
7290  */
7291 void
7292 mono_llvm_emit_aot_data (const char *symbol, guint8 *data, int data_len)
7293 {
7294         MonoLLVMModule *module = &aot_module;
7295         LLVMTypeRef type;
7296         LLVMValueRef d;
7297
7298         type = LLVMArrayType (LLVMInt8Type (), data_len);
7299         d = LLVMAddGlobal (module->lmodule, type, symbol);
7300         LLVMSetVisibility (d, LLVMHiddenVisibility);
7301         LLVMSetLinkage (d, LLVMInternalLinkage);
7302         LLVMSetInitializer (d, mono_llvm_create_constant_data_array (data, data_len));
7303         mono_llvm_set_is_constant (d);
7304 }
7305
7306 /* Add a reference to a global defined in JITted code */
7307 static LLVMValueRef
7308 AddJitGlobal (MonoLLVMModule *module, LLVMTypeRef type, const char *name)
7309 {
7310         char *s;
7311         LLVMValueRef v;
7312
7313         s = g_strdup_printf ("%s%s", module->global_prefix, name);
7314         v = LLVMAddGlobal (module->lmodule, LLVMInt8Type (), s);
7315         g_free (s);
7316         return v;
7317 }
7318
7319 static void
7320 emit_aot_file_info (MonoLLVMModule *module)
7321 {
7322         LLVMTypeRef file_info_type;
7323         LLVMTypeRef *eltypes, eltype;
7324         LLVMValueRef info_var;
7325         LLVMValueRef *fields;
7326         int i, nfields, tindex;
7327         MonoAotFileInfo *info;
7328         LLVMModuleRef lmodule = module->lmodule;
7329
7330         info = &module->aot_info;
7331
7332         /* Create an LLVM type to represent MonoAotFileInfo */
7333         nfields = 2 + MONO_AOT_FILE_INFO_NUM_SYMBOLS + 14 + 4;
7334         eltypes = g_new (LLVMTypeRef, nfields);
7335         tindex = 0;
7336         eltypes [tindex ++] = LLVMInt32Type ();
7337         eltypes [tindex ++] = LLVMInt32Type ();
7338         /* Symbols */
7339         for (i = 0; i < MONO_AOT_FILE_INFO_NUM_SYMBOLS; ++i)
7340                 eltypes [tindex ++] = LLVMPointerType (LLVMInt8Type (), 0);
7341         /* Scalars */
7342         for (i = 0; i < 14; ++i)
7343                 eltypes [tindex ++] = LLVMInt32Type ();
7344         /* Arrays */
7345         for (i = 0; i < 4; ++i)
7346                 eltypes [tindex ++] = LLVMArrayType (LLVMInt32Type (), MONO_AOT_TRAMP_NUM);
7347         g_assert (tindex == nfields);
7348         file_info_type = LLVMStructCreateNamed (module->context, "MonoAotFileInfo");
7349         LLVMStructSetBody (file_info_type, eltypes, nfields, FALSE);
7350
7351         info_var = LLVMAddGlobal (lmodule, file_info_type, "mono_aot_file_info");
7352         if (module->static_link) {
7353                 LLVMSetVisibility (info_var, LLVMHiddenVisibility);
7354                 LLVMSetLinkage (info_var, LLVMInternalLinkage);
7355         }
7356         fields = g_new (LLVMValueRef, nfields);
7357         tindex = 0;
7358         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->version, FALSE);
7359         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->dummy, FALSE);
7360
7361         /* Symbols */
7362         /*
7363          * We use LLVMGetNamedGlobal () for symbol which are defined in LLVM code, and LLVMAddGlobal ()
7364          * for symbols defined in the .s file emitted by the aot compiler.
7365          */
7366         eltype = eltypes [tindex];
7367         if (module->llvm_only)
7368                 fields [tindex ++] = LLVMConstNull (eltype);
7369         else
7370                 fields [tindex ++] = AddJitGlobal (module, eltype, "jit_got");
7371         fields [tindex ++] = module->got_var;
7372         /* llc defines this directly */
7373         if (!module->llvm_only) {
7374                 fields [tindex ++] = LLVMAddGlobal (lmodule, eltype, module->eh_frame_symbol);
7375                 fields [tindex ++] = LLVMConstNull (eltype);
7376                 fields [tindex ++] = LLVMConstNull (eltype);
7377         } else {
7378                 fields [tindex ++] = LLVMConstNull (eltype);
7379                 fields [tindex ++] = module->get_method;
7380                 fields [tindex ++] = module->get_unbox_tramp;
7381         }
7382         if (module->has_jitted_code) {
7383                 fields [tindex ++] = AddJitGlobal (module, eltype, "jit_code_start");
7384                 fields [tindex ++] = AddJitGlobal (module, eltype, "jit_code_end");
7385         } else {
7386                 fields [tindex ++] = LLVMConstNull (eltype);
7387                 fields [tindex ++] = LLVMConstNull (eltype);
7388         }
7389         if (!module->llvm_only)
7390                 fields [tindex ++] = AddJitGlobal (module, eltype, "method_addresses");
7391         else
7392                 fields [tindex ++] = LLVMConstNull (eltype);
7393         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "blob");
7394         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "class_name_table");
7395         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "class_info_offsets");
7396         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "method_info_offsets");
7397         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "ex_info_offsets");
7398         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "extra_method_info_offsets");
7399         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "extra_method_table");
7400         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "got_info_offsets");
7401         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "llvm_got_info_offsets");
7402         /* Not needed (mem_end) */
7403         fields [tindex ++] = LLVMConstNull (eltype);
7404         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "image_table");
7405         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "assembly_guid");
7406         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "runtime_version");
7407         if (info->trampoline_size [0]) {
7408                 fields [tindex ++] = AddJitGlobal (module, eltype, "specific_trampolines");
7409                 fields [tindex ++] = AddJitGlobal (module, eltype, "static_rgctx_trampolines");
7410                 fields [tindex ++] = AddJitGlobal (module, eltype, "imt_thunks");
7411                 fields [tindex ++] = AddJitGlobal (module, eltype, "gsharedvt_arg_trampolines");
7412         } else {
7413                 fields [tindex ++] = LLVMConstNull (eltype);
7414                 fields [tindex ++] = LLVMConstNull (eltype);
7415                 fields [tindex ++] = LLVMConstNull (eltype);
7416                 fields [tindex ++] = LLVMConstNull (eltype);
7417         }
7418         if (module->static_link)
7419                 fields [tindex ++] = AddJitGlobal (module, eltype, "globals");
7420         else
7421                 fields [tindex ++] = LLVMConstNull (eltype);
7422         fields [tindex ++] = LLVMGetNamedGlobal (lmodule, "assembly_name");
7423         if (!module->llvm_only) {
7424                 fields [tindex ++] = AddJitGlobal (module, eltype, "plt");
7425                 fields [tindex ++] = AddJitGlobal (module, eltype, "plt_end");
7426                 fields [tindex ++] = AddJitGlobal (module, eltype, "unwind_info");
7427                 fields [tindex ++] = AddJitGlobal (module, eltype, "unbox_trampolines");
7428                 fields [tindex ++] = AddJitGlobal (module, eltype, "unbox_trampolines_end");
7429                 fields [tindex ++] = AddJitGlobal (module, eltype, "unbox_trampoline_addresses");
7430         } else {
7431                 fields [tindex ++] = LLVMConstNull (eltype);
7432                 fields [tindex ++] = LLVMConstNull (eltype);
7433                 fields [tindex ++] = LLVMConstNull (eltype);
7434                 fields [tindex ++] = LLVMConstNull (eltype);
7435                 fields [tindex ++] = LLVMConstNull (eltype);
7436                 fields [tindex ++] = LLVMConstNull (eltype);
7437         }
7438
7439         for (i = 0; i < MONO_AOT_FILE_INFO_NUM_SYMBOLS; ++i)
7440                 fields [2 + i] = LLVMConstBitCast (fields [2 + i], eltype);
7441
7442         /* Scalars */
7443         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->plt_got_offset_base, FALSE);
7444         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->got_size, FALSE);
7445         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->plt_size, FALSE);
7446         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->nmethods, FALSE);
7447         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->flags, FALSE);
7448         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->opts, FALSE);
7449         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->simd_opts, FALSE);
7450         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->gc_name_index, FALSE);
7451         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->num_rgctx_fetch_trampolines, FALSE);
7452         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->double_align, FALSE);
7453         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->long_align, FALSE);
7454         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->generic_tramp_num, FALSE);
7455         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->tramp_page_size, FALSE);
7456         fields [tindex ++] = LLVMConstInt (LLVMInt32Type (), info->nshared_got_entries, FALSE);
7457         /* Arrays */
7458         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->num_trampolines, MONO_AOT_TRAMP_NUM);
7459         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->trampoline_got_offset_base, MONO_AOT_TRAMP_NUM);
7460         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->trampoline_size, MONO_AOT_TRAMP_NUM);
7461         fields [tindex ++] = llvm_array_from_uints (LLVMInt32Type (), info->tramp_page_code_offsets, MONO_AOT_TRAMP_NUM);
7462         g_assert (tindex == nfields);
7463
7464         LLVMSetInitializer (info_var, LLVMConstNamedStruct (file_info_type, fields, nfields));
7465
7466         if (module->static_link) {
7467                 char *s, *p;
7468                 LLVMValueRef var;
7469
7470                 s = g_strdup_printf ("mono_aot_module_%s_info", module->assembly->aname.name);
7471                 /* Get rid of characters which cannot occur in symbols */
7472                 p = s;
7473                 for (p = s; *p; ++p) {
7474                         if (!(isalnum (*p) || *p == '_'))
7475                                 *p = '_';
7476                 }
7477                 var = LLVMAddGlobal (module->lmodule, LLVMPointerType (LLVMInt8Type (), 0), s);
7478                 g_free (s);
7479                 LLVMSetInitializer (var, LLVMConstBitCast (LLVMGetNamedGlobal (module->lmodule, "mono_aot_file_info"), LLVMPointerType (LLVMInt8Type (), 0)));
7480                 LLVMSetLinkage (var, LLVMExternalLinkage);
7481         }
7482 }
7483
7484 /*
7485  * Emit the aot module into the LLVM bitcode file FILENAME.
7486  */
7487 void
7488 mono_llvm_emit_aot_module (const char *filename, const char *cu_name)
7489 {
7490         LLVMTypeRef got_type, inited_type;
7491         LLVMValueRef real_got, real_inited;
7492         MonoLLVMModule *module = &aot_module;
7493
7494         emit_llvm_code_end (module);
7495
7496         /* 
7497          * Create the real got variable and replace all uses of the dummy variable with
7498          * the real one.
7499          */
7500         got_type = LLVMArrayType (module->ptr_type, module->max_got_offset + 1);
7501         real_got = LLVMAddGlobal (module->lmodule, got_type, module->got_symbol);
7502         LLVMSetInitializer (real_got, LLVMConstNull (got_type));
7503         if (module->external_symbols) {
7504                 LLVMSetLinkage (real_got, LLVMExternalLinkage);
7505                 LLVMSetVisibility (real_got, LLVMHiddenVisibility);
7506         } else {
7507                 LLVMSetLinkage (real_got, LLVMInternalLinkage);
7508         }
7509         mono_llvm_replace_uses_of (module->got_var, real_got);
7510
7511         mark_as_used (&aot_module, real_got);
7512
7513         /* Delete the dummy got so it doesn't become a global */
7514         LLVMDeleteGlobal (module->got_var);
7515         module->got_var = real_got;
7516
7517         /*
7518          * Same for the init_var
7519          */
7520         if (module->llvm_only) {
7521                 inited_type = LLVMArrayType (LLVMInt8Type (), module->max_inited_idx + 1);
7522                 real_inited = LLVMAddGlobal (module->lmodule, inited_type, "mono_inited");
7523                 LLVMSetInitializer (real_inited, LLVMConstNull (inited_type));
7524                 LLVMSetLinkage (real_inited, LLVMInternalLinkage);
7525                 mono_llvm_replace_uses_of (module->inited_var, real_inited);
7526                 LLVMDeleteGlobal (module->inited_var);
7527         }
7528
7529         if (module->llvm_only) {
7530                 emit_get_method (&aot_module);
7531                 emit_get_unbox_tramp (&aot_module);
7532         }
7533
7534         emit_llvm_used (&aot_module);
7535         emit_dbg_info (&aot_module, filename, cu_name);
7536         emit_aot_file_info (&aot_module);
7537
7538         /*
7539          * Replace GOT entries for directly callable methods with the methods themselves.
7540          * It would be easier to implement this by predefining all methods before compiling
7541          * their bodies, but that couldn't handle the case when a method fails to compile
7542          * with llvm.
7543          */
7544         if (module->llvm_only) {
7545                 GHashTableIter iter;
7546                 MonoMethod *method;
7547                 GSList *callers, *l;
7548
7549                 g_hash_table_iter_init (&iter, module->method_to_callers);
7550                 while (g_hash_table_iter_next (&iter, (void**)&method, (void**)&callers)) {
7551                         LLVMValueRef lmethod;
7552
7553                         lmethod = g_hash_table_lookup (module->method_to_lmethod, method);
7554                         if (lmethod) {
7555                                 for (l = callers; l; l = l->next) {
7556                                         LLVMValueRef caller = l->data;
7557
7558                                         mono_llvm_replace_uses_of (caller, lmethod);
7559                                 }
7560                         }
7561                 }
7562         }
7563
7564         /* Replace PLT entries for directly callable methods with the methods themselves */
7565         {
7566                 GHashTableIter iter;
7567                 MonoJumpInfo *ji;
7568                 LLVMValueRef callee;
7569
7570                 g_hash_table_iter_init (&iter, module->plt_entries_ji);
7571                 while (g_hash_table_iter_next (&iter, (void**)&ji, (void**)&callee)) {
7572                         if (mono_aot_is_direct_callable (ji)) {
7573                                 LLVMValueRef lmethod;
7574
7575                                 lmethod = g_hash_table_lookup (module->method_to_lmethod, ji->data.method);
7576                                 /* The types might not match because the caller might pass an rgctx */
7577                                 if (lmethod && LLVMTypeOf (callee) == LLVMTypeOf (lmethod)) {
7578                                         mono_llvm_replace_uses_of (callee, lmethod);
7579                                         mono_aot_mark_unused_llvm_plt_entry (ji);
7580                                 }
7581                         }
7582                 }
7583         }
7584
7585 #if 0
7586         {
7587                 char *verifier_err;
7588
7589                 if (LLVMVerifyModule (module->module, LLVMReturnStatusAction, &verifier_err)) {
7590                         g_assert_not_reached ();
7591                 }
7592         }
7593 #endif
7594
7595         LLVMWriteBitcodeToFile (module->lmodule, filename);
7596 }
7597
7598
7599 static LLVMValueRef
7600 md_string (const char *s)
7601 {
7602         return LLVMMDString (s, strlen (s));
7603 }
7604
7605 /* Debugging support */
7606
7607 static void
7608 emit_dbg_info (MonoLLVMModule *module, const char *filename, const char *cu_name)
7609 {
7610         LLVMModuleRef lmodule = module->lmodule;
7611         LLVMValueRef args [16], cu_args [16], cu, ver;
7612         int n_cuargs;
7613         char *build_info, *s, *dir;
7614
7615         /*
7616          * This can only be enabled when LLVM code is emitted into a separate object
7617          * file, since the AOT compiler also emits dwarf info,
7618          * and the abbrev indexes will not be correct since llvm has added its own
7619          * abbrevs.
7620          */
7621         if (!module->emit_dwarf)
7622                 return;
7623
7624         /*
7625          * Emit dwarf info in the form of LLVM metadata. There is some
7626          * out-of-date documentation at:
7627          * http://llvm.org/docs/SourceLevelDebugging.html
7628          * but most of this was gathered from the llvm and
7629          * clang sources.
7630          */
7631
7632         n_cuargs = 0;
7633         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_TAG_compile_unit, FALSE);
7634         /* CU name/compilation dir */
7635         dir = g_path_get_dirname (filename);
7636         args [0] = LLVMMDString (cu_name, strlen (cu_name));
7637         args [1] = LLVMMDString (dir, strlen (dir));
7638         cu_args [n_cuargs ++] = LLVMMDNode (args, 2);
7639         g_free (dir);
7640         /* Language */
7641         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), DW_LANG_C99, FALSE);
7642         /* Producer */
7643         build_info = mono_get_runtime_build_info ();
7644         s = g_strdup_printf ("Mono AOT Compiler %s (LLVM)", build_info);
7645         cu_args [n_cuargs ++] = LLVMMDString (s, strlen (s));
7646         g_free (build_info);
7647         /* Optimized */
7648         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
7649         /* Flags */
7650         cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
7651         /* Runtime version */
7652         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
7653         /* Enums */
7654         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
7655         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
7656         /* Subprograms */
7657         if (module->subprogram_mds) {
7658                 LLVMValueRef *mds;
7659                 int i;
7660
7661                 mds = g_new0 (LLVMValueRef, module->subprogram_mds->len);
7662                 for (i = 0; i < module->subprogram_mds->len; ++i)
7663                         mds [i] = g_ptr_array_index (module->subprogram_mds, i);
7664                 cu_args [n_cuargs ++] = LLVMMDNode (mds, module->subprogram_mds->len);
7665         } else {
7666                 cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
7667         }
7668         /* GVs */
7669         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
7670         /* Imported modules */
7671         cu_args [n_cuargs ++] = LLVMMDNode (args, 0);
7672         /* SplitName */
7673         cu_args [n_cuargs ++] = LLVMMDString ("", strlen (""));
7674         /* DebugEmissionKind = FullDebug */
7675         cu_args [n_cuargs ++] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
7676         cu = LLVMMDNode (cu_args, n_cuargs);
7677         LLVMAddNamedMetadataOperand (lmodule, "llvm.dbg.cu", cu);
7678
7679         args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
7680         args [1] = LLVMMDString ("Dwarf Version", strlen ("Dwarf Version"));
7681         args [2] = LLVMConstInt (LLVMInt32Type (), 2, FALSE);
7682         ver = LLVMMDNode (args, 3);
7683         LLVMAddNamedMetadataOperand (lmodule, "llvm.module.flags", ver);
7684
7685         args [0] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
7686         args [1] = LLVMMDString ("Debug Info Version", strlen ("Debug Info Version"));
7687         args [2] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
7688         ver = LLVMMDNode (args, 3);
7689         LLVMAddNamedMetadataOperand (lmodule, "llvm.module.flags", ver);
7690 }
7691
7692 static LLVMValueRef
7693 emit_dbg_subprogram (EmitContext *ctx, MonoCompile *cfg, LLVMValueRef method, const char *name)
7694 {
7695         MonoLLVMModule *module = ctx->module;
7696         MonoDebugMethodInfo *minfo = ctx->minfo;
7697         char *source_file, *dir, *filename;
7698         LLVMValueRef md, args [16], ctx_args [16], md_args [64], type_args [16], ctx_md, type_md;
7699         MonoSymSeqPoint *sym_seq_points;
7700         int n_seq_points;
7701
7702         if (!minfo)
7703                 return NULL;
7704
7705         mono_debug_symfile_get_seq_points (minfo, &source_file, NULL, NULL, &sym_seq_points, &n_seq_points);
7706         if (!source_file)
7707                 source_file = g_strdup ("<unknown>");
7708         dir = g_path_get_dirname (source_file);
7709         filename = g_path_get_basename (source_file);
7710
7711         ctx_args [0] = LLVMConstInt (LLVMInt32Type (), 0x29, FALSE);
7712         args [0] = md_string (filename);
7713         args [1] = md_string (dir);
7714         ctx_args [1] = LLVMMDNode (args, 2);
7715         ctx_md = LLVMMDNode (ctx_args, 2);
7716
7717         type_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subroutine_type, FALSE);
7718         type_args [1] = NULL;
7719         type_args [2] = NULL;
7720         type_args [3] = LLVMMDString ("", 0);
7721         type_args [4] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
7722         type_args [5] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
7723         type_args [6] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
7724         type_args [7] = LLVMConstInt (LLVMInt64Type (), 0, FALSE);
7725         type_args [8] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
7726         type_args [9] = NULL;
7727         type_args [10] = NULL;
7728         type_args [11] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
7729         type_args [12] = NULL;
7730         type_args [13] = NULL;
7731         type_args [14] = NULL;
7732         type_md = LLVMMDNode (type_args, 14);
7733
7734         /* http://llvm.org/docs/SourceLevelDebugging.html#subprogram-descriptors */
7735         md_args [0] = LLVMConstInt (LLVMInt32Type (), DW_TAG_subprogram, FALSE);
7736         /* Source directory + file pair */
7737         args [0] = md_string (filename);
7738         args [1] = md_string (dir);
7739         md_args [1] = LLVMMDNode (args ,2);
7740         md_args [2] = ctx_md;
7741         md_args [3] = md_string (cfg->method->name);
7742         md_args [4] = md_string (name);
7743         md_args [5] = md_string (name);
7744         /* Line number */
7745         if (n_seq_points)
7746                 md_args [6] = LLVMConstInt (LLVMInt32Type (), sym_seq_points [0].line, FALSE);
7747         else
7748                 md_args [6] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
7749         /* Type */
7750         md_args [7] = type_md;
7751         /* static */
7752         md_args [8] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
7753         /* not extern */
7754         md_args [9] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
7755         /* Virtuality */
7756         md_args [10] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
7757         /* Index into a virtual function */
7758         md_args [11] = NULL;
7759         md_args [12] = NULL;
7760         /* Flags */
7761         md_args [13] = LLVMConstInt (LLVMInt1Type (), 0, FALSE);
7762         /* isOptimized */
7763         md_args [14] = LLVMConstInt (LLVMInt1Type (), 1, FALSE);
7764         /* Pointer to LLVM function */
7765         md_args [15] = method;
7766         /* Function template parameter */
7767         md_args [16] = NULL;
7768         /* Function declaration descriptor */
7769         md_args [17] = NULL;
7770         /* List of function variables */
7771         md_args [18] = LLVMMDNode (args, 0);
7772         /* Line number */
7773         md_args [19] = LLVMConstInt (LLVMInt32Type (), 1, FALSE);
7774         md = LLVMMDNode (md_args, 20);
7775
7776         if (!module->subprogram_mds)
7777                 module->subprogram_mds = g_ptr_array_new ();
7778         g_ptr_array_add (module->subprogram_mds, md);
7779
7780         g_free (dir);
7781         g_free (filename);
7782         g_free (source_file);
7783         g_free (sym_seq_points);
7784
7785         return md;
7786 }
7787
7788 static void
7789 emit_dbg_loc (EmitContext *ctx, LLVMBuilderRef builder, const unsigned char *cil_code)
7790 {
7791         MonoCompile *cfg = ctx->cfg;
7792
7793         if (ctx->minfo && cil_code && cil_code >= cfg->header->code && cil_code < cfg->header->code + cfg->header->code_size) {
7794                 MonoDebugSourceLocation *loc;
7795                 LLVMValueRef loc_md, md_args [16];
7796                 int nmd_args;
7797
7798                 loc = mono_debug_symfile_lookup_location (ctx->minfo, cil_code - cfg->header->code);
7799
7800                 if (loc) {
7801                         nmd_args = 0;
7802                         md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->row, FALSE);
7803                         md_args [nmd_args ++] = LLVMConstInt (LLVMInt32Type (), loc->column, FALSE);
7804                         md_args [nmd_args ++] = ctx->dbg_md;
7805                         md_args [nmd_args ++] = NULL;
7806                         loc_md = LLVMMDNode (md_args, nmd_args);
7807                         LLVMSetCurrentDebugLocation (builder, loc_md);
7808                         mono_debug_symfile_free_location (loc);
7809                 }
7810         }
7811 }
7812
7813 void
7814 default_mono_llvm_unhandled_exception (void)
7815 {
7816         MonoJitTlsData *jit_tls = mono_get_jit_tls ();
7817         MonoObject *target = mono_gchandle_get_target (jit_tls->thrown_exc);
7818
7819         mono_unhandled_exception (target);
7820         exit (mono_environment_exitcode_get ());
7821 }
7822
7823 /*
7824   DESIGN:
7825   - Emit LLVM IR from the mono IR using the LLVM C API.
7826   - The original arch specific code remains, so we can fall back to it if we run
7827     into something we can't handle.
7828 */
7829
7830 /*  
7831   A partial list of issues:
7832   - Handling of opcodes which can throw exceptions.
7833
7834       In the mono JIT, these are implemented using code like this:
7835           method:
7836       <compare>
7837           throw_pos:
7838           b<cond> ex_label
7839           <rest of code>
7840       ex_label:
7841           push throw_pos - method
7842           call <exception trampoline>
7843
7844           The problematic part is push throw_pos - method, which cannot be represented
7845       in the LLVM IR, since it does not support label values.
7846           -> this can be implemented in AOT mode using inline asm + labels, but cannot
7847           be implemented in JIT mode ?
7848           -> a possible but slower implementation would use the normal exception 
7849       throwing code but it would need to control the placement of the throw code
7850       (it needs to be exactly after the compare+branch).
7851           -> perhaps add a PC offset intrinsics ?
7852
7853   - efficient implementation of .ovf opcodes.
7854
7855           These are currently implemented as:
7856           <ins which sets the condition codes>
7857           b<cond> ex_label
7858
7859           Some overflow opcodes are now supported by LLVM SVN.
7860
7861   - exception handling, unwinding.
7862     - SSA is disabled for methods with exception handlers    
7863         - How to obtain unwind info for LLVM compiled methods ?
7864           -> this is now solved by converting the unwind info generated by LLVM
7865              into our format.
7866         - LLVM uses the c++ exception handling framework, while we use our home grown
7867       code, and couldn't use the c++ one:
7868       - its not supported under VC++, other exotic platforms.
7869           - it might be impossible to support filter clauses with it.
7870
7871   - trampolines.
7872   
7873     The trampolines need a predictable call sequence, since they need to disasm
7874     the calling code to obtain register numbers / offsets.
7875
7876     LLVM currently generates this code in non-JIT mode:
7877            mov    -0x98(%rax),%eax
7878            callq  *%rax
7879     Here, the vtable pointer is lost. 
7880     -> solution: use one vtable trampoline per class.
7881
7882   - passing/receiving the IMT pointer/RGCTX.
7883     -> solution: pass them as normal arguments ?
7884
7885   - argument passing.
7886   
7887           LLVM does not allow the specification of argument registers etc. This means
7888       that all calls are made according to the platform ABI.
7889
7890   - passing/receiving vtypes.
7891
7892       Vtypes passed/received in registers are handled by the front end by using
7893           a signature with scalar arguments, and loading the parts of the vtype into those
7894           arguments.
7895
7896           Vtypes passed on the stack are handled using the 'byval' attribute.
7897
7898   - ldaddr.
7899
7900     Supported though alloca, we need to emit the load/store code.
7901
7902   - types.
7903
7904     The mono JIT uses pointer sized iregs/double fregs, while LLVM uses precisely
7905     typed registers, so we have to keep track of the precise LLVM type of each vreg.
7906     This is made easier because the IR is already in SSA form.
7907     An additional problem is that our IR is not consistent with types, i.e. i32/ia64 
7908         types are frequently used incorrectly.
7909 */
7910
7911 /*
7912   AOT SUPPORT:
7913   Emit LLVM bytecode into a .bc file, compile it using llc into a .s file, then link
7914   it with the file containing the methods emitted by the JIT and the AOT data
7915   structures.
7916 */
7917
7918 /* FIXME: Normalize some aspects of the mono IR to allow easier translation, like:
7919  *   - each bblock should end with a branch
7920  *   - setting the return value, making cfg->ret non-volatile
7921  * - avoid some transformations in the JIT which make it harder for us to generate
7922  *   code.
7923  * - use pointer types to help optimizations.
7924  */