1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bytecode in an efficient manner.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/CodeGen/MachineCodeEmitter.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/ExecutionEngine/GenericValue.h"
25 #include "llvm/Support/MutexGuard.h"
26 #include "llvm/System/DynamicLibrary.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetJITInfo.h"
34 // __dso_handle is resolved by Mac OS X dynamic linker.
35 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
38 static struct RegisterJIT {
39 RegisterJIT() { JIT::Register(); }
47 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
48 : ExecutionEngine(MP), TM(tm), TJI(tji), state(MP) {
49 setTargetData(TM.getTargetData());
52 MCE = createEmitter(*this);
55 MutexGuard locked(lock);
56 FunctionPassManager& PM = state.getPM(locked);
57 PM.add(new TargetData(*TM.getTargetData()));
59 // Compile LLVM Code down to machine code in the intermediate representation
60 TJI.addPassesToJITCompile(PM);
62 // Turn the machine code intermediate representation into bytes in memory that
64 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
65 std::cerr << "Target '" << TM.getName()
66 << "' doesn't support machine code emission!\n";
76 /// run - Start execution with the specified function and arguments.
78 GenericValue JIT::runFunction(Function *F,
79 const std::vector<GenericValue> &ArgValues) {
80 assert(F && "Function *F was null at entry to run()");
82 void *FPtr = getPointerToFunction(F);
83 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
84 const FunctionType *FTy = F->getFunctionType();
85 const Type *RetTy = FTy->getReturnType();
87 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
88 "Too many arguments passed into function!");
89 assert(FTy->getNumParams() == ArgValues.size() &&
90 "This doesn't support passing arguments through varargs (yet)!");
92 // Handle some common cases first. These cases correspond to common `main'
94 if (RetTy == Type::IntTy || RetTy == Type::UIntTy || RetTy == Type::VoidTy) {
95 switch (ArgValues.size()) {
97 if ((FTy->getParamType(0) == Type::IntTy ||
98 FTy->getParamType(0) == Type::UIntTy) &&
99 isa<PointerType>(FTy->getParamType(1)) &&
100 isa<PointerType>(FTy->getParamType(2))) {
101 int (*PF)(int, char **, const char **) =
102 (int(*)(int, char **, const char **))(intptr_t)FPtr;
104 // Call the function.
106 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]),
107 (const char **)GVTOP(ArgValues[2]));
112 if ((FTy->getParamType(0) == Type::IntTy ||
113 FTy->getParamType(0) == Type::UIntTy) &&
114 isa<PointerType>(FTy->getParamType(1))) {
115 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
117 // Call the function.
119 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]));
124 if (FTy->getNumParams() == 1 &&
125 (FTy->getParamType(0) == Type::IntTy ||
126 FTy->getParamType(0) == Type::UIntTy)) {
128 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
129 rv.IntVal = PF(ArgValues[0].IntVal);
136 // Handle cases where no arguments are passed first.
137 if (ArgValues.empty()) {
139 switch (RetTy->getTypeID()) {
140 default: assert(0 && "Unknown return type for function call!");
142 rv.BoolVal = ((bool(*)())(intptr_t)FPtr)();
144 case Type::SByteTyID:
145 case Type::UByteTyID:
146 rv.SByteVal = ((char(*)())(intptr_t)FPtr)();
148 case Type::ShortTyID:
149 case Type::UShortTyID:
150 rv.ShortVal = ((short(*)())(intptr_t)FPtr)();
155 rv.IntVal = ((int(*)())(intptr_t)FPtr)();
158 case Type::ULongTyID:
159 rv.LongVal = ((int64_t(*)())(intptr_t)FPtr)();
161 case Type::FloatTyID:
162 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
164 case Type::DoubleTyID:
165 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
167 case Type::PointerTyID:
168 return PTOGV(((void*(*)())(intptr_t)FPtr)());
172 // Okay, this is not one of our quick and easy cases. Because we don't have a
173 // full FFI, we have to codegen a nullary stub function that just calls the
174 // function we are interested in, passing in constants for all of the
175 // arguments. Make this function and return.
177 // First, create the function.
178 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
179 Function *Stub = new Function(STy, Function::InternalLinkage, "",
182 // Insert a basic block.
183 BasicBlock *StubBB = new BasicBlock("", Stub);
185 // Convert all of the GenericValue arguments over to constants. Note that we
186 // currently don't support varargs.
187 std::vector<Value*> Args;
188 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
190 const Type *ArgTy = FTy->getParamType(i);
191 const GenericValue &AV = ArgValues[i];
192 switch (ArgTy->getTypeID()) {
193 default: assert(0 && "Unknown argument type for function call!");
194 case Type::BoolTyID: C = ConstantBool::get(AV.BoolVal); break;
195 case Type::SByteTyID: C = ConstantSInt::get(ArgTy, AV.SByteVal); break;
196 case Type::UByteTyID: C = ConstantUInt::get(ArgTy, AV.UByteVal); break;
197 case Type::ShortTyID: C = ConstantSInt::get(ArgTy, AV.ShortVal); break;
198 case Type::UShortTyID: C = ConstantUInt::get(ArgTy, AV.UShortVal); break;
199 case Type::IntTyID: C = ConstantSInt::get(ArgTy, AV.IntVal); break;
200 case Type::UIntTyID: C = ConstantUInt::get(ArgTy, AV.UIntVal); break;
201 case Type::LongTyID: C = ConstantSInt::get(ArgTy, AV.LongVal); break;
202 case Type::ULongTyID: C = ConstantUInt::get(ArgTy, AV.ULongVal); break;
203 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break;
204 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
205 case Type::PointerTyID:
206 void *ArgPtr = GVTOP(AV);
207 if (sizeof(void*) == 4) {
208 C = ConstantSInt::get(Type::IntTy, (int)(intptr_t)ArgPtr);
210 C = ConstantSInt::get(Type::LongTy, (intptr_t)ArgPtr);
212 C = ConstantExpr::getCast(C, ArgTy); // Cast the integer to pointer
218 CallInst *TheCall = new CallInst(F, Args, "", StubBB);
219 TheCall->setTailCall();
220 if (TheCall->getType() != Type::VoidTy)
221 new ReturnInst(TheCall, StubBB); // Return result of the call.
223 new ReturnInst(StubBB); // Just return void.
225 // Finally, return the value returned by our nullary stub function.
226 return runFunction(Stub, std::vector<GenericValue>());
229 /// runJITOnFunction - Run the FunctionPassManager full of
230 /// just-in-time compilation passes on F, hopefully filling in
231 /// GlobalAddress[F] with the address of F's machine code.
233 void JIT::runJITOnFunction(Function *F) {
234 static bool isAlreadyCodeGenerating = false;
235 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
237 MutexGuard locked(lock);
240 isAlreadyCodeGenerating = true;
241 state.getPM(locked).run(*F);
242 isAlreadyCodeGenerating = false;
244 // If the function referred to a global variable that had not yet been
245 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
246 // all of these globals now.
247 while (!state.getPendingGlobals(locked).empty()) {
248 const GlobalVariable *GV = state.getPendingGlobals(locked).back();
249 state.getPendingGlobals(locked).pop_back();
250 EmitGlobalVariable(GV);
254 /// getPointerToFunction - This method is used to get the address of the
255 /// specified function, compiling it if neccesary.
257 void *JIT::getPointerToFunction(Function *F) {
258 MutexGuard locked(lock);
260 if (void *Addr = getPointerToGlobalIfAvailable(F))
261 return Addr; // Check if function already code gen'd
263 // Make sure we read in the function if it exists in this Module
264 if (F->hasNotBeenReadFromBytecode()) {
265 std::string ErrorMsg;
266 if (MP->materializeFunction(F, &ErrorMsg)) {
267 std::cerr << "Error reading function '" << F->getName()
268 << "' from bytecode file: " << ErrorMsg << "\n";
273 if (F->isExternal()) {
274 void *Addr = getPointerToNamedFunction(F->getName());
275 addGlobalMapping(F, Addr);
281 void *Addr = getPointerToGlobalIfAvailable(F);
282 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
286 /// getOrEmitGlobalVariable - Return the address of the specified global
287 /// variable, possibly emitting it to memory if needed. This is used by the
289 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
290 MutexGuard locked(lock);
292 void *Ptr = getPointerToGlobalIfAvailable(GV);
295 // If the global is external, just remember the address.
296 if (GV->isExternal()) {
298 // __dso_handle is resolved by the Mac OS X dynamic linker.
299 if (GV->getName() == "__dso_handle")
300 return (void*)&__dso_handle;
302 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
304 std::cerr << "Could not resolve external global address: "
305 << GV->getName() << "\n";
309 // If the global hasn't been emitted to memory yet, allocate space. We will
310 // actually initialize the global after current function has finished
312 const Type *GlobalType = GV->getType()->getElementType();
313 size_t S = getTargetData()->getTypeSize(GlobalType);
314 size_t A = getTargetData()->getTypeAlignment(GlobalType);
318 // Allocate S+A bytes of memory, then use an aligned pointer within that
321 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
322 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
324 state.getPendingGlobals(locked).push_back(GV);
326 addGlobalMapping(GV, Ptr);
331 /// recompileAndRelinkFunction - This method is used to force a function
332 /// which has already been compiled, to be compiled again, possibly
333 /// after it has been modified. Then the entry to the old copy is overwritten
334 /// with a branch to the new copy. If there was no old copy, this acts
335 /// just like JIT::getPointerToFunction().
337 void *JIT::recompileAndRelinkFunction(Function *F) {
338 void *OldAddr = getPointerToGlobalIfAvailable(F);
340 // If it's not already compiled there is no reason to patch it up.
341 if (OldAddr == 0) { return getPointerToFunction(F); }
343 // Delete the old function mapping.
344 addGlobalMapping(F, 0);
346 // Recodegen the function
349 // Update state, forward the old function to the new function.
350 void *Addr = getPointerToGlobalIfAvailable(F);
351 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
352 TJI.replaceMachineCodeForFunction(OldAddr, Addr);