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/PassManager.h>
25 #include <llvm/ExecutionEngine/ExecutionEngine.h>
26 #include <llvm/ExecutionEngine/JITMemoryManager.h>
27 #include <llvm/Target/TargetOptions.h>
28 #include <llvm/Target/TargetData.h>
29 #include <llvm/Analysis/Verifier.h>
30 #include <llvm/Transforms/Scalar.h>
32 #include "llvm-c/Core.h"
33 #include "llvm-c/ExecutionEngine.h"
35 #include "mini-llvm-cpp.h"
39 class MonoJITMemoryManager : public JITMemoryManager
45 /* Callbacks installed by mono */
46 AllocCodeMemoryCb *alloc_cb;
47 FunctionEmittedCb *emitted_cb;
49 MonoJITMemoryManager ();
50 ~MonoJITMemoryManager ();
52 void setMemoryWritable (void);
54 void setMemoryExecutable (void);
58 unsigned char *getGOTBase() const {
59 return mm->getGOTBase ();
62 void *getDlsymTable() const {
63 return mm->getDlsymTable ();
66 void SetDlsymTable(void *ptr);
68 unsigned char *startFunctionBody(const Function *F,
69 uintptr_t &ActualSize);
71 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
74 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
75 unsigned char *FunctionEnd);
77 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
79 void deallocateMemForFunction(const Function *F);
81 unsigned char*startExceptionTable(const Function* F,
82 uintptr_t &ActualSize);
84 void endExceptionTable(const Function *F, unsigned char *TableStart,
85 unsigned char *TableEnd,
86 unsigned char* FrameRegister);
89 MonoJITMemoryManager::MonoJITMemoryManager ()
92 mm = JITMemoryManager::CreateDefaultMemManager ();
95 MonoJITMemoryManager::~MonoJITMemoryManager ()
100 MonoJITMemoryManager::setMemoryWritable (void)
105 MonoJITMemoryManager::setMemoryExecutable (void)
110 MonoJITMemoryManager::AllocateGOT()
116 MonoJITMemoryManager::SetDlsymTable(void *ptr)
118 mm->SetDlsymTable (ptr);
122 MonoJITMemoryManager::startFunctionBody(const Function *F,
123 uintptr_t &ActualSize)
125 return alloc_cb (wrap (F), ActualSize);
129 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
132 return alloc_cb (wrap (F), StubSize);
136 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
137 unsigned char *FunctionEnd)
139 emitted_cb (wrap (F), FunctionStart, FunctionEnd);
143 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
145 return new unsigned char [Size];
149 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
154 MonoJITMemoryManager::startExceptionTable(const Function* F,
155 uintptr_t &ActualSize)
157 return alloc_cb (wrap (F), ActualSize);
161 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
162 unsigned char *TableEnd,
163 unsigned char* FrameRegister)
167 static MonoJITMemoryManager *mono_mm;
169 static FunctionPassManager *fpm;
172 mono_llvm_optimize_method (LLVMValueRef method)
174 verifyFunction (*(unwrap<Function> (method)));
175 fpm->run (*unwrap<Function> (method));
178 /* Missing overload for building an alloca with an alignment */
180 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
181 LLVMValueRef ArraySize,
182 int alignment, const char *Name)
184 return wrap (unwrap (builder)->Insert (new AllocaInst(unwrap (Ty), unwrap (ArraySize), alignment), Name));
187 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef, LLVMTypeRef Ty,
188 LLVMValueRef Val, const char *Name);
191 LLVMExecutionEngineRef
192 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
196 mono_mm = new MonoJITMemoryManager ();
197 mono_mm->alloc_cb = alloc_cb;
198 mono_mm->emitted_cb = emitted_cb;
200 ExceptionHandling = true;
202 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, false);
203 EE->InstallExceptionTableRegister (exception_cb);
205 fpm = new FunctionPassManager (unwrap (MP));
207 fpm->add(new TargetData(*EE->getTargetData()));
208 /* Add a random set of passes */
209 /* Make this run-time configurable */
210 fpm->add(createInstructionCombiningPass());
211 fpm->add(createReassociatePass());
212 fpm->add(createGVNPass());
213 fpm->add(createCFGSimplificationPass());