blob: c212abd2342051ca4062e3091b508adcbd2d33ca [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<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000038 cl::desc("Force interpretation: disable JIT"),
39 cl::init(false));
Chris Lattnere69671d2003-10-28 22:51:44 +000040
41 cl::opt<std::string>
42 FakeArgv0("fake-argv0",
43 cl::desc("Override the 'argv[0]' value passed into the executing"
44 " program"), cl::value_desc("executable"));
Chris Lattnerfe11a972002-12-23 23:59:41 +000045}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000046
Brian Gaeke70975ee2003-09-05 18:42:01 +000047static void *CreateArgv(ExecutionEngine *EE,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000048 const std::vector<std::string> &InputArgv) {
Chris Lattner3ef3dd32003-12-26 06:36:20 +000049 unsigned PtrSize = EE->getTargetData().getPointerSize();
50 char *Result = new char[(InputArgv.size()+1)*PtrSize];
Brian Gaeke70975ee2003-09-05 18:42:01 +000051
Chris Lattner3ef3dd32003-12-26 06:36:20 +000052 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
53 const Type *SBytePtr = PointerType::get(Type::SByteTy);
Brian Gaeke70975ee2003-09-05 18:42:01 +000054
Chris Lattner3ef3dd32003-12-26 06:36:20 +000055 for (unsigned i = 0; i != InputArgv.size(); ++i) {
56 unsigned Size = InputArgv[i].size()+1;
57 char *Dest = new char[Size];
58 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
Brian Gaeke70975ee2003-09-05 18:42:01 +000059
Chris Lattner3ef3dd32003-12-26 06:36:20 +000060 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
61 Dest[Size-1] = 0;
Brian Gaeke70975ee2003-09-05 18:42:01 +000062
Chris Lattner3ef3dd32003-12-26 06:36:20 +000063 // Endian safe: Result[i] = (PointerTy)Dest;
64 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
65 SBytePtr);
Brian Gaeke70975ee2003-09-05 18:42:01 +000066 }
Chris Lattner3ef3dd32003-12-26 06:36:20 +000067
68 // Null terminate it
69 EE->StoreValueToMemory(PTOGV(0),
70 (GenericValue*)(Result+InputArgv.size()*PtrSize),
71 SBytePtr);
72 return Result;
Brian Gaeke70975ee2003-09-05 18:42:01 +000073}
74
Chris Lattner92101ac2001-08-23 17:05:04 +000075//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000076// main Driver function
77//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000078int main(int argc, char **argv, char * const *envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000079 cl::ParseCommandLineOptions(argc, argv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000080 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +000081
Chris Lattnerfe11a972002-12-23 23:59:41 +000082 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +000083 std::string ErrorMsg;
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000084 ModuleProvider *MP = 0;
85 try {
86 MP = getBytecodeModuleProvider(InputFile);
87 } catch (std::string &err) {
Brian Gaeke3f6e7982003-12-12 00:47:19 +000088 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +000089 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +000090 }
91
Brian Gaeke82d82772003-09-03 20:34:19 +000092 ExecutionEngine *EE =
Brian Gaeke2f828c32003-10-24 20:00:17 +000093 ExecutionEngine::create(MP, ForceInterpreter);
Misha Brukman3d8a54d2003-09-25 18:10:34 +000094 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +000095
Chris Lattnere69671d2003-10-28 22:51:44 +000096 // If the user specifically requested an argv[0] to pass into the program, do
97 // it now.
98 if (!FakeArgv0.empty()) {
99 InputFile = FakeArgv0;
100 } else {
101 // Otherwise, if there is a .bc suffix on the executable strip it off, it
102 // might confuse the program.
103 if (InputFile.rfind(".bc") == InputFile.length() - 3)
104 InputFile.erase(InputFile.length() - 3);
Brian Gaeke6ae73dc2003-05-23 20:28:07 +0000105 }
Chris Lattnere69671d2003-10-28 22:51:44 +0000106
107 // Add the module's name to the start of the vector of arguments to main().
Chris Lattnerfe11a972002-12-23 23:59:41 +0000108 InputArgv.insert(InputArgv.begin(), InputFile);
109
Chris Lattnerefec9662003-12-26 06:14:47 +0000110 // Call the main function from M as if its signature were:
111 // int main (int argc, char **argv, const char **envp)
112 // using the contents of Args to determine argc & argv, and the contents of
113 // EnvVars to determine envp.
114 //
115 Function *Fn = MP->getModule()->getMainFunction();
116 if (!Fn) {
117 std::cerr << "'main' function not found in module.\n";
118 return -1;
119 }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000120
Chris Lattnerefec9662003-12-26 06:14:47 +0000121 std::vector<GenericValue> GVArgs;
122 GenericValue GVArgc;
123 GVArgc.IntVal = InputArgv.size();
124 GVArgs.push_back(GVArgc); // Arg #0 = argc.
125 GVArgs.push_back(PTOGV(CreateArgv(EE, InputArgv))); // Arg #1 = argv.
126 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
127
Chris Lattner3ef3dd32003-12-26 06:36:20 +0000128 std::vector<std::string> EnvVars;
129 for (unsigned i = 0; envp[i]; ++i)
130 EnvVars.push_back(envp[i]);
Chris Lattnerefec9662003-12-26 06:14:47 +0000131 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
132 GenericValue Result = EE->runFunction(Fn, GVArgs);
133
134 // If the program didn't explicitly call exit, call exit now, for the program.
135 // This ensures that any atexit handlers get called correctly.
136 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
137 Type::IntTy, 0);
138
139 GVArgs.clear();
140 GVArgs.push_back(Result);
141 EE->runFunction(Exit, GVArgs);
142
143 std::cerr << "ERROR: exit(" << Result.IntVal << ") returned!\n";
144 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000145}