[win/asan] GetInstructionSize: Fix `83 E4 XX` to return 3. (#119644)
[llvm-project.git] / llvm / lib / ExecutionEngine / Interpreter / Interpreter.cpp
blob2b9149e825fdb8f598dbc7340f5924384c8e22df
1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the top-level functionality for the LLVM interpreter.
10 // This interpreter is designed to be a very simple, portable, inefficient
11 // interpreter.
13 //===----------------------------------------------------------------------===//
15 #include "Interpreter.h"
16 #include "llvm/CodeGen/IntrinsicLowering.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Module.h"
19 #include <cstring>
20 using namespace llvm;
22 namespace {
24 static struct RegisterInterp {
25 RegisterInterp() { Interpreter::Register(); }
26 } InterpRegistrator;
30 extern "C" void LLVMLinkInInterpreter() { }
32 /// Create a new interpreter object.
33 ///
34 ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
35 std::string *ErrStr) {
36 // Tell this Module to materialize everything and release the GVMaterializer.
37 if (Error Err = M->materializeAll()) {
38 std::string Msg;
39 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
40 Msg = EIB.message();
41 });
42 if (ErrStr)
43 *ErrStr = Msg;
44 // We got an error, just return 0
45 return nullptr;
48 return new Interpreter(std::move(M));
51 //===----------------------------------------------------------------------===//
52 // Interpreter ctor - Initialize stuff
54 Interpreter::Interpreter(std::unique_ptr<Module> M)
55 : ExecutionEngine(std::move(M)) {
57 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
58 // Initialize the "backend"
59 initializeExecutionEngine();
60 initializeExternalFunctions();
61 emitGlobals();
63 IL = new IntrinsicLowering(getDataLayout());
66 Interpreter::~Interpreter() {
67 delete IL;
70 void Interpreter::runAtExitHandlers () {
71 while (!AtExitHandlers.empty()) {
72 callFunction(AtExitHandlers.back(), {});
73 AtExitHandlers.pop_back();
74 run();
78 /// run - Start execution with the specified function and arguments.
79 ///
80 GenericValue Interpreter::runFunction(Function *F,
81 ArrayRef<GenericValue> ArgValues) {
82 assert (F && "Function *F was null at entry to run()");
84 // Try extra hard not to pass extra args to a function that isn't
85 // expecting them. C programmers frequently bend the rules and
86 // declare main() with fewer parameters than it actually gets
87 // passed, and the interpreter barfs if you pass a function more
88 // parameters than it is declared to take. This does not attempt to
89 // take into account gratuitous differences in declared types,
90 // though.
91 const size_t ArgCount = F->getFunctionType()->getNumParams();
92 ArrayRef<GenericValue> ActualArgs =
93 ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
95 // Set up the function call.
96 callFunction(F, ActualArgs);
98 // Start executing the function.
99 run();
101 return ExitValue;