Skip to content

Commit

Permalink
Add support to debug itself
Browse files Browse the repository at this point in the history
  • Loading branch information
s123unny committed Jun 30, 2023
1 parent b5a1cf9 commit 244fc70
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion aeneas/src/main/Compiler.v3
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def BASH_HEADER = [
];
// phases of compilation, including parsing, typechecking, initialization, etc
class Compilation(compiler: Compiler, prog: Program) {
var inRunMain: bool;
new() {
if (compiler.target != null) compiler.target.configureProgram(prog);
}
Expand Down Expand Up @@ -241,6 +242,7 @@ class Compilation(compiler: Compiler, prog: Program) {
def runMain() -> Result {
var main = prog.getMain();
if (main == null) return null;
inRunMain = true;
var invoker = getInterpreter();
var argVals: Array<Val>;
var atypes = prog.getMain().getParamTypes();
Expand All @@ -259,7 +261,7 @@ class Compilation(compiler: Compiler, prog: Program) {
return invoker(Closure.new(Values.BOTTOM, main), argVals);
}
def getInterpreter() -> (Closure, Array<Val>) -> Result {
if (CLOptions.DEBUG.get()) {
if (CLOptions.DEBUG.get() && inRunMain) {
var interp = SsaInterpreter.new(prog, genSsa);
var debugger = SsaDebugger.new(prog, genSsa, interp);
return debugger.invoke;
Expand Down
23 changes: 21 additions & 2 deletions aeneas/src/ssa/SsaDebugger.v3
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class SsaDebugger(prog: Program, genSsa: (IrSpec, int) -> SsaGraph, interp: SsaI
def start() {
interp.reset();
srcPrinter.reset();
Terminal.put1("Starting program: %s\n", interp.prog.name());
Terminal.put2("Starting program: %s with method %q\n", interp.prog.name(), startDel.memberRef.render);
if (startArgs != null) for (i < startArgs.length) Terminal.put2("args %d: %s\n", i, V3.renderVal(startArgs[i]));
interp.pushFrame(startDel.memberRef, startDel.val, startArgs);
running = true;
}
Expand Down Expand Up @@ -503,6 +504,7 @@ class SrcPrinter(vstFiles: Array<VstFile>, interp: SsaInterpreter) {
var on: bool;
var traceCallee: bool;
var indent: int;
var cacheVstFile: VstFile;

def set(on: bool) {
this.on = on;
Expand All @@ -518,12 +520,13 @@ class SrcPrinter(vstFiles: Array<VstFile>, interp: SsaInterpreter) {
if (SsaCheckpoint.?(instr)) {
var source = SsaCheckpoint.!(instr).source;
var point = FilePoint.new(null, source.line, source.column);
var vstFile = getVstFile(source.mdecl.token.fileName);
extractTmpConstraint(0);
addEnds(outBuf.length);
tmpBuf.putd(source.line).tab().rjustify(' ', padNum, padNum);
putNewIndent(targetSp);
outBuf.puts(tmpBuf.extract());
outBuf.puts(point.extractLine(vstFiles[0].input, vstFiles[0].lineEnds)).ln();
outBuf.puts(point.extractLine(vstFile.input, vstFile.lineEnds)).ln();
} else if (SsaApplyOp.?(instr)) {
putVal(instr);
} else if (SsaReturn.?(instr)) {
Expand All @@ -532,6 +535,16 @@ class SrcPrinter(vstFiles: Array<VstFile>, interp: SsaInterpreter) {
putVal(i);
}
}
def getVstFile(fileName: string) -> VstFile {
if (cacheVstFile != null && Strings.equal(cacheVstFile.fileName, fileName)) return cacheVstFile;
for (i < vstFiles.length) {
if (Strings.equal(vstFiles[i].fileName, fileName)) {
cacheVstFile = vstFiles[i];
return cacheVstFile;
}
}
return V3.fail("source filename not found");
}
def addEnds(i: int) {
ends.put(i);
if (ends.length > storeLimit) {
Expand All @@ -547,6 +560,12 @@ class SrcPrinter(vstFiles: Array<VstFile>, interp: SsaInterpreter) {
var vtype = instr.getType();
if (VoidType.?(vtype) || instr.valueNum < 0) return;
var source = SsaApplyOp.!(instr).source;
if (source == null) {
extractTmpConstraint(padNum);
tmpBuf.pad(' ', padNum);
V3.renderResult(interp.getVal(instr), vtype, tmpBuf);
return;
}
var point = source.column - 1;
var start = if(source.startColumn != -1, source.startColumn-1, point);
var end = if(source.endColumn != -1, source.endColumn-1, point);
Expand Down
7 changes: 7 additions & 0 deletions bin/dev/v3c-debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
DEV=$(dirname ${BASH_SOURCE[0]})
BIN=$(cd $DEV/.. && pwd)
SRC=$(cd $BIN/../aeneas/src && pwd)
LIB=$(cd $BIN/../lib/ && pwd)
# TODO: use aeneas/src/DEPS ?
exec $BIN/v3c -fp -debug $SRC/*/*.v3 $LIB/util/*.v3 $LIB/asm/arm/*.v3 $LIB/asm/x86/*.v3 $LIB/asm/x86-64/*.v3 -a "$@"

0 comments on commit 244fc70

Please sign in to comment.