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

View File

@ -104,16 +104,15 @@ public abstract class DisassemblyWriter {
if (currentSequenceIndex >= debugLines.sequences().size()) {
return;
}
var force = false;
if (currentCommandIndex < 0) {
if (addressWithinSection < debugLines.sequences().get(currentSequenceIndex).startAddress()) {
return;
}
currentCommandIndex = 0;
force = true;
printSingleDebugAnnotation("start debug line sequence");
} else {
if (addressWithinSection >= debugLines.sequences().get(currentSequenceIndex).endAddress()) {
printSingleDebugAnnotation("<end debug line sequence>");
printSingleDebugAnnotation("end debug line sequence");
++currentSequenceIndex;
currentCommandIndex = -1;
lineInfoIndent = 0;
@ -122,29 +121,24 @@ public abstract class DisassemblyWriter {
}
var sequence = debugLines.sequences().get(currentSequenceIndex);
if (currentCommandIndex >= sequence.commands().size()) {
return;
}
while (currentCommandIndex < sequence.commands().size()) {
var command = sequence.commands().get(currentCommandIndex);
if (!force) {
if (currentCommandIndex + 1 < sequence.commands().size()
&& addressWithinSection >= sequence.commands().get(currentCommandIndex + 1).address()) {
command = sequence.commands().get(++currentCommandIndex);
} else {
return;
}
if (addressWithinSection < command.address()) {
break;
}
command.acceptVisitor(new LineInfoCommandVisitor() {
@Override
public void visit(LineInfoEnterCommand command) {
printSingleDebugAnnotation(" at " + command.method().fullName());
printSingleDebugAnnotation("enter inline " + command.method().cls().name() + "."
+ command.method().name());
++lineInfoIndent;
}
@Override
public void visit(LineInfoExitCommand command) {
--lineInfoIndent;
printSingleDebugAnnotation("exit inline");
}
@Override
@ -161,6 +155,8 @@ public abstract class DisassemblyWriter {
printSingleDebugAnnotation("at " + command.line());
}
});
++currentCommandIndex;
}
}
private void printSingleDebugAnnotation(String text) {

View File

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