2002-12-24 00:59:41 +01:00
|
|
|
//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02: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 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-08-23 19:05:04 +02:00
|
|
|
//
|
2003-12-26 06:07:35 +01: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 19:05:04 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-09-05 22:08:15 +02:00
|
|
|
#include "llvm/Module.h"
|
2003-10-14 23:39:53 +02:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2003-12-26 07:49:53 +01:00
|
|
|
#include "llvm/Type.h"
|
2003-09-05 22:08:15 +02:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2006-08-02 00:34:35 +02:00
|
|
|
#include "llvm/CodeGen/LinkAllCodegenComponents.h"
|
2006-03-24 03:53:49 +01:00
|
|
|
#include "llvm/ExecutionEngine/JIT.h"
|
|
|
|
#include "llvm/ExecutionEngine/Interpreter.h"
|
2003-09-05 21:42:34 +02:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/PluginLoader.h"
|
2006-09-14 08:17:09 +02:00
|
|
|
#include "llvm/System/Process.h"
|
2004-05-27 07:41:36 +02:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-07-04 14:20:55 +02:00
|
|
|
#include <iostream>
|
2001-08-23 19:05:04 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-12-24 00:59:41 +01:00
|
|
|
namespace {
|
|
|
|
cl::opt<std::string>
|
|
|
|
InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2002-12-24 00:59:41 +01:00
|
|
|
cl::list<std::string>
|
|
|
|
InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2002-12-24 00:59:41 +01:00
|
|
|
cl::opt<bool> ForceInterpreter("force-interpreter",
|
2003-09-25 20:10:34 +02:00
|
|
|
cl::desc("Force interpretation: disable JIT"),
|
|
|
|
cl::init(false));
|
2005-12-16 06:00:21 +01:00
|
|
|
cl::opt<std::string>
|
2005-12-16 06:19:18 +01:00
|
|
|
TargetTriple("mtriple", cl::desc("Override target triple for module"));
|
2005-12-16 06:00:21 +01:00
|
|
|
|
2003-10-28 23:51:44 +01:00
|
|
|
cl::opt<std::string>
|
|
|
|
FakeArgv0("fake-argv0",
|
|
|
|
cl::desc("Override the 'argv[0]' value passed into the executing"
|
|
|
|
" program"), cl::value_desc("executable"));
|
2006-09-14 08:17:09 +02:00
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
DisableCoreFiles("disable-core-files", cl::Hidden,
|
|
|
|
cl::desc("Disable emission of core files if possible"));
|
2002-12-24 00:59:41 +01:00
|
|
|
}
|
2001-10-27 10:43:52 +02:00
|
|
|
|
2001-08-23 19:05:04 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main Driver function
|
|
|
|
//
|
2003-10-14 23:39:53 +02:00
|
|
|
int main(int argc, char **argv, char * const *envp) {
|
|
|
|
try {
|
2004-12-30 06:36:08 +01:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm interpreter & dynamic compiler\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
|
2006-09-14 08:17:09 +02:00
|
|
|
// If the user doesn't want core files, disable them.
|
|
|
|
if (DisableCoreFiles)
|
|
|
|
sys::Process::PreventCoreFiles();
|
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Load the bytecode...
|
|
|
|
std::string ErrorMsg;
|
|
|
|
ModuleProvider *MP = 0;
|
2006-08-25 19:43:11 +02:00
|
|
|
MP = getBytecodeModuleProvider(InputFile, &ErrorMsg);
|
|
|
|
if (!MP) {
|
2006-03-08 19:43:36 +01:00
|
|
|
std::cerr << "Error loading program '" << InputFile << "': "
|
2006-08-25 19:43:11 +02:00
|
|
|
<< ErrorMsg << "\n";
|
2004-12-30 06:36:08 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-12-16 06:00:21 +01:00
|
|
|
// If we are supposed to override the target triple, do so now.
|
|
|
|
if (!TargetTriple.empty())
|
|
|
|
MP->getModule()->setTargetTriple(TargetTriple);
|
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
|
2006-03-08 19:43:36 +01:00
|
|
|
assert(EE &&"Couldn't create an ExecutionEngine, not even an interpreter?");
|
2004-12-30 06:36:08 +01:00
|
|
|
|
2006-03-08 19:43:36 +01:00
|
|
|
// If the user specifically requested an argv[0] to pass into the program,
|
|
|
|
// do it now.
|
2004-12-30 06:36:08 +01:00
|
|
|
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();
|
2005-12-02 20:00:22 +01:00
|
|
|
if (!Fn) {
|
2004-12-30 06:36:08 +01:00
|
|
|
std::cerr << "'main' function not found in module.\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-03-08 19:43:36 +01:00
|
|
|
// Run static constructors.
|
|
|
|
EE->runStaticConstructorsDestructors(false);
|
|
|
|
|
|
|
|
// Run main.
|
2004-12-30 06:36:08 +01:00
|
|
|
int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
|
|
|
|
|
2006-03-08 19:43:36 +01:00
|
|
|
// Run static destructors.
|
|
|
|
EE->runStaticConstructorsDestructors(true);
|
|
|
|
|
|
|
|
// If the program didn't explicitly call exit, call exit now, for the
|
|
|
|
// program. This ensures that any atexit handlers get called correctly.
|
2004-12-30 06:36:08 +01:00
|
|
|
Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
|
2005-10-23 06:37:20 +02:00
|
|
|
Type::IntTy,
|
|
|
|
(Type *)0);
|
2004-12-30 06:36:08 +01: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 07:14:47 +01:00
|
|
|
}
|
|
|
|
abort();
|
2001-08-23 19:05:04 +02:00
|
|
|
}
|