CurFrame = ECStack.size()-1;
}
-void Interpreter::stepInstruction() { // Do the 'step' command
- if (ECStack.empty()) {
- std::cout << "Error: no program running, cannot step!\n";
- return;
- }
-
- // Run an instruction...
- executeInstruction();
-
- // Print the next instruction to execute...
- printCurrentInstruction();
-}
-
-// --- UI Stuff...
-void Interpreter::nextInstruction() { // Do the 'next' command
- if (ECStack.empty()) {
- std::cout << "Error: no program running, cannot 'next'!\n";
- return;
- }
-
- // If this is a call instruction, step over the call instruction...
- // TODO: ICALL, CALL WITH, ...
- if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
- unsigned StackSize = ECStack.size();
- // Step into the function...
- executeInstruction();
-
- // If we we able to step into the function, finish it now. We might not be
- // able the step into a function, if it's external for example.
- if (ECStack.size() != StackSize)
- finish(); // Finish executing the function...
- else
- printCurrentInstruction();
-
- } else {
- // Normal instruction, just step...
- stepInstruction();
- }
-}
-
void Interpreter::run() {
if (ECStack.empty()) {
std::cout << "Error: no program running, cannot run!\n";
// Run an instruction...
executeInstruction();
}
-
- // Print the next instruction to execute...
- printCurrentInstruction();
-}
-
-void Interpreter::finish() {
- if (ECStack.empty()) {
- std::cout << "Error: no program running, cannot run!\n";
- return;
- }
-
- unsigned StackSize = ECStack.size();
- while (ECStack.size() >= StackSize) {
- // Run an instruction...
- executeInstruction();
- }
-
- // Print the next instruction to execute...
- printCurrentInstruction();
-}
-
-// printCurrentInstruction - Print out the instruction that the virtual PC is
-// at, or fail silently if no program is running.
-//
-void Interpreter::printCurrentInstruction() {
- if (!ECStack.empty()) {
- if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
- WriteAsOperand(std::cout, ECStack.back().CurBB) << ":\n";
-
- Instruction &I = *ECStack.back().CurInst;
- InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
- assert(IN && "Instruction has no numbering annotation!");
- std::cout << "#" << IN->InstNum << I;
- }
}
void Interpreter::printValue(const Type *Ty, GenericValue V) {
std::cout << "\n";
}
}
-
-void Interpreter::infoValue(const std::string &Name) {
- Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
- if (!PickedVal) return;
-
- std::cout << "Value: ";
- print(PickedVal->getType(),
- getOperandValue(PickedVal, ECStack[CurFrame]));
- std::cout << "\n";
- printOperandInfo(PickedVal, ECStack[CurFrame]);
-}
-
-// printStackFrame - Print information about the specified stack frame, or -1
-// for the default one.
-//
-void Interpreter::printStackFrame(int FrameNo) {
- if (FrameNo == -1) FrameNo = CurFrame;
- Function *F = ECStack[FrameNo].CurFunction;
- const Type *RetTy = F->getReturnType();
-
- CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
- << (Value*)RetTy << " \"" << F->getName() << "\"(";
-
- unsigned i = 0;
- for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
- if (i != 0) std::cout << ", ";
- CW << *I << "=";
-
- printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
- }
-
- std::cout << ")\n";
-
- if (FrameNo != int(ECStack.size()-1)) {
- BasicBlock::iterator I = ECStack[FrameNo].CurInst;
- CW << --I;
- } else {
- CW << *ECStack[FrameNo].CurInst;
- }
-}
-
#include "Interpreter.h"
#include "llvm/Module.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Function.h"
/// create - Create a new interpreter object. This can never fail.
///
return ExitCode;
}
+
+// callMainFunction - Construct call to typical C main() function and
+// call it using callFunction().
+//
+bool Interpreter::callMainFunction(const std::string &Name,
+ const std::vector<std::string> &InputArgv) {
+ Function *M = getModule().getNamedFunction(Name);
+ if (M == 0) {
+ std::cerr << "Could not find function '" << Name << "' in module!\n";
+ return 1;
+ }
+ const FunctionType *MT = M->getFunctionType();
+
+ std::vector<GenericValue> Args;
+ if (MT->getParamTypes().size() >= 2) {
+ PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
+ if (MT->getParamTypes()[1] != SPP) {
+ CW << "Second argument of '" << Name << "' should have type: '"
+ << SPP << "'!\n";
+ return true;
+ }
+ Args.push_back(PTOGV(CreateArgv(InputArgv)));
+ }
+
+ if (MT->getParamTypes().size() >= 1) {
+ if (!MT->getParamTypes()[0]->isInteger()) {
+ std::cout << "First argument of '" << Name
+ << "' should be an integer!\n";
+ return true;
+ } else {
+ GenericValue GV; GV.UIntVal = InputArgv.size();
+ Args.insert(Args.begin(), GV);
+ }
+ }
+
+ callFunction(M, Args); // Start executing it...
+
+ // Reset the current frame location to the top of stack
+ CurFrame = ECStack.size()-1;
+
+ return false;
+}
//
class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
int ExitCode; // The exit code to be returned by the lli util
- bool Profile; // Profiling enabled?
bool Trace; // Tracing enabled?
int CurFrame; // The current stack frame being inspected
TargetData TD;
///
static ExecutionEngine *create(Module *M, bool TraceMode);
- /// getExitCode - return the code that should be the exit code for the lli
- /// utility.
- ///
- inline int getExitCode() const { return ExitCode; }
-
/// run - Start execution with the specified function and arguments.
///
virtual int run(const std::string &FnName,
const char ** envp);
- // enableProfiling() - Turn profiling on, clear stats?
- void enableProfiling() { Profile = true; }
void enableTracing() { Trace = true; }
void handleUserInput();
// User Interation Methods...
bool callFunction(const std::string &Name); // return true on failure
- void infoValue(const std::string &Name);
void print(const std::string &Name);
static void print(const Type *Ty, GenericValue V);
static void printValue(const Type *Ty, GenericValue V);
bool callMainFunction(const std::string &MainName,
const std::vector<std::string> &InputFilename);
- void list(); // Do the 'list' command
- void printStackTrace(); // Do the 'backtrace' command
-
// Code execution methods...
void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
void 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
void visitReturnInst(ReturnInst &I);
//
void printCurrentInstruction();
- // printStackFrame - Print information about the specified stack frame, or -1
- // for the default one.
- //
- void printStackFrame(int FrameNo = -1);
-
// 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
+++ /dev/null
-//===-- UserInput.cpp - Interpreter Input Loop support --------------------===//
-//
-// This file implements the interpreter Input I/O loop.
-//
-//===----------------------------------------------------------------------===//
-
-#include "Interpreter.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/Function.h"
-#include "llvm/Module.h"
-
-// callMainFunction - This is a nasty gross hack that will dissapear when
-// callFunction can parse command line options and stuff for us.
-//
-bool Interpreter::callMainFunction(const std::string &Name,
- const std::vector<std::string> &InputArgv) {
- Function *M = getModule().getNamedFunction(Name);
- if (M == 0) {
- std::cerr << "Could not find function '" << Name << "' in module!\n";
- return 1;
- }
- const FunctionType *MT = M->getFunctionType();
-
- std::vector<GenericValue> Args;
- if (MT->getParamTypes().size() >= 2) {
- PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
- if (MT->getParamTypes()[1] != SPP) {
- CW << "Second argument of '" << Name << "' should have type: '"
- << SPP << "'!\n";
- return true;
- }
- Args.push_back(PTOGV(CreateArgv(InputArgv)));
- }
-
- if (MT->getParamTypes().size() >= 1) {
- if (!MT->getParamTypes()[0]->isInteger()) {
- std::cout << "First argument of '" << Name << "' should be an integer!\n";
- return true;
- } else {
- GenericValue GV; GV.UIntVal = InputArgv.size();
- Args.insert(Args.begin(), GV);
- }
- }
-
- callFunction(M, Args); // Start executing it...
-
- // Reset the current frame location to the top of stack
- CurFrame = ECStack.size()-1;
-
- return false;
-}