LLVM  14.0.0git
Interpreter.cpp
Go to the documentation of this file.
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.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "Interpreter.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Module.h"
19 #include <cstring>
20 using namespace llvm;
21 
22 namespace {
23 
24 static struct RegisterInterp {
25  RegisterInterp() { Interpreter::Register(); }
26 } InterpRegistrator;
27 
28 }
29 
30 extern "C" void LLVMLinkInInterpreter() { }
31 
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;
46  }
47 
48  return new Interpreter(std::move(M));
49 }
50 
51 //===----------------------------------------------------------------------===//
52 // Interpreter ctor - Initialize stuff
53 //
54 Interpreter::Interpreter(std::unique_ptr<Module> M)
55  : ExecutionEngine(std::move(M)) {
56 
57  memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
58  // Initialize the "backend"
59  initializeExecutionEngine();
60  initializeExternalFunctions();
61  emitGlobals();
62 
63  IL = new IntrinsicLowering(getDataLayout());
64 }
65 
67  delete IL;
68 }
69 
71  while (!AtExitHandlers.empty()) {
72  callFunction(AtExitHandlers.back(), None);
73  AtExitHandlers.pop_back();
74  run();
75  }
76 }
77 
78 /// run - Start execution with the specified function and arguments.
79 ///
81  ArrayRef<GenericValue> ArgValues) {
82  assert (F && "Function *F was null at entry to run()");
83 
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));
94 
95  // Set up the function call.
96  callFunction(F, ActualArgs);
97 
98  // Start executing the function.
99  run();
100 
101  return ExitValue;
102 }
llvm::Interpreter::create
static ExecutionEngine * create(std::unique_ptr< Module > M, std::string *ErrorStr=nullptr)
Create an interpreter ExecutionEngine.
Definition: Interpreter.cpp:34
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::Function
Definition: Function.h:61
Interpreter.h
llvm::IntrinsicLowering
Definition: IntrinsicLowering.h:24
llvm::Interpreter::runAtExitHandlers
void runAtExitHandlers()
runAtExitHandlers - Run any functions registered by the program's calls to atexit(3),...
Definition: Interpreter.cpp:70
Module.h
llvm::Interpreter::run
void run()
Definition: Execution.cpp:2156
llvm::Interpreter::callFunction
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: Execution.cpp:2119
IntrinsicLowering.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::GenericValue::Untyped
unsigned char Untyped[8]
Definition: GenericValue.h:33
llvm::Interpreter::~Interpreter
~Interpreter() override
Definition: Interpreter.cpp:66
llvm::GenericValue
Definition: GenericValue.h:23
llvm::Interpreter::runFunction
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
run - Start execution with the specified function and arguments.
Definition: Interpreter.cpp:80
llvm::ArrayRef::slice
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
llvm::None
const NoneType None
Definition: None.h:23
llvm::ErrorInfoBase
Base class for error info classes.
Definition: Error.h:48
llvm::ErrorInfoBase::message
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:56
llvm::ExecutionEngine::getDataLayout
const DataLayout & getDataLayout() const
Definition: ExecutionEngine.h:196
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1605
llvm::Interpreter::Register
static void Register()
Definition: Interpreter.h:95
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
LLVMLinkInInterpreter
void LLVMLinkInInterpreter()
Definition: Interpreter.cpp:30
llvm::ExecutionEngine
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
Definition: ExecutionEngine.h:99
std
Definition: BitVector.h:838
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::ExecutionEngine::emitGlobals
void emitGlobals()
EmitGlobals - Emit all of the global variables to memory, storing their addresses into GlobalAddress.
Definition: ExecutionEngine.cpp:1188
llvm::Interpreter::Interpreter
Interpreter(std::unique_ptr< Module > M)
Definition: Interpreter.cpp:54
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
DerivedTypes.h
llvm::handleAllErrors
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:958