blob: 0283b5da8cb6ddbf37b1f969212e2839fa32001d [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 Lattnerfd131292003-09-05 20:08:15 +000012#include "llvm/DerivedTypes.h"
13#include "llvm/Module.h"
14#include "llvm/Bytecode/Reader.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000015#include "llvm/ExecutionEngine/ExecutionEngine.h"
16#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000017#include "llvm/Target/TargetMachineImpls.h"
Brian Gaeke70975ee2003-09-05 18:42:01 +000018#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000019#include "Support/CommandLine.h"
20#include "Support/Debug.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>
Misha Brukman3d8a54d2003-09-25 18:10:34 +000030 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",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000036 cl::desc("Force interpretation: disable JIT"),
37 cl::init(false));
Chris Lattnerfe11a972002-12-23 23:59:41 +000038}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000039
Misha Brukman3d8a54d2003-09-25 18:10:34 +000040static std::vector<std::string> makeStringVector(const char **envp) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000041 std::vector<std::string> rv;
42 for (unsigned i = 0; envp[i]; ++i)
Misha Brukman3d8a54d2003-09-25 18:10:34 +000043 rv.push_back(envp[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000044 return rv;
45}
46
47static void *CreateArgv(ExecutionEngine *EE,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000048 const std::vector<std::string> &InputArgv) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000049 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),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000063 Type::LongTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000064 }
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),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000081 Type::IntTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000082 }
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///
Misha Brukman3d8a54d2003-09-25 18:10:34 +000095int 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);
Brian Gaeke70975ee2003-09-05 18:42:01 +000099 if (!Fn) {
100 std::cerr << "Function '" << FnName << "' not found in module.\n";
101 return -1;
102 }
103 std::vector<GenericValue> GVArgs;
104 GenericValue GVArgc;
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000105 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;
Brian Gaeke70975ee2003-09-05 18:42:01 +0000110}
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,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000117 " 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 =
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000129 ExecutionEngine::create(M, ForceInterpreter, TraceMode);
130 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.
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000135 const std::string ByteCodeFileSuffix(".bc");
136 if (InputFile.rfind(ByteCodeFileSuffix) ==
137 InputFile.length() - ByteCodeFileSuffix.length()) {
138 InputFile.erase(InputFile.length() - ByteCodeFileSuffix.length());
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000139 }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000140 InputArgv.insert(InputArgv.begin(), InputFile);
141
142 // Run the main function!
Misha Brukman3d8a54d2003-09-25 18:10:34 +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}