LLVM  3.7.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 (std::error_code EC = M->materializeAllPermanently()) {
39  if (ErrStr)
40  *ErrStr = EC.message();
41  // We got an error, just return 0
42  return nullptr;
43  }
44 
45  return new Interpreter(std::move(M));
46 }
47 
48 //===----------------------------------------------------------------------===//
49 // Interpreter ctor - Initialize stuff
50 //
51 Interpreter::Interpreter(std::unique_ptr<Module> M)
52  : ExecutionEngine(std::move(M)), TD(Modules.back().get()) {
53 
54  memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
55  setDataLayout(&TD);
56  // Initialize the "backend"
57  initializeExecutionEngine();
58  initializeExternalFunctions();
59  emitGlobals();
60 
61  IL = new IntrinsicLowering(TD);
62 }
63 
65  delete IL;
66 }
67 
69  while (!AtExitHandlers.empty()) {
70  callFunction(AtExitHandlers.back(), None);
71  AtExitHandlers.pop_back();
72  run();
73  }
74 }
75 
76 /// run - Start execution with the specified function and arguments.
77 ///
79  ArrayRef<GenericValue> ArgValues) {
80  assert (F && "Function *F was null at entry to run()");
81 
82  // Try extra hard not to pass extra args to a function that isn't
83  // expecting them. C programmers frequently bend the rules and
84  // declare main() with fewer parameters than it actually gets
85  // passed, and the interpreter barfs if you pass a function more
86  // parameters than it is declared to take. This does not attempt to
87  // take into account gratuitous differences in declared types,
88  // though.
89  const size_t ArgCount = F->getFunctionType()->getNumParams();
90  ArrayRef<GenericValue> ActualArgs =
91  ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
92 
93  // Set up the function call.
94  callFunction(F, ActualArgs);
95 
96  // Start executing the function.
97  run();
98 
99  return ExitValue;
100 }
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:51
unsigned getNumParams() const
getNumParams - Return the number of fixed parameters this function type requires. ...
Definition: DerivedTypes.h:136
unsigned char Untyped[8]
Definition: GenericValue.h:36
F(f)
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
ArrayRef< T > slice(unsigned N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:165
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: Execution.cpp:2076
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
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:134
void setDataLayout(const DataLayout *Val)
~Interpreter() override
Definition: Interpreter.cpp:64
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:78
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:68
void LLVMLinkInInterpreter()
Definition: Interpreter.cpp:31
FunctionType * getFunctionType() const
Definition: Function.cpp:227