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 way to execute LLVM bytecode without static
11 // compilation. This consists of a very simple and slow (but portable)
12 // interpreter, along with capability for system specific dynamic compilers. At
13 // runtime, the fastest (stable) execution engine is selected to run the
14 // program. This means the JIT compiler for the current platform if it's
17 //===----------------------------------------------------------------------===//
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/Bytecode/Reader.h"
23 #include "llvm/ExecutionEngine/ExecutionEngine.h"
24 #include "llvm/ExecutionEngine/GenericValue.h"
25 #include "llvm/Target/TargetMachineImpls.h"
26 #include "llvm/Target/TargetData.h"
27 #include "Support/CommandLine.h"
28 #include "Support/Debug.h"
29 #include "Support/SystemUtils.h"
35 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
38 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
41 MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
42 cl::value_desc("function name"));
44 cl::opt<bool> ForceInterpreter("force-interpreter",
45 cl::desc("Force interpretation: disable JIT"),
49 FakeArgv0("fake-argv0",
50 cl::desc("Override the 'argv[0]' value passed into the executing"
51 " program"), cl::value_desc("executable"));
54 static std::vector<std::string> makeStringVector(char * const *envp) {
55 std::vector<std::string> rv;
56 for (unsigned i = 0; envp[i]; ++i)
57 rv.push_back(envp[i]);
61 static void *CreateArgv(ExecutionEngine *EE,
62 const std::vector<std::string> &InputArgv) {
63 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target?
64 PointerTy *Result = new PointerTy[InputArgv.size()+1];
65 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
67 for (unsigned i = 0; i < InputArgv.size(); ++i) {
68 unsigned Size = InputArgv[i].size()+1;
69 char *Dest = new char[Size];
70 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
72 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
75 // Endian safe: Result[i] = (PointerTy)Dest;
76 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
79 Result[InputArgv.size()] = 0;
81 } else { // 32 bit target?
82 int *Result = new int[InputArgv.size()+1];
83 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
85 for (unsigned i = 0; i < InputArgv.size(); ++i) {
86 unsigned Size = InputArgv[i].size()+1;
87 char *Dest = new char[Size];
88 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
90 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
93 // Endian safe: Result[i] = (PointerTy)Dest;
94 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
97 Result[InputArgv.size()] = 0; // null terminate it
102 /// callAsMain - Call the function named FnName from M as if its
103 /// signature were int main (int argc, char **argv, const char
104 /// **envp), using the contents of Args to determine argc & argv, and
105 /// the contents of EnvVars to determine envp. Returns the result
106 /// from calling FnName, or -1 and prints an error msg. if the named
107 /// function cannot be found.
109 int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
110 const std::string &FnName,
111 const std::vector<std::string> &Args,
112 const std::vector<std::string> &EnvVars) {
113 Function *Fn = MP->getModule()->getNamedFunction(FnName);
115 std::cerr << "Function '" << FnName << "' not found in module.\n";
118 std::vector<GenericValue> GVArgs;
120 GVArgc.IntVal = Args.size();
121 GVArgs.push_back(GVArgc); // Arg #0 = argc.
122 GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
123 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
124 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
125 return EE->run(Fn, GVArgs).IntVal;
128 //===----------------------------------------------------------------------===//
129 // main Driver function
131 int main(int argc, char **argv, char * const *envp) {
132 cl::ParseCommandLineOptions(argc, argv,
133 " llvm interpreter & dynamic compiler\n");
135 // Load the bytecode...
136 std::string ErrorMsg;
137 ModuleProvider *MP = 0;
139 MP = getBytecodeModuleProvider(InputFile);
140 } catch (std::string &err) {
141 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
145 ExecutionEngine *EE =
146 ExecutionEngine::create(MP, ForceInterpreter);
147 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
149 // If the user specifically requested an argv[0] to pass into the program, do
151 if (!FakeArgv0.empty()) {
152 InputFile = FakeArgv0;
154 // Otherwise, if there is a .bc suffix on the executable strip it off, it
155 // might confuse the program.
156 if (InputFile.rfind(".bc") == InputFile.length() - 3)
157 InputFile.erase(InputFile.length() - 3);
160 // Add the module's name to the start of the vector of arguments to main().
161 InputArgv.insert(InputArgv.begin(), InputFile);
163 // Run the main function!
164 int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
165 makeStringVector(envp));
167 // Now that we are done executing the program, shut down the execution engine