lli.cpp revision 7efea1dd98cd9e20ef160c838190f5249123f494
1//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 7// 8//===----------------------------------------------------------------------===// 9// 10// This utility provides a simple wrapper around the LLVM Execution Engines, 11// which allow the direct execution of LLVM programs through a Just-In-Time 12// compiler, or through an intepreter if no JIT is available for this platform. 13// 14//===----------------------------------------------------------------------===// 15 16#include "llvm/DerivedTypes.h" 17#include "llvm/Module.h" 18#include "llvm/ModuleProvider.h" 19#include "llvm/Bytecode/Reader.h" 20#include "llvm/ExecutionEngine/ExecutionEngine.h" 21#include "llvm/ExecutionEngine/GenericValue.h" 22#include "llvm/Target/TargetMachineImpls.h" 23#include "llvm/Target/TargetData.h" 24#include "Support/CommandLine.h" 25#include "Support/Debug.h" 26#include "Support/SystemUtils.h" 27 28using namespace llvm; 29 30namespace { 31 cl::opt<std::string> 32 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-")); 33 34 cl::list<std::string> 35 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); 36 37 cl::opt<std::string> 38 MainFunction("f", cl::desc("Function to execute"), cl::init("main"), 39 cl::value_desc("function name")); 40 41 cl::opt<bool> ForceInterpreter("force-interpreter", 42 cl::desc("Force interpretation: disable JIT"), 43 cl::init(false)); 44 45 cl::opt<std::string> 46 FakeArgv0("fake-argv0", 47 cl::desc("Override the 'argv[0]' value passed into the executing" 48 " program"), cl::value_desc("executable")); 49} 50 51static std::vector<std::string> makeStringVector(char * const *envp) { 52 std::vector<std::string> rv; 53 for (unsigned i = 0; envp[i]; ++i) 54 rv.push_back(envp[i]); 55 return rv; 56} 57 58static void *CreateArgv(ExecutionEngine *EE, 59 const std::vector<std::string> &InputArgv) { 60 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target? 61 PointerTy *Result = new PointerTy[InputArgv.size()+1]; 62 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 63 64 for (unsigned i = 0; i < InputArgv.size(); ++i) { 65 unsigned Size = InputArgv[i].size()+1; 66 char *Dest = new char[Size]; 67 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 68 69 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 70 Dest[Size-1] = 0; 71 72 // Endian safe: Result[i] = (PointerTy)Dest; 73 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), 74 Type::LongTy); 75 } 76 Result[InputArgv.size()] = 0; 77 return Result; 78 } else { // 32 bit target? 79 int *Result = new int[InputArgv.size()+1]; 80 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 81 82 for (unsigned i = 0; i < InputArgv.size(); ++i) { 83 unsigned Size = InputArgv[i].size()+1; 84 char *Dest = new char[Size]; 85 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 86 87 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 88 Dest[Size-1] = 0; 89 90 // Endian safe: Result[i] = (PointerTy)Dest; 91 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), 92 Type::IntTy); 93 } 94 Result[InputArgv.size()] = 0; // null terminate it 95 return Result; 96 } 97} 98 99/// callAsMain - Call the function named FnName from M as if its 100/// signature were int main (int argc, char **argv, const char 101/// **envp), using the contents of Args to determine argc & argv, and 102/// the contents of EnvVars to determine envp. Returns the result 103/// from calling FnName, or -1 and prints an error msg. if the named 104/// function cannot be found. 105/// 106int callAsMain(ExecutionEngine *EE, ModuleProvider *MP, 107 const std::string &FnName, 108 const std::vector<std::string> &Args, 109 const std::vector<std::string> &EnvVars) { 110 Function *Fn = MP->getModule()->getNamedFunction(FnName); 111 if (!Fn) { 112 std::cerr << "Function '" << FnName << "' not found in module.\n"; 113 return -1; 114 } 115 std::vector<GenericValue> GVArgs; 116 GenericValue GVArgc; 117 GVArgc.IntVal = Args.size(); 118 GVArgs.push_back(GVArgc); // Arg #0 = argc. 119 GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv. 120 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv"); 121 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp. 122 return EE->run(Fn, GVArgs).IntVal; 123} 124 125//===----------------------------------------------------------------------===// 126// main Driver function 127// 128int main(int argc, char **argv, char * const *envp) { 129 cl::ParseCommandLineOptions(argc, argv, 130 " llvm interpreter & dynamic compiler\n"); 131 132 // Load the bytecode... 133 std::string ErrorMsg; 134 ModuleProvider *MP = 0; 135 try { 136 MP = getBytecodeModuleProvider(InputFile); 137 } catch (std::string &err) { 138 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n"; 139 exit(1); 140 } 141 142 ExecutionEngine *EE = 143 ExecutionEngine::create(MP, ForceInterpreter); 144 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?"); 145 146 // If the user specifically requested an argv[0] to pass into the program, do 147 // it now. 148 if (!FakeArgv0.empty()) { 149 InputFile = FakeArgv0; 150 } else { 151 // Otherwise, if there is a .bc suffix on the executable strip it off, it 152 // might confuse the program. 153 if (InputFile.rfind(".bc") == InputFile.length() - 3) 154 InputFile.erase(InputFile.length() - 3); 155 } 156 157 // Add the module's name to the start of the vector of arguments to main(). 158 InputArgv.insert(InputArgv.begin(), InputFile); 159 160 // Run the main function! 161 int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv, 162 makeStringVector(envp)); 163 164 // Now that we are done executing the program, shut down the execution engine 165 delete EE; 166 return ExitCode; 167} 168