#ifndef LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
#define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
class IntrinsicLowering;
template<typename T> class generic_gep_type_iterator;
class ConstantExpr;
typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
class AllocaHolder {
std::vector<void *> Allocations;
public:
AllocaHolder() = default;
AllocaHolder(AllocaHolder &&) = default;
AllocaHolder &operator=(AllocaHolder &&RHS) = default;
~AllocaHolder() {
for (void *Allocation : Allocations)
free(Allocation);
}
void add(void *Mem) { Allocations.push_back(Mem); }
};
typedef std::vector<GenericValue> ValuePlaneTy;
struct ExecutionContext {
Function *CurFunction; BasicBlock *CurBB; BasicBlock::iterator CurInst; CallBase *Caller; std::map<Value *, GenericValue> Values; std::vector<GenericValue> VarArgs; AllocaHolder Allocas;
ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
};
class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
GenericValue ExitValue; IntrinsicLowering *IL;
std::vector<ExecutionContext> ECStack;
std::vector<Function*> AtExitHandlers;
public:
explicit Interpreter(std::unique_ptr<Module> M);
~Interpreter() override;
void runAtExitHandlers();
static void Register() {
InterpCtor = create;
}
static ExecutionEngine *create(std::unique_ptr<Module> M,
std::string *ErrorStr = nullptr);
GenericValue runFunction(Function *F,
ArrayRef<GenericValue> ArgValues) override;
void *getPointerToNamedFunction(StringRef Name,
bool AbortOnFailure = true) override {
return nullptr;
}
void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
void run();
void visitReturnInst(ReturnInst &I);
void visitBranchInst(BranchInst &I);
void visitSwitchInst(SwitchInst &I);
void visitIndirectBrInst(IndirectBrInst &I);
void visitUnaryOperator(UnaryOperator &I);
void visitBinaryOperator(BinaryOperator &I);
void visitICmpInst(ICmpInst &I);
void visitFCmpInst(FCmpInst &I);
void visitAllocaInst(AllocaInst &I);
void visitLoadInst(LoadInst &I);
void visitStoreInst(StoreInst &I);
void visitGetElementPtrInst(GetElementPtrInst &I);
void visitPHINode(PHINode &PN) {
llvm_unreachable("PHI nodes already handled!");
}
void visitTruncInst(TruncInst &I);
void visitZExtInst(ZExtInst &I);
void visitSExtInst(SExtInst &I);
void visitFPTruncInst(FPTruncInst &I);
void visitFPExtInst(FPExtInst &I);
void visitUIToFPInst(UIToFPInst &I);
void visitSIToFPInst(SIToFPInst &I);
void visitFPToUIInst(FPToUIInst &I);
void visitFPToSIInst(FPToSIInst &I);
void visitPtrToIntInst(PtrToIntInst &I);
void visitIntToPtrInst(IntToPtrInst &I);
void visitBitCastInst(BitCastInst &I);
void visitSelectInst(SelectInst &I);
void visitVAStartInst(VAStartInst &I);
void visitVAEndInst(VAEndInst &I);
void visitVACopyInst(VACopyInst &I);
void visitIntrinsicInst(IntrinsicInst &I);
void visitCallBase(CallBase &I);
void visitUnreachableInst(UnreachableInst &I);
void visitShl(BinaryOperator &I);
void visitLShr(BinaryOperator &I);
void visitAShr(BinaryOperator &I);
void visitVAArgInst(VAArgInst &I);
void visitExtractElementInst(ExtractElementInst &I);
void visitInsertElementInst(InsertElementInst &I);
void visitShuffleVectorInst(ShuffleVectorInst &I);
void visitExtractValueInst(ExtractValueInst &I);
void visitInsertValueInst(InsertValueInst &I);
void visitInstruction(Instruction &I) {
errs() << I << "\n";
llvm_unreachable("Instruction not interpretable yet!");
}
GenericValue callExternalFunction(Function *F,
ArrayRef<GenericValue> ArgVals);
void exitCalled(GenericValue GV);
void addAtExitHandler(Function *F) {
AtExitHandlers.push_back(F);
}
GenericValue *getFirstVarArg () {
return &(ECStack.back ().VarArgs[0]);
}
private: GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
gep_type_iterator E, ExecutionContext &SF);
void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
void *getPointerToFunction(Function *F) override { return (void*)F; }
void initializeExecutionEngine() { }
void initializeExternalFunctions();
GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
GenericValue getOperandValue(Value *V, ExecutionContext &SF);
GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
ExecutionContext &SF);
GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
Type *Ty, ExecutionContext &SF);
void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
};
}
#endif