blob: 3d4ee369ead00b04374f8494f71721f40f169876 [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.
55 TargetMachine *TM = MCJIT::selectTarget(M, MArch, MCPU, MAttrs, ErrorStr);
56 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
57 TM->setCodeModel(CMM);
58
Daniel Dunbar6aec2982010-11-17 16:06:43 +000059 // If the target supports JIT code generation, create the JIT.
60 if (TargetJITInfo *TJ = TM->getJITInfo())
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000061 return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM), OptLevel,
62 GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000063
64 if (ErrorStr)
65 *ErrorStr = "target does not support JIT code generation";
66 return 0;
67}
68
Jim Grosbach31649e62011-03-18 22:48:41 +000069MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000070 RTDyldMemoryManager *MM, CodeGenOpt::Level OptLevel,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000071 bool AllocateGVsWithCode)
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000072 : ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) {
Jim Grosbach31649e62011-03-18 22:48:41 +000073
74 PM.add(new TargetData(*TM->getTargetData()));
75
76 // Turn the machine code intermediate representation into bytes in memory
77 // that may be executed.
78 if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
79 report_fatal_error("Target does not support MC emission!");
80 }
81
82 // Initialize passes.
Jim Grosbach31649e62011-03-18 22:48:41 +000083 // FIXME: When we support multiple modules, we'll want to move the code
84 // gen and finalization out of the constructor here and do it more
85 // on-demand as part of getPointerToFunction().
86 PM.run(*M);
87 // Flush the output buffer so the SmallVector gets its data.
88 OS.flush();
Jim Grosbachf9229102011-03-22 01:06:42 +000089
90 // Load the object into the dynamic linker.
91 // FIXME: It would be nice to avoid making yet another copy.
92 MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(),
93 Buffer.size()));
Jim Grosbach8086f3b2011-03-23 19:51:34 +000094 if (Dyld.loadObject(MB))
95 report_fatal_error(Dyld.getErrorString());
Jim Grosbach69e81322011-04-13 15:28:10 +000096 // Resolve any relocations.
97 Dyld.resolveRelocations();
Daniel Dunbar6aec2982010-11-17 16:06:43 +000098}
99
100MCJIT::~MCJIT() {
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000101 delete MemMgr;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000102}
103
104void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
105 report_fatal_error("not yet implemented");
106 return 0;
107}
108
109void *MCJIT::getPointerToFunction(Function *F) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000110 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
111 bool AbortOnFailure = !F->hasExternalWeakLinkage();
112 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
113 addGlobalMapping(F, Addr);
114 return Addr;
115 }
116
Jim Grosbachf9229102011-03-22 01:06:42 +0000117 Twine Name = TM->getMCAsmInfo()->getGlobalPrefix() + F->getName();
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000118 return (void*)Dyld.getSymbolAddress(Name.str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000119}
120
121void *MCJIT::recompileAndRelinkFunction(Function *F) {
122 report_fatal_error("not yet implemented");
123}
124
125void MCJIT::freeMachineCodeForFunction(Function *F) {
126 report_fatal_error("not yet implemented");
127}
128
129GenericValue MCJIT::runFunction(Function *F,
130 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000131 assert(F && "Function *F was null at entry to run()");
132
Jim Grosbach31649e62011-03-18 22:48:41 +0000133 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000134 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
135 const FunctionType *FTy = F->getFunctionType();
136 const Type *RetTy = FTy->getReturnType();
137
138 assert((FTy->getNumParams() == ArgValues.size() ||
139 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
140 "Wrong number of arguments passed into function!");
141 assert(FTy->getNumParams() == ArgValues.size() &&
142 "This doesn't support passing arguments through varargs (yet)!");
143
144 // Handle some common cases first. These cases correspond to common `main'
145 // prototypes.
146 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
147 switch (ArgValues.size()) {
148 case 3:
149 if (FTy->getParamType(0)->isIntegerTy(32) &&
150 FTy->getParamType(1)->isPointerTy() &&
151 FTy->getParamType(2)->isPointerTy()) {
152 int (*PF)(int, char **, const char **) =
153 (int(*)(int, char **, const char **))(intptr_t)FPtr;
154
155 // Call the function.
156 GenericValue rv;
157 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
158 (char **)GVTOP(ArgValues[1]),
159 (const char **)GVTOP(ArgValues[2])));
160 return rv;
161 }
162 break;
163 case 2:
164 if (FTy->getParamType(0)->isIntegerTy(32) &&
165 FTy->getParamType(1)->isPointerTy()) {
166 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
167
168 // Call the function.
169 GenericValue rv;
170 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
171 (char **)GVTOP(ArgValues[1])));
172 return rv;
173 }
174 break;
175 case 1:
176 if (FTy->getNumParams() == 1 &&
177 FTy->getParamType(0)->isIntegerTy(32)) {
178 GenericValue rv;
179 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
180 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
181 return rv;
182 }
183 break;
184 }
185 }
186
187 // Handle cases where no arguments are passed first.
188 if (ArgValues.empty()) {
189 GenericValue rv;
190 switch (RetTy->getTypeID()) {
191 default: llvm_unreachable("Unknown return type for function call!");
192 case Type::IntegerTyID: {
193 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
194 if (BitWidth == 1)
195 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
196 else if (BitWidth <= 8)
197 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
198 else if (BitWidth <= 16)
199 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
200 else if (BitWidth <= 32)
201 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
202 else if (BitWidth <= 64)
203 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
204 else
205 llvm_unreachable("Integer types > 64 bits not supported");
206 return rv;
207 }
208 case Type::VoidTyID:
209 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
210 return rv;
211 case Type::FloatTyID:
212 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
213 return rv;
214 case Type::DoubleTyID:
215 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
216 return rv;
217 case Type::X86_FP80TyID:
218 case Type::FP128TyID:
219 case Type::PPC_FP128TyID:
220 llvm_unreachable("long double not supported yet");
221 return rv;
222 case Type::PointerTyID:
223 return PTOGV(((void*(*)())(intptr_t)FPtr)());
224 }
225 }
226
227 assert("Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000228 return GenericValue();
229}