1
0

Implement BACKTRACE

This commit is contained in:
otya128
2016-12-15 17:26:27 +09:00
parent 10eb0803fe
commit 1fc36cdc90
3 changed files with 39 additions and 1 deletions

View File

@@ -9,6 +9,12 @@ import std.uni;
import std.utf; import std.utf;
import std.conv; import std.conv;
import std.stdio; import std.stdio;
struct Trace
{
int slot;
int line;
wstring name;
}
struct VMVariable struct VMVariable
{ {
int index; int index;
@@ -50,9 +56,12 @@ class VM
Value[] stack; Value[] stack;
int bp; int bp;
PetitComputer petitcomputer; PetitComputer petitcomputer;
Trace[] backTraceStack;
int traceI;
this() this()
{ {
this.stack = new Value[16384]; this.stack = new Value[16384];
this.backTraceStack = new Trace[16384];
for(int i = 0; i < slots.length; i++) for(int i = 0; i < slots.length; i++)
{ {
slots[i] = new VMSlot(); slots[i] = new VMSlot();
@@ -126,6 +135,7 @@ class VM
void init(PetitComputer petitcomputer) void init(PetitComputer petitcomputer)
{ {
this.petitcomputer = petitcomputer; this.petitcomputer = petitcomputer;
traceI = 0;
bp = 0;//globalを実行なのでbaseは0(グローバル変数をスタックに取るようにしない限り)(挙動的にスタックに確保していなさそう) bp = 0;//globalを実行なのでbaseは0(グローバル変数をスタックに取るようにしない限り)(挙動的にスタックに確保していなさそう)
} }
void processBreakPoint() void processBreakPoint()
@@ -244,6 +254,18 @@ class VM
{ {
this.currentSlot.globalDataTable.dataIndex = olddti; this.currentSlot.globalDataTable.dataIndex = olddti;
} }
void pushBackTrace(wstring name)
{
backTraceStack[traceI++] = Trace(currentSlotNumber, currentLocation.line, name);
}
void popBackTrace()
{
traceI--;
}
Trace[] backTrace()
{
return backTraceStack[0..traceI];
}
} }
enum CodeType enum CodeType
{ {
@@ -872,6 +894,7 @@ class GosubAddr : Code
} }
override void execute(VM vm) override void execute(VM vm)
{ {
vm.pushBackTrace("");
vm.pushpc;//vm.push(Value(vm.pc)); vm.pushpc;//vm.push(Value(vm.pc));
vm.pc = address - 1; vm.pc = address - 1;
} }
@@ -908,6 +931,7 @@ class GosubExpr : Code
vm.pop(label); vm.pop(label);
if(label.isString) if(label.isString)
{ {
vm.pushBackTrace(label.castString);
vm.pushpc; vm.pushpc;
int pc; int pc;
if (sc && sc.func) if (sc && sc.func)
@@ -953,6 +977,7 @@ class ReturnSubroutine : Code
vm.pc = pc.integerValue; vm.pc = pc.integerValue;
*/ */
vm.poppc; vm.poppc;
vm.popBackTrace();
} }
override string toString(VM vm) override string toString(VM vm)
{ {
@@ -1269,6 +1294,7 @@ class ReturnFunction : Code
} }
//vm.pc = pc.integerValue; //vm.pc = pc.integerValue;
vm.bp = bp.integerValue; vm.bp = bp.integerValue;
vm.popBackTrace();
} }
override string toString(VM vm) override string toString(VM vm)
{ {
@@ -1314,6 +1340,7 @@ class CallFunctionCode : Code
{ {
throw new IllegalFunctionCall(name.to!string); throw new IllegalFunctionCall(name.to!string);
} }
vm.pushBackTrace(name);
//TODO:args //TODO:args
auto bp = vm.stacki; auto bp = vm.stacki;
vm.push(Value()); vm.push(Value());
@@ -1421,6 +1448,7 @@ class CallFunctionS : Code
callBuintinFunc(bfunc, vm); callBuintinFunc(bfunc, vm);
return; return;
} }
vm.pushBackTrace(name);
callFunc(func, vm); callFunc(func, vm);
} }
override string toString(VM vm) override string toString(VM vm)
@@ -1543,6 +1571,7 @@ class OnGosub : OnBase
int index = on(vm); int index = on(vm);
if(index < 0) return; if(index < 0) return;
vm.pushpc;//vm.push(Value(vm.pc)); vm.pushpc;//vm.push(Value(vm.pc));
vm.pushBackTrace("");
vm.pc = index - 1; vm.pc = index - 1;
} }
override string toString(VM vm) override string toString(VM vm)

View File

@@ -2691,6 +2691,15 @@ class BuiltinFunction
throw new TypeMismatch(); throw new TypeMismatch();
} }
} }
static void BACKTRACE(PetitComputer p)
{
auto bt = p.vm.backTrace;
foreach (t; bt)
{
p.console.print(t.slot, ":\t", t.line, ":\t", t.name, "\n");
//TODO:=== Press ENTER ===
}
}
//alias void function(PetitComputer, Value[], Value[]) BuiltinFunc; //alias void function(PetitComputer, Value[], Value[]) BuiltinFunc;
static BuiltinFunctions[wstring] builtinFunctions; static BuiltinFunctions[wstring] builtinFunctions;
static wstring getBasicName(BFD)(const wstring def) static wstring getBasicName(BFD)(const wstring def)

View File

@@ -573,7 +573,7 @@ class PetitComputer
{ {
writeln(t); writeln(t);
} }
bool renderprofile = true; bool renderprofile = false;
try try
{ {
version(Windows) version(Windows)