#include "Interpreter.h"
#include "llvm/CodeGen/IntrinsicLowering.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Module.h"
#include <cstring>
using namespace llvm;
namespace {
static struct RegisterInterp {
RegisterInterp() { Interpreter::Register(); }
} InterpRegistrator;
}
extern "C" void LLVMLinkInInterpreter() { }
ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
std::string *ErrStr) {
if (std::error_code EC = M->materializeAllPermanently()) {
if (ErrStr)
*ErrStr = EC.message();
return nullptr;
}
return new Interpreter(std::move(M));
}
Interpreter::Interpreter(std::unique_ptr<Module> M)
: ExecutionEngine(std::move(M)), TD(Modules.back().get()) {
memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
setDataLayout(&TD);
initializeExecutionEngine();
initializeExternalFunctions();
emitGlobals();
IL = new IntrinsicLowering(TD);
}
Interpreter::~Interpreter() {
delete IL;
}
void Interpreter::runAtExitHandlers () {
while (!AtExitHandlers.empty()) {
callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
AtExitHandlers.pop_back();
run();
}
}
GenericValue
Interpreter::runFunction(Function *F,
const std::vector<GenericValue> &ArgValues) {
assert (F && "Function *F was null at entry to run()");
std::vector<GenericValue> ActualArgs;
const unsigned ArgCount = F->getFunctionType()->getNumParams();
for (unsigned i = 0; i < ArgCount; ++i)
ActualArgs.push_back(ArgValues[i]);
callFunction(F, ActualArgs);
run();
return ExitValue;
}