1 //===- lli.cpp - LLVM Interpreter / Dynamic 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 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.
14 //===----------------------------------------------------------------------===//
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"
28 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
31 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
33 cl::opt<bool> ForceInterpreter("force-interpreter",
34 cl::desc("Force interpretation: disable JIT"),
38 FakeArgv0("fake-argv0",
39 cl::desc("Override the 'argv[0]' value passed into the executing"
40 " program"), cl::value_desc("executable"));
43 //===----------------------------------------------------------------------===//
44 // main Driver function
46 int main(int argc, char **argv, char * const *envp) {
47 cl::ParseCommandLineOptions(argc, argv,
48 " llvm interpreter & dynamic compiler\n");
50 // Load the bytecode...
52 ModuleProvider *MP = 0;
54 MP = getBytecodeModuleProvider(InputFile);
55 } catch (std::string &err) {
56 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
60 ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
61 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
63 // If the user specifically requested an argv[0] to pass into the program, do
65 if (!FakeArgv0.empty()) {
66 InputFile = FakeArgv0;
68 // Otherwise, if there is a .bc suffix on the executable strip it off, it
69 // might confuse the program.
70 if (InputFile.rfind(".bc") == InputFile.length() - 3)
71 InputFile.erase(InputFile.length() - 3);
74 // Add the module's name to the start of the vector of arguments to main().
75 InputArgv.insert(InputArgv.begin(), InputFile);
77 // Call the main function from M as if its signature were:
78 // int main (int argc, char **argv, const char **envp)
79 // using the contents of Args to determine argc & argv, and the contents of
80 // EnvVars to determine envp.
82 Function *Fn = MP->getModule()->getMainFunction();
84 std::cerr << "'main' function not found in module.\n";
89 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
91 // If the program didn't explicitly call exit, call exit now, for the program.
92 // This ensures that any atexit handlers get called correctly.
93 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
96 std::vector<GenericValue> Args;
97 GenericValue ResultGV;
98 ResultGV.IntVal = Result;
99 Args.push_back(ResultGV);
100 EE->runFunction(Exit, Args);
102 std::cerr << "ERROR: exit(" << Result << ") returned!\n";