1 //===-- JIT.cpp - MC-based Just-in-Time Compiler --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "llvm/Function.h"
12 #include "llvm/ExecutionEngine/GenericValue.h"
13 #include "llvm/ExecutionEngine/MCJIT.h"
14 #include "llvm/ExecutionEngine/JITMemoryManager.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/DynamicLibrary.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Target/TargetData.h"
25 static struct RegisterJIT {
26 RegisterJIT() { MCJIT::Register(); }
31 extern "C" void LLVMLinkInMCJIT() {
34 ExecutionEngine *MCJIT::createJIT(Module *M,
35 std::string *ErrorStr,
36 JITMemoryManager *JMM,
37 CodeGenOpt::Level OptLevel,
42 const SmallVectorImpl<std::string>& MAttrs) {
43 // Try to register the program as a source of symbols to resolve against.
45 // FIXME: Don't do this here.
46 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
48 // Pick a target either via -march or by guessing the native arch.
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
53 TargetMachine *TM = MCJIT::selectTarget(M, MArch, MCPU, MAttrs, ErrorStr);
54 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
55 TM->setCodeModel(CMM);
57 // If the target supports JIT code generation, create the JIT.
58 if (TargetJITInfo *TJ = TM->getJITInfo())
59 return new MCJIT(M, TM, *TJ, JMM, OptLevel, GVsWithCode);
62 *ErrorStr = "target does not support JIT code generation";
66 MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
67 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68 bool AllocateGVsWithCode)
69 : ExecutionEngine(m), TM(tm), M(m), OS(Buffer) {
71 PM.add(new TargetData(*TM->getTargetData()));
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!");
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().
85 // Flush the output buffer so the SmallVector gets its data.
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(),
98 void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
99 report_fatal_error("not yet implemented");
103 void *MCJIT::getPointerToFunction(Function *F) {
104 Twine Name = TM->getMCAsmInfo()->getGlobalPrefix() + F->getName();
105 return Dyld.getSymbolAddress(Name.str());
108 void *MCJIT::recompileAndRelinkFunction(Function *F) {
109 report_fatal_error("not yet implemented");
112 void MCJIT::freeMachineCodeForFunction(Function *F) {
113 report_fatal_error("not yet implemented");
116 GenericValue MCJIT::runFunction(Function *F,
117 const std::vector<GenericValue> &ArgValues) {
118 assert(ArgValues.size() == 0 && "JIT arg passing not supported yet");
119 void *FPtr = getPointerToFunction(F);
121 report_fatal_error("Unable to locate function: '" + F->getName() + "'");
122 ((void(*)(void))(intptr_t)FPtr)();
123 return GenericValue();