blob: ebe618a90eba2f898e1e717eb7813ea37b503f75 [file] [log] [blame]
Chris Lattnerfe11a972002-12-23 23:59:41 +00001//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00002//
Chris Lattnerfe11a972002-12-23 23:59:41 +00003// 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 Lattner92101ac2001-08-23 17:05:04 +00009//
10//===----------------------------------------------------------------------===//
11
Chris Lattnercee8f9a2001-11-27 00:03:19 +000012#include "Support/CommandLine.h"
Brian Gaeke70975ee2003-09-05 18:42:01 +000013#include "Support/Debug.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000014#include "llvm/ExecutionEngine/ExecutionEngine.h"
15#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000016#include "llvm/Bytecode/Reader.h"
17#include "llvm/Module.h"
18#include "llvm/Target/TargetMachineImpls.h"
Brian Gaeke70975ee2003-09-05 18:42:01 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/Target/TargetData.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000021
Chris Lattnerfe11a972002-12-23 23:59:41 +000022namespace {
23 cl::opt<std::string>
24 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000025
Chris Lattnerfe11a972002-12-23 23:59:41 +000026 cl::list<std::string>
27 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000028
Chris Lattnerfe11a972002-12-23 23:59:41 +000029 cl::opt<std::string>
30 MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
31 cl::value_desc("function name"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000032
Chris Lattnerfe11a972002-12-23 23:59:41 +000033 cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000034
Chris Lattnerfe11a972002-12-23 23:59:41 +000035 cl::opt<bool> ForceInterpreter("force-interpreter",
36 cl::desc("Force interpretation: disable JIT"),
Chris Lattner19669f32003-05-12 14:31:57 +000037 cl::init(false));
Chris Lattnerfe11a972002-12-23 23:59:41 +000038}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000039
Brian Gaeke70975ee2003-09-05 18:42:01 +000040static 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
47static 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///
95int 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 Lattner92101ac2001-08-23 17:05:04 +0000112//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000113// main Driver function
114//
Brian Gaeke70975ee2003-09-05 18:42:01 +0000115int main(int argc, char **argv, const char **envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000116 cl::ParseCommandLineOptions(argc, argv,
117 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +0000118
Chris Lattnerfe11a972002-12-23 23:59:41 +0000119 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000120 std::string ErrorMsg;
Chris Lattnerfe11a972002-12-23 23:59:41 +0000121 Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
122 if (M == 0) {
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000123 std::cout << "Error parsing '" << InputFile << "': "
124 << ErrorMsg << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +0000125 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000126 }
127
Brian Gaeke82d82772003-09-03 20:34:19 +0000128 ExecutionEngine *EE =
Brian Gaekef58815e2003-09-04 22:21:24 +0000129 ExecutionEngine::create (M, ForceInterpreter, TraceMode);
Brian Gaeke82d82772003-09-03 20:34:19 +0000130 assert (EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +0000131
Brian Gaekef58815e2003-09-04 22:21:24 +0000132 // Add the module's name to the start of the vector of arguments to main().
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000133 // 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 Lattnerfe11a972002-12-23 23:59:41 +0000140 InputArgv.insert(InputArgv.begin(), InputFile);
141
142 // Run the main function!
Brian Gaeke70975ee2003-09-05 18:42:01 +0000143 int ExitCode = callAsMain (EE, M, MainFunction, InputArgv,
144 makeStringVector (envp));
Chris Lattnerfe11a972002-12-23 23:59:41 +0000145
146 // Now that we are done executing the program, shut down the execution engine
147 delete EE;
148 return ExitCode;
Chris Lattner92101ac2001-08-23 17:05:04 +0000149}