blob: 0547c433cb9f1cc6394ff27a97c8f438e4f1bb5e [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
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattnerfe11a972002-12-23 23:59:41 +000033namespace {
34 cl::opt<std::string>
35 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000036
Chris Lattnerfe11a972002-12-23 23:59:41 +000037 cl::list<std::string>
38 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000039
Chris Lattnerfe11a972002-12-23 23:59:41 +000040 cl::opt<std::string>
Misha Brukman3d8a54d2003-09-25 18:10:34 +000041 MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
42 cl::value_desc("function name"));
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 Lattnere69671d2003-10-28 22:51:44 +000047
48 cl::opt<std::string>
49 FakeArgv0("fake-argv0",
50 cl::desc("Override the 'argv[0]' value passed into the executing"
51 " program"), cl::value_desc("executable"));
Chris Lattnerfe11a972002-12-23 23:59:41 +000052}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000053
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000054static std::vector<std::string> makeStringVector(char * const *envp) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000055 std::vector<std::string> rv;
56 for (unsigned i = 0; envp[i]; ++i)
Misha Brukman3d8a54d2003-09-25 18:10:34 +000057 rv.push_back(envp[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000058 return rv;
59}
60
61static void *CreateArgv(ExecutionEngine *EE,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000062 const std::vector<std::string> &InputArgv) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000063 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target?
64 PointerTy *Result = new PointerTy[InputArgv.size()+1];
65 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
66
67 for (unsigned i = 0; i < InputArgv.size(); ++i) {
68 unsigned Size = InputArgv[i].size()+1;
69 char *Dest = new char[Size];
70 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
71
72 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
73 Dest[Size-1] = 0;
74
75 // Endian safe: Result[i] = (PointerTy)Dest;
76 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000077 Type::LongTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000078 }
79 Result[InputArgv.size()] = 0;
80 return Result;
81 } else { // 32 bit target?
82 int *Result = new int[InputArgv.size()+1];
83 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
84
85 for (unsigned i = 0; i < InputArgv.size(); ++i) {
86 unsigned Size = InputArgv[i].size()+1;
87 char *Dest = new char[Size];
88 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
89
90 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
91 Dest[Size-1] = 0;
92
93 // Endian safe: Result[i] = (PointerTy)Dest;
94 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000095 Type::IntTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000096 }
97 Result[InputArgv.size()] = 0; // null terminate it
98 return Result;
99 }
100}
101
102/// callAsMain - Call the function named FnName from M as if its
103/// signature were int main (int argc, char **argv, const char
104/// **envp), using the contents of Args to determine argc & argv, and
105/// the contents of EnvVars to determine envp. Returns the result
106/// from calling FnName, or -1 and prints an error msg. if the named
107/// function cannot be found.
108///
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000109int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
110 const std::string &FnName,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000111 const std::vector<std::string> &Args,
112 const std::vector<std::string> &EnvVars) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000113 Function *Fn = MP->getModule()->getNamedFunction(FnName);
Brian Gaeke70975ee2003-09-05 18:42:01 +0000114 if (!Fn) {
115 std::cerr << "Function '" << FnName << "' not found in module.\n";
116 return -1;
117 }
118 std::vector<GenericValue> GVArgs;
119 GenericValue GVArgc;
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000120 GVArgc.IntVal = Args.size();
121 GVArgs.push_back(GVArgc); // Arg #0 = argc.
122 GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
Brian Gaeke3f6e7982003-12-12 00:47:19 +0000123 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000124 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
125 return EE->run(Fn, GVArgs).IntVal;
Brian Gaeke70975ee2003-09-05 18:42:01 +0000126}
127
Chris Lattner92101ac2001-08-23 17:05:04 +0000128//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000129// main Driver function
130//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000131int main(int argc, char **argv, char * const *envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000132 cl::ParseCommandLineOptions(argc, argv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000133 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +0000134
Chris Lattnerfe11a972002-12-23 23:59:41 +0000135 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000136 std::string ErrorMsg;
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000137 ModuleProvider *MP = 0;
138 try {
139 MP = getBytecodeModuleProvider(InputFile);
140 } catch (std::string &err) {
Brian Gaeke3f6e7982003-12-12 00:47:19 +0000141 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +0000142 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000143 }
144
Brian Gaeke82d82772003-09-03 20:34:19 +0000145 ExecutionEngine *EE =
Brian Gaeke2f828c32003-10-24 20:00:17 +0000146 ExecutionEngine::create(MP, ForceInterpreter);
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000147 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +0000148
Chris Lattnere69671d2003-10-28 22:51:44 +0000149 // If the user specifically requested an argv[0] to pass into the program, do
150 // it now.
151 if (!FakeArgv0.empty()) {
152 InputFile = FakeArgv0;
153 } else {
154 // Otherwise, if there is a .bc suffix on the executable strip it off, it
155 // might confuse the program.
156 if (InputFile.rfind(".bc") == InputFile.length() - 3)
157 InputFile.erase(InputFile.length() - 3);
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000158 }
Chris Lattnere69671d2003-10-28 22:51:44 +0000159
160 // Add the module's name to the start of the vector of arguments to main().
Chris Lattnerfe11a972002-12-23 23:59:41 +0000161 InputArgv.insert(InputArgv.begin(), InputFile);
162
163 // Run the main function!
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000164 int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000165 makeStringVector(envp));
Chris Lattnerfe11a972002-12-23 23:59:41 +0000166
167 // Now that we are done executing the program, shut down the execution engine
168 delete EE;
169 return ExitCode;
Chris Lattner92101ac2001-08-23 17:05:04 +0000170}