2001-08-23 17:05:04 +00:00
|
|
|
//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
|
2005-04-21 22:43:08 +00:00
|
|
|
//
|
2003-10-21 15:17:13 +00: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.
|
2005-04-21 22:43:08 +00:00
|
|
|
//
|
2003-10-21 15:17:13 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-08-23 17:05:04 +00:00
|
|
|
//
|
|
|
|
// This header file defines the interpreter structure
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLI_INTERPRETER_H
|
|
|
|
#define LLI_INTERPRETER_H
|
|
|
|
|
2003-11-05 06:20:27 +00:00
|
|
|
#include "llvm/Function.h"
|
2003-09-05 19:39:22 +00:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2003-05-10 21:22:39 +00:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
2003-11-07 19:26:23 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2003-09-05 20:08:15 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2004-07-04 12:19:56 +00:00
|
|
|
#include <iostream>
|
2001-11-07 04:23:00 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2006-03-23 05:22:51 +00:00
|
|
|
class IntrinsicLowering;
|
2004-02-26 07:59:22 +00:00
|
|
|
struct FunctionInfo;
|
2004-04-04 17:30:06 +00:00
|
|
|
template<typename T> class generic_gep_type_iterator;
|
2003-12-11 00:23:28 +00:00
|
|
|
class ConstantExpr;
|
2004-04-04 19:47:06 +00:00
|
|
|
typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
|
2004-04-04 17:30:06 +00:00
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2002-02-19 18:50:09 +00:00
|
|
|
// AllocaHolder - Object to track all of the blocks of memory allocated by
|
2003-12-11 00:23:28 +00:00
|
|
|
// alloca. When the function returns, this object is popped off the execution
|
2002-02-19 18:50:09 +00:00
|
|
|
// 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-20 22:54:45 +00:00
|
|
|
typedef std::vector<GenericValue> ValuePlaneTy;
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
// ExecutionContext struct - This struct represents one stack frame currently
|
|
|
|
// executing.
|
|
|
|
//
|
|
|
|
struct ExecutionContext {
|
2003-05-08 16:18:31 +00:00
|
|
|
Function *CurFunction;// The currently executing function
|
2001-08-23 17:05:04 +00:00
|
|
|
BasicBlock *CurBB; // The currently executing BB
|
|
|
|
BasicBlock::iterator CurInst; // The next instruction to execute
|
2003-10-24 19:59:37 +00:00
|
|
|
std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
|
2003-05-08 16:06:52 +00:00
|
|
|
std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
|
2003-11-07 19:26:23 +00:00
|
|
|
CallSite Caller; // Holds the call that called subframes.
|
|
|
|
// NULL if main func or debugger invoked fn
|
2002-02-19 18:50:09 +00:00
|
|
|
AllocaHolderHandle Allocas; // Track memory allocated by alloca
|
2001-08-23 17:05:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Interpreter - This class represents the entirety of the interpreter.
|
|
|
|
//
|
2003-05-10 21:22:39 +00:00
|
|
|
class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
|
2006-02-07 05:29:44 +00:00
|
|
|
GenericValue ExitValue; // The return value of the called function
|
2002-12-23 23:59:41 +00:00
|
|
|
TargetData TD;
|
2003-12-28 09:44:37 +00:00
|
|
|
IntrinsicLowering *IL;
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
// The runtime stack of executing code. The top of the stack is the current
|
2002-04-07 20:49:59 +00:00
|
|
|
// function record.
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<ExecutionContext> ECStack;
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
// AtExitHandlers - List of functions to call when the program exits,
|
|
|
|
// registered with the atexit() library function.
|
2003-05-14 14:21:30 +00:00
|
|
|
std::vector<Function*> AtExitHandlers;
|
2003-09-17 17:26:22 +00:00
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
public:
|
2006-06-16 18:08:38 +00:00
|
|
|
Interpreter(Module *M);
|
2003-12-28 09:44:37 +00:00
|
|
|
~Interpreter();
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2003-12-26 06:13:05 +00:00
|
|
|
/// runAtExitHandlers - Run any functions registered by the program's calls to
|
|
|
|
/// atexit(3), which we intercept and store in AtExitHandlers.
|
2003-09-05 18:42:01 +00:00
|
|
|
///
|
2003-12-26 06:13:05 +00:00
|
|
|
void runAtExitHandlers();
|
2003-09-05 18:42:01 +00:00
|
|
|
|
2006-03-22 06:07:50 +00:00
|
|
|
static void Register() {
|
|
|
|
InterpCtor = create;
|
|
|
|
}
|
|
|
|
|
2006-03-23 05:22:51 +00:00
|
|
|
/// create - Create an interpreter ExecutionEngine. This can never fail.
|
2003-09-03 20:34:19 +00:00
|
|
|
///
|
2006-03-23 05:22:51 +00:00
|
|
|
static ExecutionEngine *create(ModuleProvider *M);
|
2003-09-03 20:34:19 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
/// run - Start execution with the specified function and arguments.
|
|
|
|
///
|
2003-12-26 06:13:05 +00:00
|
|
|
virtual GenericValue runFunction(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgValues);
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2003-12-08 08:23:04 +00:00
|
|
|
/// recompileAndRelinkFunction - For the interpreter, functions are always
|
|
|
|
/// up-to-date.
|
|
|
|
///
|
|
|
|
virtual void *recompileAndRelinkFunction(Function *F) {
|
|
|
|
return getPointerToFunction(F);
|
|
|
|
}
|
|
|
|
|
2004-11-07 23:58:46 +00:00
|
|
|
/// freeMachineCodeForFunction - The interpreter does not generate any code.
|
|
|
|
///
|
|
|
|
void freeMachineCodeForFunction(Function *F) { }
|
2005-04-21 22:43:08 +00:00
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
// Methods used to execute code:
|
|
|
|
// Place a call on the stack
|
2003-05-08 16:18:31 +00:00
|
|
|
void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
|
2003-09-05 18:42:01 +00:00
|
|
|
void run(); // Execute instructions until nothing left to do
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
// Opcode Implementations
|
2003-05-10 21:22:39 +00: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);
|
2004-04-20 16:43:21 +00:00
|
|
|
void visitSelectInst(SelectInst &I);
|
|
|
|
|
2003-11-07 20:04:22 +00:00
|
|
|
|
|
|
|
void visitCallSite(CallSite CS);
|
|
|
|
void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
|
|
|
|
void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
|
2003-11-07 20:07:06 +00:00
|
|
|
void visitUnwindInst(UnwindInst &I);
|
2004-10-16 18:21:33 +00:00
|
|
|
void visitUnreachableInst(UnreachableInst &I);
|
2003-11-07 20:04:22 +00:00
|
|
|
|
2003-05-10 21:22:39 +00:00
|
|
|
void visitShl(ShiftInst &I);
|
|
|
|
void visitShr(ShiftInst &I);
|
2003-11-07 21:20:47 +00:00
|
|
|
void visitVAArgInst(VAArgInst &I);
|
2003-05-10 21:22:39 +00:00
|
|
|
void visitInstruction(Instruction &I) {
|
|
|
|
std::cerr << I;
|
|
|
|
assert(0 && "Instruction not interpretable yet!");
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:43:08 +00:00
|
|
|
GenericValue callExternalFunction(Function *F,
|
2003-05-08 16:18:31 +00:00
|
|
|
const std::vector<GenericValue> &ArgVals);
|
2001-10-27 04:15:57 +00:00
|
|
|
void exitCalled(GenericValue GV);
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2003-05-14 14:21:30 +00:00
|
|
|
void addAtExitHandler(Function *F) {
|
|
|
|
AtExitHandlers.push_back(F);
|
|
|
|
}
|
|
|
|
|
2003-11-13 06:06:01 +00:00
|
|
|
GenericValue *getFirstVarArg () {
|
2004-02-13 06:18:39 +00:00
|
|
|
return &(ECStack.back ().VarArgs[0]);
|
2003-11-13 06:06:01 +00:00
|
|
|
}
|
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
//FIXME: private:
|
|
|
|
public:
|
2003-11-25 20:44:56 +00:00
|
|
|
GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
|
2005-04-22 04:08:30 +00:00
|
|
|
gep_type_iterator E, ExecutionContext &SF);
|
2002-12-23 23:59:41 +00:00
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
private: // Helper functions
|
2003-05-10 20:21:16 +00: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.
|
2005-04-21 22:43:08 +00:00
|
|
|
//
|
2003-05-10 20:21:16 +00:00
|
|
|
void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
|
|
|
|
|
2003-08-13 18:17:54 +00:00
|
|
|
void *getPointerToFunction(Function *F) { return (void*)F; }
|
2002-12-23 23:59:41 +00:00
|
|
|
|
2001-10-30 20:28:23 +00:00
|
|
|
void initializeExecutionEngine();
|
2003-05-08 16:18:31 +00:00
|
|
|
void initializeExternalFunctions();
|
2003-12-11 00:23:28 +00:00
|
|
|
GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
|
2003-09-05 18:55:03 +00:00
|
|
|
GenericValue getOperandValue(Value *V, ExecutionContext &SF);
|
|
|
|
GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
|
2005-04-22 04:08:30 +00:00
|
|
|
ExecutionContext &SF);
|
2003-11-07 05:22:49 +00:00
|
|
|
void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
|
2001-08-23 17:05:04 +00:00
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
#endif
|