blob: ea493e291497e743d729adedf45610dcda3ef0be [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,
Jakob Stoklund Olesen701529b2011-05-07 03:12:54 +000041 CodeModel::Model CMM,
42 StringRef MArch,
43 StringRef MCPU,
44 const SmallVectorImpl<std::string>& MAttrs) {
Daniel Dunbar6aec2982010-11-17 16:06:43 +000045 // Try to register the program as a source of symbols to resolve against.
46 //
47 // FIXME: Don't do this here.
48 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
49
Jakob Stoklund Olesen701529b2011-05-07 03:12:54 +000050 // Pick a target either via -march or by guessing the native arch.
51 //
52 // FIXME: This should be lifted out of here, it isn't something which should
53 // be part of the JIT policy, rather the burden for this selection should be
54 // pushed to clients.
Dylan Noblesmith2ea29ba2011-05-13 21:36:16 +000055 TargetMachine *TM =
56 EngineBuilder::selectTarget(M, MArch, MCPU, MAttrs, ErrorStr);
Jakob Stoklund Olesen701529b2011-05-07 03:12:54 +000057 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
58 TM->setCodeModel(CMM);
59
Daniel Dunbar6aec2982010-11-17 16:06:43 +000060 // If the target supports JIT code generation, create the JIT.
61 if (TargetJITInfo *TJ = TM->getJITInfo())
Jim Grosbachc1545142011-05-12 18:21:23 +000062 return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM, M), OptLevel,
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000063 GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000064
65 if (ErrorStr)
66 *ErrorStr = "target does not support JIT code generation";
67 return 0;
68}
69
Jim Grosbach31649e62011-03-18 22:48:41 +000070MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000071 RTDyldMemoryManager *MM, CodeGenOpt::Level OptLevel,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000072 bool AllocateGVsWithCode)
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000073 : ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) {
Jim Grosbach31649e62011-03-18 22:48:41 +000074
75 PM.add(new TargetData(*TM->getTargetData()));
76
77 // Turn the machine code intermediate representation into bytes in memory
78 // that may be executed.
79 if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
80 report_fatal_error("Target does not support MC emission!");
81 }
82
83 // Initialize passes.
Jim Grosbach31649e62011-03-18 22:48:41 +000084 // FIXME: When we support multiple modules, we'll want to move the code
85 // gen and finalization out of the constructor here and do it more
86 // on-demand as part of getPointerToFunction().
87 PM.run(*M);
88 // Flush the output buffer so the SmallVector gets its data.
89 OS.flush();
Jim Grosbachf9229102011-03-22 01:06:42 +000090
91 // Load the object into the dynamic linker.
92 // FIXME: It would be nice to avoid making yet another copy.
93 MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(),
94 Buffer.size()));
Jim Grosbach8086f3b2011-03-23 19:51:34 +000095 if (Dyld.loadObject(MB))
96 report_fatal_error(Dyld.getErrorString());
Jim Grosbach69e81322011-04-13 15:28:10 +000097 // Resolve any relocations.
98 Dyld.resolveRelocations();
Daniel Dunbar6aec2982010-11-17 16:06:43 +000099}
100
101MCJIT::~MCJIT() {
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000102 delete MemMgr;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000103}
104
105void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
106 report_fatal_error("not yet implemented");
107 return 0;
108}
109
110void *MCJIT::getPointerToFunction(Function *F) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000111 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
112 bool AbortOnFailure = !F->hasExternalWeakLinkage();
113 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
114 addGlobalMapping(F, Addr);
115 return Addr;
116 }
117
Jim Grosbachf9229102011-03-22 01:06:42 +0000118 Twine Name = TM->getMCAsmInfo()->getGlobalPrefix() + F->getName();
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000119 return (void*)Dyld.getSymbolAddress(Name.str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000120}
121
122void *MCJIT::recompileAndRelinkFunction(Function *F) {
123 report_fatal_error("not yet implemented");
124}
125
126void MCJIT::freeMachineCodeForFunction(Function *F) {
127 report_fatal_error("not yet implemented");
128}
129
130GenericValue MCJIT::runFunction(Function *F,
131 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000132 assert(F && "Function *F was null at entry to run()");
133
Jim Grosbach31649e62011-03-18 22:48:41 +0000134 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000135 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
136 const FunctionType *FTy = F->getFunctionType();
137 const Type *RetTy = FTy->getReturnType();
138
139 assert((FTy->getNumParams() == ArgValues.size() ||
140 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
141 "Wrong number of arguments passed into function!");
142 assert(FTy->getNumParams() == ArgValues.size() &&
143 "This doesn't support passing arguments through varargs (yet)!");
144
145 // Handle some common cases first. These cases correspond to common `main'
146 // prototypes.
147 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
148 switch (ArgValues.size()) {
149 case 3:
150 if (FTy->getParamType(0)->isIntegerTy(32) &&
151 FTy->getParamType(1)->isPointerTy() &&
152 FTy->getParamType(2)->isPointerTy()) {
153 int (*PF)(int, char **, const char **) =
154 (int(*)(int, char **, const char **))(intptr_t)FPtr;
155
156 // Call the function.
157 GenericValue rv;
158 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
159 (char **)GVTOP(ArgValues[1]),
160 (const char **)GVTOP(ArgValues[2])));
161 return rv;
162 }
163 break;
164 case 2:
165 if (FTy->getParamType(0)->isIntegerTy(32) &&
166 FTy->getParamType(1)->isPointerTy()) {
167 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
168
169 // Call the function.
170 GenericValue rv;
171 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
172 (char **)GVTOP(ArgValues[1])));
173 return rv;
174 }
175 break;
176 case 1:
177 if (FTy->getNumParams() == 1 &&
178 FTy->getParamType(0)->isIntegerTy(32)) {
179 GenericValue rv;
180 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
181 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
182 return rv;
183 }
184 break;
185 }
186 }
187
188 // Handle cases where no arguments are passed first.
189 if (ArgValues.empty()) {
190 GenericValue rv;
191 switch (RetTy->getTypeID()) {
192 default: llvm_unreachable("Unknown return type for function call!");
193 case Type::IntegerTyID: {
194 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
195 if (BitWidth == 1)
196 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
197 else if (BitWidth <= 8)
198 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
199 else if (BitWidth <= 16)
200 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
201 else if (BitWidth <= 32)
202 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
203 else if (BitWidth <= 64)
204 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
205 else
206 llvm_unreachable("Integer types > 64 bits not supported");
207 return rv;
208 }
209 case Type::VoidTyID:
210 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
211 return rv;
212 case Type::FloatTyID:
213 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
214 return rv;
215 case Type::DoubleTyID:
216 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
217 return rv;
218 case Type::X86_FP80TyID:
219 case Type::FP128TyID:
220 case Type::PPC_FP128TyID:
221 llvm_unreachable("long double not supported yet");
222 return rv;
223 case Type::PointerTyID:
224 return PTOGV(((void*(*)())(intptr_t)FPtr)());
225 }
226 }
227
228 assert("Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000229 return GenericValue();
230}