LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lib/ExecutionEngine/Interpreter/Interpreter.h
Go to the documentation of this file.
1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
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 header file defines the interpreter structure
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLI_INTERPRETER_H
15 #define LLI_INTERPRETER_H
16 
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/InstVisitor.h"
22 #include "llvm/Support/CallSite.h"
23 #include "llvm/Support/DataTypes.h"
26 namespace llvm {
27 
28 class IntrinsicLowering;
29 struct FunctionInfo;
30 template<typename T> class generic_gep_type_iterator;
31 class ConstantExpr;
32 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
33 
34 
35 // AllocaHolder - Object to track all of the blocks of memory allocated by
36 // alloca. When the function returns, this object is popped off the execution
37 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
38 //
39 class AllocaHolder {
40  friend class AllocaHolderHandle;
41  std::vector<void*> Allocations;
42  unsigned RefCnt;
43 public:
44  AllocaHolder() : RefCnt(0) {}
45  void add(void *mem) { Allocations.push_back(mem); }
47  for (unsigned i = 0; i < Allocations.size(); ++i)
48  free(Allocations[i]);
49  }
50 };
51 
52 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
53 // a vector...
54 //
56  AllocaHolder *H;
57 public:
58  AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
59  AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
60  ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
61 
62  void add(void *mem) { H->add(mem); }
63 };
64 
65 typedef std::vector<GenericValue> ValuePlaneTy;
66 
67 // ExecutionContext struct - This struct represents one stack frame currently
68 // executing.
69 //
71  Function *CurFunction;// The currently executing function
72  BasicBlock *CurBB; // The currently executing BB
73  BasicBlock::iterator CurInst; // The next instruction to execute
74  std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
75  std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
76  CallSite Caller; // Holds the call that called subframes.
77  // NULL if main func or debugger invoked fn
78  AllocaHolderHandle Allocas; // Track memory allocated by alloca
79 };
80 
81 // Interpreter - This class represents the entirety of the interpreter.
82 //
83 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
84  GenericValue ExitValue; // The return value of the called function
85  DataLayout TD;
87 
88  // The runtime stack of executing code. The top of the stack is the current
89  // function record.
90  std::vector<ExecutionContext> ECStack;
91 
92  // AtExitHandlers - List of functions to call when the program exits,
93  // registered with the atexit() library function.
94  std::vector<Function*> AtExitHandlers;
95 
96 public:
97  explicit Interpreter(Module *M);
98  ~Interpreter();
99 
100  /// runAtExitHandlers - Run any functions registered by the program's calls to
101  /// atexit(3), which we intercept and store in AtExitHandlers.
102  ///
103  void runAtExitHandlers();
104 
105  static void Register() {
106  InterpCtor = create;
107  }
108 
109  /// create - Create an interpreter ExecutionEngine. This can never fail.
110  ///
111  static ExecutionEngine *create(Module *M, std::string *ErrorStr = 0);
112 
113  /// run - Start execution with the specified function and arguments.
114  ///
116  const std::vector<GenericValue> &ArgValues);
117 
118  virtual void *getPointerToNamedFunction(const std::string &Name,
119  bool AbortOnFailure = true) {
120  // FIXME: not implemented.
121  return 0;
122  }
123 
124  /// recompileAndRelinkFunction - For the interpreter, functions are always
125  /// up-to-date.
126  ///
128  return getPointerToFunction(F);
129  }
130 
131  /// freeMachineCodeForFunction - The interpreter does not generate any code.
132  ///
134 
135  // Methods used to execute code:
136  // Place a call on the stack
137  void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
138  void run(); // Execute instructions until nothing left to do
139 
140  // Opcode Implementations
145 
147  void visitICmpInst(ICmpInst &I);
148  void visitFCmpInst(FCmpInst &I);
150  void visitLoadInst(LoadInst &I);
151  void visitStoreInst(StoreInst &I);
153  void visitPHINode(PHINode &PN) {
154  llvm_unreachable("PHI nodes already handled!");
155  }
156  void visitTruncInst(TruncInst &I);
157  void visitZExtInst(ZExtInst &I);
158  void visitSExtInst(SExtInst &I);
160  void visitFPExtInst(FPExtInst &I);
169 
170 
171  void visitCallSite(CallSite CS);
175 
176  void visitShl(BinaryOperator &I);
177  void visitLShr(BinaryOperator &I);
178  void visitAShr(BinaryOperator &I);
179 
180  void visitVAArgInst(VAArgInst &I);
184 
187 
189  errs() << I << "\n";
190  llvm_unreachable("Instruction not interpretable yet!");
191  }
192 
194  const std::vector<GenericValue> &ArgVals);
195  void exitCalled(GenericValue GV);
196 
198  AtExitHandlers.push_back(F);
199  }
200 
202  return &(ECStack.back ().VarArgs[0]);
203  }
204 
205 private: // Helper functions
206  GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
208 
209  // SwitchToNewBasicBlock - Start execution in a new basic block and run any
210  // PHI nodes in the top of the block. This is used for intraprocedural
211  // control flow.
212  //
213  void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
214 
215  void *getPointerToFunction(Function *F) { return (void*)F; }
216  void *getPointerToBasicBlock(BasicBlock *BB) { return (void*)BB; }
217 
218  void initializeExecutionEngine() { }
219  void initializeExternalFunctions();
220  GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
221  GenericValue getOperandValue(Value *V, ExecutionContext &SF);
222  GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
223  ExecutionContext &SF);
224  GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
225  ExecutionContext &SF);
226  GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
227  ExecutionContext &SF);
228  GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
229  ExecutionContext &SF);
230  GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
231  ExecutionContext &SF);
232  GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
233  ExecutionContext &SF);
234  GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
235  ExecutionContext &SF);
236  GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
237  ExecutionContext &SF);
238  GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
239  ExecutionContext &SF);
240  GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
241  ExecutionContext &SF);
242  GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
243  ExecutionContext &SF);
244  GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
245  ExecutionContext &SF);
246  GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
247  Type *Ty, ExecutionContext &SF);
248  void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
249 
250 };
251 
252 } // End llvm namespace
253 
254 #endif
virtual GenericValue runFunction(Function *F, const std::vector< GenericValue > &ArgValues)
Definition: Interpreter.cpp:75
void visitVAArgInst(VAArgInst &I)
Definition: Execution.cpp:1716
COFF::RelocationTypeX86 Type
Definition: COFFYAML.cpp:227
generic_gep_type_iterator gep_type_iterator
std::vector< GenericValue > ValuePlaneTy
raw_ostream & errs()
Base class for instruction visitors.
Definition: InstVisitor.h:81
GenericValue callExternalFunction(Function *F, const std::vector< GenericValue > &ArgVals)
Various leaf nodes.
Definition: ISDOpcodes.h:60
void visitAllocaInst(AllocaInst &I)
Definition: Execution.cpp:960
void visitStoreInst(StoreInst &I)
Definition: Execution.cpp:1048
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
void visitTruncInst(TruncInst &I)
Definition: Execution.cpp:1653
void visitFPToUIInst(FPToUIInst &I)
Definition: Execution.cpp:1688
This class represents zero extension of integer types.
void visitGetElementPtrInst(GetElementPtrInst &I)
Definition: Execution.cpp:1031
std::map< Value *, GenericValue > Values
void visitExtractElementInst(ExtractElementInst &I)
Definition: Execution.cpp:1745
void visitShl(BinaryOperator &I)
Definition: Execution.cpp:1135
F(f)
This class represents a sign extension of integer types.
static ExecutionEngine *(* InterpCtor)(Module *M, std::string *ErrorStr)
void visitFCmpInst(FCmpInst &I)
Definition: Execution.cpp:608
#define llvm_unreachable(msg)
void visitSelectInst(SelectInst &I)
Definition: Execution.cpp:804
void callFunction(Function *F, const std::vector< GenericValue > &ArgVals)
Definition: Execution.cpp:2074
This class represents a cast from a pointer to an integer.
void visitInsertElementInst(InsertElementInst &I)
Definition: Execution.cpp:1778
void visitFPTruncInst(FPTruncInst &I)
Definition: Execution.cpp:1668
Represents a floating point comparison operator.
This class represents a no-op cast from one type to another.
void visitInsertValueInst(InsertValueInst &I)
Definition: Execution.cpp:1925
This class represents a cast from floating point to signed integer.
void visitBitCastInst(BitCastInst &I)
Definition: Execution.cpp:1708
This class represents a truncation of integer types.
void visitLShr(BinaryOperator &I)
Definition: Execution.cpp:1162
void visitICmpInst(ICmpInst &I)
Definition: Execution.cpp:276
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
void visitCallSite(CallSite CS)
Definition: Execution.cpp:1062
virtual void * recompileAndRelinkFunction(Function *F)
virtual void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true)
void visitIndirectBrInst(IndirectBrInst &I)
Definition: Execution.cpp:911
void free(void *ptr);
void visitZExtInst(ZExtInst &I)
Definition: Execution.cpp:1663
void exitCalled(GenericValue GV)
Definition: Execution.cpp:818
Represent an integer comparison operator.
Definition: Instructions.h:911
void visitExtractValueInst(ExtractValueInst &I)
Definition: Execution.cpp:1883
This class represents a cast from an integer to a pointer.
void visitFPToSIInst(FPToSIInst &I)
Definition: Execution.cpp:1693
void visitIntToPtrInst(IntToPtrInst &I)
Definition: Execution.cpp:1703
void visitFPExtInst(FPExtInst &I)
Definition: Execution.cpp:1673
void visitUnreachableInst(UnreachableInst &I)
Definition: Execution.cpp:875
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
static ExecutionEngine * create(Module *M, std::string *ErrorStr=0)
Definition: Interpreter.cpp:35
void visitShuffleVectorInst(ShuffleVectorInst &I)
Definition: Execution.cpp:1813
Interpreter(Module *M)
Definition: Interpreter.cpp:47
void visitReturnInst(ReturnInst &I)
Definition: Execution.cpp:861
void visitSExtInst(SExtInst &I)
Definition: Execution.cpp:1658
This class represents a cast from floating point to unsigned integer.
void visitPtrToIntInst(PtrToIntInst &I)
Definition: Execution.cpp:1698
void runAtExitHandlers()
Definition: Interpreter.cpp:64
void visitBranchInst(BranchInst &I)
Definition: Execution.cpp:879
void visitAShr(BinaryOperator &I)
Definition: Execution.cpp:1189
#define I(x, y, z)
Definition: MD5.cpp:54
void visitSIToFPInst(SIToFPInst &I)
Definition: Execution.cpp:1683
This class represents a cast unsigned integer to floating point.
void visitBinaryOperator(BinaryOperator &I)
Definition: Execution.cpp:679
void visitLoadInst(LoadInst &I)
Definition: Execution.cpp:1037
This class represents a cast from signed integer to floating point.
This class represents a truncation of floating point types.
LLVM Value Representation.
Definition: Value.h:66
void visitUIToFPInst(UIToFPInst &I)
Definition: Execution.cpp:1678
void visitSwitchInst(SwitchInst &I)
Definition: Execution.cpp:892
This class represents an extension of floating point types.