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