Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / ExecutionEngine / Interpreter / Interpreter.h
blob1c169b5ed196badb87d84d02c2aecf5ed12d7c37
1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header file defines the interpreter structure
12 //===----------------------------------------------------------------------===//
14 #ifndef LLI_INTERPRETER_H
15 #define LLI_INTERPRETER_H
17 #include "llvm/Function.h"
18 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/GenericValue.h"
20 #include "llvm/Support/InstVisitor.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Support/DataTypes.h"
24 #include <iostream>
26 namespace llvm {
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;
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.
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); }
46 ~AllocaHolder() {
47 for (unsigned i = 0; i < Allocations.size(); ++i)
48 free(Allocations[i]);
52 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
53 // a vector...
55 class AllocaHolderHandle {
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; }
62 void add(void *mem) { H->add(mem); }
65 typedef std::vector<GenericValue> ValuePlaneTy;
67 // ExecutionContext struct - This struct represents one stack frame currently
68 // executing.
70 struct 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
81 // Interpreter - This class represents the entirety of the interpreter.
83 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
84 GenericValue ExitValue; // The return value of the called function
85 TargetData TD;
86 IntrinsicLowering *IL;
88 // The runtime stack of executing code. The top of the stack is the current
89 // function record.
90 std::vector<ExecutionContext> ECStack;
92 // AtExitHandlers - List of functions to call when the program exits,
93 // registered with the atexit() library function.
94 std::vector<Function*> AtExitHandlers;
96 public:
97 Interpreter(Module *M);
98 ~Interpreter();
100 /// runAtExitHandlers - Run any functions registered by the program's calls to
101 /// atexit(3), which we intercept and store in AtExitHandlers.
103 void runAtExitHandlers();
105 static void Register() {
106 InterpCtor = create;
109 /// create - Create an interpreter ExecutionEngine. This can never fail.
111 static ExecutionEngine *create(ModuleProvider *M);
113 /// run - Start execution with the specified function and arguments.
115 virtual GenericValue runFunction(Function *F,
116 const std::vector<GenericValue> &ArgValues);
118 /// recompileAndRelinkFunction - For the interpreter, functions are always
119 /// up-to-date.
121 virtual void *recompileAndRelinkFunction(Function *F) {
122 return getPointerToFunction(F);
125 /// freeMachineCodeForFunction - The interpreter does not generate any code.
127 void freeMachineCodeForFunction(Function *F) { }
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
134 // Opcode Implementations
135 void visitReturnInst(ReturnInst &I);
136 void visitBranchInst(BranchInst &I);
137 void visitSwitchInst(SwitchInst &I);
139 void visitBinaryOperator(BinaryOperator &I);
140 void visitAllocationInst(AllocationInst &I);
141 void visitFreeInst(FreeInst &I);
142 void visitLoadInst(LoadInst &I);
143 void visitStoreInst(StoreInst &I);
144 void visitGetElementPtrInst(GetElementPtrInst &I);
145 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
146 void visitCastInst(CastInst &I);
147 void visitSelectInst(SelectInst &I);
150 void visitCallSite(CallSite CS);
151 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
152 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
153 void visitUnwindInst(UnwindInst &I);
154 void visitUnreachableInst(UnreachableInst &I);
156 void visitShl(ShiftInst &I);
157 void visitShr(ShiftInst &I);
158 void visitVAArgInst(VAArgInst &I);
159 void visitInstruction(Instruction &I) {
160 std::cerr << I;
161 assert(0 && "Instruction not interpretable yet!");
164 GenericValue callExternalFunction(Function *F,
165 const std::vector<GenericValue> &ArgVals);
166 void exitCalled(GenericValue GV);
168 void addAtExitHandler(Function *F) {
169 AtExitHandlers.push_back(F);
172 GenericValue *getFirstVarArg () {
173 return &(ECStack.back ().VarArgs[0]);
176 //FIXME: private:
177 public:
178 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
179 gep_type_iterator E, ExecutionContext &SF);
181 private: // Helper functions
182 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
183 // PHI nodes in the top of the block. This is used for intraprocedural
184 // control flow.
186 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
188 void *getPointerToFunction(Function *F) { return (void*)F; }
190 void initializeExecutionEngine();
191 void initializeExternalFunctions();
192 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
193 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
194 GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
195 ExecutionContext &SF);
196 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
199 } // End llvm namespace
201 #endif