LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

JIT.cpp

Go to the documentation of this file.
00001 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This tool implements a just-in-time compiler for LLVM, allowing direct
00011 // execution of LLVM bytecode in an efficient manner.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "JIT.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/DerivedTypes.h"
00018 #include "llvm/Function.h"
00019 #include "llvm/GlobalVariable.h"
00020 #include "llvm/Instructions.h"
00021 #include "llvm/ModuleProvider.h"
00022 #include "llvm/CodeGen/MachineCodeEmitter.h"
00023 #include "llvm/CodeGen/MachineFunction.h"
00024 #include "llvm/ExecutionEngine/GenericValue.h"
00025 #include "llvm/System/DynamicLibrary.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Target/TargetJITInfo.h"
00028 #include <iostream>
00029 
00030 using namespace llvm;
00031 
00032 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
00033   : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
00034   setTargetData(TM.getTargetData());
00035 
00036   // Initialize MCE
00037   MCE = createEmitter(*this);
00038   
00039   // Add target data
00040   PM.add(new TargetData(TM.getTargetData()));
00041 
00042   // Compile LLVM Code down to machine code in the intermediate representation
00043   TJI.addPassesToJITCompile(PM);
00044 
00045   // Turn the machine code intermediate representation into bytes in memory that
00046   // may be executed.
00047   if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
00048     std::cerr << "Target '" << TM.getName()
00049               << "' doesn't support machine code emission!\n";
00050     abort();
00051   }
00052 }
00053 
00054 JIT::~JIT() {
00055   delete MCE;
00056   delete &TM;
00057 }
00058 
00059 /// run - Start execution with the specified function and arguments.
00060 ///
00061 GenericValue JIT::runFunction(Function *F,
00062                               const std::vector<GenericValue> &ArgValues) {
00063   assert(F && "Function *F was null at entry to run()");
00064 
00065   void *FPtr = getPointerToFunction(F);
00066   assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
00067   const FunctionType *FTy = F->getFunctionType();
00068   const Type *RetTy = FTy->getReturnType();
00069 
00070   assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
00071          "Too many arguments passed into function!");
00072   assert(FTy->getNumParams() == ArgValues.size() &&
00073          "This doesn't support passing arguments through varargs (yet)!");
00074 
00075   // Handle some common cases first.  These cases correspond to common `main'
00076   // prototypes.
00077   if (RetTy == Type::IntTy || RetTy == Type::UIntTy || RetTy == Type::VoidTy) {
00078     switch (ArgValues.size()) {
00079     case 3:
00080       if ((FTy->getParamType(0) == Type::IntTy || 
00081            FTy->getParamType(0) == Type::UIntTy) &&
00082           isa<PointerType>(FTy->getParamType(1)) &&
00083           isa<PointerType>(FTy->getParamType(2))) {
00084         int (*PF)(int, char **, const char **) =
00085           (int(*)(int, char **, const char **))FPtr;
00086 
00087         // Call the function.
00088         GenericValue rv;
00089         rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]),
00090                        (const char **)GVTOP(ArgValues[2]));
00091         return rv;
00092       }
00093       break;
00094     case 2:
00095       if ((FTy->getParamType(0) == Type::IntTy || 
00096            FTy->getParamType(0) == Type::UIntTy) &&
00097           isa<PointerType>(FTy->getParamType(1))) {
00098         int (*PF)(int, char **) = (int(*)(int, char **))FPtr;
00099 
00100         // Call the function.
00101         GenericValue rv;
00102         rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]));
00103         return rv;
00104       }
00105       break;
00106     case 1:
00107       if (FTy->getNumParams() == 1 &&
00108           (FTy->getParamType(0) == Type::IntTy || 
00109            FTy->getParamType(0) == Type::UIntTy)) {
00110         GenericValue rv;
00111         int (*PF)(int) = (int(*)(int))FPtr;
00112         rv.IntVal = PF(ArgValues[0].IntVal);
00113         return rv;
00114       }
00115       break;
00116     }
00117   }
00118 
00119   // Handle cases where no arguments are passed first.
00120   if (ArgValues.empty()) {
00121     GenericValue rv;
00122     switch (RetTy->getTypeID()) {
00123     default: assert(0 && "Unknown return type for function call!");
00124     case Type::BoolTyID:
00125       rv.BoolVal = ((bool(*)())FPtr)();
00126       return rv;
00127     case Type::SByteTyID:
00128     case Type::UByteTyID:
00129       rv.SByteVal = ((char(*)())FPtr)();
00130       return rv;
00131     case Type::ShortTyID:
00132     case Type::UShortTyID:
00133       rv.ShortVal = ((short(*)())FPtr)();
00134       return rv;
00135     case Type::VoidTyID:
00136     case Type::IntTyID:
00137     case Type::UIntTyID:
00138       rv.IntVal = ((int(*)())FPtr)();
00139       return rv;
00140     case Type::LongTyID:
00141     case Type::ULongTyID:
00142       rv.LongVal = ((int64_t(*)())FPtr)();
00143       return rv;
00144     case Type::FloatTyID:
00145       rv.FloatVal = ((float(*)())FPtr)();
00146       return rv;
00147     case Type::DoubleTyID:
00148       rv.DoubleVal = ((double(*)())FPtr)();
00149       return rv;
00150     case Type::PointerTyID:
00151       return PTOGV(((void*(*)())FPtr)());
00152     }
00153   }
00154 
00155   // Okay, this is not one of our quick and easy cases.  Because we don't have a
00156   // full FFI, we have to codegen a nullary stub function that just calls the
00157   // function we are interested in, passing in constants for all of the
00158   // arguments.  Make this function and return.
00159 
00160   // First, create the function.
00161   FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
00162   Function *Stub = new Function(STy, Function::InternalLinkage, "",
00163                                 F->getParent());
00164 
00165   // Insert a basic block.
00166   BasicBlock *StubBB = new BasicBlock("", Stub);
00167 
00168   // Convert all of the GenericValue arguments over to constants.  Note that we
00169   // currently don't support varargs.
00170   std::vector<Value*> Args;
00171   for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
00172     Constant *C = 0;
00173     const Type *ArgTy = FTy->getParamType(i);
00174     const GenericValue &AV = ArgValues[i];
00175     switch (ArgTy->getTypeID()) {
00176     default: assert(0 && "Unknown argument type for function call!");
00177     case Type::BoolTyID:   C = ConstantBool::get(AV.BoolVal); break;
00178     case Type::SByteTyID:  C = ConstantSInt::get(ArgTy, AV.SByteVal);  break;
00179     case Type::UByteTyID:  C = ConstantUInt::get(ArgTy, AV.UByteVal);  break;
00180     case Type::ShortTyID:  C = ConstantSInt::get(ArgTy, AV.ShortVal);  break;
00181     case Type::UShortTyID: C = ConstantUInt::get(ArgTy, AV.UShortVal); break;
00182     case Type::IntTyID:    C = ConstantSInt::get(ArgTy, AV.IntVal);    break;
00183     case Type::UIntTyID:   C = ConstantUInt::get(ArgTy, AV.UIntVal);   break;
00184     case Type::LongTyID:   C = ConstantSInt::get(ArgTy, AV.LongVal);   break;
00185     case Type::ULongTyID:  C = ConstantUInt::get(ArgTy, AV.ULongVal);  break;
00186     case Type::FloatTyID:  C = ConstantFP  ::get(ArgTy, AV.FloatVal);  break;
00187     case Type::DoubleTyID: C = ConstantFP  ::get(ArgTy, AV.DoubleVal); break;
00188     case Type::PointerTyID:
00189       void *ArgPtr = GVTOP(AV);
00190       if (sizeof(void*) == 4) {
00191         C = ConstantSInt::get(Type::IntTy, (int)(intptr_t)ArgPtr);
00192       } else {
00193         C = ConstantSInt::get(Type::LongTy, (intptr_t)ArgPtr);
00194       }
00195       C = ConstantExpr::getCast(C, ArgTy);  // Cast the integer to pointer
00196       break;
00197     }
00198     Args.push_back(C);
00199   }
00200 
00201   Value *TheCall = new CallInst(F, Args, "", StubBB);
00202   if (TheCall->getType() != Type::VoidTy)
00203     new ReturnInst(TheCall, StubBB);             // Return result of the call.
00204   else
00205     new ReturnInst(StubBB);                      // Just return void.
00206 
00207   // Finally, return the value returned by our nullary stub function.
00208   return runFunction(Stub, std::vector<GenericValue>());
00209 }
00210 
00211 /// runJITOnFunction - Run the FunctionPassManager full of
00212 /// just-in-time compilation passes on F, hopefully filling in
00213 /// GlobalAddress[F] with the address of F's machine code.
00214 ///
00215 void JIT::runJITOnFunction(Function *F) {
00216   static bool isAlreadyCodeGenerating = false;
00217   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
00218 
00219   // JIT the function
00220   isAlreadyCodeGenerating = true;
00221   PM.run(*F);
00222   isAlreadyCodeGenerating = false;
00223 
00224   // If the function referred to a global variable that had not yet been
00225   // emitted, it allocates memory for the global, but doesn't emit it yet.  Emit
00226   // all of these globals now.
00227   while (!PendingGlobals.empty()) {
00228     const GlobalVariable *GV = PendingGlobals.back();
00229     PendingGlobals.pop_back();
00230     EmitGlobalVariable(GV);
00231   }
00232 }
00233 
00234 /// getPointerToFunction - This method is used to get the address of the
00235 /// specified function, compiling it if neccesary.
00236 ///
00237 void *JIT::getPointerToFunction(Function *F) {
00238   if (void *Addr = getPointerToGlobalIfAvailable(F))
00239     return Addr;   // Check if function already code gen'd
00240 
00241   // Make sure we read in the function if it exists in this Module
00242   if (F->hasNotBeenReadFromBytecode()) 
00243     try {
00244       MP->materializeFunction(F);
00245     } catch ( std::string& errmsg ) {
00246       std::cerr << "Error reading function '" << F->getName()
00247                 << "' from bytecode file: " << errmsg << "\n";
00248       abort();
00249     } catch (...) {
00250       std::cerr << "Error reading function '" << F->getName()
00251                 << "from bytecode file!\n";
00252       abort();
00253     }
00254 
00255   if (F->isExternal()) {
00256     void *Addr = getPointerToNamedFunction(F->getName());
00257     addGlobalMapping(F, Addr);
00258     return Addr;
00259   }
00260 
00261   runJITOnFunction(F);
00262 
00263   void *Addr = getPointerToGlobalIfAvailable(F);
00264   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
00265   return Addr;
00266 }
00267 
00268 /// getOrEmitGlobalVariable - Return the address of the specified global
00269 /// variable, possibly emitting it to memory if needed.  This is used by the
00270 /// Emitter.
00271 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
00272   void *Ptr = getPointerToGlobalIfAvailable(GV);
00273   if (Ptr) return Ptr;
00274 
00275   // If the global is external, just remember the address.
00276   if (GV->isExternal()) {
00277     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
00278     if (Ptr == 0) {
00279       std::cerr << "Could not resolve external global address: "
00280                 << GV->getName() << "\n";
00281       abort();
00282     }
00283   } else {
00284     // If the global hasn't been emitted to memory yet, allocate space.  We will
00285     // actually initialize the global after current function has finished
00286     // compilation.
00287     Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())];
00288     PendingGlobals.push_back(GV);
00289   }
00290   addGlobalMapping(GV, Ptr);
00291   return Ptr;
00292 }
00293 
00294 
00295 /// recompileAndRelinkFunction - This method is used to force a function
00296 /// which has already been compiled, to be compiled again, possibly
00297 /// after it has been modified. Then the entry to the old copy is overwritten
00298 /// with a branch to the new copy. If there was no old copy, this acts
00299 /// just like JIT::getPointerToFunction().
00300 ///
00301 void *JIT::recompileAndRelinkFunction(Function *F) {
00302   void *OldAddr = getPointerToGlobalIfAvailable(F);
00303 
00304   // If it's not already compiled there is no reason to patch it up.
00305   if (OldAddr == 0) { return getPointerToFunction(F); }
00306 
00307   // Delete the old function mapping.
00308   addGlobalMapping(F, 0);
00309 
00310   // Recodegen the function
00311   runJITOnFunction(F);
00312 
00313   // Update state, forward the old function to the new function.
00314   void *Addr = getPointerToGlobalIfAvailable(F);
00315   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
00316   TJI.replaceMachineCodeForFunction(OldAddr, Addr);
00317   return Addr;
00318 }
00319 
00320 /// freeMachineCodeForFunction - release machine code memory for given Function
00321 ///
00322 void JIT::freeMachineCodeForFunction(Function *F) {
00323   // currently a no-op
00324 }