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)
6 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
10 // We need to override some stuff in LLVM, but this cannot be done using the C
11 // interface, so we have to use some C++ code here.
12 // The things which we override are:
13 // - the default JIT code manager used by LLVM doesn't allocate memory using
14 // MAP_32BIT, we require it.
15 // - add some callbacks so we can obtain the size of methods and their exception
20 // Mono's internal header files are not C++ clean, so avoid including them if
28 #include <llvm/Support/raw_ostream.h>
29 #include <llvm/IR/Function.h>
30 #include <llvm/IR/IRBuilder.h>
31 #include <llvm/IR/Module.h>
32 #include <llvm/IR/DIBuilder.h>
34 #include "mini-llvm-cpp.h"
38 #if LLVM_API_VERSION > 100
39 // These are c++11 scoped enums in recent llvm versions
40 #define Acquire AtomicOrdering::Acquire
41 #define Release AtomicOrdering::Release
42 #define SequentiallyConsistent AtomicOrdering::SequentiallyConsistent
46 mono_llvm_dump_value (LLVMValueRef value)
48 /* Same as LLVMDumpValue (), but print to stdout */
50 outs () << (*unwrap<Value> (value));
53 /* Missing overload for building an alloca with an alignment */
55 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
56 LLVMValueRef ArraySize,
57 int alignment, const char *Name)
59 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
63 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
64 const char *Name, gboolean is_volatile)
66 LoadInst *ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
72 mono_llvm_build_atomic_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
73 const char *Name, gboolean is_volatile, int alignment, BarrierKind barrier)
75 LoadInst *ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
77 ins->setAlignment (alignment);
79 case LLVM_BARRIER_NONE:
81 case LLVM_BARRIER_ACQ:
82 ins->setOrdering(Acquire);
84 case LLVM_BARRIER_SEQ:
85 ins->setOrdering(SequentiallyConsistent);
88 g_assert_not_reached ();
96 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
97 const char *Name, gboolean is_volatile, int alignment)
101 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
102 ins->setAlignment (alignment);
108 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
109 gboolean is_volatile, BarrierKind barrier)
111 StoreInst *ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
114 case LLVM_BARRIER_NONE:
116 case LLVM_BARRIER_REL:
117 ins->setOrdering(Release);
119 case LLVM_BARRIER_SEQ:
120 ins->setOrdering(SequentiallyConsistent);
123 g_assert_not_reached ();
131 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
132 gboolean is_volatile, int alignment)
136 ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
137 ins->setAlignment (alignment);
143 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
145 AtomicCmpXchgInst *ins;
147 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent, SequentiallyConsistent);
152 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
154 AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
158 case LLVM_ATOMICRMW_OP_XCHG:
159 aop = AtomicRMWInst::Xchg;
161 case LLVM_ATOMICRMW_OP_ADD:
162 aop = AtomicRMWInst::Add;
165 g_assert_not_reached ();
169 ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), SequentiallyConsistent);
174 mono_llvm_build_fence (LLVMBuilderRef builder, BarrierKind kind)
177 AtomicOrdering ordering;
179 g_assert (kind != LLVM_BARRIER_NONE);
182 case LLVM_BARRIER_ACQ:
185 case LLVM_BARRIER_REL:
188 case LLVM_BARRIER_SEQ:
189 ordering = SequentiallyConsistent;
192 g_assert_not_reached ();
196 ins = unwrap (builder)->CreateFence (ordering);
201 mono_llvm_set_must_tail (LLVMValueRef call_ins)
203 CallInst *ins = (CallInst*)unwrap (call_ins);
205 ins->setTailCallKind (CallInst::TailCallKind::TCK_MustTail);
209 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
211 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
212 unwrap (var)->replaceAllUsesWith (V);
216 mono_llvm_create_constant_data_array (const uint8_t *data, int len)
218 return wrap(ConstantDataArray::get (*unwrap(LLVMGetGlobalContext ()), makeArrayRef(data, len)));
222 mono_llvm_set_is_constant (LLVMValueRef global_var)
224 unwrap<GlobalVariable>(global_var)->setConstant (true);
228 mono_llvm_set_preserveall_cc (LLVMValueRef func)
230 unwrap<Function>(func)->setCallingConv (CallingConv::PreserveAll);
234 mono_llvm_set_call_preserveall_cc (LLVMValueRef func)
236 unwrap<CallInst>(func)->setCallingConv (CallingConv::PreserveAll);
240 mono_llvm_set_call_notail (LLVMValueRef func)
242 #if LLVM_API_VERSION > 100
243 unwrap<CallInst>(func)->setTailCallKind (CallInst::TailCallKind::TCK_NoTail);
247 #if LLVM_API_VERSION > 100
250 mono_llvm_create_di_builder (LLVMModuleRef module)
252 return new DIBuilder (*unwrap(module));
256 mono_llvm_di_create_compile_unit (void *di_builder, const char *cu_name, const char *dir, const char *producer)
258 DIBuilder *builder = (DIBuilder*)di_builder;
260 return builder->createCompileUnit (dwarf::DW_LANG_C99, cu_name, dir, producer, true, "", 0);
264 mono_llvm_di_create_function (void *di_builder, void *cu, LLVMValueRef func, const char *name, const char *mangled_name, const char *dir, const char *file, int line)
266 DIBuilder *builder = (DIBuilder*)di_builder;
268 DISubroutineType *type;
269 DISubprogram *di_func;
271 // FIXME: Share DIFile
272 di_file = builder->createFile (file, dir);
273 type = builder->createSubroutineType (builder->getOrCreateTypeArray (ArrayRef<Metadata*> ()));
274 di_func = builder->createFunction (di_file, name, mangled_name, di_file, line, type, true, true, 0);
276 unwrap<Function>(func)->setMetadata ("dbg", di_func);
282 mono_llvm_di_create_file (void *di_builder, const char *dir, const char *file)
284 DIBuilder *builder = (DIBuilder*)di_builder;
286 return builder->createFile (file, dir);
290 mono_llvm_di_create_location (void *di_builder, void *scope, int row, int column)
292 return DILocation::get (*unwrap(LLVMGetGlobalContext ()), row, column, (Metadata*)scope);
296 mono_llvm_di_set_location (LLVMBuilderRef builder, void *loc_md)
298 unwrap(builder)->SetCurrentDebugLocation ((DILocation*)loc_md);
302 mono_llvm_di_builder_finalize (void *di_builder)
304 DIBuilder *builder = (DIBuilder*)di_builder;
306 builder->finalize ();
309 #endif /* #if LLVM_API_VERSION > 100 */