blob: 09a79f64d428cd92684713df43508c390eb92f91 [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,
39 CodeGenOpt::Level OptLevel,
40 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000041 TargetMachine *TM) {
Daniel Dunbar6aec2982010-11-17 16:06:43 +000042 // Try to register the program as a source of symbols to resolve against.
43 //
44 // FIXME: Don't do this here.
45 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
46
Daniel Dunbar6aec2982010-11-17 16:06:43 +000047 // If the target supports JIT code generation, create the JIT.
48 if (TargetJITInfo *TJ = TM->getJITInfo())
Jim Grosbachc1545142011-05-12 18:21:23 +000049 return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM, M), OptLevel,
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000050 GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000051
52 if (ErrorStr)
53 *ErrorStr = "target does not support JIT code generation";
54 return 0;
55}
56
Jim Grosbach31649e62011-03-18 22:48:41 +000057MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000058 RTDyldMemoryManager *MM, CodeGenOpt::Level OptLevel,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000059 bool AllocateGVsWithCode)
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000060 : ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) {
Jim Grosbach31649e62011-03-18 22:48:41 +000061
62 PM.add(new TargetData(*TM->getTargetData()));
63
64 // Turn the machine code intermediate representation into bytes in memory
65 // that may be executed.
66 if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
67 report_fatal_error("Target does not support MC emission!");
68 }
69
70 // Initialize passes.
Jim Grosbach31649e62011-03-18 22:48:41 +000071 // FIXME: When we support multiple modules, we'll want to move the code
72 // gen and finalization out of the constructor here and do it more
73 // on-demand as part of getPointerToFunction().
74 PM.run(*M);
75 // Flush the output buffer so the SmallVector gets its data.
76 OS.flush();
Jim Grosbachf9229102011-03-22 01:06:42 +000077
78 // Load the object into the dynamic linker.
79 // FIXME: It would be nice to avoid making yet another copy.
80 MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(),
81 Buffer.size()));
Jim Grosbach8086f3b2011-03-23 19:51:34 +000082 if (Dyld.loadObject(MB))
83 report_fatal_error(Dyld.getErrorString());
Jim Grosbach69e81322011-04-13 15:28:10 +000084 // Resolve any relocations.
85 Dyld.resolveRelocations();
Daniel Dunbar6aec2982010-11-17 16:06:43 +000086}
87
88MCJIT::~MCJIT() {
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000089 delete MemMgr;
Daniel Dunbar6aec2982010-11-17 16:06:43 +000090}
91
92void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
93 report_fatal_error("not yet implemented");
94 return 0;
95}
96
97void *MCJIT::getPointerToFunction(Function *F) {
Jim Grosbach34714a02011-03-22 18:05:27 +000098 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
99 bool AbortOnFailure = !F->hasExternalWeakLinkage();
100 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
101 addGlobalMapping(F, Addr);
102 return Addr;
103 }
104
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000105 // FIXME: Should we be using the mangler for this? Probably.
106 StringRef BaseName = F->getName();
Jim Grosbache0b58632011-05-18 23:56:43 +0000107 Twine Name;
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000108 if (BaseName[0] == '\1')
Jim Grosbache0b58632011-05-18 23:56:43 +0000109 Name = BaseName.substr(1);
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000110 else
Jim Grosbache0b58632011-05-18 23:56:43 +0000111 Name = TM->getMCAsmInfo()->getGlobalPrefix() + BaseName;
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000112 return (void*)Dyld.getSymbolAddress(Name.str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000113}
114
115void *MCJIT::recompileAndRelinkFunction(Function *F) {
116 report_fatal_error("not yet implemented");
117}
118
119void MCJIT::freeMachineCodeForFunction(Function *F) {
120 report_fatal_error("not yet implemented");
121}
122
123GenericValue MCJIT::runFunction(Function *F,
124 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000125 assert(F && "Function *F was null at entry to run()");
126
Jim Grosbach31649e62011-03-18 22:48:41 +0000127 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000128 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
129 const FunctionType *FTy = F->getFunctionType();
130 const Type *RetTy = FTy->getReturnType();
131
132 assert((FTy->getNumParams() == ArgValues.size() ||
133 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
134 "Wrong number of arguments passed into function!");
135 assert(FTy->getNumParams() == ArgValues.size() &&
136 "This doesn't support passing arguments through varargs (yet)!");
137
138 // Handle some common cases first. These cases correspond to common `main'
139 // prototypes.
140 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
141 switch (ArgValues.size()) {
142 case 3:
143 if (FTy->getParamType(0)->isIntegerTy(32) &&
144 FTy->getParamType(1)->isPointerTy() &&
145 FTy->getParamType(2)->isPointerTy()) {
146 int (*PF)(int, char **, const char **) =
147 (int(*)(int, char **, const char **))(intptr_t)FPtr;
148
149 // Call the function.
150 GenericValue rv;
151 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
152 (char **)GVTOP(ArgValues[1]),
153 (const char **)GVTOP(ArgValues[2])));
154 return rv;
155 }
156 break;
157 case 2:
158 if (FTy->getParamType(0)->isIntegerTy(32) &&
159 FTy->getParamType(1)->isPointerTy()) {
160 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
161
162 // Call the function.
163 GenericValue rv;
164 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
165 (char **)GVTOP(ArgValues[1])));
166 return rv;
167 }
168 break;
169 case 1:
170 if (FTy->getNumParams() == 1 &&
171 FTy->getParamType(0)->isIntegerTy(32)) {
172 GenericValue rv;
173 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
174 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
175 return rv;
176 }
177 break;
178 }
179 }
180
181 // Handle cases where no arguments are passed first.
182 if (ArgValues.empty()) {
183 GenericValue rv;
184 switch (RetTy->getTypeID()) {
185 default: llvm_unreachable("Unknown return type for function call!");
186 case Type::IntegerTyID: {
187 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
188 if (BitWidth == 1)
189 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
190 else if (BitWidth <= 8)
191 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
192 else if (BitWidth <= 16)
193 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
194 else if (BitWidth <= 32)
195 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
196 else if (BitWidth <= 64)
197 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
198 else
199 llvm_unreachable("Integer types > 64 bits not supported");
200 return rv;
201 }
202 case Type::VoidTyID:
203 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
204 return rv;
205 case Type::FloatTyID:
206 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
207 return rv;
208 case Type::DoubleTyID:
209 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
210 return rv;
211 case Type::X86_FP80TyID:
212 case Type::FP128TyID:
213 case Type::PPC_FP128TyID:
214 llvm_unreachable("long double not supported yet");
215 return rv;
216 case Type::PointerTyID:
217 return PTOGV(((void*(*)())(intptr_t)FPtr)());
218 }
219 }
220
221 assert("Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000222 return GenericValue();
223}