blob: df5c99e98a7eff3f0ae0c6d767e2103d6fc707c6 [file] [log] [blame]
Eric Christopherd6190672011-03-22 08:49:56 +00001//===-- MCJIT.cpp - MC-based Just-in-Time Compiler --------------------------===//
Daniel Dunbar6aec2982010-11-17 16:06:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "MCJIT.h"
Jim Grosbach31649e62011-03-18 22:48:41 +000011#include "llvm/Function.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000012#include "llvm/ExecutionEngine/GenericValue.h"
13#include "llvm/ExecutionEngine/MCJIT.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000014#include "llvm/ExecutionEngine/JITMemoryManager.h"
15#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000016#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000017#include "llvm/Support/DynamicLibrary.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000018#include "llvm/Support/MemoryBuffer.h"
Jim Grosbach31649e62011-03-18 22:48:41 +000019#include "llvm/Target/TargetData.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000020
21using namespace llvm;
22
23namespace {
24
25static struct RegisterJIT {
26 RegisterJIT() { MCJIT::Register(); }
27} JITRegistrator;
28
29}
30
31extern "C" void LLVMLinkInMCJIT() {
32}
33
34ExecutionEngine *MCJIT::createJIT(Module *M,
35 std::string *ErrorStr,
36 JITMemoryManager *JMM,
37 CodeGenOpt::Level OptLevel,
38 bool GVsWithCode,
39 CodeModel::Model CMM,
40 StringRef MArch,
41 StringRef MCPU,
42 const SmallVectorImpl<std::string>& MAttrs) {
43 // Try to register the program as a source of symbols to resolve against.
44 //
45 // FIXME: Don't do this here.
46 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
47
48 // Pick a target either via -march or by guessing the native arch.
49 //
50 // FIXME: This should be lifted out of here, it isn't something which should
51 // be part of the JIT policy, rather the burden for this selection should be
52 // pushed to clients.
53 TargetMachine *TM = MCJIT::selectTarget(M, MArch, MCPU, MAttrs, ErrorStr);
54 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
55 TM->setCodeModel(CMM);
56
57 // If the target supports JIT code generation, create the JIT.
58 if (TargetJITInfo *TJ = TM->getJITInfo())
Jim Grosbach31649e62011-03-18 22:48:41 +000059 return new MCJIT(M, TM, *TJ, JMM, OptLevel, GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000060
61 if (ErrorStr)
62 *ErrorStr = "target does not support JIT code generation";
63 return 0;
64}
65
Jim Grosbach31649e62011-03-18 22:48:41 +000066MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000067 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68 bool AllocateGVsWithCode)
Jim Grosbach31649e62011-03-18 22:48:41 +000069 : ExecutionEngine(m), TM(tm), M(m), OS(Buffer) {
70
71 PM.add(new TargetData(*TM->getTargetData()));
72
73 // Turn the machine code intermediate representation into bytes in memory
74 // that may be executed.
75 if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
76 report_fatal_error("Target does not support MC emission!");
77 }
78
79 // Initialize passes.
80 ExecutionEngine::addModule(M);
81 // FIXME: When we support multiple modules, we'll want to move the code
82 // gen and finalization out of the constructor here and do it more
83 // on-demand as part of getPointerToFunction().
84 PM.run(*M);
85 // Flush the output buffer so the SmallVector gets its data.
86 OS.flush();
Jim Grosbachf9229102011-03-22 01:06:42 +000087
88 // Load the object into the dynamic linker.
89 // FIXME: It would be nice to avoid making yet another copy.
90 MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(),
91 Buffer.size()));
92 Dyld.loadObject(MB);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000093}
94
95MCJIT::~MCJIT() {
96}
97
98void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
99 report_fatal_error("not yet implemented");
100 return 0;
101}
102
103void *MCJIT::getPointerToFunction(Function *F) {
Jim Grosbachf9229102011-03-22 01:06:42 +0000104 Twine Name = TM->getMCAsmInfo()->getGlobalPrefix() + F->getName();
105 return Dyld.getSymbolAddress(Name.str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000106}
107
108void *MCJIT::recompileAndRelinkFunction(Function *F) {
109 report_fatal_error("not yet implemented");
110}
111
112void MCJIT::freeMachineCodeForFunction(Function *F) {
113 report_fatal_error("not yet implemented");
114}
115
116GenericValue MCJIT::runFunction(Function *F,
117 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach31649e62011-03-18 22:48:41 +0000118 assert(ArgValues.size() == 0 && "JIT arg passing not supported yet");
119 void *FPtr = getPointerToFunction(F);
120 if (!FPtr)
121 report_fatal_error("Unable to locate function: '" + F->getName() + "'");
Jim Grosbach7b6747e2011-03-18 22:50:49 +0000122 ((void(*)(void))(intptr_t)FPtr)();
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000123 return GenericValue();
124}