2002-12-23 23:59:41 +00:00
|
|
|
//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
|
2003-10-20 17:47:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-08-23 17:05:04 +00:00
|
|
|
//
|
2003-12-26 05:07:35 +00:00
|
|
|
// This utility provides a simple wrapper around the LLVM Execution Engines,
|
|
|
|
// which allow the direct execution of LLVM programs through a Just-In-Time
|
|
|
|
// compiler, or through an intepreter if no JIT is available for this platform.
|
2001-08-23 17:05:04 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-09-05 20:08:15 +00:00
|
|
|
#include "llvm/Module.h"
|
2003-10-14 21:39:53 +00:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2003-12-26 06:49:53 +00:00
|
|
|
#include "llvm/Type.h"
|
2003-09-05 20:08:15 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2003-09-05 19:42:34 +00:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2003-09-05 20:08:15 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2004-02-19 20:32:39 +00:00
|
|
|
#include "Support/Signals.h"
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
namespace {
|
|
|
|
cl::opt<std::string>
|
|
|
|
InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::list<std::string>
|
|
|
|
InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::opt<bool> ForceInterpreter("force-interpreter",
|
2003-09-25 18:10:34 +00:00
|
|
|
cl::desc("Force interpretation: disable JIT"),
|
|
|
|
cl::init(false));
|
2003-10-28 22:51:44 +00:00
|
|
|
|
|
|
|
cl::opt<std::string>
|
|
|
|
FakeArgv0("fake-argv0",
|
|
|
|
cl::desc("Override the 'argv[0]' value passed into the executing"
|
|
|
|
" program"), cl::value_desc("executable"));
|
2002-12-23 23:59:41 +00:00
|
|
|
}
|
2001-10-27 08:43:52 +00:00
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main Driver function
|
|
|
|
//
|
2003-10-14 21:39:53 +00:00
|
|
|
int main(int argc, char **argv, char * const *envp) {
|
2002-12-23 23:59:41 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
2003-09-25 18:10:34 +00:00
|
|
|
" llvm interpreter & dynamic compiler\n");
|
2004-02-19 20:32:39 +00:00
|
|
|
PrintStackTraceOnErrorSignal();
|
2002-12-23 23:59:41 +00:00
|
|
|
|
|
|
|
// Load the bytecode...
|
2002-12-24 00:39:16 +00:00
|
|
|
std::string ErrorMsg;
|
2003-10-14 21:39:53 +00:00
|
|
|
ModuleProvider *MP = 0;
|
|
|
|
try {
|
|
|
|
MP = getBytecodeModuleProvider(InputFile);
|
|
|
|
} catch (std::string &err) {
|
2003-12-12 00:47:19 +00:00
|
|
|
std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
|
2002-12-23 23:59:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-12-28 09:51:04 +00:00
|
|
|
ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
|
2003-09-25 18:10:34 +00:00
|
|
|
assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2003-10-28 22:51:44 +00:00
|
|
|
// If the user specifically requested an argv[0] to pass into the program, do
|
|
|
|
// it now.
|
|
|
|
if (!FakeArgv0.empty()) {
|
|
|
|
InputFile = FakeArgv0;
|
|
|
|
} else {
|
|
|
|
// Otherwise, if there is a .bc suffix on the executable strip it off, it
|
|
|
|
// might confuse the program.
|
|
|
|
if (InputFile.rfind(".bc") == InputFile.length() - 3)
|
|
|
|
InputFile.erase(InputFile.length() - 3);
|
2003-05-23 20:28:07 +00:00
|
|
|
}
|
2003-10-28 22:51:44 +00:00
|
|
|
|
|
|
|
// Add the module's name to the start of the vector of arguments to main().
|
2002-07-22 02:10:13 +00:00
|
|
|
InputArgv.insert(InputArgv.begin(), InputFile);
|
|
|
|
|
2003-12-26 06:14:47 +00:00
|
|
|
// Call the main function from M as if its signature were:
|
|
|
|
// int main (int argc, char **argv, const char **envp)
|
|
|
|
// using the contents of Args to determine argc & argv, and the contents of
|
|
|
|
// EnvVars to determine envp.
|
|
|
|
//
|
|
|
|
Function *Fn = MP->getModule()->getMainFunction();
|
|
|
|
if (!Fn) {
|
|
|
|
std::cerr << "'main' function not found in module.\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-12-26 06:49:53 +00:00
|
|
|
// Run main...
|
|
|
|
int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
|
2003-12-26 06:14:47 +00:00
|
|
|
|
|
|
|
// If the program didn't explicitly call exit, call exit now, for the program.
|
|
|
|
// This ensures that any atexit handlers get called correctly.
|
|
|
|
Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
|
|
|
|
Type::IntTy, 0);
|
|
|
|
|
2003-12-26 06:49:53 +00:00
|
|
|
std::vector<GenericValue> Args;
|
|
|
|
GenericValue ResultGV;
|
|
|
|
ResultGV.IntVal = Result;
|
|
|
|
Args.push_back(ResultGV);
|
|
|
|
EE->runFunction(Exit, Args);
|
|
|
|
|
|
|
|
std::cerr << "ERROR: exit(" << Result << ") returned!\n";
|
2003-12-26 06:14:47 +00:00
|
|
|
abort();
|
2001-08-23 17:05:04 +00:00
|
|
|
}
|