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 void *getDlsymTable() const {
72 return mm->getDlsymTable ();
75 void setPoisonMemory(bool) {
78 void SetDlsymTable(void *ptr);
80 unsigned char *startFunctionBody(const Function *F,
81 uintptr_t &ActualSize);
83 unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
86 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
87 unsigned char *FunctionEnd);
89 unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
91 uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
93 void deallocateMemForFunction(const Function *F);
95 unsigned char*startExceptionTable(const Function* F,
96 uintptr_t &ActualSize);
98 void endExceptionTable(const Function *F, unsigned char *TableStart,
99 unsigned char *TableEnd,
100 unsigned char* FrameRegister);
103 MonoJITMemoryManager::MonoJITMemoryManager ()
106 mm = JITMemoryManager::CreateDefaultMemManager ();
109 MonoJITMemoryManager::~MonoJITMemoryManager ()
114 MonoJITMemoryManager::setMemoryWritable (void)
119 MonoJITMemoryManager::setMemoryExecutable (void)
124 MonoJITMemoryManager::AllocateGOT()
130 MonoJITMemoryManager::SetDlsymTable(void *ptr)
132 mm->SetDlsymTable (ptr);
136 MonoJITMemoryManager::startFunctionBody(const Function *F,
137 uintptr_t &ActualSize)
139 return alloc_cb (wrap (F), ActualSize);
143 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
146 return alloc_cb (wrap (F), StubSize);
150 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
151 unsigned char *FunctionEnd)
153 emitted_cb (wrap (F), FunctionStart, FunctionEnd);
157 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
159 return new unsigned char [Size];
163 MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
165 return new unsigned char [Size];
169 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
174 MonoJITMemoryManager::startExceptionTable(const Function* F,
175 uintptr_t &ActualSize)
177 return alloc_cb (wrap (F), ActualSize);
181 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
182 unsigned char *TableEnd,
183 unsigned char* FrameRegister)
187 static MonoJITMemoryManager *mono_mm;
189 static FunctionPassManager *fpm;
191 static LLVMContext ctx;
194 mono_llvm_optimize_method (LLVMValueRef method)
196 verifyFunction (*(unwrap<Function> (method)));
197 fpm->run (*unwrap<Function> (method));
201 mono_llvm_dump_value (LLVMValueRef value)
203 /* Same as LLVMDumpValue (), but print to stdout */
204 outs () << (*unwrap<Value> (value));
207 /* Missing overload for building an alloca with an alignment */
209 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
210 LLVMValueRef ArraySize,
211 int alignment, const char *Name)
213 return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
217 mono_llvm_build_volatile_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
220 return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), true, Name));
223 static cl::list<const PassInfo*, bool, PassNameParser>
224 PassList(cl::desc("Optimizations available:"));
226 LLVMExecutionEngineRef
227 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
231 LLVMInitializeX86Target ();
232 LLVMInitializeX86TargetInfo ();
234 llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
236 mono_mm = new MonoJITMemoryManager ();
237 mono_mm->alloc_cb = alloc_cb;
238 mono_mm->emitted_cb = emitted_cb;
240 DwarfExceptionHandling = true;
241 // PrettyStackTrace installs signal handlers which trip up libgc
242 DisablePrettyStackTrace = true;
244 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default);
246 errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
247 g_assert_not_reached ();
249 EE->InstallExceptionTableRegister (exception_cb);
251 fpm = new FunctionPassManager (unwrap (MP));
253 fpm->add(new TargetData(*EE->getTargetData()));
254 /* Add a random set of passes */
255 /* Make this run-time configurable */
256 fpm->add(createInstructionCombiningPass());
257 fpm->add(createReassociatePass());
258 fpm->add(createGVNPass());
259 fpm->add(createCFGSimplificationPass());
261 /* Add passes specified by the env variable */
262 /* FIXME: This can only add passes which are linked in, thus are already used */
263 for (unsigned i = 0; i < PassList.size(); ++i) {
264 const PassInfo *PassInf = PassList[i];
267 if (PassInf->getNormalCtor())
268 P = PassInf->getNormalCtor()();
269 if (dynamic_cast<MachineFunctionPass*>(P) != 0) {
270 errs () << PassInf->getPassName () << " is a machine function pass.\n";
280 mono_llvm_dispose_ee (LLVMExecutionEngineRef ee)