blob: e249a5eeb98e71d68bff15a493807ca2f6381da6 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
John Criswell856ba762003-10-21 15:17:13 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00009//
10// This header file defines the interpreter structure
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLI_INTERPRETER_H
15#define LLI_INTERPRETER_H
16
Chris Lattnerfd131292003-09-05 20:08:15 +000017#include "llvm/BasicBlock.h"
18#include "llvm/Assembly/CachedWriter.h"
Brian Gaeke97222942003-09-05 19:39:22 +000019#include "llvm/ExecutionEngine/ExecutionEngine.h"
20#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerd7916e92003-05-10 21:22:39 +000021#include "llvm/Support/InstVisitor.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000022#include "llvm/Target/TargetData.h"
23#include "Support/DataTypes.h"
Chris Lattner5af0c482001-11-07 04:23:00 +000024
Chris Lattnerfe11a972002-12-23 23:59:41 +000025extern CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner92101ac2001-08-23 17:05:04 +000026
Chris Lattnerda82ed52003-05-08 16:18:31 +000027struct FunctionInfo; // Defined in ExecutionAnnotations.h
Chris Lattner92101ac2001-08-23 17:05:04 +000028
Chris Lattner9bffa732002-02-19 18:50:09 +000029// AllocaHolder - Object to track all of the blocks of memory allocated by
30// alloca. When the function returns, this object is poped off the execution
31// stack, which causes the dtor to be run, which frees all the alloca'd memory.
32//
33class AllocaHolder {
34 friend class AllocaHolderHandle;
35 std::vector<void*> Allocations;
36 unsigned RefCnt;
37public:
38 AllocaHolder() : RefCnt(0) {}
39 void add(void *mem) { Allocations.push_back(mem); }
40 ~AllocaHolder() {
41 for (unsigned i = 0; i < Allocations.size(); ++i)
42 free(Allocations[i]);
43 }
44};
45
46// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
47// a vector...
48//
49class AllocaHolderHandle {
50 AllocaHolder *H;
51public:
52 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
53 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
54 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
55
56 void add(void *mem) { H->add(mem); }
57};
58
Chris Lattner697954c2002-01-20 22:54:45 +000059typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattner92101ac2001-08-23 17:05:04 +000060
61// ExecutionContext struct - This struct represents one stack frame currently
62// executing.
63//
64struct ExecutionContext {
Chris Lattnerda82ed52003-05-08 16:18:31 +000065 Function *CurFunction;// The currently executing function
Chris Lattner92101ac2001-08-23 17:05:04 +000066 BasicBlock *CurBB; // The currently executing BB
67 BasicBlock::iterator CurInst; // The next instruction to execute
Chris Lattnerda82ed52003-05-08 16:18:31 +000068 FunctionInfo *FuncInfo; // The FuncInfo annotation for the function
Chris Lattner697954c2002-01-20 22:54:45 +000069 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
Chris Lattnercdf51782003-05-08 16:06:52 +000070 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
Chris Lattner92101ac2001-08-23 17:05:04 +000071
Chris Lattner92101ac2001-08-23 17:05:04 +000072 CallInst *Caller; // Holds the call that called subframes.
73 // NULL if main func or debugger invoked fn
Chris Lattner9bffa732002-02-19 18:50:09 +000074 AllocaHolderHandle Allocas; // Track memory allocated by alloca
Chris Lattner92101ac2001-08-23 17:05:04 +000075};
76
Chris Lattner92101ac2001-08-23 17:05:04 +000077// Interpreter - This class represents the entirety of the interpreter.
78//
Chris Lattnerd7916e92003-05-10 21:22:39 +000079class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
Chris Lattner92101ac2001-08-23 17:05:04 +000080 int ExitCode; // The exit code to be returned by the lli util
Chris Lattner43e3f7c2001-10-27 08:43:52 +000081 bool Trace; // Tracing enabled?
Chris Lattner92101ac2001-08-23 17:05:04 +000082 int CurFrame; // The current stack frame being inspected
Chris Lattnerfe11a972002-12-23 23:59:41 +000083 TargetData TD;
Chris Lattner92101ac2001-08-23 17:05:04 +000084
85 // The runtime stack of executing code. The top of the stack is the current
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000086 // function record.
Chris Lattner697954c2002-01-20 22:54:45 +000087 std::vector<ExecutionContext> ECStack;
Chris Lattner92101ac2001-08-23 17:05:04 +000088
Brian Gaeke70975ee2003-09-05 18:42:01 +000089 // AtExitHandlers - List of functions to call when the program exits,
90 // registered with the atexit() library function.
Chris Lattner44edb6b2003-05-14 14:21:30 +000091 std::vector<Function*> AtExitHandlers;
Chris Lattner63bd6132003-09-17 17:26:22 +000092
93 std::map<Function*, FunctionInfo*> FunctionInfoMap;
Chris Lattner92101ac2001-08-23 17:05:04 +000094public:
Chris Lattner39c07262003-08-24 19:50:53 +000095 Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
Brian Gaekef58815e2003-09-04 22:21:24 +000096 bool TraceMode);
Chris Lattnerfe11a972002-12-23 23:59:41 +000097 inline ~Interpreter() { CW.setModule(0); }
Chris Lattner92101ac2001-08-23 17:05:04 +000098
Brian Gaeke70975ee2003-09-05 18:42:01 +000099 /// runAtExitHandlers - Run any functions registered by the
100 /// program's calls to atexit(3), which we intercept and store in
101 /// AtExitHandlers.
102 ///
103 void runAtExitHandlers ();
104
Brian Gaeke82d82772003-09-03 20:34:19 +0000105 /// create - Create an interpreter ExecutionEngine. This can never fail.
106 ///
Brian Gaekef58815e2003-09-04 22:21:24 +0000107 static ExecutionEngine *create(Module *M, bool TraceMode);
Brian Gaeke82d82772003-09-03 20:34:19 +0000108
Chris Lattnerfe11a972002-12-23 23:59:41 +0000109 /// run - Start execution with the specified function and arguments.
110 ///
Brian Gaeke70975ee2003-09-05 18:42:01 +0000111 virtual GenericValue run(Function *F,
112 const std::vector<GenericValue> &ArgValues);
Chris Lattner92101ac2001-08-23 17:05:04 +0000113
Brian Gaeke70975ee2003-09-05 18:42:01 +0000114 // Methods used for debug printouts:
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000115 static void print(const Type *Ty, GenericValue V);
Chris Lattner365a76e2001-09-10 04:49:44 +0000116 static void printValue(const Type *Ty, GenericValue V);
Chris Lattner92101ac2001-08-23 17:05:04 +0000117
Brian Gaeke70975ee2003-09-05 18:42:01 +0000118 // Methods used to execute code:
119 // Place a call on the stack
Chris Lattnerda82ed52003-05-08 16:18:31 +0000120 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
Brian Gaeke70975ee2003-09-05 18:42:01 +0000121 void executeInstruction(); // Execute one instruction
122 void run(); // Execute instructions until nothing left to do
Chris Lattner92101ac2001-08-23 17:05:04 +0000123
124 // Opcode Implementations
Chris Lattnerd7916e92003-05-10 21:22:39 +0000125 void visitReturnInst(ReturnInst &I);
126 void visitBranchInst(BranchInst &I);
127 void visitSwitchInst(SwitchInst &I);
128
129 void visitBinaryOperator(BinaryOperator &I);
130 void visitAllocationInst(AllocationInst &I);
131 void visitFreeInst(FreeInst &I);
132 void visitLoadInst(LoadInst &I);
133 void visitStoreInst(StoreInst &I);
134 void visitGetElementPtrInst(GetElementPtrInst &I);
135
136 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
137 void visitCastInst(CastInst &I);
138 void visitCallInst(CallInst &I);
139 void visitShl(ShiftInst &I);
140 void visitShr(ShiftInst &I);
Chris Lattner4c665492003-10-18 05:55:25 +0000141 void visitVANextInst(VANextInst &I);
Chris Lattnerd7916e92003-05-10 21:22:39 +0000142 void visitInstruction(Instruction &I) {
143 std::cerr << I;
144 assert(0 && "Instruction not interpretable yet!");
145 }
146
Chris Lattnerda82ed52003-05-08 16:18:31 +0000147 GenericValue callExternalFunction(Function *F,
148 const std::vector<GenericValue> &ArgVals);
Chris Lattnere43db882001-10-27 04:15:57 +0000149 void exitCalled(GenericValue GV);
Chris Lattner92101ac2001-08-23 17:05:04 +0000150
Chris Lattner44edb6b2003-05-14 14:21:30 +0000151 void addAtExitHandler(Function *F) {
152 AtExitHandlers.push_back(F);
153 }
154
Chris Lattnerfe11a972002-12-23 23:59:41 +0000155 //FIXME: private:
156public:
157 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
158 User::op_iterator E, ExecutionContext &SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000159
Chris Lattner92101ac2001-08-23 17:05:04 +0000160private: // Helper functions
Chris Lattner77113b62003-05-10 20:21:16 +0000161 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
162 // PHI nodes in the top of the block. This is used for intraprocedural
163 // control flow.
164 //
165 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
166
Brian Gaekefb0ef2e2003-08-13 18:17:54 +0000167 void *getPointerToFunction(Function *F) { return (void*)F; }
Chris Lattnerfe11a972002-12-23 23:59:41 +0000168
Chris Lattner5deea3c2001-10-30 20:28:23 +0000169 void initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000170 void initializeExternalFunctions();
Brian Gaeke29794cb2003-09-05 18:55:03 +0000171 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
172 GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
173 ExecutionContext &SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000174};
175
176#endif