2001-08-23 17:05:04 +00:00
|
|
|
//===-- Interpreter.h ------------------------------------------*- C++ -*--===//
|
|
|
|
//
|
|
|
|
// This header file defines the interpreter structure
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLI_INTERPRETER_H
|
|
|
|
#define LLI_INTERPRETER_H
|
|
|
|
|
2001-11-12 16:19:45 +00:00
|
|
|
// Uncomment this line to enable profiling of structure field accesses.
|
2002-10-02 21:11:16 +00:00
|
|
|
//#define PROFILE_STRUCTURE_FIELDS 1
|
2001-11-12 16:19:45 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
#include "../ExecutionEngine.h"
|
2001-11-26 23:04:08 +00:00
|
|
|
#include "Support/DataTypes.h"
|
2001-11-07 04:23:00 +00:00
|
|
|
#include "llvm/Assembly/CachedWriter.h"
|
2002-12-23 23:59:41 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "../GenericValue.h"
|
2001-11-07 04:23:00 +00:00
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
extern CachedWriter CW; // Object to accelerate printing of LLVM
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
struct MethodInfo; // Defined in ExecutionAnnotations.h
|
|
|
|
class CallInst;
|
|
|
|
class ReturnInst;
|
|
|
|
class BranchInst;
|
2002-12-23 23:59:41 +00:00
|
|
|
class LoadInst;
|
|
|
|
class StoreInst;
|
2001-08-27 05:16:50 +00:00
|
|
|
class AllocationInst;
|
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
|
|
|
|
// 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-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 {
|
2002-04-07 20:49:59 +00:00
|
|
|
Function *CurMethod; // 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
|
2002-04-07 20:49:59 +00:00
|
|
|
MethodInfo *MethInfo; // The MethInfo annotation for the function
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
|
2003-05-08 16:06:52 +00:00
|
|
|
std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
BasicBlock *PrevBB; // The previous BB or null if in first BB
|
|
|
|
CallInst *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.
|
|
|
|
//
|
2002-12-23 23:59:41 +00:00
|
|
|
class Interpreter : public ExecutionEngine {
|
2001-08-23 17:05:04 +00:00
|
|
|
int ExitCode; // The exit code to be returned by the lli util
|
2002-12-23 23:59:41 +00:00
|
|
|
bool Debug; // Debug mode enabled?
|
2001-08-23 17:05:04 +00:00
|
|
|
bool Profile; // Profiling enabled?
|
2001-10-27 08:43:52 +00:00
|
|
|
bool Trace; // Tracing enabled?
|
2001-08-23 17:05:04 +00:00
|
|
|
int CurFrame; // The current stack frame being inspected
|
2002-12-23 23:59:41 +00:00
|
|
|
TargetData TD;
|
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
|
|
|
|
|
|
|
public:
|
2002-12-23 23:59:41 +00:00
|
|
|
Interpreter(Module *M, unsigned Config, bool DebugMode, bool TraceMode);
|
|
|
|
inline ~Interpreter() { CW.setModule(0); }
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
// getExitCode - return the code that should be the exit code for the lli
|
|
|
|
// utility.
|
|
|
|
inline int getExitCode() const { return ExitCode; }
|
2002-12-23 23:59:41 +00:00
|
|
|
|
|
|
|
/// run - Start execution with the specified function and arguments.
|
|
|
|
///
|
|
|
|
virtual int run(const std::string &FnName,
|
|
|
|
const std::vector<std::string> &Args);
|
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
// enableProfiling() - Turn profiling on, clear stats?
|
|
|
|
void enableProfiling() { Profile = true; }
|
2001-10-27 08:43:52 +00:00
|
|
|
void enableTracing() { Trace = true; }
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
void handleUserInput();
|
|
|
|
|
|
|
|
// User Interation Methods...
|
2002-01-20 22:54:45 +00:00
|
|
|
bool callMethod(const std::string &Name); // return true on failure
|
|
|
|
void setBreakpoint(const std::string &Name);
|
|
|
|
void infoValue(const std::string &Name);
|
|
|
|
void print(const std::string &Name);
|
2001-10-15 05:51:48 +00:00
|
|
|
static void print(const Type *Ty, GenericValue V);
|
2001-09-10 04:49:44 +00:00
|
|
|
static void printValue(const Type *Ty, GenericValue V);
|
2001-08-23 17:05:04 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
bool callMainMethod(const std::string &MainName,
|
|
|
|
const std::vector<std::string> &InputFilename);
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
void list(); // Do the 'list' command
|
|
|
|
void printStackTrace(); // Do the 'backtrace' command
|
|
|
|
|
|
|
|
// Code execution methods...
|
2002-04-07 20:49:59 +00:00
|
|
|
void callMethod(Function *F, const std::vector<GenericValue> &ArgVals);
|
2001-08-23 17:05:04 +00:00
|
|
|
bool executeInstruction(); // Execute one instruction...
|
|
|
|
|
|
|
|
void stepInstruction(); // Do the 'step' command
|
|
|
|
void nextInstruction(); // Do the 'next' command
|
|
|
|
void run(); // Do the 'run' command
|
|
|
|
void finish(); // Do the 'finish' command
|
|
|
|
|
|
|
|
// Opcode Implementations
|
2002-06-25 16:13:21 +00:00
|
|
|
void executeCallInst(CallInst &I, ExecutionContext &SF);
|
|
|
|
void executeRetInst(ReturnInst &I, ExecutionContext &SF);
|
|
|
|
void executeBrInst(BranchInst &I, ExecutionContext &SF);
|
|
|
|
void executeAllocInst(AllocationInst &I, ExecutionContext &SF);
|
2002-04-07 20:49:59 +00:00
|
|
|
GenericValue callExternalMethod(Function *F,
|
2002-01-20 22:54:45 +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
|
|
|
|
|
|
|
// getCurrentMethod - Return the currently executing method
|
2002-04-07 20:49:59 +00:00
|
|
|
inline Function *getCurrentMethod() const {
|
2001-08-23 17:05:04 +00:00
|
|
|
return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
// isStopped - Return true if a program is stopped. Return false if no
|
|
|
|
// program is running.
|
|
|
|
//
|
|
|
|
inline bool isStopped() const { return !ECStack.empty(); }
|
|
|
|
|
2002-12-23 23:59:41 +00:00
|
|
|
//FIXME: private:
|
|
|
|
public:
|
|
|
|
GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
|
|
|
|
User::op_iterator E, ExecutionContext &SF);
|
|
|
|
void executeLoadInst(LoadInst &I, ExecutionContext &SF);
|
|
|
|
void executeStoreInst(StoreInst &I, ExecutionContext &SF);
|
|
|
|
|
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
private: // Helper functions
|
2002-12-23 23:59:41 +00:00
|
|
|
void *getPointerToFunction(const Function *F) { return (void*)F; }
|
|
|
|
|
2001-10-27 04:15:57 +00:00
|
|
|
// getCurrentExecutablePath() - Return the directory that the lli executable
|
|
|
|
// lives in.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string getCurrentExecutablePath() const;
|
2001-10-27 04:15:57 +00:00
|
|
|
|
2001-08-23 17:05:04 +00:00
|
|
|
// printCurrentInstruction - Print out the instruction that the virtual PC is
|
|
|
|
// at, or fail silently if no program is running.
|
|
|
|
//
|
|
|
|
void printCurrentInstruction();
|
|
|
|
|
2001-11-07 05:31:27 +00:00
|
|
|
// printStackFrame - Print information about the specified stack frame, or -1
|
|
|
|
// for the default one.
|
|
|
|
//
|
|
|
|
void printStackFrame(int FrameNo = -1);
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// LookupMatchingNames - Search the current function namespace, then the
|
|
|
|
// global namespace looking for values that match the specified name. Return
|
|
|
|
// ALL matches to that name. This is obviously slow, and should only be used
|
|
|
|
// for user interaction.
|
2001-08-23 17:05:04 +00:00
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> LookupMatchingNames(const std::string &Name);
|
2001-08-23 17:05:04 +00:00
|
|
|
|
|
|
|
// ChooseOneOption - Prompt the user to choose among the specified options to
|
|
|
|
// pick one value. If no options are provided, emit an error. If a single
|
|
|
|
// option is provided, just return that option.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
Value *ChooseOneOption(const std::string &Name,
|
|
|
|
const std::vector<Value*> &Opts);
|
2001-10-30 20:28:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
void initializeExecutionEngine();
|
|
|
|
void initializeExternalMethods();
|
2001-08-23 17:05:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|