wasm gc: fix inlining support in debug info builder, fix support of debug info in disassembler

This commit is contained in:
Alexey Andreev 2024-10-06 16:54:57 +02:00
parent c2eb11e056
commit f95250ddf7
3 changed files with 49 additions and 40 deletions

View File

@ -28,6 +28,7 @@ public class DebugLinesBuilder extends DebugSectionBuilder implements DebugLines
private String file; private String file;
private int line = 1; private int line = 1;
private Deque<State> states = new ArrayDeque<>(); private Deque<State> states = new ArrayDeque<>();
private StringBuilder indent = new StringBuilder(" ");
public DebugLinesBuilder(DebugFiles files, DebugMethods methods) { public DebugLinesBuilder(DebugFiles files, DebugMethods methods) {
super(DebugConstants.SECTION_LINES); super(DebugConstants.SECTION_LINES);
@ -47,6 +48,7 @@ public class DebugLinesBuilder extends DebugSectionBuilder implements DebugLines
public void location(String file, int line) { public void location(String file, int line) {
if (Objects.equals(file, this.file) && this.ptr != lastWrittenPtr && this.line != line) { if (Objects.equals(file, this.file) && this.ptr != lastWrittenPtr && this.line != line) {
if (this.ptr - lastWrittenPtr < 32 && Math.abs(line - this.line) <= 3) { if (this.ptr - lastWrittenPtr < 32 && Math.abs(line - this.line) <= 3) {
System.out.println(blob.ptr() + indent.toString() + "[user] ptr: " + this.ptr + ", line: " + line);
blob.writeByte(DebugConstants.LOC_USER + (this.ptr - lastWrittenPtr) + 32 * (line - this.line + 3)); blob.writeByte(DebugConstants.LOC_USER + (this.ptr - lastWrittenPtr) + 32 * (line - this.line + 3));
this.line = line; this.line = line;
lastWrittenPtr = ptr; lastWrittenPtr = ptr;
@ -57,10 +59,12 @@ public class DebugLinesBuilder extends DebugSectionBuilder implements DebugLines
flushPtr(); flushPtr();
this.line = 1; this.line = 1;
this.file = file; this.file = file;
System.out.println(blob.ptr() + indent.toString() + "[file] " + file);
blob.writeByte(DebugConstants.LOC_FILE).writeLEB(file != null ? files.filePtr(file) : 0); blob.writeByte(DebugConstants.LOC_FILE).writeLEB(file != null ? files.filePtr(file) : 0);
} }
if (this.line != line) { if (this.line != line) {
flushPtr(); flushPtr();
System.out.println(blob.ptr() + indent.toString() + "[line] " + line);
blob.writeByte(DebugConstants.LOC_LINE).writeSLEB(line - this.line); blob.writeByte(DebugConstants.LOC_LINE).writeSLEB(line - this.line);
this.line = line; this.line = line;
} }
@ -73,6 +77,7 @@ public class DebugLinesBuilder extends DebugSectionBuilder implements DebugLines
private void flushPtr() { private void flushPtr() {
if (ptr != lastWrittenPtr) { if (ptr != lastWrittenPtr) {
System.out.println(blob.ptr() + indent.toString() + "[ptr] " + ptr);
blob.writeLEB(DebugConstants.LOC_PTR); blob.writeLEB(DebugConstants.LOC_PTR);
blob.writeLEB(ptr - lastWrittenPtr); blob.writeLEB(ptr - lastWrittenPtr);
lastWrittenPtr = ptr; lastWrittenPtr = ptr;
@ -82,6 +87,8 @@ public class DebugLinesBuilder extends DebugSectionBuilder implements DebugLines
@Override @Override
public void start(MethodReference methodReference) { public void start(MethodReference methodReference) {
flushPtr(); flushPtr();
System.out.println(blob.ptr() + indent.toString() + "[start] method: " + methodReference);
indent.append(".");
blob.writeLEB(DebugConstants.LOC_START); blob.writeLEB(DebugConstants.LOC_START);
blob.writeLEB(methods.methodPtr(methodReference)); blob.writeLEB(methods.methodPtr(methodReference));
states.push(new State(file, line)); states.push(new State(file, line));
@ -92,6 +99,8 @@ public class DebugLinesBuilder extends DebugSectionBuilder implements DebugLines
@Override @Override
public void end() { public void end() {
flushPtr(); flushPtr();
indent.setLength(indent.length() - 1);
System.out.println(blob.ptr() + indent.toString() + "[end]");
blob.writeLEB(DebugConstants.LOC_END); blob.writeLEB(DebugConstants.LOC_END);
if (!states.isEmpty()) { if (!states.isEmpty()) {
var state = states.pop(); var state = states.pop();

View File

@ -104,16 +104,15 @@ public abstract class DisassemblyWriter {
if (currentSequenceIndex >= debugLines.sequences().size()) { if (currentSequenceIndex >= debugLines.sequences().size()) {
return; return;
} }
var force = false;
if (currentCommandIndex < 0) { if (currentCommandIndex < 0) {
if (addressWithinSection < debugLines.sequences().get(currentSequenceIndex).startAddress()) { if (addressWithinSection < debugLines.sequences().get(currentSequenceIndex).startAddress()) {
return; return;
} }
currentCommandIndex = 0; currentCommandIndex = 0;
force = true; printSingleDebugAnnotation("start debug line sequence");
} else { } else {
if (addressWithinSection >= debugLines.sequences().get(currentSequenceIndex).endAddress()) { if (addressWithinSection >= debugLines.sequences().get(currentSequenceIndex).endAddress()) {
printSingleDebugAnnotation("<end debug line sequence>"); printSingleDebugAnnotation("end debug line sequence");
++currentSequenceIndex; ++currentSequenceIndex;
currentCommandIndex = -1; currentCommandIndex = -1;
lineInfoIndent = 0; lineInfoIndent = 0;
@ -122,45 +121,42 @@ public abstract class DisassemblyWriter {
} }
var sequence = debugLines.sequences().get(currentSequenceIndex); var sequence = debugLines.sequences().get(currentSequenceIndex);
if (currentCommandIndex >= sequence.commands().size()) { while (currentCommandIndex < sequence.commands().size()) {
return; var command = sequence.commands().get(currentCommandIndex);
} if (addressWithinSection < command.address()) {
var command = sequence.commands().get(currentCommandIndex); break;
if (!force) {
if (currentCommandIndex + 1 < sequence.commands().size()
&& addressWithinSection >= sequence.commands().get(currentCommandIndex + 1).address()) {
command = sequence.commands().get(++currentCommandIndex);
} else {
return;
}
}
command.acceptVisitor(new LineInfoCommandVisitor() {
@Override
public void visit(LineInfoEnterCommand command) {
printSingleDebugAnnotation(" at " + command.method().fullName());
++lineInfoIndent;
} }
@Override command.acceptVisitor(new LineInfoCommandVisitor() {
public void visit(LineInfoExitCommand command) { @Override
--lineInfoIndent; public void visit(LineInfoEnterCommand command) {
} printSingleDebugAnnotation("enter inline " + command.method().cls().name() + "."
+ command.method().name());
@Override ++lineInfoIndent;
public void visit(LineInfoFileCommand command) {
if (command.file() == null) {
printSingleDebugAnnotation("at <unknown>:" + command.line());
} else {
printSingleDebugAnnotation(" at " + command.file().name() + ":" + command.line());
} }
}
@Override @Override
public void visit(LineInfoLineCommand command) { public void visit(LineInfoExitCommand command) {
printSingleDebugAnnotation(" at " + command.line()); --lineInfoIndent;
} printSingleDebugAnnotation("exit inline");
}); }
@Override
public void visit(LineInfoFileCommand command) {
if (command.file() == null) {
printSingleDebugAnnotation("at <unknown>:" + command.line());
} else {
printSingleDebugAnnotation("at " + command.file().name() + ":" + command.line());
}
}
@Override
public void visit(LineInfoLineCommand command) {
printSingleDebugAnnotation("at " + command.line());
}
});
++currentCommandIndex;
}
} }
private void printSingleDebugAnnotation(String text) { private void printSingleDebugAnnotation(String text) {
@ -172,7 +168,7 @@ public abstract class DisassemblyWriter {
out.print(" "); out.print(" ");
} }
startAnnotation(); startAnnotation();
out.print("(;"); out.print("(; ");
write(text); write(text);
out.print(" ;)"); out.print(" ;)");
endAnnotation(); endAnnotation();

View File

@ -1405,6 +1405,10 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
flushLocation(); flushLocation();
if (debugLines != null) { if (debugLines != null) {
debugLines.advance(writer.getPosition() + addressOffset); debugLines.advance(writer.getPosition() + addressOffset);
while (!methodStack.isEmpty()) {
methodStack.removeLast();
debugLines.end();
}
debugLines.end(); debugLines.end();
} }
} }
@ -1433,7 +1437,7 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
var loc = textLocationToEmit; var loc = textLocationToEmit;
var inlining = loc != null ? loc.getInlining() : null; var inlining = loc != null ? loc.getInlining() : null;
while (inlining != null) { while (inlining != null) {
currentMethodStack.add(loc.getInlining().getMethod()); currentMethodStack.add(inlining.getMethod());
inlining = inlining.getParent(); inlining = inlining.getParent();
} }
Collections.reverse(currentMethodStack); Collections.reverse(currentMethodStack);