2 // mini-llvm-cpp.cpp: C++ support classes for the mono LLVM integration
4 // (C) 2009-2011 Novell, Inc.
5 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
9 // We need to override some stuff in LLVM, but this cannot be done using the C
10 // interface, so we have to use some C++ code here.
11 // The things which we override are:
12 // - the default JIT code manager used by LLVM doesn't allocate memory using
13 // MAP_32BIT, we require it.
14 // - add some callbacks so we can obtain the size of methods and their exception
19 // Mono's internal header files are not C++ clean, so avoid including them if
27 #include <llvm/Support/raw_ostream.h>
28 #include <llvm/IR/Function.h>
29 #include <llvm/IR/IRBuilder.h>
30 #include <llvm/IR/Module.h>
31 #include <llvm/IR/DIBuilder.h>
33 #include "mini-llvm-cpp.h"
38 mono_llvm_dump_value (LLVMValueRef value)
40 /* Same as LLVMDumpValue (), but print to stdout */
42 outs () << (*unwrap<Value> (value));
45 /* Missing overload for building an alloca with an alignment */
47 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
48 LLVMValueRef ArraySize,
49 int alignment, const char *Name)
51 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
55 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
56 const char *Name, gboolean is_volatile, BarrierKind barrier)
58 LoadInst *ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
61 case LLVM_BARRIER_NONE:
63 case LLVM_BARRIER_ACQ:
64 ins->setOrdering(Acquire);
66 case LLVM_BARRIER_SEQ:
67 ins->setOrdering(SequentiallyConsistent);
70 g_assert_not_reached ();
78 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
79 const char *Name, gboolean is_volatile, int alignment)
83 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
84 ins->setAlignment (alignment);
90 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
91 gboolean is_volatile, BarrierKind barrier)
93 StoreInst *ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
96 case LLVM_BARRIER_NONE:
98 case LLVM_BARRIER_REL:
99 ins->setOrdering(Release);
101 case LLVM_BARRIER_SEQ:
102 ins->setOrdering(SequentiallyConsistent);
105 g_assert_not_reached ();
113 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
114 gboolean is_volatile, int alignment)
118 ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
119 ins->setAlignment (alignment);
125 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
127 AtomicCmpXchgInst *ins;
129 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent, SequentiallyConsistent);
134 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
136 AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
140 case LLVM_ATOMICRMW_OP_XCHG:
141 aop = AtomicRMWInst::Xchg;
143 case LLVM_ATOMICRMW_OP_ADD:
144 aop = AtomicRMWInst::Add;
147 g_assert_not_reached ();
151 ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), SequentiallyConsistent);
156 mono_llvm_build_fence (LLVMBuilderRef builder, BarrierKind kind)
159 AtomicOrdering ordering;
161 g_assert (kind != LLVM_BARRIER_NONE);
164 case LLVM_BARRIER_ACQ:
167 case LLVM_BARRIER_REL:
170 case LLVM_BARRIER_SEQ:
171 ordering = SequentiallyConsistent;
174 g_assert_not_reached ();
178 ins = unwrap (builder)->CreateFence (ordering);
183 mono_llvm_set_must_tail (LLVMValueRef call_ins)
185 CallInst *ins = (CallInst*)unwrap (call_ins);
187 ins->setTailCallKind (CallInst::TailCallKind::TCK_MustTail);
191 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
193 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
194 unwrap (var)->replaceAllUsesWith (V);
198 mono_llvm_create_constant_data_array (const uint8_t *data, int len)
200 return wrap(ConstantDataArray::get (*unwrap(LLVMGetGlobalContext ()), makeArrayRef(data, len)));
204 mono_llvm_set_is_constant (LLVMValueRef global_var)
206 unwrap<GlobalVariable>(global_var)->setConstant (true);
210 mono_llvm_set_preserveall_cc (LLVMValueRef func)
212 unwrap<Function>(func)->setCallingConv (CallingConv::PreserveAll);
216 mono_llvm_set_call_preserveall_cc (LLVMValueRef func)
218 unwrap<CallInst>(func)->setCallingConv (CallingConv::PreserveAll);
221 #if LLVM_API_VERSION > 100
224 mono_llvm_create_di_builder (LLVMModuleRef module)
226 return new DIBuilder (*unwrap(module));
230 mono_llvm_di_create_compile_unit (void *di_builder, const char *cu_name, const char *dir, const char *producer)
232 DIBuilder *builder = (DIBuilder*)di_builder;
234 return builder->createCompileUnit (dwarf::DW_LANG_C99, cu_name, dir, producer, true, "", 0);
238 mono_llvm_di_create_function (void *di_builder, void *cu, const char *name, const char *mangled_name, const char *dir, const char *file, int line)
240 DIBuilder *builder = (DIBuilder*)di_builder;
242 DISubroutineType *type;
244 // FIXME: Share DIFile
245 di_file = builder->createFile (file, dir);
246 type = builder->createSubroutineType (builder->getOrCreateTypeArray (ArrayRef<Metadata*> ()));
247 return builder->createFunction (di_file, name, mangled_name, di_file, line, type, true, true, 0);
251 mono_llvm_di_create_file (void *di_builder, const char *dir, const char *file)
253 DIBuilder *builder = (DIBuilder*)di_builder;
255 return builder->createFile (file, dir);
259 mono_llvm_di_create_location (void *di_builder, void *scope, int row, int column)
261 return DILocation::get (*unwrap(LLVMGetGlobalContext ()), row, column, (Metadata*)scope);
265 mono_llvm_di_set_location (LLVMBuilderRef builder, void *loc_md)
267 unwrap(builder)->SetCurrentDebugLocation ((DILocation*)loc_md);
271 mono_llvm_di_builder_finalize (void *di_builder)
273 DIBuilder *builder = (DIBuilder*)di_builder;
275 builder->finalize ();
278 #endif /* #if LLVM_API_VERSION > 100 */