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>
31 #include <llvm/Support/CommandLine.h>
32 #include "llvm/Support/PassNameParser.h"
33 #include <llvm/CodeGen/Passes.h>
34 #include <llvm/CodeGen/MachineFunctionPass.h>
35 //#include <llvm/LinkAllPasses.h>
37 #include "llvm-c/Core.h"
38 #include "llvm-c/ExecutionEngine.h"
40 #include "mini-llvm-cpp.h"
44 class MonoJITMemoryManager : public JITMemoryManager
50 /* Callbacks installed by mono */
51 AllocCodeMemoryCb *alloc_cb;
52 FunctionEmittedCb *emitted_cb;
54 MonoJITMemoryManager ();
55 ~MonoJITMemoryManager ();
57 void setMemoryWritable (void);
59 void setMemoryExecutable (void);
63 unsigned char *getGOTBase() const {
64 return mm->getGOTBase ();
67 void *getDlsymTable() const {
68 return mm->getDlsymTable ();
71 void SetDlsymTable(void *ptr);
73 unsigned char *startFunctionBody(const Function *F,
74 uintptr_t &ActualSize);
76 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
79 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
80 unsigned char *FunctionEnd);
82 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
84 void deallocateMemForFunction(const Function *F);
86 unsigned char*startExceptionTable(const Function* F,
87 uintptr_t &ActualSize);
89 void endExceptionTable(const Function *F, unsigned char *TableStart,
90 unsigned char *TableEnd,
91 unsigned char* FrameRegister);
94 MonoJITMemoryManager::MonoJITMemoryManager ()
97 mm = JITMemoryManager::CreateDefaultMemManager ();
100 MonoJITMemoryManager::~MonoJITMemoryManager ()
105 MonoJITMemoryManager::setMemoryWritable (void)
110 MonoJITMemoryManager::setMemoryExecutable (void)
115 MonoJITMemoryManager::AllocateGOT()
121 MonoJITMemoryManager::SetDlsymTable(void *ptr)
123 mm->SetDlsymTable (ptr);
127 MonoJITMemoryManager::startFunctionBody(const Function *F,
128 uintptr_t &ActualSize)
130 return alloc_cb (wrap (F), ActualSize);
134 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
137 return alloc_cb (wrap (F), StubSize);
141 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
142 unsigned char *FunctionEnd)
144 emitted_cb (wrap (F), FunctionStart, FunctionEnd);
148 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
150 return new unsigned char [Size];
154 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
159 MonoJITMemoryManager::startExceptionTable(const Function* F,
160 uintptr_t &ActualSize)
162 return alloc_cb (wrap (F), ActualSize);
166 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
167 unsigned char *TableEnd,
168 unsigned char* FrameRegister)
172 static MonoJITMemoryManager *mono_mm;
174 static FunctionPassManager *fpm;
177 mono_llvm_optimize_method (LLVMValueRef method)
179 verifyFunction (*(unwrap<Function> (method)));
180 fpm->run (*unwrap<Function> (method));
184 mono_llvm_dump_value (LLVMValueRef value)
186 /* Same as LLVMDumpValue (), but print to stdout */
187 cout << (*unwrap<Value> (value));
190 /* Missing overload for building an alloca with an alignment */
192 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
193 LLVMValueRef ArraySize,
194 int alignment, const char *Name)
196 return wrap (unwrap (builder)->Insert (new AllocaInst(unwrap (Ty), unwrap (ArraySize), alignment), Name));
200 mono_llvm_build_volatile_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
203 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), true, Name));
206 static cl::list<const PassInfo*, bool, PassNameParser>
207 PassList(cl::desc("Optimizations available:"));
209 LLVMExecutionEngineRef
210 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
214 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
216 mono_mm = new MonoJITMemoryManager ();
217 mono_mm->alloc_cb = alloc_cb;
218 mono_mm->emitted_cb = emitted_cb;
220 ExceptionHandling = true;
222 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default);
224 cerr << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
225 g_assert_not_reached ();
227 EE->InstallExceptionTableRegister (exception_cb);
229 fpm = new FunctionPassManager (unwrap (MP));
231 fpm->add(new TargetData(*EE->getTargetData()));
232 /* Add a random set of passes */
233 /* Make this run-time configurable */
234 fpm->add(createInstructionCombiningPass());
235 fpm->add(createReassociatePass());
236 fpm->add(createGVNPass());
237 fpm->add(createCFGSimplificationPass());
239 /* Add passes specified by the env variable */
240 /* FIXME: This can only add passes which are linked in, thus are already used */
241 for (unsigned i = 0; i < PassList.size(); ++i) {
242 const PassInfo *PassInf = PassList[i];
245 if (PassInf->getNormalCtor())
246 P = PassInf->getNormalCtor()();
247 if (dynamic_cast<MachineFunctionPass*>(P) != 0) {
248 cerr << PassInf->getPassName () << " is a machine function pass.\n";
258 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee)