blob: 2cd4e823c3bb8133783bcf016b3fb0331af39024 [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//===----------------------------------------------------------------------===//
9//
Chris Lattner92101ac2001-08-23 17:05:04 +000010//
Chris Lattnerfe11a972002-12-23 23:59:41 +000011// 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
16// available.
Chris Lattner92101ac2001-08-23 17:05:04 +000017//
18//===----------------------------------------------------------------------===//
19
Chris Lattnerfd131292003-09-05 20:08:15 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000022#include "llvm/ModuleProvider.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000023#include "llvm/Bytecode/Reader.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000024#include "llvm/ExecutionEngine/ExecutionEngine.h"
25#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000026#include "llvm/Target/TargetMachineImpls.h"
Brian Gaeke70975ee2003-09-05 18:42:01 +000027#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000028#include "Support/CommandLine.h"
29#include "Support/Debug.h"
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000030#include "Support/SystemUtils.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000031
Chris Lattnerfe11a972002-12-23 23:59:41 +000032namespace {
33 cl::opt<std::string>
34 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000035
Chris Lattnerfe11a972002-12-23 23:59:41 +000036 cl::list<std::string>
37 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000038
Chris Lattnerfe11a972002-12-23 23:59:41 +000039 cl::opt<std::string>
Misha Brukman3d8a54d2003-09-25 18:10:34 +000040 MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
41 cl::value_desc("function name"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000042
Chris Lattnerfe11a972002-12-23 23:59:41 +000043 cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000044
Chris Lattnerfe11a972002-12-23 23:59:41 +000045 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000046 cl::desc("Force interpretation: disable JIT"),
47 cl::init(false));
Chris Lattnerfe11a972002-12-23 23:59:41 +000048}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000049
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000050static std::vector<std::string> makeStringVector(char * const *envp) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000051 std::vector<std::string> rv;
52 for (unsigned i = 0; envp[i]; ++i)
Misha Brukman3d8a54d2003-09-25 18:10:34 +000053 rv.push_back(envp[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000054 return rv;
55}
56
57static void *CreateArgv(ExecutionEngine *EE,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000058 const std::vector<std::string> &InputArgv) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000059 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target?
60 PointerTy *Result = new PointerTy[InputArgv.size()+1];
61 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
62
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");
67
68 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
69 Dest[Size-1] = 0;
70
71 // Endian safe: Result[i] = (PointerTy)Dest;
72 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000073 Type::LongTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000074 }
75 Result[InputArgv.size()] = 0;
76 return Result;
77 } else { // 32 bit target?
78 int *Result = new int[InputArgv.size()+1];
79 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
80
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");
85
86 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
87 Dest[Size-1] = 0;
88
89 // Endian safe: Result[i] = (PointerTy)Dest;
90 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000091 Type::IntTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000092 }
93 Result[InputArgv.size()] = 0; // null terminate it
94 return Result;
95 }
96}
97
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.
104///
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000105int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
106 const std::string &FnName,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000107 const std::vector<std::string> &Args,
108 const std::vector<std::string> &EnvVars) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000109 Function *Fn = MP->getModule()->getNamedFunction(FnName);
Brian Gaeke70975ee2003-09-05 18:42:01 +0000110 if (!Fn) {
111 std::cerr << "Function '" << FnName << "' not found in module.\n";
112 return -1;
113 }
114 std::vector<GenericValue> GVArgs;
115 GenericValue GVArgc;
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000116 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;
Brian Gaeke70975ee2003-09-05 18:42:01 +0000121}
122
Chris Lattner92101ac2001-08-23 17:05:04 +0000123//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000124// main Driver function
125//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000126int main(int argc, char **argv, char * const *envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000127 cl::ParseCommandLineOptions(argc, argv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000128 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +0000129
Chris Lattnerfe11a972002-12-23 23:59:41 +0000130 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000131 std::string ErrorMsg;
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000132 ModuleProvider *MP = 0;
133 try {
134 MP = getBytecodeModuleProvider(InputFile);
135 } catch (std::string &err) {
136 std::cerr << "Error parsing '" << InputFile << "': " << err << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +0000137 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000138 }
139
Brian Gaeke82d82772003-09-03 20:34:19 +0000140 ExecutionEngine *EE =
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000141 ExecutionEngine::create(MP, ForceInterpreter, TraceMode);
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000142 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +0000143
Brian Gaekef58815e2003-09-04 22:21:24 +0000144 // Add the module's name to the start of the vector of arguments to main().
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000145 // But delete .bc first, since programs (and users) might not expect to
146 // see it.
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000147 const std::string ByteCodeFileSuffix(".bc");
148 if (InputFile.rfind(ByteCodeFileSuffix) ==
149 InputFile.length() - ByteCodeFileSuffix.length()) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000150 InputFile.erase (InputFile.length() - ByteCodeFileSuffix.length());
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000151 }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000152 InputArgv.insert(InputArgv.begin(), InputFile);
153
154 // Run the main function!
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000155 int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000156 makeStringVector(envp));
Chris Lattnerfe11a972002-12-23 23:59:41 +0000157
158 // Now that we are done executing the program, shut down the execution engine
159 delete EE;
160 return ExitCode;
Chris Lattner92101ac2001-08-23 17:05:04 +0000161}