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/Support/PrettyStackTrace.h"
34 #include <llvm/CodeGen/Passes.h>
35 #include <llvm/CodeGen/MachineFunctionPass.h>
36 //#include <llvm/LinkAllPasses.h>
38 #include "llvm-c/Core.h"
39 #include "llvm-c/ExecutionEngine.h"
41 #include "mini-llvm-cpp.h"
43 extern "C" void LLVMInitializeX86TargetInfo();
47 class MonoJITMemoryManager : public JITMemoryManager
53 /* Callbacks installed by mono */
54 AllocCodeMemoryCb *alloc_cb;
55 FunctionEmittedCb *emitted_cb;
57 MonoJITMemoryManager ();
58 ~MonoJITMemoryManager ();
60 void setMemoryWritable (void);
62 void setMemoryExecutable (void);
66 unsigned char *getGOTBase() const {
67 return mm->getGOTBase ();
70 void *getDlsymTable() const {
71 return mm->getDlsymTable ();
74 void setPoisonMemory(bool) {
77 void SetDlsymTable(void *ptr);
79 unsigned char *startFunctionBody(const Function *F,
80 uintptr_t &ActualSize);
82 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
85 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
86 unsigned char *FunctionEnd);
88 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
90 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
92 void deallocateMemForFunction(const Function *F);
94 unsigned char*startExceptionTable(const Function* F,
95 uintptr_t &ActualSize);
97 void endExceptionTable(const Function *F, unsigned char *TableStart,
98 unsigned char *TableEnd,
99 unsigned char* FrameRegister);
102 MonoJITMemoryManager::MonoJITMemoryManager ()
105 mm = JITMemoryManager::CreateDefaultMemManager ();
108 MonoJITMemoryManager::~MonoJITMemoryManager ()
113 MonoJITMemoryManager::setMemoryWritable (void)
118 MonoJITMemoryManager::setMemoryExecutable (void)
123 MonoJITMemoryManager::AllocateGOT()
129 MonoJITMemoryManager::SetDlsymTable(void *ptr)
131 mm->SetDlsymTable (ptr);
135 MonoJITMemoryManager::startFunctionBody(const Function *F,
136 uintptr_t &ActualSize)
138 return alloc_cb (wrap (F), ActualSize);
142 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
145 return alloc_cb (wrap (F), StubSize);
149 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
150 unsigned char *FunctionEnd)
152 emitted_cb (wrap (F), FunctionStart, FunctionEnd);
156 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
158 return new unsigned char [Size];
162 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
164 return new unsigned char [Size];
168 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
173 MonoJITMemoryManager::startExceptionTable(const Function* F,
174 uintptr_t &ActualSize)
176 return alloc_cb (wrap (F), ActualSize);
180 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
181 unsigned char *TableEnd,
182 unsigned char* FrameRegister)
186 static MonoJITMemoryManager *mono_mm;
188 static FunctionPassManager *fpm;
190 static LLVMContext ctx;
193 mono_llvm_optimize_method (LLVMValueRef method)
195 verifyFunction (*(unwrap<Function> (method)));
196 fpm->run (*unwrap<Function> (method));
200 mono_llvm_dump_value (LLVMValueRef value)
202 /* Same as LLVMDumpValue (), but print to stdout */
203 cout << (*unwrap<Value> (value));
206 /* Missing overload for building an alloca with an alignment */
208 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
209 LLVMValueRef ArraySize,
210 int alignment, const char *Name)
212 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
216 mono_llvm_build_volatile_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
219 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), true, Name));
222 static cl::list<const PassInfo*, bool, PassNameParser>
223 PassList(cl::desc("Optimizations available:"));
225 LLVMExecutionEngineRef
226 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
230 LLVMInitializeX86Target ();
231 LLVMInitializeX86TargetInfo ();
233 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
235 mono_mm = new MonoJITMemoryManager ();
236 mono_mm->alloc_cb = alloc_cb;
237 mono_mm->emitted_cb = emitted_cb;
239 ExceptionHandling = true;
240 // PrettyStackTrace installs signal handlers which trip up libgc
241 DisablePrettyStackTrace = true;
243 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default);
245 cerr << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
246 g_assert_not_reached ();
248 EE->InstallExceptionTableRegister (exception_cb);
250 fpm = new FunctionPassManager (unwrap (MP));
252 fpm->add(new TargetData(*EE->getTargetData()));
253 /* Add a random set of passes */
254 /* Make this run-time configurable */
255 fpm->add(createInstructionCombiningPass());
256 fpm->add(createReassociatePass());
257 fpm->add(createGVNPass());
258 fpm->add(createCFGSimplificationPass());
260 /* Add passes specified by the env variable */
261 /* FIXME: This can only add passes which are linked in, thus are already used */
262 for (unsigned i = 0; i < PassList.size(); ++i) {
263 const PassInfo *PassInf = PassList[i];
266 if (PassInf->getNormalCtor())
267 P = PassInf->getNormalCtor()();
268 if (dynamic_cast<MachineFunctionPass*>(P) != 0) {
269 cerr << PassInf->getPassName () << " is a machine function pass.\n";
279 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee)