LLVM  4.0.0
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
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:137
unsigned char Untyped[8]
Definition: GenericValue.h:37
Base class for error info classes.
Definition: Support/Error.h:46
const DataLayout & getDataLayout() const
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:171
#define F(x, y, z)
Definition: MD5.cpp:51
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: Execution.cpp:2075
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...
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
void handleAllErrors(Error E, HandlerTs &&...Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
~Interpreter() override
Definition: Interpreter.cpp:67
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
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
void runAtExitHandlers()
runAtExitHandlers - Run any functions registered by the program's calls to atexit(3), which we intercept and store in AtExitHandlers.
Definition: Interpreter.cpp:71
void LLVMLinkInInterpreter()
Definition: Interpreter.cpp:31
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.cpp:230
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
virtual std::string message() const
Return the error message as a string.
Definition: Support/Error.h:54