blob: 90d8a2fcdddb94087ac7113f00e1e560a32ceaac [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> TraceMode("trace", cl::desc("Enable Tracing"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000043
Chris Lattnerfe11a972002-12-23 23:59:41 +000044 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000045 cl::desc("Force interpretation: disable JIT"),
46 cl::init(false));
Chris Lattnerfe11a972002-12-23 23:59:41 +000047}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000048
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000049static std::vector<std::string> makeStringVector(char * const *envp) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000050 std::vector<std::string> rv;
51 for (unsigned i = 0; envp[i]; ++i)
Misha Brukman3d8a54d2003-09-25 18:10:34 +000052 rv.push_back(envp[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000053 return rv;
54}
55
56static void *CreateArgv(ExecutionEngine *EE,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000057 const std::vector<std::string> &InputArgv) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000058 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target?
59 PointerTy *Result = new PointerTy[InputArgv.size()+1];
60 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
61
62 for (unsigned i = 0; i < InputArgv.size(); ++i) {
63 unsigned Size = InputArgv[i].size()+1;
64 char *Dest = new char[Size];
65 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
66
67 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
68 Dest[Size-1] = 0;
69
70 // Endian safe: Result[i] = (PointerTy)Dest;
71 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000072 Type::LongTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000073 }
74 Result[InputArgv.size()] = 0;
75 return Result;
76 } else { // 32 bit target?
77 int *Result = new int[InputArgv.size()+1];
78 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
79
80 for (unsigned i = 0; i < InputArgv.size(); ++i) {
81 unsigned Size = InputArgv[i].size()+1;
82 char *Dest = new char[Size];
83 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
84
85 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
86 Dest[Size-1] = 0;
87
88 // Endian safe: Result[i] = (PointerTy)Dest;
89 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000090 Type::IntTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000091 }
92 Result[InputArgv.size()] = 0; // null terminate it
93 return Result;
94 }
95}
96
97/// callAsMain - Call the function named FnName from M as if its
98/// signature were int main (int argc, char **argv, const char
99/// **envp), using the contents of Args to determine argc & argv, and
100/// the contents of EnvVars to determine envp. Returns the result
101/// from calling FnName, or -1 and prints an error msg. if the named
102/// function cannot be found.
103///
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000104int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
105 const std::string &FnName,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000106 const std::vector<std::string> &Args,
107 const std::vector<std::string> &EnvVars) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000108 Function *Fn = MP->getModule()->getNamedFunction(FnName);
Brian Gaeke70975ee2003-09-05 18:42:01 +0000109 if (!Fn) {
110 std::cerr << "Function '" << FnName << "' not found in module.\n";
111 return -1;
112 }
113 std::vector<GenericValue> GVArgs;
114 GenericValue GVArgc;
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000115 GVArgc.IntVal = Args.size();
116 GVArgs.push_back(GVArgc); // Arg #0 = argc.
117 GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
118 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
119 return EE->run(Fn, GVArgs).IntVal;
Brian Gaeke70975ee2003-09-05 18:42:01 +0000120}
121
Chris Lattner92101ac2001-08-23 17:05:04 +0000122//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000123// main Driver function
124//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000125int main(int argc, char **argv, char * const *envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000126 cl::ParseCommandLineOptions(argc, argv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000127 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +0000128
Chris Lattnerfe11a972002-12-23 23:59:41 +0000129 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000130 std::string ErrorMsg;
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000131 ModuleProvider *MP = 0;
132 try {
133 MP = getBytecodeModuleProvider(InputFile);
134 } catch (std::string &err) {
135 std::cerr << "Error parsing '" << InputFile << "': " << err << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +0000136 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000137 }
138
Brian Gaeke82d82772003-09-03 20:34:19 +0000139 ExecutionEngine *EE =
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000140 ExecutionEngine::create(MP, ForceInterpreter, TraceMode);
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000141 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +0000142
Brian Gaekef58815e2003-09-04 22:21:24 +0000143 // Add the module's name to the start of the vector of arguments to main().
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000144 // But delete .bc first, since programs (and users) might not expect to
145 // see it.
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000146 const std::string ByteCodeFileSuffix(".bc");
147 if (InputFile.rfind(ByteCodeFileSuffix) ==
148 InputFile.length() - ByteCodeFileSuffix.length()) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000149 InputFile.erase (InputFile.length() - ByteCodeFileSuffix.length());
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000150 }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000151 InputArgv.insert(InputArgv.begin(), InputFile);
152
153 // Run the main function!
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000154 int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000155 makeStringVector(envp));
Chris Lattnerfe11a972002-12-23 23:59:41 +0000156
157 // Now that we are done executing the program, shut down the execution engine
158 delete EE;
159 return ExitCode;
Chris Lattner92101ac2001-08-23 17:05:04 +0000160}