2 // mini-llvm-cpp.cpp: C++ support classes for the mono LLVM integration
4 // (C) 2009 Novell, Inc.
8 // We need to override some stuff in LLVM, but this cannot be done using the C
9 // interface, so we have to use some C++ code here.
10 // The things which we override are:
11 // - the default JIT code manager used by LLVM doesn't allocate memory using
12 // MAP_32BIT, we require it.
13 // - add some callbacks so we can obtain the size of methods and their exception
18 // Mono's internal header files are not C++ clean, so avoid including them if
24 #include <llvm/Support/raw_ostream.h>
25 #include <llvm/PassManager.h>
26 #include <llvm/ExecutionEngine/ExecutionEngine.h>
27 #include <llvm/ExecutionEngine/JITMemoryManager.h>
28 #include <llvm/Target/TargetOptions.h>
29 #include <llvm/Target/TargetData.h>
30 #include <llvm/Analysis/Verifier.h>
31 #include <llvm/Transforms/Scalar.h>
32 #include <llvm/Support/CommandLine.h>
33 #include "llvm/Support/PassNameParser.h"
34 #include "llvm/Support/PrettyStackTrace.h"
35 #include <llvm/CodeGen/Passes.h>
36 #include <llvm/CodeGen/MachineFunctionPass.h>
37 //#include <llvm/LinkAllPasses.h>
39 #include "llvm-c/Core.h"
40 #include "llvm-c/ExecutionEngine.h"
42 #include "mini-llvm-cpp.h"
44 extern "C" void LLVMInitializeX86TargetInfo();
48 class MonoJITMemoryManager : public JITMemoryManager
54 /* Callbacks installed by mono */
55 AllocCodeMemoryCb *alloc_cb;
56 FunctionEmittedCb *emitted_cb;
58 MonoJITMemoryManager ();
59 ~MonoJITMemoryManager ();
61 void setMemoryWritable (void);
63 void setMemoryExecutable (void);
67 unsigned char *getGOTBase() const {
68 return mm->getGOTBase ();
71 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7
72 void *getDlsymTable() const {
73 return mm->getDlsymTable ();
76 void SetDlsymTable(void *ptr);
79 void setPoisonMemory(bool) {
82 unsigned char *startFunctionBody(const Function *F,
83 uintptr_t &ActualSize);
85 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
88 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
89 unsigned char *FunctionEnd);
91 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
93 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
95 void deallocateMemForFunction(const Function *F);
97 unsigned char*startExceptionTable(const Function* F,
98 uintptr_t &ActualSize);
100 void endExceptionTable(const Function *F, unsigned char *TableStart,
101 unsigned char *TableEnd,
102 unsigned char* FrameRegister);
104 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION >= 7
105 virtual void deallocateFunctionBody(void*) {
108 virtual void deallocateExceptionTable(void*) {
113 MonoJITMemoryManager::MonoJITMemoryManager ()
116 mm = JITMemoryManager::CreateDefaultMemManager ();
119 MonoJITMemoryManager::~MonoJITMemoryManager ()
124 MonoJITMemoryManager::setMemoryWritable (void)
129 MonoJITMemoryManager::setMemoryExecutable (void)
134 MonoJITMemoryManager::AllocateGOT()
139 #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7
141 MonoJITMemoryManager::SetDlsymTable(void *ptr)
143 mm->SetDlsymTable (ptr);
148 MonoJITMemoryManager::startFunctionBody(const Function *F,
149 uintptr_t &ActualSize)
151 return alloc_cb (wrap (F), ActualSize);
155 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
158 return alloc_cb (wrap (F), StubSize);
162 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
163 unsigned char *FunctionEnd)
165 emitted_cb (wrap (F), FunctionStart, FunctionEnd);
169 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
171 return new unsigned char [Size];
175 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
177 return new unsigned char [Size];
181 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
186 MonoJITMemoryManager::startExceptionTable(const Function* F,
187 uintptr_t &ActualSize)
189 return alloc_cb (wrap (F), ActualSize);
193 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
194 unsigned char *TableEnd,
195 unsigned char* FrameRegister)
199 static MonoJITMemoryManager *mono_mm;
201 static FunctionPassManager *fpm;
203 static LLVMContext ctx;
206 mono_llvm_optimize_method (LLVMValueRef method)
208 verifyFunction (*(unwrap<Function> (method)));
209 fpm->run (*unwrap<Function> (method));
213 mono_llvm_dump_value (LLVMValueRef value)
215 /* Same as LLVMDumpValue (), but print to stdout */
216 outs () << (*unwrap<Value> (value));
219 /* Missing overload for building an alloca with an alignment */
221 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
222 LLVMValueRef ArraySize,
223 int alignment, const char *Name)
225 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
229 mono_llvm_build_volatile_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
232 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), true, Name));
236 mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
238 Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
239 unwrap (var)->replaceAllUsesWith (V);
242 static cl::list<const PassInfo*, bool, PassNameParser>
243 PassList(cl::desc("Optimizations available:"));
245 LLVMExecutionEngineRef
246 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
250 LLVMInitializeX86Target ();
251 LLVMInitializeX86TargetInfo ();
253 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
255 mono_mm = new MonoJITMemoryManager ();
256 mono_mm->alloc_cb = alloc_cb;
257 mono_mm->emitted_cb = emitted_cb;
259 DwarfExceptionHandling = true;
260 // PrettyStackTrace installs signal handlers which trip up libgc
261 DisablePrettyStackTrace = true;
263 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default);
265 errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
266 g_assert_not_reached ();
268 EE->InstallExceptionTableRegister (exception_cb);
270 fpm = new FunctionPassManager (unwrap (MP));
272 fpm->add(new TargetData(*EE->getTargetData()));
273 /* Add a random set of passes */
274 /* Make this run-time configurable */
275 fpm->add(createInstructionCombiningPass());
276 fpm->add(createReassociatePass());
277 fpm->add(createGVNPass());
278 fpm->add(createCFGSimplificationPass());
280 /* Add passes specified by the env variable */
281 /* FIXME: This can only add passes which are linked in, thus are already used */
282 for (unsigned i = 0; i < PassList.size(); ++i) {
283 const PassInfo *PassInf = PassList[i];
286 if (PassInf->getNormalCtor())
287 P = PassInf->getNormalCtor()();
288 if (dynamic_cast<MachineFunctionPass*>(P) != 0) {
289 errs () << PassInf->getPassName () << " is a machine function pass.\n";
299 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee)