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 //===----------------------------------------------------------------------===//
11 // This utility provides a way to execute LLVM bytecode without static
12 // compilation. This consists of a very simple and slow (but portable)
13 // interpreter, along with capability for system specific dynamic compilers. At
14 // runtime, the fastest (stable) execution engine is selected to run the
15 // program. This means the JIT compiler for the current platform if it's
18 //===----------------------------------------------------------------------===//
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/ModuleProvider.h"
23 #include "llvm/Bytecode/Reader.h"
24 #include "llvm/ExecutionEngine/ExecutionEngine.h"
25 #include "llvm/ExecutionEngine/GenericValue.h"
26 #include "llvm/Target/TargetMachineImpls.h"
27 #include "llvm/Target/TargetData.h"
28 #include "Support/CommandLine.h"
29 #include "Support/Debug.h"
30 #include "Support/SystemUtils.h"
34 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
37 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
40 MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
41 cl::value_desc("function name"));
43 cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
45 cl::opt<bool> ForceInterpreter("force-interpreter",
46 cl::desc("Force interpretation: disable JIT"),
50 static std::vector<std::string> makeStringVector(char * const *envp) {
51 std::vector<std::string> rv;
52 for (unsigned i = 0; envp[i]; ++i)
53 rv.push_back(envp[i]);
57 static void *CreateArgv(ExecutionEngine *EE,
58 const std::vector<std::string> &InputArgv) {
59 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target?
60 PointerTy *Result = new PointerTy[InputArgv.size()+1];
61 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
63 for (unsigned i = 0; i < InputArgv.size(); ++i) {
64 unsigned Size = InputArgv[i].size()+1;
65 char *Dest = new char[Size];
66 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
68 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
71 // Endian safe: Result[i] = (PointerTy)Dest;
72 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
75 Result[InputArgv.size()] = 0;
77 } else { // 32 bit target?
78 int *Result = new int[InputArgv.size()+1];
79 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
81 for (unsigned i = 0; i < InputArgv.size(); ++i) {
82 unsigned Size = InputArgv[i].size()+1;
83 char *Dest = new char[Size];
84 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
86 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
89 // Endian safe: Result[i] = (PointerTy)Dest;
90 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
93 Result[InputArgv.size()] = 0; // null terminate it
98 /// callAsMain - Call the function named FnName from M as if its
99 /// signature were int main (int argc, char **argv, const char
100 /// **envp), using the contents of Args to determine argc & argv, and
101 /// the contents of EnvVars to determine envp. Returns the result
102 /// from calling FnName, or -1 and prints an error msg. if the named
103 /// function cannot be found.
105 int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
106 const std::string &FnName,
107 const std::vector<std::string> &Args,
108 const std::vector<std::string> &EnvVars) {
109 Function *Fn = MP->getModule()->getNamedFunction(FnName);
111 std::cerr << "Function '" << FnName << "' not found in module.\n";
114 std::vector<GenericValue> GVArgs;
116 GVArgc.IntVal = Args.size();
117 GVArgs.push_back(GVArgc); // Arg #0 = argc.
118 GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
119 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
120 return EE->run(Fn, GVArgs).IntVal;
123 //===----------------------------------------------------------------------===//
124 // main Driver function
126 int main(int argc, char **argv, char * const *envp) {
127 cl::ParseCommandLineOptions(argc, argv,
128 " llvm interpreter & dynamic compiler\n");
130 // Load the bytecode...
131 std::string ErrorMsg;
132 ModuleProvider *MP = 0;
134 MP = getBytecodeModuleProvider(InputFile);
135 } catch (std::string &err) {
136 std::cerr << "Error parsing '" << InputFile << "': " << err << "\n";
140 ExecutionEngine *EE =
141 ExecutionEngine::create(MP, ForceInterpreter, TraceMode);
142 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
144 // Add the module's name to the start of the vector of arguments to main().
145 // But delete .bc first, since programs (and users) might not expect to
147 const std::string ByteCodeFileSuffix(".bc");
148 if (InputFile.rfind(ByteCodeFileSuffix) ==
149 InputFile.length() - ByteCodeFileSuffix.length()) {
150 InputFile.erase (InputFile.length() - ByteCodeFileSuffix.length());
152 InputArgv.insert(InputArgv.begin(), InputFile);
154 // Run the main function!
155 int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
156 makeStringVector(envp));
158 // Now that we are done executing the program, shut down the execution engine