2009-09-06 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-llvm.c
1 /*
2  * mini-llvm.c: llvm "Backend" for the mono JIT
3  *
4  * (C) 2009 Novell, Inc.
5  */
6
7 #include "mini.h"
8 #include <mono/metadata/debug-helpers.h>
9 #include <mono/metadata/mempool-internals.h>
10
11 #define __STDC_LIMIT_MACROS
12 #define __STDC_CONSTANT_MACROS
13
14 #include "llvm-c/Core.h"
15 #include "llvm-c/ExecutionEngine.h"
16
17 #include "mini-llvm-cpp.h"
18
19  /*
20   * Information associated by mono with LLVM modules.
21   */
22 typedef struct {
23         LLVMModuleRef module;
24         LLVMValueRef throw, throw_corlib_exception;     
25         GHashTable *llvm_types;
26         LLVMValueRef got_var;
27 } MonoLLVMModule;
28
29 /*
30  * Structure containing emit state
31  */
32 typedef struct {
33         MonoMemPool *mempool;
34
35         /* Maps method names to the corresponding LLVMValueRef */
36         GHashTable *emitted_method_decls;
37
38         MonoCompile *cfg;
39         LLVMValueRef lmethod;
40         MonoLLVMModule *lmodule;
41         LLVMModuleRef module;
42         LLVMBasicBlockRef *bblocks, *end_bblocks;
43         int sindex, default_index, ex_index;
44         LLVMBuilderRef builder;
45         LLVMValueRef *values, *addresses;
46         LLVMCallInfo *linfo;
47         MonoMethodSignature *sig;
48         GSList *builders;
49
50         char temp_name [32];
51 } EmitContext;
52
53 typedef struct {
54         MonoBasicBlock *bb;
55         MonoInst *phi;
56         int index;
57 } PhiNode;
58
59 /*
60  * Instruction metadata
61  * This is the same as ins_info, but LREG != IREG.
62  */
63 #ifdef MINI_OP
64 #undef MINI_OP
65 #endif
66 #ifdef MINI_OP3
67 #undef MINI_OP3
68 #endif
69 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
70 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
71 #define NONE ' '
72 #define IREG 'i'
73 #define FREG 'f'
74 #define VREG 'v'
75 #define XREG 'x'
76 #define LREG 'l'
77 /* keep in sync with the enum in mini.h */
78 const char
79 llvm_ins_info[] = {
80 #include "mini-ops.h"
81 };
82 #undef MINI_OP
83 #undef MINI_OP3
84
85 #if SIZEOF_VOID_P == 4
86 #define GET_LONG_IMM(ins) (((guint64)(ins)->inst_ms_word << 32) | (guint64)(guint32)(ins)->inst_ls_word)
87 #else
88 #define GET_LONG_IMM(ins) ((ins)->inst_imm)
89 #endif
90
91 #define LLVM_INS_INFO(opcode) (&llvm_ins_info [((opcode) - OP_START - 1) * 4])
92
93 #define LLVM_FAILURE(ctx, reason) do { \
94         (ctx)->cfg->exception_message = g_strdup (reason); \
95         (ctx)->cfg->disable_llvm = TRUE; \
96         goto FAILURE; \
97 } while (0)
98
99 #define CHECK_FAILURE(ctx) do { \
100     if ((ctx)->cfg->disable_llvm) \
101                 goto FAILURE; \
102 } while (0)
103
104 static LLVMIntPredicate cond_to_llvm_cond [] = {
105         LLVMIntEQ,
106         LLVMIntNE,
107         LLVMIntSLE,
108         LLVMIntSGE,
109         LLVMIntSLT,
110         LLVMIntSGT,
111         LLVMIntULE,
112         LLVMIntUGE,
113         LLVMIntULT,
114         LLVMIntUGT,
115 };
116
117 static LLVMRealPredicate fpcond_to_llvm_cond [] = {
118         LLVMRealOEQ,
119         LLVMRealUNE,
120         LLVMRealOLE,
121         LLVMRealOGE,
122         LLVMRealOLT,
123         LLVMRealOGT,
124         LLVMRealULE,
125         LLVMRealUGE,
126         LLVMRealULT,
127         LLVMRealUGT,
128 };
129
130 static LLVMExecutionEngineRef ee;
131 static guint32 current_cfg_tls_id;
132
133 static MonoLLVMModule jit_module, aot_module;
134 static GHashTable *plt_entries;
135
136 /*
137  * IntPtrType:
138  *
139  *   The LLVM type with width == sizeof (gpointer)
140  */
141 static LLVMTypeRef
142 IntPtrType (void)
143 {
144         return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
145 }
146
147 /*
148  * get_vtype_size:
149  *
150  *   Return the size of the LLVM representation of the vtype T.
151  */
152 static guint32
153 get_vtype_size (MonoType *t)
154 {
155         int size;
156
157         size = mono_class_value_size (mono_class_from_mono_type (t), NULL);
158
159         while (size < sizeof (gpointer) && mono_is_power_of_two (size) == -1)
160                 size ++;
161
162         return size;
163 }
164
165 /*
166  * type_to_llvm_type:
167  *
168  *   Return the LLVM type corresponding to T.
169  */
170 static LLVMTypeRef
171 type_to_llvm_type (EmitContext *ctx, MonoType *t)
172 {
173         if (t->byref)
174                 return LLVMPointerType (LLVMInt8Type (), 0);
175         switch (t->type) {
176         case MONO_TYPE_VOID:
177                 return LLVMVoidType ();
178         case MONO_TYPE_I1:
179                 return LLVMInt8Type ();
180         case MONO_TYPE_I2:
181                 return LLVMInt16Type ();
182         case MONO_TYPE_I4:
183                 return LLVMInt32Type ();
184         case MONO_TYPE_U1:
185                 return LLVMInt8Type ();
186         case MONO_TYPE_U2:
187                 return LLVMInt16Type ();
188         case MONO_TYPE_U4:
189                 return LLVMInt32Type ();
190         case MONO_TYPE_BOOLEAN:
191                 return LLVMInt8Type ();
192         case MONO_TYPE_I8:
193         case MONO_TYPE_U8:
194                 return LLVMInt64Type ();
195         case MONO_TYPE_CHAR:
196                 return LLVMInt16Type ();
197         case MONO_TYPE_R4:
198                 return LLVMFloatType ();
199         case MONO_TYPE_R8:
200                 return LLVMDoubleType ();
201         case MONO_TYPE_I:
202         case MONO_TYPE_U:
203                 return IntPtrType ();
204         case MONO_TYPE_OBJECT:
205         case MONO_TYPE_CLASS:
206         case MONO_TYPE_ARRAY:
207         case MONO_TYPE_SZARRAY:
208         case MONO_TYPE_STRING:
209         case MONO_TYPE_PTR:
210                 return LLVMPointerType (IntPtrType (), 0);
211         case MONO_TYPE_VAR:
212         case MONO_TYPE_MVAR:
213                 /* Because of generic sharing */
214                 return IntPtrType ();
215         case MONO_TYPE_GENERICINST:
216                 if (!mono_type_generic_inst_is_valuetype (t))
217                         return IntPtrType ();
218                 /* Fall through */
219         case MONO_TYPE_VALUETYPE: {
220                 MonoClass *klass;
221                 LLVMTypeRef ltype;
222
223                 klass = mono_class_from_mono_type (t);
224
225                 if (klass->enumtype)
226                         return type_to_llvm_type (ctx, mono_class_enum_basetype (t->data.klass));
227                 ltype = g_hash_table_lookup (ctx->lmodule->llvm_types, klass);
228                 if (!ltype) {
229                         int i, size;
230                         LLVMTypeRef *eltypes;
231
232                         size = get_vtype_size (t);
233
234                         eltypes = g_new (LLVMTypeRef, size);
235                         for (i = 0; i < size; ++i)
236                                 eltypes [i] = LLVMInt8Type ();
237
238                         ltype = LLVMStructType (eltypes, size, FALSE);
239                         g_hash_table_insert (ctx->lmodule->llvm_types, klass, ltype);
240                         g_free (eltypes);
241                 }
242                 return ltype;
243         }
244
245         default:
246                 ctx->cfg->exception_message = g_strdup_printf ("type %s", mono_type_full_name (t));
247                 ctx->cfg->disable_llvm = TRUE;
248                 return NULL;
249         }
250 }
251
252 /*
253  * type_to_llvm_arg_type:
254  *
255  *   Same as type_to_llvm_type, but treat i8/i16 as i32.
256  */
257 static LLVMTypeRef
258 type_to_llvm_arg_type (EmitContext *ctx, MonoType *t)
259 {
260         LLVMTypeRef ptype = type_to_llvm_type (ctx, t);
261         
262         if (ptype == LLVMInt8Type () || ptype == LLVMInt16Type ()) {
263                 /* 
264                  * LLVM generates code which only sets the lower bits, while JITted
265                  * code expects all the bits to be set.
266                  */
267                 ptype = LLVMInt32Type ();
268         }
269
270         return ptype;
271 }
272
273 /*
274  * llvm_type_to_stack_type:
275  *
276  *   Return the LLVM type which needs to be used when a value of type TYPE is pushed
277  * on the IL stack.
278  */
279 static G_GNUC_UNUSED LLVMTypeRef
280 llvm_type_to_stack_type (LLVMTypeRef type)
281 {
282         if (type == NULL)
283                 return NULL;
284         if (type == LLVMInt8Type ())
285                 return LLVMInt32Type ();
286         else if (type == LLVMInt16Type ())
287                 return LLVMInt32Type ();
288         else if (type == LLVMFloatType ())
289                 return LLVMDoubleType ();
290         else
291                 return type;
292 }
293
294 /*
295  * regtype_to_llvm_type:
296  *
297  *   Return the LLVM type corresponding to the regtype C used in instruction 
298  * descriptions.
299  */
300 static LLVMTypeRef
301 regtype_to_llvm_type (char c)
302 {
303         switch (c) {
304         case 'i':
305                 return LLVMInt32Type ();
306         case 'l':
307                 return LLVMInt64Type ();
308         case 'f':
309                 return LLVMDoubleType ();
310         default:
311                 return NULL;
312         }
313 }
314
315 /*
316  * op_to_llvm_type:
317  *
318  *   Return the LLVM type corresponding to the unary/binary opcode OPCODE.
319  */
320 static LLVMTypeRef
321 op_to_llvm_type (int opcode)
322 {
323         switch (opcode) {
324         case OP_ICONV_TO_I1:
325         case OP_LCONV_TO_I1:
326                 return LLVMInt8Type ();
327         case OP_ICONV_TO_U1:
328         case OP_LCONV_TO_U1:
329                 return LLVMInt8Type ();
330         case OP_ICONV_TO_I2:
331         case OP_LCONV_TO_I2:
332                 return LLVMInt16Type ();
333         case OP_ICONV_TO_U2:
334         case OP_LCONV_TO_U2:
335                 return LLVMInt16Type ();
336         case OP_ICONV_TO_I4:
337         case OP_LCONV_TO_I4:
338                 return LLVMInt32Type ();
339         case OP_ICONV_TO_U4:
340         case OP_LCONV_TO_U4:
341                 return LLVMInt32Type ();
342         case OP_ICONV_TO_I8:
343                 return LLVMInt64Type ();
344         case OP_ICONV_TO_R4:
345                 return LLVMFloatType ();
346         case OP_ICONV_TO_R8:
347                 return LLVMDoubleType ();
348         case OP_ICONV_TO_U8:
349                 return LLVMInt64Type ();
350         case OP_FCONV_TO_I4:
351                 return LLVMInt32Type ();
352         case OP_FCONV_TO_I8:
353                 return LLVMInt64Type ();
354         case OP_FCONV_TO_I1:
355         case OP_FCONV_TO_U1:
356                 return LLVMInt8Type ();
357         case OP_FCONV_TO_I2:
358         case OP_FCONV_TO_U2:
359                 return LLVMInt16Type ();
360         case OP_FCONV_TO_I:
361         case OP_FCONV_TO_U:
362                 return sizeof (gpointer) == 8 ? LLVMInt64Type () : LLVMInt32Type ();
363         case OP_IADD_OVF:
364         case OP_IADD_OVF_UN:
365         case OP_ISUB_OVF:
366         case OP_ISUB_OVF_UN:
367         case OP_IMUL_OVF:
368         case OP_IMUL_OVF_UN:
369                 return LLVMInt32Type ();
370         case OP_LADD_OVF:
371         case OP_LSUB_OVF:
372         case OP_LSUB_OVF_UN:
373         case OP_LMUL_OVF:
374         case OP_LMUL_OVF_UN:
375                 return LLVMInt64Type ();
376         default:
377                 printf ("%s\n", mono_inst_name (opcode));
378                 g_assert_not_reached ();
379                 return NULL;
380         }
381 }               
382
383 /*
384  * load_store_to_llvm_type:
385  *
386  *   Return the size/sign/zero extension corresponding to the load/store opcode
387  * OPCODE.
388  */
389 static LLVMTypeRef
390 load_store_to_llvm_type (int opcode, int *size, gboolean *sext, gboolean *zext)
391 {
392         *sext = FALSE;
393         *zext = FALSE;
394
395         switch (opcode) {
396         case OP_LOADI1_MEMBASE:
397         case OP_STOREI1_MEMBASE_REG:
398         case OP_STOREI1_MEMBASE_IMM:
399                 *size = 1;
400                 *sext = TRUE;
401                 return LLVMInt8Type ();
402         case OP_LOADU1_MEMBASE:
403         case OP_LOADU1_MEM:
404                 *size = 1;
405                 *zext = TRUE;
406                 return LLVMInt8Type ();
407         case OP_LOADI2_MEMBASE:
408         case OP_STOREI2_MEMBASE_REG:
409         case OP_STOREI2_MEMBASE_IMM:
410                 *size = 2;
411                 *sext = TRUE;
412                 return LLVMInt16Type ();
413         case OP_LOADU2_MEMBASE:
414         case OP_LOADU2_MEM:
415                 *size = 2;
416                 *zext = TRUE;
417                 return LLVMInt16Type ();
418         case OP_LOADI4_MEMBASE:
419         case OP_LOADU4_MEMBASE:
420         case OP_LOADI4_MEM:
421         case OP_LOADU4_MEM:
422         case OP_STOREI4_MEMBASE_REG:
423         case OP_STOREI4_MEMBASE_IMM:
424                 *size = 4;
425                 return LLVMInt32Type ();
426         case OP_LOADI8_MEMBASE:
427         case OP_LOADI8_MEM:
428         case OP_STOREI8_MEMBASE_REG:
429         case OP_STOREI8_MEMBASE_IMM:
430                 *size = 8;
431                 return LLVMInt64Type ();
432         case OP_LOADR4_MEMBASE:
433         case OP_STORER4_MEMBASE_REG:
434                 *size = 4;
435                 return LLVMFloatType ();
436         case OP_LOADR8_MEMBASE:
437         case OP_STORER8_MEMBASE_REG:
438                 *size = 8;
439                 return LLVMDoubleType ();
440         case OP_LOAD_MEMBASE:
441         case OP_LOAD_MEM:
442         case OP_STORE_MEMBASE_REG:
443         case OP_STORE_MEMBASE_IMM:
444                 *size = sizeof (gpointer);
445                 return IntPtrType ();
446         default:
447                 g_assert_not_reached ();
448                 return NULL;
449         }
450 }
451
452 /*
453  * ovf_op_to_intrins:
454  *
455  *   Return the LLVM intrinsics corresponding to the overflow opcode OPCODE.
456  */
457 static const char*
458 ovf_op_to_intrins (int opcode)
459 {
460         switch (opcode) {
461         case OP_IADD_OVF:
462                 return "llvm.sadd.with.overflow.i32";
463         case OP_IADD_OVF_UN:
464                 return "llvm.uadd.with.overflow.i32";
465         case OP_ISUB_OVF:
466                 return "llvm.ssub.with.overflow.i32";
467         case OP_ISUB_OVF_UN:
468                 return "llvm.usub.with.overflow.i32";
469         case OP_IMUL_OVF:
470                 return "llvm.smul.with.overflow.i32";
471         case OP_IMUL_OVF_UN:
472                 return "llvm.umul.with.overflow.i32";
473         case OP_LADD_OVF:
474                 return "llvm.sadd.with.overflow.i64";
475         case OP_LADD_OVF_UN:
476                 return "llvm.uadd.with.overflow.i64";
477         case OP_LSUB_OVF:
478                 return "llvm.ssub.with.overflow.i64";
479         case OP_LSUB_OVF_UN:
480                 return "llvm.usub.with.overflow.i64";
481         case OP_LMUL_OVF:
482                 return "llvm.smul.with.overflow.i64";
483         case OP_LMUL_OVF_UN:
484                 return "llvm.umul.with.overflow.i64";
485         default:
486                 g_assert_not_reached ();
487                 return NULL;
488         }
489 }
490
491 /*
492  * get_bb:
493  *
494  *   Return the LLVM basic block corresponding to BB.
495  */
496 static LLVMBasicBlockRef
497 get_bb (EmitContext *ctx, MonoBasicBlock *bb)
498 {
499         char bb_name [128];
500
501         if (ctx->bblocks [bb->block_num] == NULL) {
502                 sprintf (bb_name, "BB%d", bb->block_num);
503
504                 ctx->bblocks [bb->block_num] = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
505                 ctx->end_bblocks [bb->block_num] = ctx->bblocks [bb->block_num];
506         }
507
508         return ctx->bblocks [bb->block_num];
509 }
510
511 /* 
512  * get_end_bb:
513  *
514  *   Return the last LLVM bblock corresponding to BB.
515  * This might not be equal to the bb returned by get_bb () since we need to generate
516  * multiple LLVM bblocks for a mono bblock to handle throwing exceptions.
517  */
518 static LLVMBasicBlockRef
519 get_end_bb (EmitContext *ctx, MonoBasicBlock *bb)
520 {
521         get_bb (ctx, bb);
522         return ctx->end_bblocks [bb->block_num];
523 }
524
525 /*
526  * resolve_patch:
527  *
528  *   Return the target of the patch identified by TYPE and TARGET.
529  */
530 static gpointer
531 resolve_patch (MonoCompile *cfg, MonoJumpInfoType type, gconstpointer target)
532 {
533         MonoJumpInfo ji;
534
535         memset (&ji, 0, sizeof (ji));
536         ji.type = type;
537         ji.data.target = target;
538
539         return mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
540 }
541
542 /*
543  * convert:
544  *
545  *   Emit code to convert the LLVM value V to DTYPE.
546  */
547 static LLVMValueRef
548 convert (EmitContext *ctx, LLVMValueRef v, LLVMTypeRef dtype)
549 {
550         LLVMTypeRef stype = LLVMTypeOf (v);
551
552         if (stype != dtype) {
553                 /* Extend */
554                 if (dtype == LLVMInt64Type () && (stype == LLVMInt32Type () || stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
555                         return LLVMBuildSExt (ctx->builder, v, dtype, "");
556                 else if (dtype == LLVMInt32Type () && (stype == LLVMInt16Type () || stype == LLVMInt8Type ()))
557                         return LLVMBuildSExt (ctx->builder, v, dtype, "");
558                 else if (dtype == LLVMInt16Type () && (stype == LLVMInt8Type ()))
559                         return LLVMBuildSExt (ctx->builder, v, dtype, "");
560                 else if (dtype == LLVMDoubleType () && stype == LLVMFloatType ())
561                         return LLVMBuildFPExt (ctx->builder, v, dtype, "");
562
563                 /* Trunc */
564                 if (stype == LLVMInt64Type () && (dtype == LLVMInt32Type () || dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
565                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
566                 if (stype == LLVMInt32Type () && (dtype == LLVMInt16Type () || dtype == LLVMInt8Type ()))
567                         return LLVMBuildTrunc (ctx->builder, v, dtype, "");
568                 if (stype == LLVMDoubleType () && dtype == LLVMFloatType ())
569                         return LLVMBuildFPTrunc (ctx->builder, v, dtype, "");
570
571                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind && LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
572                         return LLVMBuildBitCast (ctx->builder, v, dtype, "");
573                 if (LLVMGetTypeKind (dtype) == LLVMPointerTypeKind)
574                         return LLVMBuildIntToPtr (ctx->builder, v, dtype, "");
575                 if (LLVMGetTypeKind (stype) == LLVMPointerTypeKind)
576                         return LLVMBuildPtrToInt (ctx->builder, v, dtype, "");
577
578                 LLVMDumpValue (v);
579                 LLVMDumpValue (LLVMConstNull (dtype));
580                 g_assert_not_reached ();
581                 return NULL;
582         } else {
583                 return v;
584         }
585 }
586
587 /*
588  * emit_volatile_store:
589  *
590  *   If VREG is volatile, emit a store from its value to its address.
591  */
592 static void
593 emit_volatile_store (EmitContext *ctx, int vreg)
594 {
595         MonoInst *var = get_vreg_to_inst (ctx->cfg, vreg);
596
597         if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
598                 LLVMBuildStore (ctx->builder, convert (ctx, ctx->values [vreg], type_to_llvm_type (ctx, var->inst_vtype)), ctx->addresses [vreg]);
599         }
600 }
601
602 /*
603  * sig_to_llvm_sig:
604  *
605  *   Return the LLVM signature corresponding to the mono signature SIG using the
606  * calling convention information in CINFO.
607  */
608 static LLVMTypeRef
609 sig_to_llvm_sig (EmitContext *ctx, MonoMethodSignature *sig, LLVMCallInfo *cinfo)
610 {
611         LLVMTypeRef ret_type;
612         LLVMTypeRef *param_types = NULL;
613         LLVMTypeRef res;
614         int i, j, pindex;
615         gboolean vretaddr = FALSE;
616
617         ret_type = type_to_llvm_type (ctx, sig->ret);
618         CHECK_FAILURE (ctx);
619
620         if (cinfo && cinfo->ret.storage == LLVMArgVtypeInReg) {
621                 /* LLVM models this by returning an aggregate value */
622                 if (cinfo->ret.pair_storage [0] == LLVMArgInIReg && cinfo->ret.pair_storage [1] == LLVMArgNone) {
623                         LLVMTypeRef members [2];
624
625                         members [0] = IntPtrType ();
626                         ret_type = LLVMStructType (members, 1, FALSE);
627                 } else {
628                         g_assert_not_reached ();
629                 }
630         } else if (cinfo && MONO_TYPE_ISSTRUCT (sig->ret)) {
631                 g_assert (cinfo->ret.storage == LLVMArgVtypeRetAddr);
632                 vretaddr = TRUE;
633         }
634
635         param_types = g_new0 (LLVMTypeRef, (sig->param_count * 2) + 2);
636         pindex = 0;
637         if (vretaddr) {
638                 ret_type = LLVMVoidType ();
639                 param_types [pindex ++] = IntPtrType ();
640         }
641         if (sig->hasthis)
642                 param_types [pindex ++] = IntPtrType ();
643         for (i = 0; i < sig->param_count; ++i) {
644                 if (cinfo && cinfo->args [i + sig->hasthis].storage == LLVMArgVtypeInReg) {
645                         for (j = 0; j < 2; ++j) {
646                                 switch (cinfo->args [i + sig->hasthis].pair_storage [j]) {
647                                 case LLVMArgInIReg:
648                                         param_types [pindex ++] = LLVMIntType (sizeof (gpointer) * 8);
649                                         break;
650                                 case LLVMArgNone:
651                                         break;
652                                 default:
653                                         g_assert_not_reached ();
654                                 }
655                         }
656                 } else if (cinfo && cinfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal) {
657                         param_types [pindex] = type_to_llvm_arg_type (ctx, sig->params [i]);
658                         CHECK_FAILURE (ctx);
659                         param_types [pindex] = LLVMPointerType (param_types [pindex], 0);
660                         pindex ++;
661                 } else {
662                         param_types [pindex ++] = type_to_llvm_arg_type (ctx, sig->params [i]);
663                 }                       
664         }
665         CHECK_FAILURE (ctx);
666
667         res = LLVMFunctionType (ret_type, param_types, pindex, FALSE);
668         g_free (param_types);
669
670         return res;
671
672  FAILURE:
673         g_free (param_types);
674
675         return NULL;
676 }
677
678 /*
679  * LLVMFunctionType1:
680  *
681  *   Create an LLVM function type from the arguments.
682  */
683 static G_GNUC_UNUSED LLVMTypeRef 
684 LLVMFunctionType1(LLVMTypeRef ReturnType,
685                                   LLVMTypeRef ParamType1,
686                                   int IsVarArg)
687 {
688         LLVMTypeRef param_types [1];
689
690         param_types [0] = ParamType1;
691
692         return LLVMFunctionType (ReturnType, param_types, 1, IsVarArg);
693 }
694
695 /*
696  * LLVMFunctionType2:
697  *
698  *   Create an LLVM function type from the arguments.
699  */
700 static LLVMTypeRef 
701 LLVMFunctionType2(LLVMTypeRef ReturnType,
702                                   LLVMTypeRef ParamType1,
703                                   LLVMTypeRef ParamType2,
704                                   int IsVarArg)
705 {
706         LLVMTypeRef param_types [2];
707
708         param_types [0] = ParamType1;
709         param_types [1] = ParamType2;
710
711         return LLVMFunctionType (ReturnType, param_types, 2, IsVarArg);
712 }
713
714 /*
715  * LLVMFunctionType3:
716  *
717  *   Create an LLVM function type from the arguments.
718  */
719 static LLVMTypeRef 
720 LLVMFunctionType3(LLVMTypeRef ReturnType,
721                                   LLVMTypeRef ParamType1,
722                                   LLVMTypeRef ParamType2,
723                                   LLVMTypeRef ParamType3,
724                                   int IsVarArg)
725 {
726         LLVMTypeRef param_types [3];
727
728         param_types [0] = ParamType1;
729         param_types [1] = ParamType2;
730         param_types [2] = ParamType3;
731
732         return LLVMFunctionType (ReturnType, param_types, 3, IsVarArg);
733 }
734
735 /*
736  * create_builder:
737  *
738  *   Create an LLVM builder and remember it so it can be freed later.
739  */
740 static LLVMBuilderRef
741 create_builder (EmitContext *ctx)
742 {
743         LLVMBuilderRef builder = LLVMCreateBuilder ();
744
745         ctx->builders = g_slist_prepend_mempool (ctx->cfg->mempool, ctx->builders, builder);
746
747         return builder;
748 }
749
750 static LLVMValueRef
751 get_plt_entry (EmitContext *ctx, LLVMTypeRef llvm_sig, MonoJumpInfoType type, gconstpointer data)
752 {
753         NOT_IMPLEMENTED;
754         return NULL;
755 }
756
757 static void
758 emit_cond_throw_pos (EmitContext *ctx)
759 {
760 }
761
762 /*
763  * emit_cond_system_exception:
764  *
765  *   Emit code to throw the exception EXC_TYPE if the condition CMP is false.
766  */
767 static void
768 emit_cond_system_exception (EmitContext *ctx, MonoBasicBlock *bb, const char *exc_type, LLVMValueRef cmp)
769 {
770         char bb_name [128];
771         LLVMBasicBlockRef ex_bb, noex_bb;
772         LLVMBuilderRef builder;
773         MonoClass *exc_class;
774         LLVMValueRef args [2];
775
776         sprintf (bb_name, "EX_BB%d", ctx->ex_index);
777         ex_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
778
779         sprintf (bb_name, "NOEX_BB%d", ctx->ex_index);
780         noex_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
781
782         LLVMBuildCondBr (ctx->builder, cmp, ex_bb, noex_bb);
783
784         ctx->builder = create_builder (ctx);
785         LLVMPositionBuilderAtEnd (ctx->builder, noex_bb);
786
787         ctx->end_bblocks [bb->block_num] = noex_bb;
788
789         exc_class = mono_class_from_name (mono_defaults.corlib, "System", exc_type);
790         g_assert (exc_class);
791
792         /* Emit exception throwing code */
793         builder = create_builder (ctx);
794         LLVMPositionBuilderAtEnd (builder, ex_bb);
795
796         if (!ctx->lmodule->throw_corlib_exception) {
797                 LLVMValueRef callee;
798                 LLVMTypeRef sig;
799
800                 MonoMethodSignature *throw_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
801                 throw_sig->ret = &mono_defaults.void_class->byval_arg;
802                 throw_sig->params [0] = &mono_defaults.int32_class->byval_arg;
803                 throw_sig->params [1] = &mono_defaults.int32_class->byval_arg;
804                 sig = sig_to_llvm_sig (ctx, throw_sig, NULL);
805
806                 if (ctx->cfg->compile_aot) {
807                         callee = get_plt_entry (ctx, sig, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
808                 } else {
809                         callee = LLVMAddFunction (ctx->module, "throw_corlib_exception", sig_to_llvm_sig (ctx, throw_sig, NULL));
810  
811                         LLVMAddGlobalMapping (ee, callee, resolve_patch (ctx->cfg, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception"));
812                 }
813
814                 mono_memory_barrier ();
815                 ctx->lmodule->throw_corlib_exception = callee;
816         }
817
818         args [0] = LLVMConstInt (LLVMInt32Type (), exc_class->type_token, FALSE);
819         /*
820          * FIXME: The offset is 0, this is not a problem for exception handling
821          * in general, because we don't llvm compile methods with handlers, its only
822          * a problem for line numbers in stack traces.
823          */
824         args [1] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
825         LLVMBuildCall (builder, ctx->lmodule->throw_corlib_exception, args, 2, "");
826
827         LLVMBuildUnreachable (builder);
828
829         ctx->ex_index ++;
830 }
831
832 /*
833  * emit_reg_to_vtype:
834  *
835  *   Emit code to store the vtype in the registers REGS to the address ADDRESS.
836  */
837 static void
838 emit_reg_to_vtype (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *regs)
839 {
840         int j, size;
841
842         size = get_vtype_size (t);
843
844         for (j = 0; j < 2; ++j) {
845                 LLVMValueRef index [2], addr;
846                 int part_size = size > sizeof (gpointer) ? sizeof (gpointer) : size;
847                 LLVMTypeRef part_type;
848
849                 if (ainfo->pair_storage [j] == LLVMArgNone)
850                         continue;
851
852                 part_type = LLVMIntType (part_size * 8);
853                 index [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
854                 index [1] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);
855                 addr = LLVMBuildGEP (builder, address, index, 2, "");
856                 switch (ainfo->pair_storage [j]) {
857                 case LLVMArgInIReg:
858                         LLVMBuildStore (builder, convert (ctx, regs [j], part_type), LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (part_type, 0), ""));
859                         break;
860                 case LLVMArgNone:
861                         break;
862                 default:
863                         g_assert_not_reached ();
864                 }
865
866                 size -= sizeof (gpointer);
867         }
868 }
869
870 /*
871  * emit_vtype_to_reg:
872  *
873  *   Emit code to load a vtype at address ADDRESS into registers. Store the registers
874  * into REGS, and the number of registers into NREGS.
875  */
876 static void
877 emit_vtype_to_reg (EmitContext *ctx, LLVMBuilderRef builder, MonoType *t, LLVMValueRef address, LLVMArgInfo *ainfo, LLVMValueRef *regs, guint32 *nregs)
878 {
879         int pindex = 0;
880         int j, size;
881
882         size = get_vtype_size (t);
883
884         for (j = 0; j < 2; ++j) {
885                 LLVMValueRef index [2], addr;
886                 int partsize = size > sizeof (gpointer) ? sizeof (gpointer) : size;
887
888                 if (ainfo->pair_storage [j] == LLVMArgNone)
889                         continue;
890
891                 index [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
892                 index [1] = LLVMConstInt (LLVMInt32Type (), j * sizeof (gpointer), FALSE);                              
893                 addr = LLVMBuildGEP (builder, address, index, 2, "");
894                 switch (ainfo->pair_storage [j]) {
895                 case LLVMArgInIReg:
896                         regs [pindex ++] = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (ctx->builder, addr, LLVMPointerType (LLVMIntType (partsize * 8), 0), ""), ""), IntPtrType ());
897                         break;
898                 case LLVMArgNone:
899                         break;
900                 default:
901                         g_assert_not_reached ();
902                 }
903                 size -= sizeof (gpointer);
904         }
905
906         *nregs = pindex;
907 }
908
909 /*
910  * emit_entry_bb:
911  *
912  *   Emit code to load/convert arguments.
913  */
914 static void
915 emit_entry_bb (EmitContext *ctx, LLVMBuilderRef builder, int *pindexes)
916 {
917         int i, pindex;
918         MonoCompile *cfg = ctx->cfg;
919         MonoMethodSignature *sig = ctx->sig;
920         LLVMCallInfo *linfo = ctx->linfo;
921
922         /*
923          * Handle indirect/volatile variables by allocating memory for them
924          * using 'alloca', and storing their address in a temporary.
925          */
926         for (i = 0; i < cfg->num_varinfo; ++i) {
927                 MonoInst *var = cfg->varinfo [i];
928                 LLVMTypeRef vtype;
929
930                 if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || MONO_TYPE_ISSTRUCT (var->inst_vtype)) {
931                         vtype = type_to_llvm_type (ctx, var->inst_vtype);
932                         CHECK_FAILURE (ctx);
933                         ctx->addresses [var->dreg] = LLVMBuildAlloca (builder, vtype, "");
934                 }
935         }
936
937         for (i = 0; i < sig->param_count; ++i) {
938                 LLVMArgInfo *ainfo = &linfo->args [i + sig->hasthis];
939                 int reg = cfg->args [i + sig->hasthis]->dreg;
940
941                 if (ainfo->storage == LLVMArgVtypeInReg) {
942                         LLVMValueRef regs [2];
943
944                         /* 
945                          * Emit code to save the argument from the registers to 
946                          * the real argument.
947                          */
948                         pindex = pindexes [i];
949                         regs [0] = LLVMGetParam (ctx->lmethod, pindex);
950                         if (ainfo->pair_storage [1] != LLVMArgNone)
951                                 regs [1] = LLVMGetParam (ctx->lmethod, pindex + 1);
952                         else
953                                 regs [1] = NULL;
954
955                         ctx->addresses [reg] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &(mono_class_from_mono_type (sig->params [i])->byval_arg)), "");
956
957                         emit_reg_to_vtype (ctx, builder, sig->params [i], ctx->addresses [reg], ainfo, regs);
958                 } else if (ainfo->storage == LLVMArgVtypeByVal) {
959                         ctx->addresses [reg] = LLVMGetParam (ctx->lmethod, pindexes [i]);
960                 } else {
961                         ctx->values [reg] = convert (ctx, ctx->values [reg], llvm_type_to_stack_type (type_to_llvm_type (ctx, sig->params [i])));
962                 }
963         }
964
965         for (i = 0; i < sig->param_count; ++i)
966                 if (!MONO_TYPE_ISSTRUCT (sig->params [i]))
967                         emit_volatile_store (ctx, cfg->args [i + sig->hasthis]->dreg);
968
969  FAILURE:
970         ;
971 }
972
973 /*
974  * mono_llvm_emit_method:
975  *
976  *   Emit LLVM IL from the mono IL, and compile it to native code using LLVM.
977  */
978 void
979 mono_llvm_emit_method (MonoCompile *cfg)
980 {
981         EmitContext *ctx;
982         MonoMethodSignature *sig;
983         MonoBasicBlock *bb;
984         LLVMTypeRef method_type;
985         LLVMValueRef method = NULL;
986         char *method_name, *debug_name;
987         LLVMValueRef *values, *addresses;
988         LLVMTypeRef *vreg_types;
989         int i, max_block_num, pindex;
990         int *pindexes = NULL;
991         GHashTable *phi_nodes;
992         gboolean last = FALSE;
993         GPtrArray *phi_values;
994         LLVMCallInfo *linfo;
995         GSList *l;
996         LLVMModuleRef module;
997
998         /* The code below might acquire the loader lock, so use it for global locking */
999         mono_loader_lock ();
1000
1001         /* Used to communicate with the callbacks */
1002         TlsSetValue (current_cfg_tls_id, cfg);
1003
1004         ctx = g_new0 (EmitContext, 1);
1005         ctx->cfg = cfg;
1006         ctx->mempool = cfg->mempool;
1007
1008         /*
1009          * This maps vregs to the LLVM instruction defining them
1010          */
1011         values = g_new0 (LLVMValueRef, cfg->next_vreg);
1012         /*
1013          * This maps vregs for volatile variables to the LLVM instruction defining their
1014          * address.
1015          */
1016         addresses = g_new0 (LLVMValueRef, cfg->next_vreg);
1017         vreg_types = g_new0 (LLVMTypeRef, cfg->next_vreg);
1018         phi_nodes = g_hash_table_new (NULL, NULL);
1019         phi_values = g_ptr_array_new ();
1020
1021         ctx->values = values;
1022         ctx->addresses = addresses;
1023  
1024         if (cfg->compile_aot) {
1025                 ctx->lmodule = &aot_module;
1026                 NOT_IMPLEMENTED;
1027         } else {
1028                 ctx->lmodule = &jit_module;
1029                 method_name = mono_method_full_name (cfg->method, TRUE);
1030                 debug_name = NULL;
1031         }
1032         
1033         module = ctx->module = ctx->lmodule->module;
1034
1035 #if 1
1036         {
1037                 static int count = 0;
1038                 count ++;
1039
1040                 if (getenv ("LLVM_COUNT")) {
1041                         if (count == atoi (getenv ("LLVM_COUNT"))) {
1042                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1043                                 last = TRUE;
1044                         }
1045                         if (count > atoi (getenv ("LLVM_COUNT")))
1046                                 LLVM_FAILURE (ctx, "");
1047                 }
1048         }
1049 #endif
1050
1051         sig = mono_method_signature (cfg->method);
1052         ctx->sig = sig;
1053
1054         linfo = mono_arch_get_llvm_call_info (cfg, sig);
1055         ctx->linfo = linfo;
1056         CHECK_FAILURE (ctx);
1057
1058         method_type = sig_to_llvm_sig (ctx, sig, linfo);
1059         CHECK_FAILURE (ctx);
1060
1061         method = LLVMAddFunction (module, method_name, method_type);
1062         ctx->lmethod = method;
1063         g_free (method_name);
1064
1065         LLVMSetLinkage (method, LLVMPrivateLinkage);
1066
1067         if (cfg->method->save_lmf)
1068                 LLVM_FAILURE (ctx, "lmf");
1069
1070         if (sig->pinvoke)
1071                 LLVM_FAILURE (ctx, "pinvoke signature");
1072
1073         /* 
1074          * This maps parameter indexes in the original signature to the indexes in
1075          * the LLVM signature.
1076          */
1077         pindexes = g_new0 (int, sig->param_count);
1078         pindex = 0;
1079         if (cfg->vret_addr) {
1080                 values [cfg->vret_addr->dreg] = LLVMGetParam (method, pindex);
1081                 pindex ++;
1082         }
1083         if (sig->hasthis) {
1084                 values [cfg->args [0]->dreg] = LLVMGetParam (method, pindex);
1085                 pindex ++;
1086         }
1087         for (i = 0; i < sig->param_count; ++i) {
1088                 values [cfg->args [i + sig->hasthis]->dreg] = LLVMGetParam (method, pindex);
1089                 pindexes [i] = pindex;
1090                 if (linfo->args [i + sig->hasthis].storage == LLVMArgVtypeInReg) {
1091                         if (linfo->args [i + sig->hasthis].pair_storage [0] != LLVMArgNone)
1092                                 pindex ++;
1093                         if (linfo->args [i + sig->hasthis].pair_storage [1] != LLVMArgNone)
1094                                 pindex ++;
1095                 } else if (linfo->args [i + sig->hasthis].storage == LLVMArgVtypeByVal) {
1096                         LLVMAddAttribute (LLVMGetParam (method, pindex), LLVMByValAttribute);
1097                         pindex ++;
1098                 } else {
1099                         pindex ++;
1100                 }
1101         }
1102
1103         max_block_num = 0;
1104         for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
1105                 max_block_num = MAX (max_block_num, bb->block_num);
1106         ctx->bblocks = g_new0 (LLVMBasicBlockRef, max_block_num + 1);
1107         ctx->end_bblocks = g_new0 (LLVMBasicBlockRef, max_block_num + 1);
1108
1109         /* Add branches between non-consecutive bblocks */
1110         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1111                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
1112                         bb->next_bb != bb->last_ins->inst_false_bb) {
1113                         
1114                         MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
1115                         inst->opcode = OP_BR;
1116                         inst->inst_target_bb = bb->last_ins->inst_false_bb;
1117                         mono_bblock_add_inst (bb, inst);
1118                 }
1119         }
1120
1121         /*
1122          * Make a first pass over the code to precreate PHI nodes.
1123          */
1124         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1125                 MonoInst *ins;
1126                 LLVMBuilderRef builder;
1127                 char *dname;
1128                 char dname_buf[128];
1129
1130                 builder = create_builder (ctx);
1131
1132                 for (ins = bb->code; ins; ins = ins->next) {
1133                         switch (ins->opcode) {
1134                         case OP_PHI:
1135                         case OP_FPHI: {
1136                                 LLVMTypeRef phi_type = llvm_type_to_stack_type (type_to_llvm_type (ctx, &ins->klass->byval_arg));
1137
1138                                 CHECK_FAILURE (ctx);
1139
1140                                 /* 
1141                                  * Have to precreate these, as they can be referenced by
1142                                  * earlier instructions.
1143                                  */
1144                                 sprintf (dname_buf, "t%d", ins->dreg);
1145                                 dname = dname_buf;
1146                                 values [ins->dreg] = LLVMBuildPhi (builder, phi_type, dname);
1147
1148                                 g_ptr_array_add (phi_values, values [ins->dreg]);
1149
1150                                 /* 
1151                                  * Set the expected type of the incoming arguments since these have
1152                                  * to have the same type.
1153                                  */
1154                                 for (i = 0; i < ins->inst_phi_args [0]; i++) {
1155                                         int sreg1 = ins->inst_phi_args [i + 1];
1156                                         
1157                                         if (sreg1 != -1)
1158                                                 vreg_types [sreg1] = phi_type;
1159                                 }
1160                                 break;
1161                                 }
1162                         default:
1163                                 break;
1164                         }
1165                 }
1166         }
1167
1168         /*
1169          * Second pass: generate code.
1170          */
1171         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1172                 MonoInst *ins;
1173                 LLVMBasicBlockRef cbb;
1174                 LLVMBuilderRef builder;
1175                 gboolean has_terminator;
1176                 LLVMValueRef v;
1177                 LLVMValueRef lhs, rhs;
1178
1179                 if (!(bb == cfg->bb_entry || bb->in_count > 0))
1180                         continue;
1181
1182                 cbb = get_bb (ctx, bb);
1183                 builder = create_builder (ctx);
1184                 ctx->builder = builder;
1185                 LLVMPositionBuilderAtEnd (builder, cbb);
1186
1187                 if (bb == cfg->bb_entry)
1188                         emit_entry_bb (ctx, builder, pindexes);
1189                 CHECK_FAILURE (ctx);
1190
1191                 has_terminator = FALSE;
1192                 for (ins = bb->code; ins; ins = ins->next) {
1193                         const char *spec = LLVM_INS_INFO (ins->opcode);
1194                         char *dname = NULL;
1195                         char dname_buf [128];
1196
1197                         if (has_terminator)
1198                                 /* There could be instructions after a terminator, skip them */
1199                                 break;
1200
1201                         if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins)) {
1202                                 sprintf (dname_buf, "t%d", ins->dreg);
1203                                 dname = dname_buf;
1204                         }
1205
1206                         if (spec [MONO_INST_SRC1] != ' ' && spec [MONO_INST_SRC1] != 'v') {
1207                                 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg1);
1208
1209                                 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1210                                         lhs = LLVMBuildLoad (builder, addresses [ins->sreg1], "");
1211                                 } else {
1212                                         /* It is ok for SETRET to have an uninitialized argument */
1213                                         if (!values [ins->sreg1] && ins->opcode != OP_SETRET)
1214                                                 LLVM_FAILURE (ctx, "sreg1");
1215                                         lhs = values [ins->sreg1];
1216                                 }
1217                         } else {
1218                                 lhs = NULL;
1219                         }
1220
1221                         if (spec [MONO_INST_SRC2] != ' ' && spec [MONO_INST_SRC2] != ' ') {
1222                                 MonoInst *var = get_vreg_to_inst (cfg, ins->sreg2);
1223                                 if (var && var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) {
1224                                         rhs = LLVMBuildLoad (builder, addresses [ins->sreg2], "");
1225                                 } else {
1226                                         if (!values [ins->sreg2])
1227                                                 LLVM_FAILURE (ctx, "sreg2");
1228                                         rhs = values [ins->sreg2];
1229                                 }
1230                         } else {
1231                                 rhs = NULL;
1232                         }
1233
1234                         //mono_print_ins (ins);
1235                         switch (ins->opcode) {
1236                         case OP_NOP:
1237                         case OP_NOT_NULL:
1238                         case OP_LIVERANGE_START:
1239                         case OP_LIVERANGE_END:
1240                                 break;
1241                         case OP_ICONST:
1242                                 values [ins->dreg] = LLVMConstInt (LLVMInt32Type (), ins->inst_c0, FALSE);
1243                                 break;
1244                         case OP_I8CONST:
1245 #if SIZEOF_VOID_P == 4
1246                                 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
1247 #else
1248                                 values [ins->dreg] = LLVMConstInt (LLVMInt64Type (), (gint64)ins->inst_c0, FALSE);
1249 #endif
1250                                 break;
1251                         case OP_R8CONST:
1252                                 values [ins->dreg] = LLVMConstReal (LLVMDoubleType (), *(double*)ins->inst_p0);
1253                                 break;
1254                         case OP_R4CONST:
1255                                 values [ins->dreg] = LLVMConstFPExt (LLVMConstReal (LLVMFloatType (), *(float*)ins->inst_p0), LLVMDoubleType ());
1256                                 break;
1257                         case OP_BR:
1258                                 LLVMBuildBr (builder, get_bb (ctx, ins->inst_target_bb));
1259                                 has_terminator = TRUE;
1260                                 break;
1261                         case OP_SWITCH: {
1262                                 int i;
1263                                 LLVMValueRef v;
1264                                 char bb_name [128];
1265                                 LLVMBasicBlockRef new_bb;
1266                                 LLVMBuilderRef new_builder;
1267
1268                                 // The default branch is already handled
1269                                 // FIXME: Handle it here
1270
1271                                 /* Start new bblock */
1272                                 sprintf (bb_name, "SWITCH_DEFAULT_BB%d", ctx->default_index ++);
1273                                 new_bb = LLVMAppendBasicBlock (ctx->lmethod, bb_name);
1274
1275                                 lhs = convert (ctx, lhs, LLVMInt32Type ());
1276                                 v = LLVMBuildSwitch (builder, lhs, new_bb, GPOINTER_TO_UINT (ins->klass));
1277                                 for (i = 0; i < GPOINTER_TO_UINT (ins->klass); ++i) {
1278                                         MonoBasicBlock *target_bb = ins->inst_many_bb [i];
1279
1280                                         LLVMAddCase (v, LLVMConstInt (LLVMInt32Type (), i, FALSE), get_bb (ctx, target_bb));
1281                                 }
1282
1283                                 new_builder = create_builder (ctx);
1284                                 LLVMPositionBuilderAtEnd (new_builder, new_bb);
1285                                 LLVMBuildUnreachable (new_builder);
1286
1287                                 has_terminator = TRUE;
1288                                 g_assert (!ins->next);
1289                                 
1290                                 break;
1291                         }
1292
1293                         case OP_SETRET:
1294                                 if (linfo->ret.storage == LLVMArgVtypeInReg) {
1295                                         LLVMTypeRef ret_type = LLVMGetReturnType (LLVMGetElementType (LLVMTypeOf (method)));
1296                                         LLVMValueRef part1, retval;
1297                                         int size;
1298
1299                                         size = get_vtype_size (sig->ret);
1300
1301                                         g_assert (addresses [ins->sreg1]);
1302
1303                                         g_assert (linfo->ret.pair_storage [0] == LLVMArgInIReg);
1304                                         g_assert (linfo->ret.pair_storage [1] == LLVMArgNone);
1305                                         
1306                                         part1 = convert (ctx, LLVMBuildLoad (builder, LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMIntType (size * 8), 0), ""), ""), IntPtrType ());
1307
1308                                         retval = LLVMBuildInsertValue (builder, LLVMGetUndef (ret_type), part1, 0, "");
1309
1310                                         LLVMBuildRet (builder, retval);
1311                                         break;
1312                                 }
1313
1314                                 if (!lhs) {
1315                                         /* 
1316                                          * The method did not set its return value, probably because it
1317                                          * ends with a throw.
1318                                          */
1319                                         if (cfg->vret_addr)
1320                                                 LLVMBuildRetVoid (builder);
1321                                         else
1322                                                 LLVMBuildRet (builder, LLVMConstNull (type_to_llvm_type (ctx, sig->ret)));
1323                                 } else {
1324                                         LLVMBuildRet (builder, convert (ctx, lhs, type_to_llvm_type (ctx, sig->ret)));
1325                                 }
1326                                 has_terminator = TRUE;
1327                                 break;
1328                         case OP_ICOMPARE:
1329                         case OP_FCOMPARE:
1330                         case OP_LCOMPARE:
1331                         case OP_COMPARE:
1332                         case OP_ICOMPARE_IMM:
1333                         case OP_LCOMPARE_IMM:
1334                         case OP_COMPARE_IMM:
1335 #ifdef TARGET_AMD64
1336                         case OP_AMD64_ICOMPARE_MEMBASE_REG:
1337                         case OP_AMD64_ICOMPARE_MEMBASE_IMM:
1338 #endif
1339 #ifdef TARGET_X86
1340                         case OP_X86_COMPARE_MEMBASE_REG:
1341                         case OP_X86_COMPARE_MEMBASE_IMM:
1342 #endif
1343                         {
1344                                 CompRelation rel;
1345                                 LLVMValueRef cmp;
1346
1347                                 if (ins->next->opcode == OP_NOP)
1348                                         break;
1349
1350                                 if (ins->next->opcode == OP_BR)
1351                                         /* The comparison result is not needed */
1352                                         continue;
1353
1354                                 rel = mono_opcode_to_cond (ins->next->opcode);
1355
1356                                 /* Used for implementing bound checks */
1357 #ifdef TARGET_AMD64
1358                                 if ((ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_REG) || (ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_IMM)) {
1359                                         int size = 4;
1360                                         LLVMValueRef index;
1361                                         LLVMTypeRef t;
1362
1363                                         t = LLVMInt32Type ();
1364
1365                                         g_assert (ins->inst_offset % size == 0);
1366                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
1367
1368                                         lhs = LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, ""), "");
1369                                 }
1370                                 if (ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_IMM) {
1371                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1372                                         rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1373                                 }
1374                                 if (ins->opcode == OP_AMD64_ICOMPARE_MEMBASE_REG)
1375                                         rhs = convert (ctx, rhs, LLVMInt32Type ());
1376 #endif
1377
1378 #ifdef TARGET_X86
1379                                 if ((ins->opcode == OP_X86_COMPARE_MEMBASE_REG) || (ins->opcode == OP_X86_COMPARE_MEMBASE_IMM)) {
1380                                         int size = 4;
1381                                         LLVMValueRef index;
1382                                         LLVMTypeRef t;
1383
1384                                         t = LLVMInt32Type ();
1385
1386                                         g_assert (ins->inst_offset % size == 0);
1387                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
1388
1389                                         lhs = LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, ""), "");
1390                                 }
1391                                 if (ins->opcode == OP_X86_COMPARE_MEMBASE_IMM) {
1392                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1393                                         rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1394                                 }
1395                                 if (ins->opcode == OP_X86_COMPARE_MEMBASE_REG)
1396                                         rhs = convert (ctx, rhs, LLVMInt32Type ());
1397 #endif
1398
1399                                 if (ins->opcode == OP_ICOMPARE_IMM) {
1400                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1401                                         rhs = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1402                                 }
1403                                 if (ins->opcode == OP_LCOMPARE_IMM) {
1404                                         lhs = convert (ctx, lhs, LLVMInt64Type ());
1405                                         rhs = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
1406                                 }
1407                                 if (ins->opcode == OP_LCOMPARE) {
1408                                         lhs = convert (ctx, lhs, LLVMInt64Type ());
1409                                         rhs = convert (ctx, rhs, LLVMInt64Type ());
1410                                 }
1411                                 if (ins->opcode == OP_ICOMPARE) {
1412                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1413                                         rhs = convert (ctx, rhs, LLVMInt32Type ());
1414                                 }
1415
1416                                 if (lhs && rhs) {
1417                                         if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
1418                                                 rhs = convert (ctx, rhs, LLVMTypeOf (lhs));
1419                                         else if (LLVMGetTypeKind (LLVMTypeOf (rhs)) == LLVMPointerTypeKind)
1420                                                 lhs = convert (ctx, lhs, LLVMTypeOf (rhs));
1421                                 }
1422
1423                                 /* We use COMPARE+SETcc/Bcc, llvm uses SETcc+br cond */
1424                                 if (ins->opcode == OP_FCOMPARE)
1425                                         cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
1426                                 else if (ins->opcode == OP_COMPARE_IMM)
1427                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), "");
1428                                 else if (ins->opcode == OP_COMPARE)
1429                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], convert (ctx, lhs, IntPtrType ()), convert (ctx, rhs, IntPtrType ()), "");
1430                                 else
1431                                         cmp = LLVMBuildICmp (builder, cond_to_llvm_cond [rel], lhs, rhs, "");
1432
1433                                 if (MONO_IS_COND_BRANCH_OP (ins->next)) {
1434                                         LLVMBuildCondBr (builder, cmp, get_bb (ctx, ins->next->inst_true_bb), get_bb (ctx, ins->next->inst_false_bb));
1435                                         has_terminator = TRUE;
1436                                 } else if (MONO_IS_SETCC (ins->next)) {
1437                                         sprintf (dname_buf, "t%d", ins->next->dreg);
1438                                         dname = dname_buf;
1439                                         values [ins->next->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
1440
1441                                         /* Add stores for volatile variables */
1442                                         emit_volatile_store (ctx, ins->next->dreg);
1443                                 } else if (MONO_IS_COND_EXC (ins->next)) {
1444                                         //emit_cond_throw_pos (ctx);
1445                                         emit_cond_system_exception (ctx, bb, ins->next->inst_p1, cmp);
1446                                         builder = ctx->builder;
1447                                 } else {
1448                                         LLVM_FAILURE (ctx, "next");
1449                                 }
1450
1451                                 ins = ins->next;
1452                                 break;
1453                         }
1454                         case OP_FCEQ:
1455                         case OP_FCLT:
1456                         case OP_FCLT_UN:
1457                         case OP_FCGT:
1458                         case OP_FCGT_UN: {
1459                                 CompRelation rel;
1460                                 LLVMValueRef cmp;
1461
1462                                 rel = mono_opcode_to_cond (ins->opcode);
1463
1464                                 cmp = LLVMBuildFCmp (builder, fpcond_to_llvm_cond [rel], convert (ctx, lhs, LLVMDoubleType ()), convert (ctx, rhs, LLVMDoubleType ()), "");
1465                                 values [ins->dreg] = LLVMBuildZExt (builder, cmp, LLVMInt32Type (), dname);
1466                                 break;
1467                         }
1468                         case OP_PHI:
1469                         case OP_FPHI: {
1470                                 int i;
1471
1472                                 /* Created earlier, insert it now */
1473                                 LLVMInsertIntoBuilder (builder, values [ins->dreg]);
1474
1475                                 /* Check that all input bblocks really branch to us */
1476                                 for (i = 0; i < bb->in_count; ++i) {
1477                                         if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_NOT_REACHED)
1478                                                 ins->inst_phi_args [i + 1] = -1;
1479                                 }
1480
1481                                 // FIXME: If a SWITCH statement branches to the same bblock in more
1482                                 // than once case, the PHI should reference the bblock multiple times
1483                                 for (i = 0; i < bb->in_count; ++i)
1484                                         if (bb->in_bb [i]->last_ins && bb->in_bb [i]->last_ins->opcode == OP_SWITCH) {
1485                                                 LLVM_FAILURE (ctx, "switch + phi");
1486                                                 break;
1487                                         }
1488
1489                                 for (i = 0; i < ins->inst_phi_args [0]; i++) {
1490                                         int sreg1 = ins->inst_phi_args [i + 1];
1491                                         LLVMBasicBlockRef in_bb;
1492
1493                                         if (sreg1 == -1)
1494                                                 continue;
1495
1496                                         /* Add incoming values which are already defined */
1497                                         if (FALSE && values [sreg1]) {
1498                                                 in_bb = get_end_bb (ctx, bb->in_bb [i]);
1499
1500                                                 g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [ins->dreg]));
1501                                                 LLVMAddIncoming (values [ins->dreg], &values [sreg1], &in_bb, 1);
1502                                         } else {
1503                                                 /* Remember for later */
1504                                                 //LLVM_FAILURE (ctx, "phi incoming value");
1505                                                 GSList *node_list = g_hash_table_lookup (phi_nodes, GUINT_TO_POINTER (bb->in_bb [i]));
1506                                                 PhiNode *node = mono_mempool_alloc0 (ctx->mempool, sizeof (PhiNode));
1507                                                 node->bb = bb;
1508                                                 node->phi = ins;
1509                                                 node->index = i;
1510                                                 node_list = g_slist_prepend_mempool (ctx->mempool, node_list, node);
1511                                                 g_hash_table_insert (phi_nodes, GUINT_TO_POINTER (bb->in_bb [i]), node_list);
1512                                         }
1513                                 }
1514                                 break;
1515                         }
1516                         case OP_MOVE:
1517                         case OP_LMOVE:
1518                                 g_assert (lhs);
1519                                 values [ins->dreg] = lhs;
1520                                 break;
1521                         case OP_FMOVE: {
1522                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
1523                                 
1524                                 g_assert (lhs);
1525                                 values [ins->dreg] = lhs;
1526
1527                                 if (var && var->klass->byval_arg.type == MONO_TYPE_R4) {
1528                                         /* 
1529                                          * This is added by the spilling pass in case of the JIT,
1530                                          * but we have to do it ourselves.
1531                                          */
1532                                         values [ins->dreg] = convert (ctx, values [ins->dreg], LLVMFloatType ());
1533                                 }
1534                                 break;
1535                         }
1536                         case OP_IADD:
1537                         case OP_ISUB:
1538                         case OP_IAND:
1539                         case OP_IMUL:
1540                         case OP_IDIV:
1541                         case OP_IDIV_UN:
1542                         case OP_IREM:
1543                         case OP_IREM_UN:
1544                         case OP_IOR:
1545                         case OP_IXOR:
1546                         case OP_ISHL:
1547                         case OP_ISHR:
1548                         case OP_ISHR_UN:
1549                         case OP_FADD:
1550                         case OP_FSUB:
1551                         case OP_FMUL:
1552                         case OP_FDIV:
1553                         case OP_LADD:
1554                         case OP_LSUB:
1555                         case OP_LMUL:
1556                         case OP_LDIV:
1557                         case OP_LDIV_UN:
1558                         case OP_LREM:
1559                         case OP_LREM_UN:
1560                         case OP_LAND:
1561                         case OP_LOR:
1562                         case OP_LXOR:
1563                         case OP_LSHL:
1564                         case OP_LSHR:
1565                         case OP_LSHR_UN:
1566                                 lhs = convert (ctx, lhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
1567                                 rhs = convert (ctx, rhs, regtype_to_llvm_type (spec [MONO_INST_DEST]));
1568
1569                                 switch (ins->opcode) {
1570                                 case OP_IADD:
1571                                 case OP_FADD:
1572                                 case OP_LADD:
1573                                         values [ins->dreg] = LLVMBuildAdd (builder, lhs, rhs, dname);
1574                                         break;
1575                                 case OP_ISUB:
1576                                 case OP_FSUB:
1577                                 case OP_LSUB:
1578                                         values [ins->dreg] = LLVMBuildSub (builder, lhs, rhs, dname);
1579                                         break;
1580                                 case OP_IMUL:
1581                                 case OP_FMUL:
1582                                 case OP_LMUL:
1583                                         values [ins->dreg] = LLVMBuildMul (builder, lhs, rhs, dname);
1584                                         break;
1585                                 case OP_IREM:
1586                                 case OP_LREM:
1587                                         values [ins->dreg] = LLVMBuildSRem (builder, lhs, rhs, dname);
1588                                         break;
1589                                 case OP_IREM_UN:
1590                                 case OP_LREM_UN:
1591                                         values [ins->dreg] = LLVMBuildURem (builder, lhs, rhs, dname);
1592                                         break;
1593                                 case OP_IDIV:
1594                                 case OP_LDIV:
1595                                         values [ins->dreg] = LLVMBuildSDiv (builder, lhs, rhs, dname);
1596                                         break;
1597                                 case OP_IDIV_UN:
1598                                 case OP_LDIV_UN:
1599                                         values [ins->dreg] = LLVMBuildUDiv (builder, lhs, rhs, dname);
1600                                         break;
1601                                 case OP_FDIV:
1602                                         values [ins->dreg] = LLVMBuildFDiv (builder, lhs, rhs, dname);
1603                                         break;
1604                                 case OP_IAND:
1605                                 case OP_LAND:
1606                                         values [ins->dreg] = LLVMBuildAnd (builder, lhs, rhs, dname);
1607                                         break;
1608                                 case OP_IOR:
1609                                 case OP_LOR:
1610                                         values [ins->dreg] = LLVMBuildOr (builder, lhs, rhs, dname);
1611                                         break;
1612                                 case OP_IXOR:
1613                                 case OP_LXOR:
1614                                         values [ins->dreg] = LLVMBuildXor (builder, lhs, rhs, dname);
1615                                         break;
1616                                 case OP_ISHL:
1617                                 case OP_LSHL:
1618                                         values [ins->dreg] = LLVMBuildShl (builder, lhs, rhs, dname);
1619                                         break;
1620                                 case OP_ISHR:
1621                                 case OP_LSHR:
1622                                         values [ins->dreg] = LLVMBuildAShr (builder, lhs, rhs, dname);
1623                                         break;
1624                                 case OP_ISHR_UN:
1625                                 case OP_LSHR_UN:
1626                                         values [ins->dreg] = LLVMBuildLShr (builder, lhs, rhs, dname);
1627                                         break;
1628                                 default:
1629                                         g_assert_not_reached ();
1630                                 }
1631                                 break;
1632                         case OP_IADD_IMM:
1633                         case OP_ISUB_IMM:
1634                         case OP_IMUL_IMM:
1635                         case OP_IREM_IMM:
1636                         case OP_IREM_UN_IMM:
1637                         case OP_IDIV_IMM:
1638                         case OP_IDIV_UN_IMM:
1639                         case OP_IAND_IMM:
1640                         case OP_IOR_IMM:
1641                         case OP_IXOR_IMM:
1642                         case OP_ISHL_IMM:
1643                         case OP_ISHR_IMM:
1644                         case OP_ISHR_UN_IMM:
1645                         case OP_LADD_IMM:
1646                         case OP_LSUB_IMM:
1647                         case OP_LREM_IMM:
1648                         case OP_LAND_IMM:
1649                         case OP_LOR_IMM:
1650                         case OP_LXOR_IMM:
1651                         case OP_LSHL_IMM:
1652                         case OP_LSHR_IMM:
1653                         case OP_LSHR_UN_IMM:
1654                         case OP_ADD_IMM:
1655                         case OP_AND_IMM:
1656                         case OP_MUL_IMM:
1657                         case OP_SHL_IMM: {
1658                                 LLVMValueRef imm;
1659
1660                                 if (spec [MONO_INST_SRC1] == 'l') {
1661                                         imm = LLVMConstInt (LLVMInt64Type (), GET_LONG_IMM (ins), FALSE);
1662                                 } else {
1663                                         imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1664                                 }
1665
1666 #if SIZEOF_VOID_P == 4
1667                                 if (ins->opcode == OP_LSHL_IMM || ins->opcode == OP_LSHR_IMM || ins->opcode == OP_LSHR_UN_IMM)
1668                                         imm = LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE);
1669 #endif
1670
1671                                 if (LLVMGetTypeKind (LLVMTypeOf (lhs)) == LLVMPointerTypeKind)
1672                                         lhs = convert (ctx, lhs, IntPtrType ());
1673                                 imm = convert (ctx, imm, LLVMTypeOf (lhs));
1674                                 switch (ins->opcode) {
1675                                 case OP_IADD_IMM:
1676                                 case OP_LADD_IMM:
1677                                 case OP_ADD_IMM:
1678                                         values [ins->dreg] = LLVMBuildAdd (builder, lhs, imm, dname);
1679                                         break;
1680                                 case OP_ISUB_IMM:
1681                                 case OP_LSUB_IMM:
1682                                         values [ins->dreg] = LLVMBuildSub (builder, lhs, imm, dname);
1683                                         break;
1684                                 case OP_IMUL_IMM:
1685                                 case OP_MUL_IMM:
1686                                         values [ins->dreg] = LLVMBuildMul (builder, lhs, imm, dname);
1687                                         break;
1688                                 case OP_IDIV_IMM:
1689                                 case OP_LDIV_IMM:
1690                                         values [ins->dreg] = LLVMBuildSDiv (builder, lhs, imm, dname);
1691                                         break;
1692                                 case OP_IDIV_UN_IMM:
1693                                 case OP_LDIV_UN_IMM:
1694                                         values [ins->dreg] = LLVMBuildUDiv (builder, lhs, imm, dname);
1695                                         break;
1696                                 case OP_IREM_IMM:
1697                                 case OP_LREM_IMM:
1698                                         values [ins->dreg] = LLVMBuildSRem (builder, lhs, imm, dname);
1699                                         break;
1700                                 case OP_IREM_UN_IMM:
1701                                         values [ins->dreg] = LLVMBuildURem (builder, lhs, imm, dname);
1702                                         break;
1703                                 case OP_IAND_IMM:
1704                                 case OP_LAND_IMM:
1705                                 case OP_AND_IMM:
1706                                         values [ins->dreg] = LLVMBuildAnd (builder, lhs, imm, dname);
1707                                         break;
1708                                 case OP_IOR_IMM:
1709                                 case OP_LOR_IMM:
1710                                         values [ins->dreg] = LLVMBuildOr (builder, lhs, imm, dname);
1711                                         break;
1712                                 case OP_IXOR_IMM:
1713                                 case OP_LXOR_IMM:
1714                                         values [ins->dreg] = LLVMBuildXor (builder, lhs, imm, dname);
1715                                         break;
1716                                 case OP_ISHL_IMM:
1717                                 case OP_LSHL_IMM:
1718                                 case OP_SHL_IMM:
1719                                         values [ins->dreg] = LLVMBuildShl (builder, lhs, imm, dname);
1720                                         break;
1721                                 case OP_ISHR_IMM:
1722                                 case OP_LSHR_IMM:
1723                                         values [ins->dreg] = LLVMBuildAShr (builder, lhs, imm, dname);
1724                                         break;
1725                                 case OP_ISHR_UN_IMM:
1726                                         /* This is used to implement conv.u4, so the lhs could be an i8 */
1727                                         lhs = convert (ctx, lhs, LLVMInt32Type ());
1728                                         imm = convert (ctx, imm, LLVMInt32Type ());
1729                                         values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
1730                                         break;
1731                                 case OP_LSHR_UN_IMM:
1732                                         values [ins->dreg] = LLVMBuildLShr (builder, lhs, imm, dname);
1733                                         break;
1734                                 default:
1735                                         g_assert_not_reached ();
1736                                 }
1737                                 break;
1738                         }
1739                         case OP_INEG:
1740                                 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt32Type (), 0, FALSE), convert (ctx, lhs, LLVMInt32Type ()), dname);
1741                                 break;
1742                         case OP_LNEG:
1743                                 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstInt (LLVMInt64Type (), 0, FALSE), lhs, dname);
1744                                 break;
1745                         case OP_FNEG:
1746                                 values [ins->dreg] = LLVMBuildSub (builder, LLVMConstReal (LLVMDoubleType (), 0.0), lhs, dname);
1747                                 break;
1748                         case OP_INOT: {
1749                                 guint32 v = 0xffffffff;
1750                                 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt32Type (), v, FALSE), lhs, dname);
1751                                 break;
1752                         }
1753                         case OP_LNOT: {
1754                                 guint64 v = 0xffffffffffffffffLL;
1755                                 values [ins->dreg] = LLVMBuildXor (builder, LLVMConstInt (LLVMInt64Type (), v, FALSE), lhs, dname);
1756                                 break;
1757                         }
1758                         case OP_X86_LEA: {
1759                                 LLVMValueRef v1, v2;
1760
1761                                 v1 = LLVMBuildMul (builder, convert (ctx, rhs, IntPtrType ()), LLVMConstInt (IntPtrType (), (1 << ins->backend.shift_amount), FALSE), "");
1762                                 v2 = LLVMBuildAdd (builder, convert (ctx, lhs, IntPtrType ()), v1, "");
1763                                 values [ins->dreg] = LLVMBuildAdd (builder, v2, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), dname);
1764                                 break;
1765                         }
1766
1767                         case OP_ICONV_TO_I1:
1768                         case OP_ICONV_TO_I2:
1769                         case OP_ICONV_TO_I4:
1770                         case OP_ICONV_TO_U1:
1771                         case OP_ICONV_TO_U2:
1772                         case OP_ICONV_TO_U4:
1773                         case OP_LCONV_TO_I1:
1774                         case OP_LCONV_TO_I2:
1775                         case OP_LCONV_TO_U1:
1776                         case OP_LCONV_TO_U2:
1777                         case OP_LCONV_TO_U4: {
1778                                 gboolean sign;
1779
1780                                 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);
1781
1782                                 /* Have to do two casts since our vregs have type int */
1783                                 v = LLVMBuildTrunc (builder, lhs, op_to_llvm_type (ins->opcode), "");
1784                                 if (sign)
1785                                         values [ins->dreg] = LLVMBuildSExt (builder, v, LLVMInt32Type (), dname);
1786                                 else
1787                                         values [ins->dreg] = LLVMBuildZExt (builder, v, LLVMInt32Type (), dname);
1788                                 break;
1789                         }
1790                         case OP_ICONV_TO_I8:
1791                                 values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
1792                                 break;
1793                         case OP_ICONV_TO_U8:
1794                                 values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
1795                                 break;
1796                         case OP_FCONV_TO_I4:
1797                                 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt32Type (), dname);
1798                                 break;
1799                         case OP_FCONV_TO_I1:
1800                                 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
1801                                 break;
1802                         case OP_FCONV_TO_U1:
1803                                 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt8Type (), dname), LLVMInt32Type (), "");
1804                                 break;
1805                         case OP_FCONV_TO_I2:
1806                                 values [ins->dreg] = LLVMBuildSExt (builder, LLVMBuildFPToSI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
1807                                 break;
1808                         case OP_FCONV_TO_U2:
1809                                 values [ins->dreg] = LLVMBuildZExt (builder, LLVMBuildFPToUI (builder, lhs, LLVMInt16Type (), dname), LLVMInt32Type (), "");
1810                                 break;
1811                         case OP_FCONV_TO_I8:
1812                                 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, LLVMInt64Type (), dname);
1813                                 break;
1814                         case OP_FCONV_TO_I:
1815                                 values [ins->dreg] = LLVMBuildFPToSI (builder, lhs, IntPtrType (), dname);
1816                                 break;
1817                         case OP_ICONV_TO_R8:
1818                         case OP_LCONV_TO_R8:
1819                                 values [ins->dreg] = LLVMBuildSIToFP (builder, lhs, LLVMDoubleType (), dname);
1820                                 break;
1821                         case OP_LCONV_TO_R_UN:
1822                                 values [ins->dreg] = LLVMBuildUIToFP (builder, lhs, LLVMDoubleType (), dname);
1823                                 break;
1824 #if SIZEOF_VOID_P == 4
1825                         case OP_LCONV_TO_U:
1826 #endif
1827                         case OP_LCONV_TO_I4:
1828                                 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
1829                                 break;
1830                         case OP_ICONV_TO_R4:
1831                         case OP_LCONV_TO_R4:
1832                                 v = LLVMBuildSIToFP (builder, lhs, LLVMFloatType (), "");
1833                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
1834                                 break;
1835                         case OP_FCONV_TO_R4:
1836                                 v = LLVMBuildFPTrunc (builder, lhs, LLVMFloatType (), "");
1837                                 values [ins->dreg] = LLVMBuildFPExt (builder, v, LLVMDoubleType (), dname);
1838                                 break;
1839                         case OP_SEXT_I4:
1840                                 values [ins->dreg] = LLVMBuildSExt (builder, lhs, LLVMInt64Type (), dname);
1841                                 break;
1842                         case OP_ZEXT_I4:
1843                                 values [ins->dreg] = LLVMBuildZExt (builder, lhs, LLVMInt64Type (), dname);
1844                                 break;
1845                         case OP_TRUNC_I4:
1846                                 values [ins->dreg] = LLVMBuildTrunc (builder, lhs, LLVMInt32Type (), dname);
1847                                 break;
1848                         case OP_LOCALLOC_IMM: {
1849                                 LLVMValueRef v;
1850
1851                                 guint32 size = ins->inst_imm;
1852                                 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
1853
1854                                 v = mono_llvm_build_alloca (builder, LLVMInt8Type (), LLVMConstInt (LLVMInt32Type (), size, FALSE), MONO_ARCH_FRAME_ALIGNMENT, "");
1855
1856                                 if (ins->flags & MONO_INST_INIT) {
1857                                         LLVMValueRef args [4];
1858
1859                                         args [0] = v;
1860                                         args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
1861                                         args [2] = LLVMConstInt (LLVMInt32Type (), size, FALSE);
1862                                         args [3] = LLVMConstInt (LLVMInt32Type (), MONO_ARCH_FRAME_ALIGNMENT, FALSE);
1863                                         LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memset.i32"), args, 4, "");
1864                                 }
1865
1866                                 values [ins->dreg] = v;
1867                                 break;
1868                         }
1869                         case OP_LOADI1_MEMBASE:
1870                         case OP_LOADU1_MEMBASE:
1871                         case OP_LOADI2_MEMBASE:
1872                         case OP_LOADU2_MEMBASE:
1873                         case OP_LOADI4_MEMBASE:
1874                         case OP_LOADU4_MEMBASE:
1875                         case OP_LOADI8_MEMBASE:
1876                         case OP_LOADR4_MEMBASE:
1877                         case OP_LOADR8_MEMBASE:
1878                         case OP_LOAD_MEMBASE:
1879                         case OP_LOADI8_MEM:
1880                         case OP_LOADU1_MEM:
1881                         case OP_LOADU2_MEM:
1882                         case OP_LOADI4_MEM:
1883                         case OP_LOADU4_MEM:
1884                         case OP_LOAD_MEM: {
1885                                 int size = 8;
1886                                 LLVMValueRef index;
1887                                 LLVMTypeRef t;
1888                                 gboolean sext = FALSE, zext = FALSE;
1889
1890                                 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
1891
1892                                 if (sext || zext)
1893                                         dname = (char*)"";
1894
1895                                 /* 
1896                                  * We emit volatile loads because otherwise LLVM will
1897                                  * generate invalid code when encountering a load from a
1898                                  * NULL address.
1899                                  * FIXME: Avoid this somehow.
1900                                  */
1901                                 g_assert (ins->inst_offset % size == 0);
1902                                 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)) {
1903                                         values [ins->dreg] = mono_llvm_build_volatile_load (builder, convert (ctx, LLVMConstInt (IntPtrType (), ins->inst_imm, FALSE), LLVMPointerType (t, 0)), dname);
1904                                 } else if (ins->inst_offset == 0) {
1905                                         values [ins->dreg] = mono_llvm_build_volatile_load (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), dname);
1906                                 } else {
1907                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
1908                                         values [ins->dreg] = mono_llvm_build_volatile_load (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (t, 0)), &index, 1, ""), dname);
1909                                 }
1910                                 if (sext)
1911                                         values [ins->dreg] = LLVMBuildSExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
1912                                 else if (zext)
1913                                         values [ins->dreg] = LLVMBuildZExt (builder, values [ins->dreg], LLVMInt32Type (), dname);
1914                                 else if (ins->opcode == OP_LOADR4_MEMBASE)
1915                                         values [ins->dreg] = LLVMBuildFPExt (builder, values [ins->dreg], LLVMDoubleType (), dname);
1916                                 break;
1917                         }
1918                                 
1919                         case OP_STOREI1_MEMBASE_REG:
1920                         case OP_STOREI2_MEMBASE_REG:
1921                         case OP_STOREI4_MEMBASE_REG:
1922                         case OP_STOREI8_MEMBASE_REG:
1923                         case OP_STORER4_MEMBASE_REG:
1924                         case OP_STORER8_MEMBASE_REG:
1925                         case OP_STORE_MEMBASE_REG: {
1926                                 int size = 8;
1927                                 LLVMValueRef index;
1928                                 LLVMTypeRef t;
1929                                 gboolean sext = FALSE, zext = FALSE;
1930
1931                                 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
1932
1933                                 g_assert (ins->inst_offset % size == 0);
1934                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
1935                                 LLVMBuildStore (builder, convert (ctx, values [ins->sreg1], t), LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, ""));
1936                                 break;
1937                         }
1938
1939                         case OP_STOREI1_MEMBASE_IMM:
1940                         case OP_STOREI2_MEMBASE_IMM:
1941                         case OP_STOREI4_MEMBASE_IMM:
1942                         case OP_STOREI8_MEMBASE_IMM:
1943                         case OP_STORE_MEMBASE_IMM: {
1944                                 int size = 8;
1945                                 LLVMValueRef index;
1946                                 LLVMTypeRef t;
1947                                 gboolean sext = FALSE, zext = FALSE;
1948
1949                                 t = load_store_to_llvm_type (ins->opcode, &size, &sext, &zext);
1950
1951                                 g_assert (ins->inst_offset % size == 0);
1952                                 index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
1953                                 LLVMBuildStore (builder, convert (ctx, LLVMConstInt (LLVMInt32Type (), ins->inst_imm, FALSE), t), LLVMBuildGEP (builder, convert (ctx, values [ins->inst_destbasereg], LLVMPointerType (t, 0)), &index, 1, ""));
1954                                 break;
1955                         }
1956
1957                         case OP_CHECK_THIS:
1958                                 mono_llvm_build_volatile_load (builder, convert (ctx, values [ins->sreg1], LLVMPointerType (IntPtrType (), 0)), "");
1959                                 break;
1960                         case OP_OUTARG_VTRETADDR:
1961                                 break;
1962                         case OP_VOIDCALL:
1963                         case OP_CALL:
1964                         case OP_LCALL:
1965                         case OP_FCALL:
1966                         case OP_VCALL:
1967                         case OP_VOIDCALL_MEMBASE:
1968                         case OP_CALL_MEMBASE:
1969                         case OP_LCALL_MEMBASE:
1970                         case OP_FCALL_MEMBASE:
1971                         case OP_VCALL_MEMBASE: {
1972                                 MonoCallInst *call = (MonoCallInst*)ins;
1973                                 MonoMethodSignature *sig = call->signature;
1974                                 LLVMValueRef callee, lcall;
1975                                 LLVMValueRef *args;
1976                                 LLVMCallInfo *cinfo;
1977                                 GSList *l;
1978                                 int i, pindex;
1979                                 gboolean vretaddr;
1980                                 LLVMTypeRef llvm_sig;
1981                                 gpointer target;
1982                                 int *pindexes;
1983
1984                                 cinfo = call->cinfo;
1985
1986                                 vretaddr = cinfo && cinfo->ret.storage == LLVMArgVtypeRetAddr;
1987
1988                                 llvm_sig = sig_to_llvm_sig (ctx, sig, cinfo);
1989                                 CHECK_FAILURE (ctx);
1990
1991                                 if (call->rgctx_reg) {
1992                                         LLVM_FAILURE (ctx, "rgctx reg");
1993                                 }
1994
1995                                 pindexes = mono_mempool_alloc0 (cfg->mempool, sig->param_count + 2);
1996
1997                                 /* FIXME: Avoid creating duplicate methods */
1998
1999                                 if (ins->flags & MONO_INST_HAS_METHOD) {
2000                                         if (cfg->compile_aot) {
2001                                                 callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_METHOD, call->method);
2002                                         } else {
2003                                                 callee = LLVMAddFunction (module, "", llvm_sig);
2004  
2005                                                 target = 
2006                                                         mono_create_jit_trampoline_in_domain (mono_domain_get (),
2007                                                                                                                           call->method);
2008                                                 LLVMAddGlobalMapping (ee, callee, target);
2009                                         }
2010                                 } else {
2011                                         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
2012
2013                                         if (info) {
2014                                                 /*
2015                                                 MonoJumpInfo ji;
2016
2017                                                 memset (&ji, 0, sizeof (ji));
2018                                                 ji.type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
2019                                                 ji.data.target = info->name;
2020
2021                                                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &ji, FALSE);
2022                                                 */
2023                                                 if (cfg->compile_aot) {
2024                                                         callee = get_plt_entry (ctx, llvm_sig, MONO_PATCH_INFO_INTERNAL_METHOD, (char*)info->name);
2025                                                 } else {
2026                                                         callee = LLVMAddFunction (module, "", llvm_sig);
2027                                                         target = (gpointer)mono_icall_get_wrapper (info);
2028                                                         LLVMAddGlobalMapping (ee, callee, target);
2029                                                 }
2030                                         } else {
2031                                                 if (cfg->compile_aot) {
2032                                                         callee = NULL;
2033                                                         if (cfg->abs_patches) {
2034                                                                 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2035                                                                 if (abs_ji) {
2036                                                                         callee = get_plt_entry (ctx, llvm_sig, abs_ji->type, abs_ji->data.target);
2037                                                                 }
2038                                                         }
2039                                                         if (!callee)
2040                                                                 LLVM_FAILURE (ctx, "aot");
2041                                                 } else {
2042                                                         callee = LLVMAddFunction (module, "", llvm_sig);
2043                                                         target = NULL;
2044                                                         if (cfg->abs_patches) {
2045                                                                 MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, call->fptr);
2046                                                                 if (abs_ji) {
2047                                                                         target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, abs_ji, FALSE);
2048                                                                         LLVMAddGlobalMapping (ee, callee, target);
2049                                                                 }
2050                                                         }
2051                                                         if (!target)
2052                                                                 LLVMAddGlobalMapping (ee, callee, (gpointer)call->fptr);
2053                                                 }
2054                                         }
2055                                 }
2056
2057                                 if (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) {
2058                                         int size = sizeof (gpointer);
2059                                         LLVMValueRef index;
2060
2061                                         g_assert (ins->inst_offset % size == 0);
2062                                         index = LLVMConstInt (LLVMInt32Type (), ins->inst_offset / size, FALSE);                                
2063
2064                                         callee = convert (ctx, LLVMBuildLoad (builder, LLVMBuildGEP (builder, convert (ctx, values [ins->inst_basereg], LLVMPointerType (LLVMPointerType (IntPtrType (), 0), 0)), &index, 1, ""), ""), LLVMPointerType (llvm_sig, 0));
2065
2066                                         // FIXME: mono_arch_get_vcall_slot () can't decode the code
2067                                         // generated by LLVM
2068                                         //LLVM_FAILURE (ctx, "virtual call");
2069
2070                                         if (call->method && call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
2071                                                 /* No support for passing the IMT argument */
2072                                                 LLVM_FAILURE (ctx, "imt");
2073                                 } else {
2074                                         if (ins->flags & MONO_INST_HAS_METHOD) {
2075                                         }
2076                                 }
2077
2078                                 /* 
2079                                  * Collect and convert arguments
2080                                  */
2081
2082                                 args = alloca (sizeof (LLVMValueRef) * ((sig->param_count * 2) + sig->hasthis + vretaddr));
2083                                 l = call->out_ireg_args;
2084                                 pindex = 0;
2085                                 if (vretaddr) {
2086                                         if (!addresses [call->inst.dreg])
2087                                                 addresses [call->inst.dreg] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &(mono_class_from_mono_type (sig->ret)->byval_arg)), "");
2088                                         args [pindex ++] = LLVMBuildPtrToInt (builder, addresses [call->inst.dreg], IntPtrType (), "");
2089                                 }
2090
2091                                 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2092                                         guint32 regpair;
2093                                         int reg;
2094                                         LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i] : NULL;
2095
2096                                         regpair = (guint32)(gssize)(l->data);
2097                                         reg = regpair & 0xffffff;
2098                                         args [pindex] = values [reg];
2099                                         if (ainfo->storage == LLVMArgVtypeInReg) {
2100                                                 int j;
2101                                                 LLVMValueRef regs [2];
2102                                                 guint32 nregs;
2103
2104                                                 g_assert (ainfo);
2105
2106                                                 g_assert (addresses [reg]);
2107
2108                                                 emit_vtype_to_reg (ctx, builder, sig->params [i - sig->hasthis], addresses [reg], ainfo, regs, &nregs);
2109                                                 for (j = 0; j < nregs; ++j)
2110                                                         args [pindex ++] = regs [j];
2111
2112                                                 // FIXME: alignment
2113                                                 // FIXME: Get rid of the VMOVE
2114                                         } else if (ainfo->storage == LLVMArgVtypeByVal) {
2115                                                 g_assert (addresses [reg]);
2116                                                 args [pindex] = addresses [reg];
2117                                                 pindexes [i] = pindex;
2118                                                 pindex ++;
2119                                         } else {
2120                                                 g_assert (args [pindex]);
2121                                                 if (i == 0 && sig->hasthis)
2122                                                         args [pindex] = convert (ctx, args [pindex], IntPtrType ());
2123                                                 else
2124                                                         args [pindex] = convert (ctx, args [pindex], type_to_llvm_arg_type (ctx, sig->params [i - sig->hasthis]));
2125                                                 pindex ++;
2126                                         }
2127
2128                                         l = l->next;
2129                                 }
2130
2131                                 // FIXME: Align call sites
2132
2133                                 /*
2134                                  * Emit the call
2135                                  */
2136                                 lcall = LLVMBuildCall (builder, callee, args, pindex, "");
2137
2138                                 /* Add byval attributes if needed */
2139                                 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2140                                         LLVMArgInfo *ainfo = call->cinfo ? &call->cinfo->args [i] : NULL;
2141
2142                                         if (ainfo && ainfo->storage == LLVMArgVtypeByVal) {
2143                                                 LLVMAddInstrAttribute (lcall, pindexes [i] + 1, LLVMByValAttribute);
2144                                         }
2145                                 }
2146
2147                                 /*
2148                                  * Convert the result
2149                                  */
2150                                 if (cinfo && cinfo->ret.storage == LLVMArgVtypeInReg) {
2151                                         LLVMValueRef regs [2];
2152
2153                                         if (!addresses [ins->dreg])
2154                                                 addresses [ins->dreg] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &(mono_class_from_mono_type (sig->ret)->byval_arg)), "");
2155
2156                                         regs [0] = LLVMBuildExtractValue (builder, lcall, 0, "");
2157                                         if (cinfo->ret.pair_storage [1] != LLVMArgNone)
2158                                                 regs [1] = LLVMBuildExtractValue (builder, lcall, 1, "");
2159                                         
2160                                         emit_reg_to_vtype (ctx, builder, sig->ret, addresses [ins->dreg], &cinfo->ret, regs);
2161                                 } else if (sig->ret->type != MONO_TYPE_VOID && !vretaddr) {
2162                                         values [ins->dreg] = convert (ctx, lcall, llvm_type_to_stack_type (type_to_llvm_type (ctx, sig->ret)));
2163                                 }
2164                                 break;
2165                         }
2166                         case OP_GOT_ENTRY: {
2167                                 LLVMValueRef indexes [2];
2168
2169                                 indexes [0] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2170                                 indexes [1] = LLVMConstInt (LLVMInt32Type (), (gssize)ins->inst_p0, FALSE);
2171                                 values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildGEP (builder, ctx->lmodule->got_var, indexes, 2, ""), dname);
2172                                 break;
2173                         }
2174                         case OP_THROW: {
2175                                 MonoMethodSignature *throw_sig;
2176                                 LLVMValueRef callee, arg;
2177
2178                                 if (!ctx->lmodule->throw) {
2179                                         throw_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
2180                                         throw_sig->ret = &mono_defaults.void_class->byval_arg;
2181                                         throw_sig->params [0] = &mono_defaults.object_class->byval_arg;
2182                                         if (cfg->compile_aot) {
2183                                                 callee = get_plt_entry (ctx, sig_to_llvm_sig (ctx, throw_sig, NULL), MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_exception");
2184                                         } else {
2185                                                 callee = LLVMAddFunction (module, "mono_arch_throw_exception", sig_to_llvm_sig (ctx, throw_sig, NULL));
2186                                                 LLVMAddGlobalMapping (ee, callee, resolve_patch (cfg, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_exception"));
2187                                         }
2188
2189                                         mono_memory_barrier ();
2190                                         ctx->lmodule->throw = callee;
2191                                 }
2192                                 arg = convert (ctx, values [ins->sreg1], type_to_llvm_type (ctx, &mono_defaults.object_class->byval_arg));
2193                                 LLVMBuildCall (builder, ctx->lmodule->throw, &arg, 1, "");
2194                                 break;
2195                         }
2196                         case OP_NOT_REACHED:
2197                                 LLVMBuildUnreachable (builder);
2198                                 has_terminator = TRUE;
2199                                 /* Might have instructions after this */
2200                                 while (ins->next) {
2201                                         MonoInst *next = ins->next;
2202                                         MONO_DELETE_INS (bb, next);
2203                                 }                               
2204                                 break;
2205                         case OP_LDADDR: {
2206                                 MonoInst *var = ins->inst_p0;
2207
2208                                 values [ins->dreg] = addresses [var->dreg];
2209                                 break;
2210                         }
2211                         case OP_SIN: {
2212                                 LLVMValueRef args [1];
2213
2214                                 args [0] = lhs;
2215                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sin.f64"), args, 1, dname);
2216                                 break;
2217                         }
2218                         case OP_COS: {
2219                                 LLVMValueRef args [1];
2220
2221                                 args [0] = lhs;
2222                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.cos.f64"), args, 1, dname);
2223                                 break;
2224                         }
2225                                 /* test_0_sqrt_nan fails with LLVM */
2226                                 /*
2227                         case OP_SQRT: {
2228                                 LLVMValueRef args [1];
2229
2230                                 args [0] = lhs;
2231                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.sqrt.f64"), args, 1, dname);
2232                                 break;
2233                         }
2234                                 */
2235
2236                         case OP_IMIN:
2237                         case OP_LMIN: {
2238                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntSLE, lhs, rhs, "");
2239                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2240                                 break;
2241                         }
2242                         case OP_IMAX:
2243                         case OP_LMAX: {
2244                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntSGE, lhs, rhs, "");
2245                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2246                                 break;
2247                         }
2248                         case OP_IMIN_UN:
2249                         case OP_LMIN_UN: {
2250                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntULE, lhs, rhs, "");
2251                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2252                                 break;
2253                         }
2254                         case OP_IMAX_UN:
2255                         case OP_LMAX_UN: {
2256                                 LLVMValueRef v = LLVMBuildICmp (builder, LLVMIntUGE, lhs, rhs, "");
2257                                 values [ins->dreg] = LLVMBuildSelect (builder, v, lhs, rhs, dname);
2258                                 break;
2259                         }
2260                         case OP_ATOMIC_EXCHANGE_I4: {
2261                                 LLVMValueRef args [2];
2262
2263                                 g_assert (ins->inst_offset == 0);
2264
2265                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
2266                                 args [1] = rhs;
2267                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i32.p0i32"), args, 2, dname);
2268                                 break;
2269                         }
2270                         case OP_ATOMIC_EXCHANGE_I8: {
2271                                 LLVMValueRef args [2];
2272
2273                                 g_assert (ins->inst_offset == 0);
2274
2275                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
2276                                 args [1] = rhs;
2277                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.swap.i64.p0i64"), args, 2, dname);
2278                                 break;
2279                         }
2280                         case OP_ATOMIC_ADD_NEW_I4: {
2281                                 LLVMValueRef args [2];
2282
2283                                 g_assert (ins->inst_offset == 0);
2284
2285                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt32Type (), 0));
2286                                 args [1] = rhs;
2287                                 values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i32.p0i32"), args, 2, ""), args [1], dname);
2288                                 break;
2289                         }
2290                         case OP_ATOMIC_ADD_NEW_I8: {
2291                                 LLVMValueRef args [2];
2292
2293                                 g_assert (ins->inst_offset == 0);
2294
2295                                 args [0] = convert (ctx, lhs, LLVMPointerType (LLVMInt64Type (), 0));
2296                                 args [1] = convert (ctx, rhs, LLVMInt64Type ());
2297                                 values [ins->dreg] = LLVMBuildAdd (builder, LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.atomic.load.add.i64.p0i64"), args, 2, ""), args [1], dname);
2298                                 break;
2299                         }
2300                         case OP_ATOMIC_CAS_I4:
2301                         case OP_ATOMIC_CAS_I8: {
2302                                 LLVMValueRef args [3];
2303                                 LLVMTypeRef t;
2304                                 const char *intrins;
2305                                 
2306                                 if (ins->opcode == OP_ATOMIC_CAS_I4) {
2307                                         t = LLVMInt32Type ();
2308                                         intrins = "llvm.atomic.cmp.swap.i32.p0i32";
2309                                 } else {
2310                                         t = LLVMInt64Type ();
2311                                         intrins = "llvm.atomic.cmp.swap.i64.p0i64";
2312                                 }
2313
2314                                 args [0] = convert (ctx, lhs, LLVMPointerType (t, 0));
2315                                 /* comparand */
2316                                 args [1] = convert (ctx, values [ins->sreg3], t);
2317                                 /* new value */
2318                                 args [2] = convert (ctx, values [ins->sreg2], t);
2319                                 values [ins->dreg] = LLVMBuildCall (builder, LLVMGetNamedFunction (module, intrins), args, 3, dname);
2320                                 break;
2321                         }
2322                         case OP_MEMORY_BARRIER: {
2323                                 LLVMValueRef args [5];
2324
2325                                 for (i = 0; i < 5; ++i)
2326                                         args [i] = LLVMConstInt (LLVMInt1Type (), TRUE, TRUE);
2327
2328                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memory.barrier"), args, 5, "");
2329                                 break;
2330                         }
2331
2332                         case OP_TLS_GET: {
2333 #if defined(TARGET_AMD64) || defined(TARGET_X86)
2334 #ifdef TARGET_AMD64
2335                                 // 257 == FS segment register
2336                                 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 257);
2337 #else
2338                                 // 256 == GS segment register
2339                                 LLVMTypeRef ptrtype = LLVMPointerType (IntPtrType (), 256);
2340 #endif
2341
2342                                 // FIXME: XEN
2343                                 values [ins->dreg] = LLVMBuildLoad (builder, LLVMBuildIntToPtr (builder, LLVMConstInt (IntPtrType (), ins->inst_offset, TRUE), ptrtype, ""), "");
2344 #else
2345                                 LLVM_FAILURE (ctx, "opcode tls-get");
2346 #endif
2347
2348                                 break;
2349                         }
2350
2351                         /*
2352                          * Overflow opcodes.
2353                          */
2354                         case OP_IADD_OVF:
2355                         case OP_IADD_OVF_UN:
2356                         case OP_ISUB_OVF:
2357                         case OP_ISUB_OVF_UN:
2358                         case OP_IMUL_OVF:
2359                         case OP_IMUL_OVF_UN:
2360 #if SIZEOF_VOID_P == 8
2361                         case OP_LADD_OVF:
2362                         case OP_LSUB_OVF:
2363                         case OP_LSUB_OVF_UN:
2364                         case OP_LMUL_OVF:
2365                         case OP_LMUL_OVF_UN:
2366 #endif
2367                         {
2368                                 LLVMValueRef args [2], val, ovf, func;
2369
2370                                 emit_cond_throw_pos (ctx);
2371
2372                                 args [0] = convert (ctx, lhs, op_to_llvm_type (ins->opcode));
2373                                 args [1] = convert (ctx, rhs, op_to_llvm_type (ins->opcode));
2374                                 func = LLVMGetNamedFunction (module, ovf_op_to_intrins (ins->opcode));
2375                                 g_assert (func);
2376                                 val = LLVMBuildCall (builder, func, args, 2, "");
2377                                 values [ins->dreg] = LLVMBuildExtractValue (builder, val, 0, dname);
2378                                 ovf = LLVMBuildExtractValue (builder, val, 1, "");
2379                                 emit_cond_system_exception (ctx, bb, "OverflowException", ovf);
2380                                 builder = ctx->builder;
2381                                 break;
2382                         }
2383
2384                         /* 
2385                          * Valuetypes.
2386                          *   We currently model them using arrays. Promotion to local vregs is 
2387                          * disabled for them in mono_handle_global_vregs () in the LLVM case, 
2388                          * so we always have an entry in cfg->varinfo for them.
2389                          * FIXME: Is this needed ?
2390                          */
2391                         case OP_VZERO: {
2392                                 MonoClass *klass = ins->klass;
2393                                 LLVMValueRef args [4];
2394
2395                                 if (!klass) {
2396                                         // FIXME:
2397                                         LLVM_FAILURE (ctx, "!klass");
2398                                         break;
2399                                 }
2400
2401                                 if (!addresses [ins->dreg])
2402                                         addresses [ins->dreg] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &klass->byval_arg), "");
2403                                 args [0] = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
2404                                 args [1] = LLVMConstInt (LLVMInt8Type (), 0, FALSE);
2405                                 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
2406                                 // FIXME: Alignment
2407                                 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2408                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memset.i32"), args, 4, "");
2409                                 break;
2410                         }
2411
2412                         case OP_STOREV_MEMBASE:
2413                         case OP_LOADV_MEMBASE:
2414                         case OP_VMOVE: {
2415                                 MonoClass *klass = ins->klass;
2416                                 LLVMValueRef src, dst, args [4];
2417
2418                                 if (!klass) {
2419                                         // FIXME:
2420                                         LLVM_FAILURE (ctx, "!klass");
2421                                         break;
2422                                 }
2423
2424                                 switch (ins->opcode) {
2425                                 case OP_STOREV_MEMBASE:
2426                                         src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
2427                                         dst = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_destbasereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
2428                                         break;
2429                                 case OP_LOADV_MEMBASE:
2430                                         if (!addresses [ins->dreg])
2431                                                 addresses [ins->dreg] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &klass->byval_arg), "");
2432                                         src = convert (ctx, LLVMBuildAdd (builder, convert (ctx, values [ins->inst_basereg], IntPtrType ()), LLVMConstInt (IntPtrType (), ins->inst_offset, FALSE), ""), LLVMPointerType (LLVMInt8Type (), 0));
2433                                         dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
2434                                         break;
2435                                 case OP_VMOVE:
2436                                         if (!addresses [ins->sreg1])
2437                                                 addresses [ins->sreg1] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &klass->byval_arg), "");
2438                                         if (!addresses [ins->dreg])
2439                                                 addresses [ins->dreg] = LLVMBuildAlloca (builder, type_to_llvm_type (ctx, &klass->byval_arg), "");
2440                                         src = LLVMBuildBitCast (builder, addresses [ins->sreg1], LLVMPointerType (LLVMInt8Type (), 0), "");
2441                                         dst = LLVMBuildBitCast (builder, addresses [ins->dreg], LLVMPointerType (LLVMInt8Type (), 0), "");
2442                                         break;
2443                                 default:
2444                                         g_assert_not_reached ();
2445                                 }
2446
2447                                 args [0] = dst;
2448                                 args [1] = src;
2449                                 args [2] = LLVMConstInt (LLVMInt32Type (), mono_class_value_size (klass, NULL), FALSE);
2450                                 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2451                                 // FIXME: Alignment
2452                                 args [3] = LLVMConstInt (LLVMInt32Type (), 0, FALSE);
2453                                 LLVMBuildCall (builder, LLVMGetNamedFunction (module, "llvm.memcpy.i32"), args, 4, "");
2454                                 break;
2455                         }
2456                         case OP_LLVM_OUTARG_VT:
2457                                 g_assert (addresses [ins->sreg1]);
2458                                 addresses [ins->dreg] = addresses [ins->sreg1];
2459                                 break;
2460
2461                         default: {
2462                                 char reason [128];
2463
2464                                 sprintf (reason, "opcode %s", mono_inst_name (ins->opcode));
2465                                 LLVM_FAILURE (ctx, reason);
2466                                 break;
2467                         }
2468                         }
2469
2470                         /* Convert the value to the type required by phi nodes */
2471                         if (spec [MONO_INST_DEST] != ' ' && !MONO_IS_STORE_MEMBASE (ins) && vreg_types [ins->dreg])
2472                                 values [ins->dreg] = convert (ctx, values [ins->dreg], vreg_types [ins->dreg]);
2473
2474                         /* Add stores for volatile variables */
2475                         if (spec [MONO_INST_DEST] != ' ' && spec [MONO_INST_DEST] != 'v' && !MONO_IS_STORE_MEMBASE (ins))
2476                                 emit_volatile_store (ctx, ins->dreg);
2477                 }
2478
2479                 if (!has_terminator && bb->next_bb && (bb == cfg->bb_entry || bb->in_count > 0))
2480                         LLVMBuildBr (builder, get_bb (ctx, bb->next_bb));
2481
2482                 if (bb == cfg->bb_exit && sig->ret->type == MONO_TYPE_VOID)
2483                         LLVMBuildRetVoid (builder);
2484         }
2485
2486         /* Add incoming phi values */
2487         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2488                 GSList *ins_list = g_hash_table_lookup (phi_nodes, GUINT_TO_POINTER (bb));
2489
2490                 while (ins_list) {
2491                         PhiNode *node = ins_list->data;
2492                         MonoInst *phi = node->phi;
2493                         int sreg1 = phi->inst_phi_args [node->index + 1];
2494                         LLVMBasicBlockRef in_bb;
2495
2496                         in_bb = get_end_bb (ctx, node->bb->in_bb [node->index]);
2497
2498                         g_assert (LLVMTypeOf (values [sreg1]) == LLVMTypeOf (values [phi->dreg]));
2499                         LLVMAddIncoming (values [phi->dreg], &values [sreg1], &in_bb, 1);
2500
2501                         ins_list = ins_list->next;
2502                 }
2503         }
2504
2505         if (cfg->verbose_level > 1)
2506                 mono_llvm_dump_value (method);
2507
2508         if (cfg->compile_aot) {
2509                 /* Don't generate native code, keep the LLVM IR */
2510                 NOT_IMPLEMENTED;
2511         } else {
2512                 mono_llvm_optimize_method (method);
2513
2514                 if (cfg->verbose_level > 1)
2515                         mono_llvm_dump_value (method);
2516
2517                 cfg->native_code = LLVMGetPointerToGlobal (ee, method);
2518
2519                 /* Set by emit_cb */
2520                 g_assert (cfg->code_len);
2521
2522                 /* FIXME: Free the LLVM IL for the function */
2523         }
2524
2525         goto CLEANUP;
2526
2527  FAILURE:
2528
2529         if (method) {
2530                 /* Need to add unused phi nodes as they can be referenced by other values */
2531                 LLVMBasicBlockRef phi_bb = LLVMAppendBasicBlock (method, "PHI_BB");
2532                 LLVMBuilderRef builder;
2533
2534                 builder = create_builder (ctx);
2535                 LLVMPositionBuilderAtEnd (builder, phi_bb);
2536
2537                 for (i = 0; i < phi_values->len; ++i) {
2538                         LLVMValueRef v = g_ptr_array_index (phi_values, i);
2539                         if (LLVMGetInstructionParent (v) == NULL)
2540                                 LLVMInsertIntoBuilder (builder, v);
2541                 }
2542                 
2543                 LLVMDeleteFunction (method);
2544         }
2545
2546  CLEANUP:
2547         g_free (values);
2548         g_free (addresses);
2549         g_free (vreg_types);
2550         g_free (pindexes);
2551         g_hash_table_destroy (phi_nodes);
2552         g_ptr_array_free (phi_values, TRUE);
2553         g_free (ctx->bblocks);
2554         g_free (ctx->end_bblocks);
2555
2556         for (l = ctx->builders; l; l = l->next) {
2557                 LLVMBuilderRef builder = l->data;
2558                 LLVMDisposeBuilder (builder);
2559         }
2560
2561         g_free (ctx);
2562
2563         TlsSetValue (current_cfg_tls_id, NULL);
2564
2565         mono_loader_unlock ();
2566 }
2567
2568 /*
2569  * mono_llvm_emit_call:
2570  *
2571  *   Same as mono_arch_emit_call () for LLVM.
2572  */
2573 void
2574 mono_llvm_emit_call (MonoCompile *cfg, MonoCallInst *call)
2575 {
2576         MonoInst *in;
2577         MonoMethodSignature *sig;
2578         int i, n, stack_size;
2579         LLVMArgInfo *ainfo;
2580
2581         stack_size = 0;
2582
2583         sig = call->signature;
2584         n = sig->param_count + sig->hasthis;
2585
2586         call->cinfo = mono_arch_get_llvm_call_info (cfg, sig);
2587
2588         if (sig->call_convention == MONO_CALL_VARARG) {
2589                 cfg->exception_message = g_strdup ("varargs");
2590                 cfg->disable_llvm = TRUE;
2591         }
2592
2593         for (i = 0; i < n; ++i) {
2594                 MonoInst *ins;
2595
2596                 ainfo = call->cinfo->args + i;
2597
2598                 in = call->args [i];
2599                         
2600                 /* Simply remember the arguments */
2601                 switch (ainfo->storage) {
2602                 case LLVMArgInIReg:
2603                         MONO_INST_NEW (cfg, ins, OP_MOVE);
2604                         ins->dreg = mono_alloc_ireg (cfg);
2605                         ins->sreg1 = in->dreg;
2606                         break;
2607                 case LLVMArgInFPReg:
2608                         MONO_INST_NEW (cfg, ins, OP_FMOVE);
2609                         ins->dreg = mono_alloc_freg (cfg);
2610                         ins->sreg1 = in->dreg;
2611                         break;
2612                 case LLVMArgVtypeByVal:
2613                 case LLVMArgVtypeInReg:
2614                         MONO_INST_NEW (cfg, ins, OP_LLVM_OUTARG_VT);
2615                         ins->dreg = mono_alloc_ireg (cfg);
2616                         ins->sreg1 = in->dreg;
2617                         ins->klass = mono_class_from_mono_type (sig->params [i - sig->hasthis]);
2618                         break;
2619                 default:
2620                         cfg->exception_message = g_strdup ("ainfo->storage");
2621                         cfg->disable_llvm = TRUE;
2622                         return;
2623                 }
2624
2625                 if (!cfg->disable_llvm) {
2626                         MONO_ADD_INS (cfg->cbb, ins);
2627                         mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
2628                 }
2629         }
2630 }
2631
2632 static unsigned char*
2633 alloc_cb (LLVMValueRef function, int size)
2634 {
2635         MonoCompile *cfg;
2636
2637         cfg = TlsGetValue (current_cfg_tls_id);
2638
2639         if (cfg) {
2640                 // FIXME: dynamic
2641                 return mono_domain_code_reserve (cfg->domain, size);
2642         } else {
2643                 return mono_domain_code_reserve (mono_domain_get (), size);
2644         }
2645 }
2646
2647 static void
2648 emitted_cb (LLVMValueRef function, void *start, void *end)
2649 {
2650         MonoCompile *cfg;
2651
2652         cfg = TlsGetValue (current_cfg_tls_id);
2653         g_assert (cfg);
2654         cfg->code_len = (guint8*)end - (guint8*)start;
2655 }
2656
2657 static void
2658 exception_cb (void *data)
2659 {
2660         MonoCompile *cfg;
2661
2662         cfg = TlsGetValue (current_cfg_tls_id);
2663         g_assert (cfg);
2664
2665         /*
2666          * data points to a DWARF FDE structure, convert it to our unwind format and
2667          * save it.
2668          * An alternative would be to save it directly, and modify our unwinder to work
2669          * with it.
2670          */
2671         cfg->encoded_unwind_ops = mono_unwind_get_ops_from_fde ((guint8*)data, &cfg->encoded_unwind_ops_len, NULL);
2672 }
2673
2674 static void
2675 add_intrinsics (LLVMModuleRef module)
2676 {
2677         /* Emit declarations of instrinsics */
2678         {
2679                 LLVMTypeRef memset_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMInt8Type (), LLVMInt32Type (), LLVMInt32Type () };
2680
2681                 LLVMAddFunction (module, "llvm.memset.i32", LLVMFunctionType (LLVMVoidType (), memset_params, 4, FALSE));
2682         }
2683
2684         {
2685                 LLVMTypeRef memcpy_params [] = { LLVMPointerType (LLVMInt8Type (), 0), LLVMPointerType (LLVMInt8Type (), 0), LLVMInt32Type (), LLVMInt32Type () };
2686
2687                 LLVMAddFunction (module, "llvm.memcpy.i32", LLVMFunctionType (LLVMVoidType (), memcpy_params, 4, FALSE));
2688         }
2689
2690         {
2691                 LLVMTypeRef params [] = { LLVMDoubleType () };
2692
2693                 LLVMAddFunction (module, "llvm.sin.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
2694                 LLVMAddFunction (module, "llvm.cos.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
2695                 LLVMAddFunction (module, "llvm.sqrt.f64", LLVMFunctionType (LLVMDoubleType (), params, 1, FALSE));
2696         }
2697
2698         {
2699                 LLVMTypeRef membar_params [] = { LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type (), LLVMInt1Type () };
2700
2701                 LLVMAddFunction (module, "llvm.atomic.swap.i32.p0i32", LLVMFunctionType2 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), FALSE));
2702                 LLVMAddFunction (module, "llvm.atomic.swap.i64.p0i64", LLVMFunctionType2 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), FALSE));
2703                 LLVMAddFunction (module, "llvm.atomic.load.add.i32.p0i32", LLVMFunctionType2 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), FALSE));
2704                 LLVMAddFunction (module, "llvm.atomic.load.add.i64.p0i64", LLVMFunctionType2 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), FALSE));
2705                 LLVMAddFunction (module, "llvm.atomic.cmp.swap.i32.p0i32", LLVMFunctionType3 (LLVMInt32Type (), LLVMPointerType (LLVMInt32Type (), 0), LLVMInt32Type (), LLVMInt32Type (), FALSE));
2706                 LLVMAddFunction (module, "llvm.atomic.cmp.swap.i64.p0i64", LLVMFunctionType3 (LLVMInt64Type (), LLVMPointerType (LLVMInt64Type (), 0), LLVMInt64Type (), LLVMInt64Type (), FALSE));
2707                 LLVMAddFunction (module, "llvm.memory.barrier", LLVMFunctionType (LLVMVoidType (), membar_params, 5, FALSE));
2708         }
2709
2710         {
2711                 LLVMTypeRef ovf_res_i32 [] = { LLVMInt32Type (), LLVMInt1Type () };
2712                 LLVMTypeRef ovf_params_i32 [] = { LLVMInt32Type (), LLVMInt32Type () };
2713
2714                 LLVMAddFunction (module, "llvm.sadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
2715                 LLVMAddFunction (module, "llvm.uadd.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
2716                 LLVMAddFunction (module, "llvm.ssub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
2717                 LLVMAddFunction (module, "llvm.usub.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
2718                 LLVMAddFunction (module, "llvm.smul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
2719                 LLVMAddFunction (module, "llvm.umul.with.overflow.i32", LLVMFunctionType (LLVMStructType (ovf_res_i32, 2, FALSE), ovf_params_i32, 2, FALSE));
2720         }
2721
2722         {
2723                 LLVMTypeRef ovf_res_i64 [] = { LLVMInt64Type (), LLVMInt1Type () };
2724                 LLVMTypeRef ovf_params_i64 [] = { LLVMInt64Type (), LLVMInt64Type () };
2725
2726                 LLVMAddFunction (module, "llvm.sadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
2727                 LLVMAddFunction (module, "llvm.uadd.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
2728                 LLVMAddFunction (module, "llvm.ssub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
2729                 LLVMAddFunction (module, "llvm.usub.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
2730                 LLVMAddFunction (module, "llvm.smul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
2731                 LLVMAddFunction (module, "llvm.umul.with.overflow.i64", LLVMFunctionType (LLVMStructType (ovf_res_i64, 2, FALSE), ovf_params_i64, 2, FALSE));
2732         }
2733 }
2734
2735  void
2736 mono_llvm_init (void)
2737 {
2738         current_cfg_tls_id = TlsAlloc ();
2739
2740         jit_module.module = LLVMModuleCreateWithName ("mono");
2741
2742         ee = mono_llvm_create_ee (LLVMCreateModuleProviderForExistingModule (jit_module.module), alloc_cb, emitted_cb, exception_cb);
2743
2744         add_intrinsics (jit_module.module);
2745
2746         jit_module.llvm_types = g_hash_table_new (NULL, NULL);
2747 }
2748
2749 void
2750 mono_llvm_cleanup (void)
2751 {
2752         mono_llvm_dispose_ee (ee);
2753
2754         g_hash_table_destroy (jit_module.llvm_types);
2755 }
2756
2757 /*
2758   DESIGN:
2759   - Emit LLVM IR from the mono IR using the LLVM C API.
2760   - The original arch specific code remains, so we can fall back to it if we run
2761     into something we can't handle.
2762   FIXME:
2763   - llvm's PrettyStackTrace class seems to register a signal handler which screws
2764     up our GC. Also, it calls sigaction () a _lot_ of times instead of just once.
2765 */
2766
2767 /*  
2768   A partial list of issues:
2769   - Handling of opcodes which can throw exceptions.
2770
2771       In the mono JIT, these are implemented using code like this:
2772           method:
2773       <compare>
2774           throw_pos:
2775           b<cond> ex_label
2776           <rest of code>
2777       ex_label:
2778           push throw_pos - method
2779           call <exception trampoline>
2780
2781           The problematic part is push throw_pos - method, which cannot be represented
2782       in the LLVM IR, since it does not support label values.
2783           -> this can be implemented in AOT mode using inline asm + labels, but cannot
2784           be implemented in JIT mode ?
2785           -> a possible but slower implementation would use the normal exception 
2786       throwing code but it would need to control the placement of the throw code
2787       (it needs to be exactly after the compare+branch).
2788           -> perhaps add a PC offset intrinsics ?
2789
2790   - efficient implementation of .ovf opcodes.
2791
2792           These are currently implemented as:
2793           <ins which sets the condition codes>
2794           b<cond> ex_label
2795
2796           Some overflow opcodes are now supported by LLVM SVN.
2797
2798   - exception handling, unwinding.
2799     - SSA is disabled for methods with exception handlers    
2800         - How to obtain unwind info for LLVM compiled methods ?
2801           -> this is now solved by converting the unwind info generated by LLVM
2802              into our format.
2803         - LLVM uses the c++ exception handling framework, while we use our home grown
2804       code, and couldn't use the c++ one:
2805       - its not supported under VC++, other exotic platforms.
2806           - it might be impossible to support filter clauses with it.
2807
2808   - trampolines.
2809   
2810     The trampolines need a predictable call sequence, since they need to disasm
2811     the calling code to obtain register numbers / offsets.
2812
2813     LLVM currently generates this code in non-JIT mode:
2814            mov    -0x98(%rax),%eax
2815            callq  *%rax
2816     Here, the vtable pointer is lost. 
2817     -> solution: use one vtable trampoline per class.
2818
2819   - passing/receiving the IMT pointer/RGCTX.
2820     -> solution: pass them as normal arguments ?
2821
2822   - argument passing.
2823   
2824           LLVM does not allow the specification of argument registers etc. This means
2825       that all calls are made according to the platform ABI.
2826
2827   - passing/receiving vtypes.
2828
2829       Vtypes passed/received in registers are handled by the front end by using
2830           a signature with scalar arguments, and loading the parts of the vtype into those
2831           arguments.
2832
2833           Vtypes passed on the stack are handled using the 'byval' attribute.
2834
2835   - ldaddr.
2836
2837     Supported though alloca, we need to emit the load/store code.
2838
2839   - types.
2840
2841     The mono JIT uses pointer sized iregs/double fregs, while LLVM uses precisely
2842     typed registers, so we have to keep track of the precise LLVM type of each vreg.
2843     This is made easier because the IR is already in SSA form.
2844     An additional problem is that our IR is not consistent with types, i.e. i32/ia64 
2845         types are frequently used incorrectly.
2846 */
2847
2848 /* FIXME: Normalize some aspects of the mono IR to allow easier translation, like:
2849  *   - each bblock should end with a branch
2850  *   - setting the return value, making cfg->ret non-volatile
2851  * - merge some changes back to HEAD, to reduce the differences.
2852  * - avoid some transformations in the JIT which make it harder for us to generate
2853  *   code.
2854  * - fix memory leaks.
2855  * - use pointer types to help optimizations.
2856  */