lli.cpp revision 269a42811bf20f17a69600905cc5c03f68773043
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/Module.h" 17#include "llvm/ModuleProvider.h" 18#include "llvm/Type.h" 19#include "llvm/Bytecode/Reader.h" 20#include "llvm/ExecutionEngine/ExecutionEngine.h" 21#include "llvm/ExecutionEngine/GenericValue.h" 22#include "Support/CommandLine.h" 23 24using namespace llvm; 25 26namespace { 27 cl::opt<std::string> 28 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-")); 29 30 cl::list<std::string> 31 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); 32 33 cl::opt<bool> ForceInterpreter("force-interpreter", 34 cl::desc("Force interpretation: disable JIT"), 35 cl::init(false)); 36 37 cl::opt<std::string> 38 FakeArgv0("fake-argv0", 39 cl::desc("Override the 'argv[0]' value passed into the executing" 40 " program"), cl::value_desc("executable")); 41} 42 43//===----------------------------------------------------------------------===// 44// main Driver function 45// 46int main(int argc, char **argv, char * const *envp) { 47 cl::ParseCommandLineOptions(argc, argv, 48 " llvm interpreter & dynamic compiler\n"); 49 50 // Load the bytecode... 51 std::string ErrorMsg; 52 ModuleProvider *MP = 0; 53 try { 54 MP = getBytecodeModuleProvider(InputFile); 55 } catch (std::string &err) { 56 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n"; 57 exit(1); 58 } 59 60 ExecutionEngine *EE = 61 ExecutionEngine::create(MP, ForceInterpreter); 62 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?"); 63 64 // If the user specifically requested an argv[0] to pass into the program, do 65 // it now. 66 if (!FakeArgv0.empty()) { 67 InputFile = FakeArgv0; 68 } else { 69 // Otherwise, if there is a .bc suffix on the executable strip it off, it 70 // might confuse the program. 71 if (InputFile.rfind(".bc") == InputFile.length() - 3) 72 InputFile.erase(InputFile.length() - 3); 73 } 74 75 // Add the module's name to the start of the vector of arguments to main(). 76 InputArgv.insert(InputArgv.begin(), InputFile); 77 78 // Call the main function from M as if its signature were: 79 // int main (int argc, char **argv, const char **envp) 80 // using the contents of Args to determine argc & argv, and the contents of 81 // EnvVars to determine envp. 82 // 83 Function *Fn = MP->getModule()->getMainFunction(); 84 if (!Fn) { 85 std::cerr << "'main' function not found in module.\n"; 86 return -1; 87 } 88 89 // Run main... 90 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp); 91 92 // If the program didn't explicitly call exit, call exit now, for the program. 93 // This ensures that any atexit handlers get called correctly. 94 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy, 95 Type::IntTy, 0); 96 97 std::vector<GenericValue> Args; 98 GenericValue ResultGV; 99 ResultGV.IntVal = Result; 100 Args.push_back(ResultGV); 101 EE->runFunction(Exit, Args); 102 103 std::cerr << "ERROR: exit(" << Result << ") returned!\n"; 104 abort(); 105} 106