blob: 3007b0adb2a92ef90d4de9e52c92df47db9b2536 [file] [log] [blame]
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// 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.
Misha Brukman91fb9ab2005-04-21 22:43:08 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-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 Lattnerb5b9ba62003-11-05 06:20:27 +000017#include "llvm/Function.h"
Brian Gaekef3a300d2003-09-05 19:39:22 +000018#include "llvm/ExecutionEngine/ExecutionEngine.h"
19#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattner185045c2003-05-10 21:22:39 +000020#include "llvm/Support/InstVisitor.h"
Brian Gaeke18b59572003-11-07 19:26:23 +000021#include "llvm/Support/CallSite.h"
Chris Lattnerad481312003-09-05 20:08:15 +000022#include "llvm/Target/TargetData.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/DataTypes.h"
Chris Lattner1f017262001-11-07 04:23:00 +000024
Brian Gaeke960707c2003-11-11 22:41:34 +000025namespace llvm {
26
Chris Lattner0b2de9f2006-03-23 05:22:51 +000027class IntrinsicLowering;
Chris Lattner234a2d42004-02-26 07:59:22 +000028struct FunctionInfo;
Chris Lattner092d2602004-04-04 17:30:06 +000029template<typename T> class generic_gep_type_iterator;
Brian Gaeke2bba1522003-12-11 00:23:28 +000030class ConstantExpr;
Chris Lattnerca76d112004-04-04 19:47:06 +000031typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
Chris Lattner092d2602004-04-04 17:30:06 +000032
Chris Lattnerd7ff5782001-08-23 17:05:04 +000033
Chris Lattnerfb55ba02002-02-19 18:50:09 +000034// AllocaHolder - Object to track all of the blocks of memory allocated by
Brian Gaeke2bba1522003-12-11 00:23:28 +000035// alloca. When the function returns, this object is popped off the execution
Chris Lattnerfb55ba02002-02-19 18:50:09 +000036// stack, which causes the dtor to be run, which frees all the alloca'd memory.
37//
38class AllocaHolder {
39 friend class AllocaHolderHandle;
40 std::vector<void*> Allocations;
41 unsigned RefCnt;
42public:
43 AllocaHolder() : RefCnt(0) {}
44 void add(void *mem) { Allocations.push_back(mem); }
45 ~AllocaHolder() {
46 for (unsigned i = 0; i < Allocations.size(); ++i)
47 free(Allocations[i]);
48 }
49};
50
51// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
52// a vector...
53//
54class AllocaHolderHandle {
55 AllocaHolder *H;
56public:
57 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
58 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
59 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
60
61 void add(void *mem) { H->add(mem); }
62};
63
Chris Lattner7f74a562002-01-20 22:54:45 +000064typedef std::vector<GenericValue> ValuePlaneTy;
Chris Lattnerd7ff5782001-08-23 17:05:04 +000065
66// ExecutionContext struct - This struct represents one stack frame currently
67// executing.
68//
69struct ExecutionContext {
Chris Lattner470754e2003-05-08 16:18:31 +000070 Function *CurFunction;// The currently executing function
Chris Lattnerd7ff5782001-08-23 17:05:04 +000071 BasicBlock *CurBB; // The currently executing BB
72 BasicBlock::iterator CurInst; // The next instruction to execute
Brian Gaekeb77e5892003-10-24 19:59:37 +000073 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
Chris Lattner22e90432003-05-08 16:06:52 +000074 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
Brian Gaeke18b59572003-11-07 19:26:23 +000075 CallSite Caller; // Holds the call that called subframes.
76 // NULL if main func or debugger invoked fn
Chris Lattnerfb55ba02002-02-19 18:50:09 +000077 AllocaHolderHandle Allocas; // Track memory allocated by alloca
Chris Lattnerd7ff5782001-08-23 17:05:04 +000078};
79
Chris Lattnerd7ff5782001-08-23 17:05:04 +000080// Interpreter - This class represents the entirety of the interpreter.
81//
Chris Lattner185045c2003-05-10 21:22:39 +000082class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
Jeff Cohen24396692006-02-07 05:29:44 +000083 GenericValue ExitValue; // The return value of the called function
Chris Lattnera0d7b082002-12-23 23:59:41 +000084 TargetData TD;
Chris Lattnerc8c6c032003-12-28 09:44:37 +000085 IntrinsicLowering *IL;
Chris Lattnerd7ff5782001-08-23 17:05:04 +000086
87 // The runtime stack of executing code. The top of the stack is the current
Chris Lattner62b7fd12002-04-07 20:49:59 +000088 // function record.
Chris Lattner7f74a562002-01-20 22:54:45 +000089 std::vector<ExecutionContext> ECStack;
Chris Lattnerd7ff5782001-08-23 17:05:04 +000090
Brian Gaekea7669032003-09-05 18:42:01 +000091 // AtExitHandlers - List of functions to call when the program exits,
92 // registered with the atexit() library function.
Chris Lattner4a5bb952003-05-14 14:21:30 +000093 std::vector<Function*> AtExitHandlers;
Chris Lattnera28e1f22003-09-17 17:26:22 +000094
Chris Lattnerd7ff5782001-08-23 17:05:04 +000095public:
Chris Lattner91f228b2006-06-16 18:08:38 +000096 Interpreter(Module *M);
Chris Lattnerc8c6c032003-12-28 09:44:37 +000097 ~Interpreter();
Chris Lattnerd7ff5782001-08-23 17:05:04 +000098
Chris Lattnerd94296c2003-12-26 06:13:05 +000099 /// runAtExitHandlers - Run any functions registered by the program's calls to
100 /// atexit(3), which we intercept and store in AtExitHandlers.
Brian Gaekea7669032003-09-05 18:42:01 +0000101 ///
Chris Lattnerd94296c2003-12-26 06:13:05 +0000102 void runAtExitHandlers();
Brian Gaekea7669032003-09-05 18:42:01 +0000103
Chris Lattner2d52c1b2006-03-22 06:07:50 +0000104 static void Register() {
105 InterpCtor = create;
106 }
107
Chris Lattner0b2de9f2006-03-23 05:22:51 +0000108 /// create - Create an interpreter ExecutionEngine. This can never fail.
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000109 ///
Chris Lattner0b2de9f2006-03-23 05:22:51 +0000110 static ExecutionEngine *create(ModuleProvider *M);
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000111
Chris Lattnera0d7b082002-12-23 23:59:41 +0000112 /// run - Start execution with the specified function and arguments.
113 ///
Chris Lattnerd94296c2003-12-26 06:13:05 +0000114 virtual GenericValue runFunction(Function *F,
115 const std::vector<GenericValue> &ArgValues);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000116
Chris Lattner86ad4c02003-12-08 08:23:04 +0000117 /// recompileAndRelinkFunction - For the interpreter, functions are always
118 /// up-to-date.
119 ///
120 virtual void *recompileAndRelinkFunction(Function *F) {
121 return getPointerToFunction(F);
122 }
123
Misha Brukman624685d2004-11-07 23:58:46 +0000124 /// freeMachineCodeForFunction - The interpreter does not generate any code.
125 ///
126 void freeMachineCodeForFunction(Function *F) { }
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000127
Brian Gaekea7669032003-09-05 18:42:01 +0000128 // Methods used to execute code:
129 // Place a call on the stack
Chris Lattner470754e2003-05-08 16:18:31 +0000130 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
Brian Gaekea7669032003-09-05 18:42:01 +0000131 void run(); // Execute instructions until nothing left to do
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000132
133 // Opcode Implementations
Chris Lattner185045c2003-05-10 21:22:39 +0000134 void visitReturnInst(ReturnInst &I);
135 void visitBranchInst(BranchInst &I);
136 void visitSwitchInst(SwitchInst &I);
137
138 void visitBinaryOperator(BinaryOperator &I);
Reid Spencer266e42b2006-12-23 06:05:41 +0000139 void visitICmpInst(ICmpInst &I);
140 void visitFCmpInst(FCmpInst &I);
Chris Lattner185045c2003-05-10 21:22:39 +0000141 void visitAllocationInst(AllocationInst &I);
142 void visitFreeInst(FreeInst &I);
143 void visitLoadInst(LoadInst &I);
144 void visitStoreInst(StoreInst &I);
145 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner185045c2003-05-10 21:22:39 +0000146 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000147 void visitTruncInst(TruncInst &I);
148 void visitZExtInst(ZExtInst &I);
149 void visitSExtInst(SExtInst &I);
150 void visitFPTruncInst(FPTruncInst &I);
151 void visitFPExtInst(FPExtInst &I);
152 void visitUIToFPInst(UIToFPInst &I);
153 void visitSIToFPInst(SIToFPInst &I);
154 void visitFPToUIInst(FPToUIInst &I);
155 void visitFPToSIInst(FPToSIInst &I);
156 void visitPtrToIntInst(PtrToIntInst &I);
157 void visitIntToPtrInst(IntToPtrInst &I);
158 void visitBitCastInst(BitCastInst &I);
Chris Lattner2b2d7a92004-04-20 16:43:21 +0000159 void visitSelectInst(SelectInst &I);
160
Brian Gaekea6454d352003-11-07 20:04:22 +0000161
162 void visitCallSite(CallSite CS);
163 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
164 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
Brian Gaeke6d145eb2003-11-07 20:07:06 +0000165 void visitUnwindInst(UnwindInst &I);
Chris Lattner98e54142004-10-16 18:21:33 +0000166 void visitUnreachableInst(UnreachableInst &I);
Brian Gaekea6454d352003-11-07 20:04:22 +0000167
Chris Lattner185045c2003-05-10 21:22:39 +0000168 void visitShl(ShiftInst &I);
Reid Spencerfdff9382006-11-08 06:47:33 +0000169 void visitLShr(ShiftInst &I);
170 void visitAShr(ShiftInst &I);
Brian Gaeke9ef636c2003-11-07 21:20:47 +0000171 void visitVAArgInst(VAArgInst &I);
Chris Lattner185045c2003-05-10 21:22:39 +0000172 void visitInstruction(Instruction &I) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000173 cerr << I;
Chris Lattner185045c2003-05-10 21:22:39 +0000174 assert(0 && "Instruction not interpretable yet!");
175 }
176
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000177 GenericValue callExternalFunction(Function *F,
Chris Lattner470754e2003-05-08 16:18:31 +0000178 const std::vector<GenericValue> &ArgVals);
Chris Lattner15157b82001-10-27 04:15:57 +0000179 void exitCalled(GenericValue GV);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000180
Chris Lattner4a5bb952003-05-14 14:21:30 +0000181 void addAtExitHandler(Function *F) {
182 AtExitHandlers.push_back(F);
183 }
184
Brian Gaeked81ca472003-11-13 06:06:01 +0000185 GenericValue *getFirstVarArg () {
Brian Gaeke242ebf22004-02-13 06:18:39 +0000186 return &(ECStack.back ().VarArgs[0]);
Brian Gaeked81ca472003-11-13 06:06:01 +0000187 }
188
Chris Lattnera0d7b082002-12-23 23:59:41 +0000189 //FIXME: private:
190public:
Chris Lattnerf0788082003-11-25 20:44:56 +0000191 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukman5191b4b2005-04-22 04:08:30 +0000192 gep_type_iterator E, ExecutionContext &SF);
Chris Lattnera0d7b082002-12-23 23:59:41 +0000193
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000194private: // Helper functions
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000195 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
196 // PHI nodes in the top of the block. This is used for intraprocedural
197 // control flow.
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000198 //
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000199 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
200
Brian Gaekeaa586642003-08-13 18:17:54 +0000201 void *getPointerToFunction(Function *F) { return (void*)F; }
Chris Lattnera0d7b082002-12-23 23:59:41 +0000202
Chris Lattner8bd5c772001-10-30 20:28:23 +0000203 void initializeExecutionEngine();
Chris Lattner470754e2003-05-08 16:18:31 +0000204 void initializeExternalFunctions();
Brian Gaeke2bba1522003-12-11 00:23:28 +0000205 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
Brian Gaeke9b518bc2003-09-05 18:55:03 +0000206 GenericValue getOperandValue(Value *V, ExecutionContext &SF);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000207 GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy,
208 ExecutionContext &SF);
209 GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy,
210 ExecutionContext &SF);
211 GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy,
212 ExecutionContext &SF);
213 GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy,
214 ExecutionContext &SF);
215 GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy,
216 ExecutionContext &SF);
217 GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy,
218 ExecutionContext &SF);
219 GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy,
220 ExecutionContext &SF);
221 GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy,
222 ExecutionContext &SF);
223 GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy,
224 ExecutionContext &SF);
225 GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy,
226 ExecutionContext &SF);
227 GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
228 ExecutionContext &SF);
229 GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy,
230 ExecutionContext &SF);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000231 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
232 const Type *Ty, ExecutionContext &SF);
Brian Gaeke65cac902003-11-07 05:22:49 +0000233 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
Reid Spencer10fe0e02007-01-18 02:12:10 +0000234
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000235};
236
Reid Spencer10fe0e02007-01-18 02:12:10 +0000237inline void maskToBitWidth(GenericValue& GV, unsigned BitWidth) {
238 uint64_t BitMask = (1ull << BitWidth) - 1;
239 if (BitWidth <= 8)
240 GV.Int8Val &= BitMask;
241 else if (BitWidth <= 16)
242 GV.Int16Val &= BitMask;
243 else if (BitWidth <= 32)
244 GV.Int32Val &= BitMask;
245 else
246 GV.Int64Val &= BitMask;
247}
Brian Gaeke960707c2003-11-11 22:41:34 +0000248} // End llvm namespace
249
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000250#endif