2002-12-24 08:01:05 +08:00
|
|
|
//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
|
|
|
|
//
|
|
|
|
// This file implements the top-level functionality for the LLVM interpreter.
|
|
|
|
// This interpreter is designed to be a very simple, portable, inefficient
|
|
|
|
// interpreter.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Interpreter.h"
|
|
|
|
#include "llvm/Target/TargetMachineImpls.h"
|
|
|
|
|
|
|
|
/// createInterpreter - Create a new interpreter object. This can never fail.
|
|
|
|
///
|
|
|
|
ExecutionEngine *ExecutionEngine::createInterpreter(Module *M,
|
|
|
|
unsigned Config,
|
|
|
|
bool DebugMode,
|
|
|
|
bool TraceMode) {
|
|
|
|
return new Interpreter(M, Config, DebugMode, TraceMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Interpreter ctor - Initialize stuff
|
|
|
|
//
|
|
|
|
Interpreter::Interpreter(Module *M, unsigned Config,
|
|
|
|
bool DebugMode, bool TraceMode)
|
|
|
|
: ExecutionEngine(M), ExitCode(0), Debug(DebugMode), Trace(TraceMode),
|
|
|
|
CurFrame(-1), TD("lli", (Config & TM::EndianMask) == TM::LittleEndian,
|
|
|
|
(Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4,
|
2003-04-25 14:15:05 +08:00
|
|
|
(Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4,
|
2002-12-24 08:01:05 +08:00
|
|
|
(Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4) {
|
|
|
|
|
|
|
|
setTargetData(TD);
|
|
|
|
// Initialize the "backend"
|
|
|
|
initializeExecutionEngine();
|
2003-05-09 00:18:31 +08:00
|
|
|
initializeExternalFunctions();
|
2002-12-24 08:01:05 +08:00
|
|
|
CW.setModule(M); // Update Writer
|
2003-05-12 10:14:34 +08:00
|
|
|
emitGlobals();
|
2002-12-24 08:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// run - Start execution with the specified function and arguments.
|
|
|
|
///
|
|
|
|
int Interpreter::run(const std::string &MainFunction,
|
2003-08-22 05:12:30 +08:00
|
|
|
const std::vector<std::string> &Args,
|
|
|
|
const char ** envp) {
|
2002-12-24 08:01:05 +08:00
|
|
|
// Start interpreter into the main function...
|
|
|
|
//
|
2003-05-09 00:18:31 +08:00
|
|
|
if (!callMainFunction(MainFunction, Args) && !Debug) {
|
2002-12-24 08:01:05 +08:00
|
|
|
// If not in debug mode and if the call succeeded, run the code now...
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2003-05-14 22:21:30 +08:00
|
|
|
do {
|
|
|
|
// If debug mode, allow the user to interact... also, if the user pressed
|
|
|
|
// ctrl-c or execution hit an error, enter the event loop...
|
|
|
|
if (Debug || isStopped())
|
|
|
|
handleUserInput();
|
|
|
|
|
|
|
|
// If the program has exited, run atexit handlers...
|
|
|
|
if (ECStack.empty() && !AtExitHandlers.empty()) {
|
|
|
|
callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
|
|
|
|
AtExitHandlers.pop_back();
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
} while (!ECStack.empty());
|
|
|
|
|
|
|
|
PerformExitStuff();
|
2002-12-24 08:01:05 +08:00
|
|
|
return ExitCode;
|
|
|
|
}
|
|
|
|
|