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