blob: 43c18d0852444c6a178d19656e6f07d63f96a6e4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header file defines the interpreter structure
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLI_INTERPRETER_H
15#define LLI_INTERPRETER_H
16
17#include "llvm/Function.h"
18#include "llvm/ExecutionEngine/ExecutionEngine.h"
19#include "llvm/ExecutionEngine/GenericValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Target/TargetData.h"
Chris Lattner8a6411c2009-08-23 04:37:46 +000021#include "llvm/Support/CallSite.h"
Chandler Carruth1ec7df12009-10-26 01:35:46 +000022#include "llvm/System/DataTypes.h"
Edwin Török675d5622009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner8a6411c2009-08-23 04:37:46 +000024#include "llvm/Support/InstVisitor.h"
25#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026namespace llvm {
27
28class IntrinsicLowering;
29struct FunctionInfo;
30template<typename T> class generic_gep_type_iterator;
31class ConstantExpr;
32typedef 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//
39class AllocaHolder {
40 friend class AllocaHolderHandle;
41 std::vector<void*> Allocations;
42 unsigned RefCnt;
43public:
44 AllocaHolder() : RefCnt(0) {}
45 void add(void *mem) { Allocations.push_back(mem); }
46 ~AllocaHolder() {
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//
55class AllocaHolderHandle {
56 AllocaHolder *H;
57public:
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
65typedef std::vector<GenericValue> ValuePlaneTy;
66
67// ExecutionContext struct - This struct represents one stack frame currently
68// executing.
69//
70struct ExecutionContext {
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//
83class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
84 GenericValue ExitValue; // The return value of the called function
85 TargetData TD;
86 IntrinsicLowering *IL;
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
96public:
Chris Lattnere44be002007-12-06 01:08:09 +000097 explicit Interpreter(ModuleProvider *M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 ~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 ///
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000111 static ExecutionEngine *create(ModuleProvider *M, std::string *ErrorStr = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
113 /// run - Start execution with the specified function and arguments.
114 ///
115 virtual GenericValue runFunction(Function *F,
116 const std::vector<GenericValue> &ArgValues);
117
118 /// recompileAndRelinkFunction - For the interpreter, functions are always
119 /// up-to-date.
120 ///
121 virtual void *recompileAndRelinkFunction(Function *F) {
122 return getPointerToFunction(F);
123 }
124
125 /// freeMachineCodeForFunction - The interpreter does not generate any code.
126 ///
127 void freeMachineCodeForFunction(Function *F) { }
128
129 // Methods used to execute code:
130 // Place a call on the stack
131 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
132 void run(); // Execute instructions until nothing left to do
133
134 // Opcode Implementations
135 void visitReturnInst(ReturnInst &I);
136 void visitBranchInst(BranchInst &I);
137 void visitSwitchInst(SwitchInst &I);
138
139 void visitBinaryOperator(BinaryOperator &I);
140 void visitICmpInst(ICmpInst &I);
141 void visitFCmpInst(FCmpInst &I);
Victor Hernandezb1687302009-10-23 21:09:37 +0000142 void visitAllocaInst(AllocaInst &I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 void visitFreeInst(FreeInst &I);
144 void visitLoadInst(LoadInst &I);
145 void visitStoreInst(StoreInst &I);
146 void visitGetElementPtrInst(GetElementPtrInst &I);
Edwin Török675d5622009-07-11 20:10:48 +0000147 void visitPHINode(PHINode &PN) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000148 llvm_unreachable("PHI nodes already handled!");
Edwin Török675d5622009-07-11 20:10:48 +0000149 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 void visitTruncInst(TruncInst &I);
151 void visitZExtInst(ZExtInst &I);
152 void visitSExtInst(SExtInst &I);
153 void visitFPTruncInst(FPTruncInst &I);
154 void visitFPExtInst(FPExtInst &I);
155 void visitUIToFPInst(UIToFPInst &I);
156 void visitSIToFPInst(SIToFPInst &I);
157 void visitFPToUIInst(FPToUIInst &I);
158 void visitFPToSIInst(FPToSIInst &I);
159 void visitPtrToIntInst(PtrToIntInst &I);
160 void visitIntToPtrInst(IntToPtrInst &I);
161 void visitBitCastInst(BitCastInst &I);
162 void visitSelectInst(SelectInst &I);
163
164
165 void visitCallSite(CallSite CS);
166 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
167 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
168 void visitUnwindInst(UnwindInst &I);
169 void visitUnreachableInst(UnreachableInst &I);
170
171 void visitShl(BinaryOperator &I);
172 void visitLShr(BinaryOperator &I);
173 void visitAShr(BinaryOperator &I);
174
175 void visitVAArgInst(VAArgInst &I);
176 void visitInstruction(Instruction &I) {
Chris Lattner8a6411c2009-08-23 04:37:46 +0000177 errs() << I;
Edwin Törökbd448e32009-07-14 16:55:14 +0000178 llvm_unreachable("Instruction not interpretable yet!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 }
180
181 GenericValue callExternalFunction(Function *F,
182 const std::vector<GenericValue> &ArgVals);
183 void exitCalled(GenericValue GV);
184
185 void addAtExitHandler(Function *F) {
186 AtExitHandlers.push_back(F);
187 }
188
189 GenericValue *getFirstVarArg () {
190 return &(ECStack.back ().VarArgs[0]);
191 }
192
193 //FIXME: private:
194public:
195 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
196 gep_type_iterator E, ExecutionContext &SF);
197
198private: // Helper functions
199 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
200 // PHI nodes in the top of the block. This is used for intraprocedural
201 // control flow.
202 //
203 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
204
205 void *getPointerToFunction(Function *F) { return (void*)F; }
206
Owen Anderson0e17f632009-06-26 16:46:15 +0000207 void initializeExecutionEngine() { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 void initializeExternalFunctions();
209 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
210 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
211 GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy,
212 ExecutionContext &SF);
213 GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy,
214 ExecutionContext &SF);
215 GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy,
216 ExecutionContext &SF);
217 GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy,
218 ExecutionContext &SF);
219 GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy,
220 ExecutionContext &SF);
221 GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy,
222 ExecutionContext &SF);
223 GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy,
224 ExecutionContext &SF);
225 GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy,
226 ExecutionContext &SF);
227 GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy,
228 ExecutionContext &SF);
229 GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy,
230 ExecutionContext &SF);
231 GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
232 ExecutionContext &SF);
233 GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy,
234 ExecutionContext &SF);
235 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
236 const Type *Ty, ExecutionContext &SF);
237 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
238
239};
240
241} // End llvm namespace
242
243#endif