LLVM  8.0.1
Interpreter.cpp
Go to the documentation of this file.
1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the top-level functionality for the LLVM interpreter.
11 // This interpreter is designed to be a very simple, portable, inefficient
12 // interpreter.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "Interpreter.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Module.h"
20 #include <cstring>
21 using namespace llvm;
22 
23 namespace {
24 
25 static struct RegisterInterp {
26  RegisterInterp() { Interpreter::Register(); }
27 } InterpRegistrator;
28 
29 }
30 
31 extern "C" void LLVMLinkInInterpreter() { }
32 
33 /// Create a new interpreter object.
34 ///
35 ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
36  std::string *ErrStr) {
37  // Tell this Module to materialize everything and release the GVMaterializer.
38  if (Error Err = M->materializeAll()) {
39  std::string Msg;
40  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
41  Msg = EIB.message();
42  });
43  if (ErrStr)
44  *ErrStr = Msg;
45  // We got an error, just return 0
46  return nullptr;
47  }
48 
49  return new Interpreter(std::move(M));
50 }
51 
52 //===----------------------------------------------------------------------===//
53 // Interpreter ctor - Initialize stuff
54 //
55 Interpreter::Interpreter(std::unique_ptr<Module> M)
56  : ExecutionEngine(std::move(M)) {
57 
58  memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
59  // Initialize the "backend"
60  initializeExecutionEngine();
61  initializeExternalFunctions();
62  emitGlobals();
63 
64  IL = new IntrinsicLowering(getDataLayout());
65 }
66 
68  delete IL;
69 }
70 
72  while (!AtExitHandlers.empty()) {
73  callFunction(AtExitHandlers.back(), None);
74  AtExitHandlers.pop_back();
75  run();
76  }
77 }
78 
79 /// run - Start execution with the specified function and arguments.
80 ///
82  ArrayRef<GenericValue> ArgValues) {
83  assert (F && "Function *F was null at entry to run()");
84 
85  // Try extra hard not to pass extra args to a function that isn't
86  // expecting them. C programmers frequently bend the rules and
87  // declare main() with fewer parameters than it actually gets
88  // passed, and the interpreter barfs if you pass a function more
89  // parameters than it is declared to take. This does not attempt to
90  // take into account gratuitous differences in declared types,
91  // though.
92  const size_t ArgCount = F->getFunctionType()->getNumParams();
93  ArrayRef<GenericValue> ActualArgs =
94  ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
95 
96  // Set up the function call.
97  callFunction(F, ActualArgs);
98 
99  // Start executing the function.
100  run();
101 
102  return ExitValue;
103 }
static ExecutionEngine * create(std::unique_ptr< Module > M, std::string *ErrorStr=nullptr)
Create an interpreter ExecutionEngine.
Definition: Interpreter.cpp:35
Interpreter(std::unique_ptr< Module > M)
Definition: Interpreter.cpp:55
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned char Untyped[8]
Definition: GenericValue.h:34
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:57
F(f)
const DataLayout & getDataLayout() const
Base class for error info classes.
Definition: Error.h:49
Definition: BitVector.h:938
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: Execution.cpp:2070
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void emitGlobals()
EmitGlobals - Emit all of the global variables to memory, storing their addresses into GlobalAddress...
static void Register()
Definition: Interpreter.h:97
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
~Interpreter() override
Definition: Interpreter.cpp:67
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
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:905
Module.h This file contains the declarations for the Module class.
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
run - Start execution with the specified function and arguments.
Definition: Interpreter.cpp:81
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:164
void runAtExitHandlers()
runAtExitHandlers - Run any functions registered by the program&#39;s calls to atexit(3), which we intercept and store in AtExitHandlers.
Definition: Interpreter.cpp:71
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:179
void LLVMLinkInInterpreter()
Definition: Interpreter.cpp:31
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158