blob: a0280b7c79f10e8148949afd4fc150c346a3f155 [file] [log] [blame]
Eric Christopherbb498ca2011-04-22 03:07:06 +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 Grosbachfcbe5b72011-04-04 23:04:39 +000011#include "MCJITMemoryManager.h"
Jim Grosbach34714a02011-03-22 18:05:27 +000012#include "llvm/DerivedTypes.h"
Jim Grosbach31649e62011-03-18 22:48:41 +000013#include "llvm/Function.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000014#include "llvm/ExecutionEngine/GenericValue.h"
15#include "llvm/ExecutionEngine/MCJIT.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000016#include "llvm/ExecutionEngine/JITMemoryManager.h"
17#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000018#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000019#include "llvm/Support/DynamicLibrary.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000020#include "llvm/Support/MemoryBuffer.h"
Jim Grosbach31649e62011-03-18 22:48:41 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000022
23using namespace llvm;
24
25namespace {
26
27static struct RegisterJIT {
28 RegisterJIT() { MCJIT::Register(); }
29} JITRegistrator;
30
31}
32
33extern "C" void LLVMLinkInMCJIT() {
34}
35
36ExecutionEngine *MCJIT::createJIT(Module *M,
37 std::string *ErrorStr,
38 JITMemoryManager *JMM,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000039 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000040 TargetMachine *TM) {
Daniel Dunbar6aec2982010-11-17 16:06:43 +000041 // Try to register the program as a source of symbols to resolve against.
42 //
43 // FIXME: Don't do this here.
44 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
45
Daniel Dunbar6aec2982010-11-17 16:06:43 +000046 // If the target supports JIT code generation, create the JIT.
47 if (TargetJITInfo *TJ = TM->getJITInfo())
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000048 return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM, M), GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000049
50 if (ErrorStr)
51 *ErrorStr = "target does not support JIT code generation";
52 return 0;
53}
54
Jim Grosbach31649e62011-03-18 22:48:41 +000055MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000056 RTDyldMemoryManager *MM, bool AllocateGVsWithCode)
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000057 : ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) {
Jim Grosbach31649e62011-03-18 22:48:41 +000058
Danil Malyshev0ba3c0a2011-09-30 16:40:10 +000059 setTargetData(TM->getTargetData());
Jim Grosbach31649e62011-03-18 22:48:41 +000060 PM.add(new TargetData(*TM->getTargetData()));
61
62 // Turn the machine code intermediate representation into bytes in memory
63 // that may be executed.
Evan Chengb95fc312011-11-16 08:38:26 +000064 if (TM->addPassesToEmitMC(PM, Ctx, OS, false)) {
Jim Grosbach31649e62011-03-18 22:48:41 +000065 report_fatal_error("Target does not support MC emission!");
66 }
67
68 // Initialize passes.
Jim Grosbach31649e62011-03-18 22:48:41 +000069 // FIXME: When we support multiple modules, we'll want to move the code
70 // gen and finalization out of the constructor here and do it more
71 // on-demand as part of getPointerToFunction().
72 PM.run(*M);
73 // Flush the output buffer so the SmallVector gets its data.
74 OS.flush();
Jim Grosbachf9229102011-03-22 01:06:42 +000075
76 // Load the object into the dynamic linker.
77 // FIXME: It would be nice to avoid making yet another copy.
78 MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(),
79 Buffer.size()));
Jim Grosbach8086f3b2011-03-23 19:51:34 +000080 if (Dyld.loadObject(MB))
81 report_fatal_error(Dyld.getErrorString());
Jim Grosbach69e81322011-04-13 15:28:10 +000082 // Resolve any relocations.
83 Dyld.resolveRelocations();
Daniel Dunbar6aec2982010-11-17 16:06:43 +000084}
85
86MCJIT::~MCJIT() {
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000087 delete MemMgr;
Jim Grosbach893f4862012-01-17 23:08:46 +000088 delete TM;
Daniel Dunbar6aec2982010-11-17 16:06:43 +000089}
90
91void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
92 report_fatal_error("not yet implemented");
93 return 0;
94}
95
96void *MCJIT::getPointerToFunction(Function *F) {
Jim Grosbach34714a02011-03-22 18:05:27 +000097 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
98 bool AbortOnFailure = !F->hasExternalWeakLinkage();
99 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
100 addGlobalMapping(F, Addr);
101 return Addr;
102 }
103
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000104 // FIXME: Should we be using the mangler for this? Probably.
105 StringRef BaseName = F->getName();
106 if (BaseName[0] == '\1')
Jim Grosbachc0ceedb2011-05-19 00:45:05 +0000107 return (void*)Dyld.getSymbolAddress(BaseName.substr(1));
108 return (void*)Dyld.getSymbolAddress((TM->getMCAsmInfo()->getGlobalPrefix()
109 + BaseName).str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000110}
111
112void *MCJIT::recompileAndRelinkFunction(Function *F) {
113 report_fatal_error("not yet implemented");
114}
115
116void MCJIT::freeMachineCodeForFunction(Function *F) {
117 report_fatal_error("not yet implemented");
118}
119
120GenericValue MCJIT::runFunction(Function *F,
121 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000122 assert(F && "Function *F was null at entry to run()");
123
Jim Grosbach31649e62011-03-18 22:48:41 +0000124 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000125 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000126 FunctionType *FTy = F->getFunctionType();
127 Type *RetTy = FTy->getReturnType();
Jim Grosbach34714a02011-03-22 18:05:27 +0000128
129 assert((FTy->getNumParams() == ArgValues.size() ||
130 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
131 "Wrong number of arguments passed into function!");
132 assert(FTy->getNumParams() == ArgValues.size() &&
133 "This doesn't support passing arguments through varargs (yet)!");
134
135 // Handle some common cases first. These cases correspond to common `main'
136 // prototypes.
137 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
138 switch (ArgValues.size()) {
139 case 3:
140 if (FTy->getParamType(0)->isIntegerTy(32) &&
141 FTy->getParamType(1)->isPointerTy() &&
142 FTy->getParamType(2)->isPointerTy()) {
143 int (*PF)(int, char **, const char **) =
144 (int(*)(int, char **, const char **))(intptr_t)FPtr;
145
146 // Call the function.
147 GenericValue rv;
148 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
149 (char **)GVTOP(ArgValues[1]),
150 (const char **)GVTOP(ArgValues[2])));
151 return rv;
152 }
153 break;
154 case 2:
155 if (FTy->getParamType(0)->isIntegerTy(32) &&
156 FTy->getParamType(1)->isPointerTy()) {
157 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
158
159 // Call the function.
160 GenericValue rv;
161 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
162 (char **)GVTOP(ArgValues[1])));
163 return rv;
164 }
165 break;
166 case 1:
167 if (FTy->getNumParams() == 1 &&
168 FTy->getParamType(0)->isIntegerTy(32)) {
169 GenericValue rv;
170 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
171 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
172 return rv;
173 }
174 break;
175 }
176 }
177
178 // Handle cases where no arguments are passed first.
179 if (ArgValues.empty()) {
180 GenericValue rv;
181 switch (RetTy->getTypeID()) {
182 default: llvm_unreachable("Unknown return type for function call!");
183 case Type::IntegerTyID: {
184 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
185 if (BitWidth == 1)
186 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
187 else if (BitWidth <= 8)
188 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
189 else if (BitWidth <= 16)
190 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
191 else if (BitWidth <= 32)
192 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
193 else if (BitWidth <= 64)
194 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
195 else
196 llvm_unreachable("Integer types > 64 bits not supported");
197 return rv;
198 }
199 case Type::VoidTyID:
200 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
201 return rv;
202 case Type::FloatTyID:
203 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
204 return rv;
205 case Type::DoubleTyID:
206 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
207 return rv;
208 case Type::X86_FP80TyID:
209 case Type::FP128TyID:
210 case Type::PPC_FP128TyID:
211 llvm_unreachable("long double not supported yet");
212 return rv;
213 case Type::PointerTyID:
214 return PTOGV(((void*(*)())(intptr_t)FPtr)());
215 }
216 }
217
Richard Trieu81cbb0a2011-09-10 01:42:07 +0000218 assert(0 && "Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000219 return GenericValue();
220}