blob: 87fe461ce0d744a4fea39729595868f5102149ce [file] [log] [blame]
Chris Lattnerfe11a972002-12-23 23:59:41 +00001//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00009//
Chris Lattnerfe11a972002-12-23 23:59:41 +000010// 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
15// available.
Chris Lattner92101ac2001-08-23 17:05:04 +000016//
17//===----------------------------------------------------------------------===//
18
Chris Lattnerfd131292003-09-05 20:08:15 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000021#include "llvm/ModuleProvider.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000022#include "llvm/Bytecode/Reader.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000023#include "llvm/ExecutionEngine/ExecutionEngine.h"
24#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000025#include "llvm/Target/TargetMachineImpls.h"
Brian Gaeke70975ee2003-09-05 18:42:01 +000026#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000027#include "Support/CommandLine.h"
28#include "Support/Debug.h"
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000029#include "Support/SystemUtils.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000030
Chris Lattnerfe11a972002-12-23 23:59:41 +000031namespace {
32 cl::opt<std::string>
33 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000034
Chris Lattnerfe11a972002-12-23 23:59:41 +000035 cl::list<std::string>
36 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000037
Chris Lattnerfe11a972002-12-23 23:59:41 +000038 cl::opt<std::string>
Misha Brukman3d8a54d2003-09-25 18:10:34 +000039 MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
40 cl::value_desc("function name"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Chris Lattnerfe11a972002-12-23 23:59:41 +000042 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000043 cl::desc("Force interpretation: disable JIT"),
44 cl::init(false));
Chris Lattnere69671d2003-10-28 22:51:44 +000045
46 cl::opt<std::string>
47 FakeArgv0("fake-argv0",
48 cl::desc("Override the 'argv[0]' value passed into the executing"
49 " program"), cl::value_desc("executable"));
Chris Lattnerfe11a972002-12-23 23:59:41 +000050}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000051
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000052static std::vector<std::string> makeStringVector(char * const *envp) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000053 std::vector<std::string> rv;
54 for (unsigned i = 0; envp[i]; ++i)
Misha Brukman3d8a54d2003-09-25 18:10:34 +000055 rv.push_back(envp[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000056 return rv;
57}
58
59static void *CreateArgv(ExecutionEngine *EE,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000060 const std::vector<std::string> &InputArgv) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000061 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target?
62 PointerTy *Result = new PointerTy[InputArgv.size()+1];
63 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
64
65 for (unsigned i = 0; i < InputArgv.size(); ++i) {
66 unsigned Size = InputArgv[i].size()+1;
67 char *Dest = new char[Size];
68 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
69
70 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
71 Dest[Size-1] = 0;
72
73 // Endian safe: Result[i] = (PointerTy)Dest;
74 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000075 Type::LongTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000076 }
77 Result[InputArgv.size()] = 0;
78 return Result;
79 } else { // 32 bit target?
80 int *Result = new int[InputArgv.size()+1];
81 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
82
83 for (unsigned i = 0; i < InputArgv.size(); ++i) {
84 unsigned Size = InputArgv[i].size()+1;
85 char *Dest = new char[Size];
86 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
87
88 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
89 Dest[Size-1] = 0;
90
91 // Endian safe: Result[i] = (PointerTy)Dest;
92 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000093 Type::IntTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000094 }
95 Result[InputArgv.size()] = 0; // null terminate it
96 return Result;
97 }
98}
99
100/// callAsMain - Call the function named FnName from M as if its
101/// signature were int main (int argc, char **argv, const char
102/// **envp), using the contents of Args to determine argc & argv, and
103/// the contents of EnvVars to determine envp. Returns the result
104/// from calling FnName, or -1 and prints an error msg. if the named
105/// function cannot be found.
106///
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000107int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
108 const std::string &FnName,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000109 const std::vector<std::string> &Args,
110 const std::vector<std::string> &EnvVars) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000111 Function *Fn = MP->getModule()->getNamedFunction(FnName);
Brian Gaeke70975ee2003-09-05 18:42:01 +0000112 if (!Fn) {
113 std::cerr << "Function '" << FnName << "' not found in module.\n";
114 return -1;
115 }
116 std::vector<GenericValue> GVArgs;
117 GenericValue GVArgc;
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000118 GVArgc.IntVal = Args.size();
119 GVArgs.push_back(GVArgc); // Arg #0 = argc.
120 GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
121 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
122 return EE->run(Fn, GVArgs).IntVal;
Brian Gaeke70975ee2003-09-05 18:42:01 +0000123}
124
Chris Lattner92101ac2001-08-23 17:05:04 +0000125//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000126// main Driver function
127//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000128int main(int argc, char **argv, char * const *envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000129 cl::ParseCommandLineOptions(argc, argv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000130 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +0000131
Chris Lattnerfe11a972002-12-23 23:59:41 +0000132 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000133 std::string ErrorMsg;
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000134 ModuleProvider *MP = 0;
135 try {
136 MP = getBytecodeModuleProvider(InputFile);
137 } catch (std::string &err) {
138 std::cerr << "Error parsing '" << InputFile << "': " << err << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +0000139 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000140 }
141
Brian Gaeke82d82772003-09-03 20:34:19 +0000142 ExecutionEngine *EE =
Brian Gaeke2f828c32003-10-24 20:00:17 +0000143 ExecutionEngine::create(MP, ForceInterpreter);
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000144 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +0000145
Chris Lattnere69671d2003-10-28 22:51:44 +0000146 // If the user specifically requested an argv[0] to pass into the program, do
147 // it now.
148 if (!FakeArgv0.empty()) {
149 InputFile = FakeArgv0;
150 } else {
151 // Otherwise, if there is a .bc suffix on the executable strip it off, it
152 // might confuse the program.
153 if (InputFile.rfind(".bc") == InputFile.length() - 3)
154 InputFile.erase(InputFile.length() - 3);
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000155 }
Chris Lattnere69671d2003-10-28 22:51:44 +0000156
157 // Add the module's name to the start of the vector of arguments to main().
Chris Lattnerfe11a972002-12-23 23:59:41 +0000158 InputArgv.insert(InputArgv.begin(), InputFile);
159
160 // Run the main function!
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000161 int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000162 makeStringVector(envp));
Chris Lattnerfe11a972002-12-23 23:59:41 +0000163
164 // Now that we are done executing the program, shut down the execution engine
165 delete EE;
166 return ExitCode;
Chris Lattner92101ac2001-08-23 17:05:04 +0000167}