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"
39 mono_llvm_dump_value (LLVMValueRef value)
41 /* Same as LLVMDumpValue (), but print to stdout */
43 outs () << (*unwrap<Value> (value));
46 /* Missing overload for building an alloca with an alignment */
48 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
49 LLVMValueRef ArraySize,
50 int alignment, const char *Name)
52 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
56 mono_llvm_build_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
57 const char *Name, gboolean is_volatile)
59 LoadInst *ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
65 mono_llvm_build_atomic_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
66 const char *Name, gboolean is_volatile, int alignment, BarrierKind barrier)
68 LoadInst *ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
70 ins->setAlignment (alignment);
72 case LLVM_BARRIER_NONE:
74 case LLVM_BARRIER_ACQ:
75 ins->setOrdering(Acquire);
77 case LLVM_BARRIER_SEQ:
78 ins->setOrdering(SequentiallyConsistent);
81 g_assert_not_reached ();
89 mono_llvm_build_aligned_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
90 const char *Name, gboolean is_volatile, int alignment)
94 ins = unwrap(builder)->CreateLoad(unwrap(PointerVal), is_volatile, Name);
95 ins->setAlignment (alignment);
101 mono_llvm_build_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
102 gboolean is_volatile, BarrierKind barrier)
104 StoreInst *ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
107 case LLVM_BARRIER_NONE:
109 case LLVM_BARRIER_REL:
110 ins->setOrdering(Release);
112 case LLVM_BARRIER_SEQ:
113 ins->setOrdering(SequentiallyConsistent);
116 g_assert_not_reached ();
124 mono_llvm_build_aligned_store (LLVMBuilderRef builder, LLVMValueRef Val, LLVMValueRef PointerVal,
125 gboolean is_volatile, int alignment)
129 ins = unwrap(builder)->CreateStore(unwrap(Val), unwrap(PointerVal), is_volatile);
130 ins->setAlignment (alignment);
136 mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef val)
138 AtomicCmpXchgInst *ins;
140 ins = unwrap(builder)->CreateAtomicCmpXchg (unwrap(ptr), unwrap (cmp), unwrap (val), SequentiallyConsistent, SequentiallyConsistent);
145 mono_llvm_build_atomic_rmw (LLVMBuilderRef builder, AtomicRMWOp op, LLVMValueRef ptr, LLVMValueRef val)
147 AtomicRMWInst::BinOp aop = AtomicRMWInst::Xchg;
151 case LLVM_ATOMICRMW_OP_XCHG:
152 aop = AtomicRMWInst::Xchg;
154 case LLVM_ATOMICRMW_OP_ADD:
155 aop = AtomicRMWInst::Add;
158 g_assert_not_reached ();
162 ins = unwrap (builder)->CreateAtomicRMW (aop, unwrap (ptr), unwrap (val), SequentiallyConsistent);
167 mono_llvm_build_fence (LLVMBuilderRef builder, BarrierKind kind)
170 AtomicOrdering ordering;
172 g_assert (kind != LLVM_BARRIER_NONE);
175 case LLVM_BARRIER_ACQ:
178 case LLVM_BARRIER_REL:
181 case LLVM_BARRIER_SEQ:
182 ordering = SequentiallyConsistent;
185 g_assert_not_reached ();
189 ins = unwrap (builder)->CreateFence (ordering);
194 mono_llvm_set_must_tail (LLVMValueRef call_ins)
196 CallInst *ins = (CallInst*)unwrap (call_ins);
198 ins->setTailCallKind (CallInst::TailCallKind::TCK_MustTail);
202 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
204 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
205 unwrap (var)->replaceAllUsesWith (V);
209 mono_llvm_create_constant_data_array (const uint8_t *data, int len)
211 return wrap(ConstantDataArray::get (*unwrap(LLVMGetGlobalContext ()), makeArrayRef(data, len)));
215 mono_llvm_set_is_constant (LLVMValueRef global_var)
217 unwrap<GlobalVariable>(global_var)->setConstant (true);
221 mono_llvm_set_preserveall_cc (LLVMValueRef func)
223 unwrap<Function>(func)->setCallingConv (CallingConv::PreserveAll);
227 mono_llvm_set_call_preserveall_cc (LLVMValueRef func)
229 unwrap<CallInst>(func)->setCallingConv (CallingConv::PreserveAll);
233 mono_llvm_set_call_notail (LLVMValueRef func)
235 #if LLVM_API_VERSION > 100
236 //unwrap<CallInst>(func)->setTailCallKind (CallInst::TailCallKind::TCK_NoTail);
240 #if LLVM_API_VERSION > 100
243 mono_llvm_create_di_builder (LLVMModuleRef module)
245 return new DIBuilder (*unwrap(module));
249 mono_llvm_di_create_compile_unit (void *di_builder, const char *cu_name, const char *dir, const char *producer)
251 DIBuilder *builder = (DIBuilder*)di_builder;
253 return builder->createCompileUnit (dwarf::DW_LANG_C99, cu_name, dir, producer, true, "", 0);
257 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)
259 DIBuilder *builder = (DIBuilder*)di_builder;
261 DISubroutineType *type;
262 DISubprogram *di_func;
264 // FIXME: Share DIFile
265 di_file = builder->createFile (file, dir);
266 type = builder->createSubroutineType (builder->getOrCreateTypeArray (ArrayRef<Metadata*> ()));
267 di_func = builder->createFunction (di_file, name, mangled_name, di_file, line, type, true, true, 0);
269 unwrap<Function>(func)->setMetadata ("dbg", di_func);
275 mono_llvm_di_create_file (void *di_builder, const char *dir, const char *file)
277 DIBuilder *builder = (DIBuilder*)di_builder;
279 return builder->createFile (file, dir);
283 mono_llvm_di_create_location (void *di_builder, void *scope, int row, int column)
285 return DILocation::get (*unwrap(LLVMGetGlobalContext ()), row, column, (Metadata*)scope);
289 mono_llvm_di_set_location (LLVMBuilderRef builder, void *loc_md)
291 unwrap(builder)->SetCurrentDebugLocation ((DILocation*)loc_md);
295 mono_llvm_di_builder_finalize (void *di_builder)
297 DIBuilder *builder = (DIBuilder*)di_builder;
299 builder->finalize ();
302 #endif /* #if LLVM_API_VERSION > 100 */