109467b48Spatrick //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file implements the top-level functionality for the LLVM interpreter.
1009467b48Spatrick // This interpreter is designed to be a very simple, portable, inefficient
1109467b48Spatrick // interpreter.
1209467b48Spatrick //
1309467b48Spatrick //===----------------------------------------------------------------------===//
1409467b48Spatrick
1509467b48Spatrick #include "Interpreter.h"
1609467b48Spatrick #include "llvm/CodeGen/IntrinsicLowering.h"
1709467b48Spatrick #include "llvm/IR/DerivedTypes.h"
1809467b48Spatrick #include "llvm/IR/Module.h"
1909467b48Spatrick #include <cstring>
2009467b48Spatrick using namespace llvm;
2109467b48Spatrick
2209467b48Spatrick namespace {
2309467b48Spatrick
2409467b48Spatrick static struct RegisterInterp {
RegisterInterp__anon7600efd70111::RegisterInterp2509467b48Spatrick RegisterInterp() { Interpreter::Register(); }
2609467b48Spatrick } InterpRegistrator;
2709467b48Spatrick
2809467b48Spatrick }
2909467b48Spatrick
LLVMLinkInInterpreter()3009467b48Spatrick extern "C" void LLVMLinkInInterpreter() { }
3109467b48Spatrick
3209467b48Spatrick /// Create a new interpreter object.
3309467b48Spatrick ///
create(std::unique_ptr<Module> M,std::string * ErrStr)3409467b48Spatrick ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
3509467b48Spatrick std::string *ErrStr) {
3609467b48Spatrick // Tell this Module to materialize everything and release the GVMaterializer.
3709467b48Spatrick if (Error Err = M->materializeAll()) {
3809467b48Spatrick std::string Msg;
3909467b48Spatrick handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
4009467b48Spatrick Msg = EIB.message();
4109467b48Spatrick });
4209467b48Spatrick if (ErrStr)
4309467b48Spatrick *ErrStr = Msg;
4409467b48Spatrick // We got an error, just return 0
4509467b48Spatrick return nullptr;
4609467b48Spatrick }
4709467b48Spatrick
4809467b48Spatrick return new Interpreter(std::move(M));
4909467b48Spatrick }
5009467b48Spatrick
5109467b48Spatrick //===----------------------------------------------------------------------===//
5209467b48Spatrick // Interpreter ctor - Initialize stuff
5309467b48Spatrick //
Interpreter(std::unique_ptr<Module> M)5409467b48Spatrick Interpreter::Interpreter(std::unique_ptr<Module> M)
5509467b48Spatrick : ExecutionEngine(std::move(M)) {
5609467b48Spatrick
5709467b48Spatrick memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
5809467b48Spatrick // Initialize the "backend"
5909467b48Spatrick initializeExecutionEngine();
6009467b48Spatrick initializeExternalFunctions();
6109467b48Spatrick emitGlobals();
6209467b48Spatrick
6309467b48Spatrick IL = new IntrinsicLowering(getDataLayout());
6409467b48Spatrick }
6509467b48Spatrick
~Interpreter()6609467b48Spatrick Interpreter::~Interpreter() {
6709467b48Spatrick delete IL;
6809467b48Spatrick }
6909467b48Spatrick
runAtExitHandlers()7009467b48Spatrick void Interpreter::runAtExitHandlers () {
7109467b48Spatrick while (!AtExitHandlers.empty()) {
72*d415bd75Srobert callFunction(AtExitHandlers.back(), std::nullopt);
7309467b48Spatrick AtExitHandlers.pop_back();
7409467b48Spatrick run();
7509467b48Spatrick }
7609467b48Spatrick }
7709467b48Spatrick
7809467b48Spatrick /// run - Start execution with the specified function and arguments.
7909467b48Spatrick ///
runFunction(Function * F,ArrayRef<GenericValue> ArgValues)8009467b48Spatrick GenericValue Interpreter::runFunction(Function *F,
8109467b48Spatrick ArrayRef<GenericValue> ArgValues) {
8209467b48Spatrick assert (F && "Function *F was null at entry to run()");
8309467b48Spatrick
8409467b48Spatrick // Try extra hard not to pass extra args to a function that isn't
8509467b48Spatrick // expecting them. C programmers frequently bend the rules and
8609467b48Spatrick // declare main() with fewer parameters than it actually gets
8709467b48Spatrick // passed, and the interpreter barfs if you pass a function more
8809467b48Spatrick // parameters than it is declared to take. This does not attempt to
8909467b48Spatrick // take into account gratuitous differences in declared types,
9009467b48Spatrick // though.
9109467b48Spatrick const size_t ArgCount = F->getFunctionType()->getNumParams();
9209467b48Spatrick ArrayRef<GenericValue> ActualArgs =
9309467b48Spatrick ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
9409467b48Spatrick
9509467b48Spatrick // Set up the function call.
9609467b48Spatrick callFunction(F, ActualArgs);
9709467b48Spatrick
9809467b48Spatrick // Start executing the function.
9909467b48Spatrick run();
10009467b48Spatrick
10109467b48Spatrick return ExitValue;
10209467b48Spatrick }
103