12
12
#include " llvm/DerivedTypes.h"
13
13
#include " llvm/Function.h"
14
14
#include " llvm/ExecutionEngine/GenericValue.h"
15
- #include " llvm/ExecutionEngine/MCJIT.h"
16
15
#include " llvm/ExecutionEngine/JITMemoryManager.h"
16
+ #include " llvm/ExecutionEngine/MCJIT.h"
17
+ #include " llvm/ExecutionEngine/ObjectBuffer.h"
18
+ #include " llvm/ExecutionEngine/ObjectImage.h"
17
19
#include " llvm/MC/MCAsmInfo.h"
18
20
#include " llvm/Support/ErrorHandling.h"
19
21
#include " llvm/Support/DynamicLibrary.h"
@@ -50,7 +52,7 @@ ExecutionEngine *MCJIT::createJIT(Module *M,
50
52
MCJIT::MCJIT (Module *m, TargetMachine *tm , RTDyldMemoryManager *MM,
51
53
bool AllocateGVsWithCode)
52
54
: ExecutionEngine(m), TM(tm ), Ctx(0 ), MemMgr(MM), Dyld(MM),
53
- isCompiled(false ), M(m), OS(Buffer) {
55
+ isCompiled(false ), M(m) {
54
56
55
57
setTargetData (TM->getTargetData ());
56
58
}
@@ -63,7 +65,7 @@ MCJIT::~MCJIT() {
63
65
void MCJIT::emitObject (Module *m) {
64
66
// / Currently, MCJIT only supports a single module and the module passed to
65
67
// / this function call is expected to be the contained module. The module
66
- // / is passed as a parameter here to prepare for multiple module support in
68
+ // / is passed as a parameter here to prepare for multiple module support in
67
69
// / the future.
68
70
assert (M == m);
69
71
@@ -80,30 +82,32 @@ void MCJIT::emitObject(Module *m) {
80
82
81
83
PM.add (new TargetData (*TM->getTargetData ()));
82
84
85
+ // The RuntimeDyld will take ownership of this shortly
86
+ OwningPtr<ObjectBufferStream> Buffer (new ObjectBufferStream ());
87
+
83
88
// Turn the machine code intermediate representation into bytes in memory
84
89
// that may be executed.
85
- if (TM->addPassesToEmitMC (PM, Ctx, OS , false )) {
90
+ if (TM->addPassesToEmitMC (PM, Ctx, Buffer-> getOStream () , false )) {
86
91
report_fatal_error (" Target does not support MC emission!" );
87
92
}
88
93
89
94
// Initialize passes.
90
- // FIXME: When we support multiple modules, we'll want to move the code
91
- // gen and finalization out of the constructor here and do it more
92
- // on-demand as part of getPointerToFunction().
93
95
PM.run (*m);
94
- // Flush the output buffer so the SmallVector gets its data.
95
- OS. flush ();
96
+ // Flush the output buffer to get the generated code into memory
97
+ Buffer-> flush ();
96
98
97
99
// Load the object into the dynamic linker.
98
- MemoryBuffer* MB = MemoryBuffer::getMemBuffer (StringRef (Buffer.data (),
99
- Buffer.size ()),
100
- " " , false );
101
- if (Dyld.loadObject (MB))
100
+ // handing off ownership of the buffer
101
+ LoadedObject.reset (Dyld.loadObject (Buffer.take ()));
102
+ if (!LoadedObject)
102
103
report_fatal_error (Dyld.getErrorString ());
103
104
104
105
// Resolve any relocations.
105
106
Dyld.resolveRelocations ();
106
107
108
+ // FIXME: Make this optional, maybe even move it to a JIT event listener
109
+ LoadedObject->registerWithDebugger ();
110
+
107
111
// FIXME: Add support for per-module compilation state
108
112
isCompiled = true ;
109
113
}
0 commit comments