blob: 0f65d9d9b8598c4a1436798816a7948de3547cfc [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 Lattner7efea1d2003-12-26 05:07:35 +000010// This utility provides a simple wrapper around the LLVM Execution Engines,
11// which allow the direct execution of LLVM programs through a Just-In-Time
12// compiler, or through an intepreter if no JIT is available for this platform.
Chris Lattner92101ac2001-08-23 17:05:04 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattnerfd131292003-09-05 20:08:15 +000016#include "llvm/DerivedTypes.h"
17#include "llvm/Module.h"
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000018#include "llvm/ModuleProvider.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000019#include "llvm/Bytecode/Reader.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000020#include "llvm/ExecutionEngine/ExecutionEngine.h"
21#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000022#include "llvm/Target/TargetMachineImpls.h"
Brian Gaeke70975ee2003-09-05 18:42:01 +000023#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000024#include "Support/CommandLine.h"
25#include "Support/Debug.h"
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000026#include "Support/SystemUtils.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattnerfe11a972002-12-23 23:59:41 +000030namespace {
31 cl::opt<std::string>
32 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000033
Chris Lattnerfe11a972002-12-23 23:59:41 +000034 cl::list<std::string>
35 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000036
Chris Lattnerfe11a972002-12-23 23:59:41 +000037 cl::opt<std::string>
Misha Brukman3d8a54d2003-09-25 18:10:34 +000038 MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
39 cl::value_desc("function name"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000040
Chris Lattnerfe11a972002-12-23 23:59:41 +000041 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000042 cl::desc("Force interpretation: disable JIT"),
43 cl::init(false));
Chris Lattnere69671d2003-10-28 22:51:44 +000044
45 cl::opt<std::string>
46 FakeArgv0("fake-argv0",
47 cl::desc("Override the 'argv[0]' value passed into the executing"
48 " program"), cl::value_desc("executable"));
Chris Lattnerfe11a972002-12-23 23:59:41 +000049}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000050
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000051static std::vector<std::string> makeStringVector(char * const *envp) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000052 std::vector<std::string> rv;
53 for (unsigned i = 0; envp[i]; ++i)
Misha Brukman3d8a54d2003-09-25 18:10:34 +000054 rv.push_back(envp[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000055 return rv;
56}
57
58static void *CreateArgv(ExecutionEngine *EE,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000059 const std::vector<std::string> &InputArgv) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000060 if (EE->getTargetData().getPointerSize() == 8) { // 64 bit target?
61 PointerTy *Result = new PointerTy[InputArgv.size()+1];
62 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
63
64 for (unsigned i = 0; i < InputArgv.size(); ++i) {
65 unsigned Size = InputArgv[i].size()+1;
66 char *Dest = new char[Size];
67 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
68
69 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
70 Dest[Size-1] = 0;
71
72 // Endian safe: Result[i] = (PointerTy)Dest;
73 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000074 Type::LongTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000075 }
76 Result[InputArgv.size()] = 0;
77 return Result;
78 } else { // 32 bit target?
79 int *Result = new int[InputArgv.size()+1];
80 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
81
82 for (unsigned i = 0; i < InputArgv.size(); ++i) {
83 unsigned Size = InputArgv[i].size()+1;
84 char *Dest = new char[Size];
85 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
86
87 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
88 Dest[Size-1] = 0;
89
90 // Endian safe: Result[i] = (PointerTy)Dest;
91 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
Misha Brukman3d8a54d2003-09-25 18:10:34 +000092 Type::IntTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000093 }
94 Result[InputArgv.size()] = 0; // null terminate it
95 return Result;
96 }
97}
98
99/// callAsMain - Call the function named FnName from M as if its
100/// signature were int main (int argc, char **argv, const char
101/// **envp), using the contents of Args to determine argc & argv, and
102/// the contents of EnvVars to determine envp. Returns the result
103/// from calling FnName, or -1 and prints an error msg. if the named
104/// function cannot be found.
105///
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000106int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
107 const std::string &FnName,
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000108 const std::vector<std::string> &Args,
109 const std::vector<std::string> &EnvVars) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +0000110 Function *Fn = MP->getModule()->getNamedFunction(FnName);
Brian Gaeke70975ee2003-09-05 18:42:01 +0000111 if (!Fn) {
112 std::cerr << "Function '" << FnName << "' not found in module.\n";
113 return -1;
114 }
115 std::vector<GenericValue> GVArgs;
116 GenericValue GVArgc;
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000117 GVArgc.IntVal = Args.size();
118 GVArgs.push_back(GVArgc); // Arg #0 = argc.
119 GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
Brian Gaeke3f6e7982003-12-12 00:47:19 +0000120 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
Misha Brukman3d8a54d2003-09-25 18:10:34 +0000121 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) {
Brian Gaeke3f6e7982003-12-12 00:47:19 +0000138 std::cerr << "Error loading program '" << 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}