Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1 | //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 3 | // This utility provides a way to execute LLVM bytecode without static |
| 4 | // compilation. This consists of a very simple and slow (but portable) |
| 5 | // interpreter, along with capability for system specific dynamic compilers. At |
| 6 | // runtime, the fastest (stable) execution engine is selected to run the |
| 7 | // program. This means the JIT compiler for the current platform if it's |
| 8 | // available. |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 12 | #include "Support/CommandLine.h" |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 13 | #include "Support/Debug.h" |
Brian Gaeke | d1cab3e | 2003-09-05 19:42:34 +0000 | [diff] [blame^] | 14 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 15 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 16 | #include "llvm/Bytecode/Reader.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Target/TargetMachineImpls.h" |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 21 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | cl::opt<std::string> |
| 24 | InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 25 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 26 | cl::list<std::string> |
| 27 | InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 28 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 29 | cl::opt<std::string> |
| 30 | MainFunction ("f", cl::desc("Function to execute"), cl::init("main"), |
| 31 | cl::value_desc("function name")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 32 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 33 | cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 34 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 35 | cl::opt<bool> ForceInterpreter("force-interpreter", |
| 36 | cl::desc("Force interpretation: disable JIT"), |
Chris Lattner | 19669f3 | 2003-05-12 14:31:57 +0000 | [diff] [blame] | 37 | cl::init(false)); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 38 | } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 39 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 40 | static std::vector<std::string> makeStringVector (const char **envp) { |
| 41 | std::vector<std::string> rv; |
| 42 | for (unsigned i = 0; envp[i]; ++i) |
| 43 | rv.push_back (envp[i]); |
| 44 | return rv; |
| 45 | } |
| 46 | |
| 47 | static void *CreateArgv(ExecutionEngine *EE, |
| 48 | const std::vector<std::string> &InputArgv) { |
| 49 | if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target? |
| 50 | PointerTy *Result = new PointerTy[InputArgv.size()+1]; |
| 51 | DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); |
| 52 | |
| 53 | for (unsigned i = 0; i < InputArgv.size(); ++i) { |
| 54 | unsigned Size = InputArgv[i].size()+1; |
| 55 | char *Dest = new char[Size]; |
| 56 | DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); |
| 57 | |
| 58 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 59 | Dest[Size-1] = 0; |
| 60 | |
| 61 | // Endian safe: Result[i] = (PointerTy)Dest; |
| 62 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), |
| 63 | Type::LongTy); |
| 64 | } |
| 65 | Result[InputArgv.size()] = 0; |
| 66 | return Result; |
| 67 | } else { // 32 bit target? |
| 68 | int *Result = new int[InputArgv.size()+1]; |
| 69 | DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); |
| 70 | |
| 71 | for (unsigned i = 0; i < InputArgv.size(); ++i) { |
| 72 | unsigned Size = InputArgv[i].size()+1; |
| 73 | char *Dest = new char[Size]; |
| 74 | DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); |
| 75 | |
| 76 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 77 | Dest[Size-1] = 0; |
| 78 | |
| 79 | // Endian safe: Result[i] = (PointerTy)Dest; |
| 80 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), |
| 81 | Type::IntTy); |
| 82 | } |
| 83 | Result[InputArgv.size()] = 0; // null terminate it |
| 84 | return Result; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// callAsMain - Call the function named FnName from M as if its |
| 89 | /// signature were int main (int argc, char **argv, const char |
| 90 | /// **envp), using the contents of Args to determine argc & argv, and |
| 91 | /// the contents of EnvVars to determine envp. Returns the result |
| 92 | /// from calling FnName, or -1 and prints an error msg. if the named |
| 93 | /// function cannot be found. |
| 94 | /// |
| 95 | int callAsMain (ExecutionEngine *EE, Module *M, const std::string &FnName, |
| 96 | const std::vector<std::string> &Args, |
| 97 | const std::vector<std::string> &EnvVars) { |
| 98 | Function *Fn = M->getNamedFunction (FnName); |
| 99 | if (!Fn) { |
| 100 | std::cerr << "Function '" << FnName << "' not found in module.\n"; |
| 101 | return -1; |
| 102 | } |
| 103 | std::vector<GenericValue> GVArgs; |
| 104 | GenericValue GVArgc; |
| 105 | GVArgc.IntVal = Args.size (); |
| 106 | GVArgs.push_back (GVArgc); // Arg #0 = argc. |
| 107 | GVArgs.push_back (PTOGV (CreateArgv (EE, Args))); // Arg #1 = argv. |
| 108 | GVArgs.push_back (PTOGV (CreateArgv (EE, EnvVars))); // Arg #2 = envp. |
| 109 | return EE->run (Fn, GVArgs).IntVal; |
| 110 | } |
| 111 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 112 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 113 | // main Driver function |
| 114 | // |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 115 | int main(int argc, char **argv, const char **envp) { |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 116 | cl::ParseCommandLineOptions(argc, argv, |
| 117 | " llvm interpreter & dynamic compiler\n"); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 118 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 119 | // Load the bytecode... |
Chris Lattner | d6840ac | 2002-12-24 00:39:16 +0000 | [diff] [blame] | 120 | std::string ErrorMsg; |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 121 | Module *M = ParseBytecodeFile(InputFile, &ErrorMsg); |
| 122 | if (M == 0) { |
Chris Lattner | d6840ac | 2002-12-24 00:39:16 +0000 | [diff] [blame] | 123 | std::cout << "Error parsing '" << InputFile << "': " |
| 124 | << ErrorMsg << "\n"; |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 125 | exit(1); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 128 | ExecutionEngine *EE = |
Brian Gaeke | f58815e | 2003-09-04 22:21:24 +0000 | [diff] [blame] | 129 | ExecutionEngine::create (M, ForceInterpreter, TraceMode); |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 130 | assert (EE && "Couldn't create an ExecutionEngine, not even an interpreter?"); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 131 | |
Brian Gaeke | f58815e | 2003-09-04 22:21:24 +0000 | [diff] [blame] | 132 | // Add the module's name to the start of the vector of arguments to main(). |
Brian Gaeke | 6ae73dc | 2003-05-23 20:28:07 +0000 | [diff] [blame] | 133 | // But delete .bc first, since programs (and users) might not expect to |
| 134 | // see it. |
| 135 | const std::string ByteCodeFileSuffix (".bc"); |
| 136 | if (InputFile.rfind (ByteCodeFileSuffix) == |
| 137 | InputFile.length () - ByteCodeFileSuffix.length ()) { |
| 138 | InputFile.erase (InputFile.length () - ByteCodeFileSuffix.length ()); |
| 139 | } |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 140 | InputArgv.insert(InputArgv.begin(), InputFile); |
| 141 | |
| 142 | // Run the main function! |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 143 | int ExitCode = callAsMain (EE, M, MainFunction, InputArgv, |
| 144 | makeStringVector (envp)); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 145 | |
| 146 | // Now that we are done executing the program, shut down the execution engine |
| 147 | delete EE; |
| 148 | return ExitCode; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 149 | } |