blob: 2b5ec5de7f949f40dea848a0a37052e45239b3b0 [file] [log] [blame]
Daniel Dunbar6aec2982010-11-17 16:06:43 +00001//===-- JIT.cpp - MC-based Just-in-Time Compiler --------------------------===//
2//
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"
14#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000015#include "llvm/Support/DynamicLibrary.h"
Jim Grosbach31649e62011-03-18 22:48:41 +000016#include "llvm/Target/TargetData.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000017
18using namespace llvm;
19
20namespace {
21
22static struct RegisterJIT {
23 RegisterJIT() { MCJIT::Register(); }
24} JITRegistrator;
25
26}
27
28extern "C" void LLVMLinkInMCJIT() {
29}
30
31ExecutionEngine *MCJIT::createJIT(Module *M,
32 std::string *ErrorStr,
33 JITMemoryManager *JMM,
34 CodeGenOpt::Level OptLevel,
35 bool GVsWithCode,
36 CodeModel::Model CMM,
37 StringRef MArch,
38 StringRef MCPU,
39 const SmallVectorImpl<std::string>& MAttrs) {
40 // Try to register the program as a source of symbols to resolve against.
41 //
42 // FIXME: Don't do this here.
43 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
44
45 // Pick a target either via -march or by guessing the native arch.
46 //
47 // FIXME: This should be lifted out of here, it isn't something which should
48 // be part of the JIT policy, rather the burden for this selection should be
49 // pushed to clients.
50 TargetMachine *TM = MCJIT::selectTarget(M, MArch, MCPU, MAttrs, ErrorStr);
51 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
52 TM->setCodeModel(CMM);
53
54 // If the target supports JIT code generation, create the JIT.
55 if (TargetJITInfo *TJ = TM->getJITInfo())
Jim Grosbach31649e62011-03-18 22:48:41 +000056 return new MCJIT(M, TM, *TJ, JMM, OptLevel, GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000057
58 if (ErrorStr)
59 *ErrorStr = "target does not support JIT code generation";
60 return 0;
61}
62
Jim Grosbach31649e62011-03-18 22:48:41 +000063MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000064 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
65 bool AllocateGVsWithCode)
Jim Grosbach31649e62011-03-18 22:48:41 +000066 : ExecutionEngine(m), TM(tm), M(m), OS(Buffer) {
67
68 PM.add(new TargetData(*TM->getTargetData()));
69
70 // Turn the machine code intermediate representation into bytes in memory
71 // that may be executed.
72 if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
73 report_fatal_error("Target does not support MC emission!");
74 }
75
76 // Initialize passes.
77 ExecutionEngine::addModule(M);
78 // FIXME: When we support multiple modules, we'll want to move the code
79 // gen and finalization out of the constructor here and do it more
80 // on-demand as part of getPointerToFunction().
81 PM.run(*M);
82 // Flush the output buffer so the SmallVector gets its data.
83 OS.flush();
Daniel Dunbar6aec2982010-11-17 16:06:43 +000084}
85
86MCJIT::~MCJIT() {
87}
88
89void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
90 report_fatal_error("not yet implemented");
91 return 0;
92}
93
94void *MCJIT::getPointerToFunction(Function *F) {
Daniel Dunbar6aec2982010-11-17 16:06:43 +000095 return 0;
96}
97
98void *MCJIT::recompileAndRelinkFunction(Function *F) {
99 report_fatal_error("not yet implemented");
100}
101
102void MCJIT::freeMachineCodeForFunction(Function *F) {
103 report_fatal_error("not yet implemented");
104}
105
106GenericValue MCJIT::runFunction(Function *F,
107 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach31649e62011-03-18 22:48:41 +0000108 assert(ArgValues.size() == 0 && "JIT arg passing not supported yet");
109 void *FPtr = getPointerToFunction(F);
110 if (!FPtr)
111 report_fatal_error("Unable to locate function: '" + F->getName() + "'");
112 ((void(*)(void))FPtr)();
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000113 return GenericValue();
114}