2001-08-24 01:05:04 +08:00
|
|
|
//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
|
2003-10-21 23:17:13 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-08-24 01:05:04 +08:00
|
|
|
//
|
|
|
|
// This header file defines the interpreter structure
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLI_INTERPRETER_H
|
|
|
|
#define LLI_INTERPRETER_H
|
|
|
|
|
2003-11-05 14:20:27 +08:00
|
|
|
#include "llvm/Function.h"
|
2003-09-06 03:39:22 +08:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2003-05-11 05:22:39 +08:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
2003-11-08 03:26:23 +08:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2003-09-06 04:08:15 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "Support/DataTypes.h"
|
2001-11-07 12:23:00 +08:00
|
|
|
|
2003-05-09 00:18:31 +08:00
|
|
|
struct FunctionInfo; // Defined in ExecutionAnnotations.h
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2002-02-20 02:50:09 +08:00
|
|
|
// AllocaHolder - Object to track all of the blocks of memory allocated by
|
|
|
|
// alloca. When the function returns, this object is poped off the execution
|
|
|
|
// stack, which causes the dtor to be run, which frees all the alloca'd memory.
|
|
|
|
//
|
|
|
|
class AllocaHolder {
|
|
|
|
friend class AllocaHolderHandle;
|
|
|
|
std::vector<void*> Allocations;
|
|
|
|
unsigned RefCnt;
|
|
|
|
public:
|
|
|
|
AllocaHolder() : RefCnt(0) {}
|
|
|
|
void add(void *mem) { Allocations.push_back(mem); }
|
|
|
|
~AllocaHolder() {
|
|
|
|
for (unsigned i = 0; i < Allocations.size(); ++i)
|
|
|
|
free(Allocations[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
|
|
|
|
// a vector...
|
|
|
|
//
|
|
|
|
class AllocaHolderHandle {
|
|
|
|
AllocaHolder *H;
|
|
|
|
public:
|
|
|
|
AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
|
|
|
|
AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
|
|
|
|
~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
|
|
|
|
|
|
|
|
void add(void *mem) { H->add(mem); }
|
|
|
|
};
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
typedef std::vector<GenericValue> ValuePlaneTy;
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
// ExecutionContext struct - This struct represents one stack frame currently
|
|
|
|
// executing.
|
|
|
|
//
|
|
|
|
struct ExecutionContext {
|
2003-05-09 00:18:31 +08:00
|
|
|
Function *CurFunction;// The currently executing function
|
2001-08-24 01:05:04 +08:00
|
|
|
BasicBlock *CurBB; // The currently executing BB
|
|
|
|
BasicBlock::iterator CurInst; // The next instruction to execute
|
2003-10-25 03:59:37 +08:00
|
|
|
std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
|
2003-05-09 00:06:52 +08:00
|
|
|
std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
|
2003-11-08 03:26:23 +08:00
|
|
|
CallSite Caller; // Holds the call that called subframes.
|
|
|
|
// NULL if main func or debugger invoked fn
|
2002-02-20 02:50:09 +08:00
|
|
|
AllocaHolderHandle Allocas; // Track memory allocated by alloca
|
2001-08-24 01:05:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Interpreter - This class represents the entirety of the interpreter.
|
|
|
|
//
|
2003-05-11 05:22:39 +08:00
|
|
|
class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
|
2001-08-24 01:05:04 +08:00
|
|
|
int ExitCode; // The exit code to be returned by the lli util
|
2002-12-24 07:59:41 +08:00
|
|
|
TargetData TD;
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
// The runtime stack of executing code. The top of the stack is the current
|
2002-04-08 04:49:59 +08:00
|
|
|
// function record.
|
2002-01-21 06:54:45 +08:00
|
|
|
std::vector<ExecutionContext> ECStack;
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
// AtExitHandlers - List of functions to call when the program exits,
|
|
|
|
// registered with the atexit() library function.
|
2003-05-14 22:21:30 +08:00
|
|
|
std::vector<Function*> AtExitHandlers;
|
2003-09-18 01:26:22 +08:00
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
public:
|
2003-10-25 03:59:37 +08:00
|
|
|
Interpreter(Module *M, bool isLittleEndian, bool isLongPointer);
|
|
|
|
inline ~Interpreter() { }
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
/// runAtExitHandlers - Run any functions registered by the
|
|
|
|
/// program's calls to atexit(3), which we intercept and store in
|
|
|
|
/// AtExitHandlers.
|
|
|
|
///
|
|
|
|
void runAtExitHandlers ();
|
|
|
|
|
2003-09-04 04:34:19 +08:00
|
|
|
/// create - Create an interpreter ExecutionEngine. This can never fail.
|
|
|
|
///
|
2003-10-25 03:59:37 +08:00
|
|
|
static ExecutionEngine *create(Module *M);
|
2003-09-04 04:34:19 +08:00
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
/// run - Start execution with the specified function and arguments.
|
|
|
|
///
|
2003-09-06 02:42:01 +08:00
|
|
|
virtual GenericValue run(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgValues);
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2003-09-06 02:42:01 +08:00
|
|
|
// Methods used to execute code:
|
|
|
|
// Place a call on the stack
|
2003-05-09 00:18:31 +08:00
|
|
|
void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
|
2003-09-06 02:42:01 +08:00
|
|
|
void run(); // Execute instructions until nothing left to do
|
2001-08-24 01:05:04 +08:00
|
|
|
|
|
|
|
// Opcode Implementations
|
2003-05-11 05:22:39 +08:00
|
|
|
void visitReturnInst(ReturnInst &I);
|
|
|
|
void visitBranchInst(BranchInst &I);
|
|
|
|
void visitSwitchInst(SwitchInst &I);
|
|
|
|
|
|
|
|
void visitBinaryOperator(BinaryOperator &I);
|
|
|
|
void visitAllocationInst(AllocationInst &I);
|
|
|
|
void visitFreeInst(FreeInst &I);
|
|
|
|
void visitLoadInst(LoadInst &I);
|
|
|
|
void visitStoreInst(StoreInst &I);
|
|
|
|
void visitGetElementPtrInst(GetElementPtrInst &I);
|
|
|
|
void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
|
|
|
|
void visitCastInst(CastInst &I);
|
2003-11-08 04:04:22 +08:00
|
|
|
|
|
|
|
void visitCallSite(CallSite CS);
|
|
|
|
void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
|
|
|
|
void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
|
2003-11-08 04:07:06 +08:00
|
|
|
void visitUnwindInst(UnwindInst &I);
|
2003-11-08 04:04:22 +08:00
|
|
|
|
2003-05-11 05:22:39 +08:00
|
|
|
void visitShl(ShiftInst &I);
|
|
|
|
void visitShr(ShiftInst &I);
|
2003-10-18 13:55:25 +08:00
|
|
|
void visitVANextInst(VANextInst &I);
|
2003-11-08 05:20:47 +08:00
|
|
|
void visitVAArgInst(VAArgInst &I);
|
2003-05-11 05:22:39 +08:00
|
|
|
void visitInstruction(Instruction &I) {
|
|
|
|
std::cerr << I;
|
|
|
|
assert(0 && "Instruction not interpretable yet!");
|
|
|
|
}
|
|
|
|
|
2003-05-09 00:18:31 +08:00
|
|
|
GenericValue callExternalFunction(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgVals);
|
2001-10-27 12:15:57 +08:00
|
|
|
void exitCalled(GenericValue GV);
|
2001-08-24 01:05:04 +08:00
|
|
|
|
2003-05-14 22:21:30 +08:00
|
|
|
void addAtExitHandler(Function *F) {
|
|
|
|
AtExitHandlers.push_back(F);
|
|
|
|
}
|
|
|
|
|
2002-12-24 07:59:41 +08:00
|
|
|
//FIXME: private:
|
|
|
|
public:
|
|
|
|
GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
|
|
|
|
User::op_iterator E, ExecutionContext &SF);
|
|
|
|
|
2001-08-24 01:05:04 +08:00
|
|
|
private: // Helper functions
|
2003-05-11 04:21:16 +08:00
|
|
|
// SwitchToNewBasicBlock - Start execution in a new basic block and run any
|
|
|
|
// PHI nodes in the top of the block. This is used for intraprocedural
|
|
|
|
// control flow.
|
|
|
|
//
|
|
|
|
void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
|
|
|
|
|
2003-08-14 02:17:54 +08:00
|
|
|
void *getPointerToFunction(Function *F) { return (void*)F; }
|
2002-12-24 07:59:41 +08:00
|
|
|
|
2001-10-31 04:28:23 +08:00
|
|
|
void initializeExecutionEngine();
|
2003-05-09 00:18:31 +08:00
|
|
|
void initializeExternalFunctions();
|
2003-09-06 02:55:03 +08:00
|
|
|
GenericValue getOperandValue(Value *V, ExecutionContext &SF);
|
|
|
|
GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
|
|
|
|
ExecutionContext &SF);
|
2003-11-07 13:22:49 +08:00
|
|
|
void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
|
2001-08-24 01:05:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|