2002-12-23 23:59:41 +00:00
|
|
|
//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
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.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/PluginLoader.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-07-04 12:20:55 +00:00
|
|
|
#include <iostream>
|
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) {
|
|
|
|
try {
|
2004-12-30 05:36:08 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm interpreter & dynamic compiler\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
|
|
|
|
// Load the bytecode...
|
|
|
|
std::string ErrorMsg;
|
|
|
|
ModuleProvider *MP = 0;
|
|
|
|
try {
|
|
|
|
MP = getBytecodeModuleProvider(InputFile);
|
|
|
|
} catch (std::string &err) {
|
|
|
|
std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
|
|
|
|
assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the module's name to the start of the vector of arguments to main().
|
|
|
|
InputArgv.insert(InputArgv.begin(), InputFile);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run main...
|
|
|
|
int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
|
|
|
|
|
|
|
|
// 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,
|
2005-10-23 04:37:20 +00:00
|
|
|
Type::IntTy,
|
|
|
|
(Type *)0);
|
2004-12-30 05:36:08 +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";
|
|
|
|
abort();
|
|
|
|
} catch (const std::string& msg) {
|
|
|
|
std::cerr << argv[0] << ": " << msg << "\n";
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2003-12-26 06:14:47 +00:00
|
|
|
}
|
|
|
|
abort();
|
2001-08-23 17:05:04 +00:00
|
|
|
}
|