2002-12-24 08:01:05 +08:00
|
|
|
//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
|
2005-04-22 06:43:08 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 06:43:08 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-24 08:01:05 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2004-06-20 15:49:54 +08:00
|
|
|
#include "llvm/CodeGen/IntrinsicLowering.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2008-02-20 19:08:44 +08:00
|
|
|
#include <cstring>
|
2003-12-15 07:25:48 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
namespace {
|
|
|
|
|
2006-03-22 14:07:50 +08:00
|
|
|
static struct RegisterInterp {
|
|
|
|
RegisterInterp() { Interpreter::Register(); }
|
|
|
|
} InterpRegistrator;
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 05:09:18 +08:00
|
|
|
extern "C" void LLVMLinkInInterpreter() { }
|
2006-03-24 10:53:49 +08:00
|
|
|
|
2014-08-19 12:04:25 +08:00
|
|
|
/// Create a new interpreter object.
|
2002-12-24 08:01:05 +08:00
|
|
|
///
|
2014-08-19 12:04:25 +08:00
|
|
|
ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
|
|
|
|
std::string *ErrStr) {
|
2010-01-28 04:34:15 +08:00
|
|
|
// Tell this Module to materialize everything and release the GVMaterializer.
|
2015-12-19 04:13:39 +08:00
|
|
|
if (std::error_code EC = M->materializeAll()) {
|
2014-01-15 07:51:27 +08:00
|
|
|
if (ErrStr)
|
|
|
|
*ErrStr = EC.message();
|
2007-03-04 02:19:18 +08:00
|
|
|
// We got an error, just return 0
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2014-01-15 07:51:27 +08:00
|
|
|
}
|
2007-03-04 02:19:18 +08:00
|
|
|
|
2014-08-19 12:04:25 +08:00
|
|
|
return new Interpreter(std::move(M));
|
2002-12-24 08:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Interpreter ctor - Initialize stuff
|
|
|
|
//
|
2014-08-19 12:04:25 +08:00
|
|
|
Interpreter::Interpreter(std::unique_ptr<Module> M)
|
2015-07-17 00:34:23 +08:00
|
|
|
: ExecutionEngine(std::move(M)) {
|
2014-08-19 12:04:25 +08:00
|
|
|
|
2007-06-02 06:23:29 +08:00
|
|
|
memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
|
2002-12-24 08:01:05 +08:00
|
|
|
// Initialize the "backend"
|
|
|
|
initializeExecutionEngine();
|
2003-05-09 00:18:31 +08:00
|
|
|
initializeExternalFunctions();
|
2003-05-12 10:14:34 +08:00
|
|
|
emitGlobals();
|
2003-12-28 17:44:37 +08:00
|
|
|
|
2015-07-17 00:34:23 +08:00
|
|
|
IL = new IntrinsicLowering(getDataLayout());
|
2003-12-28 17:44:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Interpreter::~Interpreter() {
|
|
|
|
delete IL;
|
2002-12-24 08:01:05 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
void Interpreter::runAtExitHandlers () {
|
|
|
|
while (!AtExitHandlers.empty()) {
|
2015-06-14 03:50:29 +08:00
|
|
|
callFunction(AtExitHandlers.back(), None);
|
2003-09-06 02:42:01 +08:00
|
|
|
AtExitHandlers.pop_back();
|
2002-12-24 08:01:05 +08:00
|
|
|
run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
/// run - Start execution with the specified function and arguments.
|
|
|
|
///
|
2015-06-14 03:50:29 +08:00
|
|
|
GenericValue Interpreter::runFunction(Function *F,
|
|
|
|
ArrayRef<GenericValue> ArgValues) {
|
2003-09-06 02:42:01 +08:00
|
|
|
assert (F && "Function *F was null at entry to run()");
|
|
|
|
|
|
|
|
// Try extra hard not to pass extra args to a function that isn't
|
|
|
|
// expecting them. C programmers frequently bend the rules and
|
|
|
|
// declare main() with fewer parameters than it actually gets
|
|
|
|
// passed, and the interpreter barfs if you pass a function more
|
|
|
|
// parameters than it is declared to take. This does not attempt to
|
|
|
|
// take into account gratuitous differences in declared types,
|
|
|
|
// though.
|
2015-06-14 03:50:29 +08:00
|
|
|
const size_t ArgCount = F->getFunctionType()->getNumParams();
|
|
|
|
ArrayRef<GenericValue> ActualArgs =
|
|
|
|
ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
|
2005-04-22 06:43:08 +08:00
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
// Set up the function call.
|
|
|
|
callFunction(F, ActualArgs);
|
2003-09-05 12:46:26 +08:00
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
// Start executing the function.
|
|
|
|
run();
|
2005-04-22 06:43:08 +08:00
|
|
|
|
2006-02-07 13:29:44 +08:00
|
|
|
return ExitValue;
|
2003-09-05 12:46:26 +08:00
|
|
|
}
|