From f95250ddf7228378ba9009aa982b492ef25bd675 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 6 Oct 2024 16:54:57 +0200 Subject: [PATCH] wasm gc: fix inlining support in debug info builder, fix support of debug info in disassembler --- .../backend/wasm/debug/DebugLinesBuilder.java | 9 +++ .../wasm/disasm/DisassemblyWriter.java | 74 +++++++++---------- .../render/WasmBinaryRenderingVisitor.java | 6 +- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/debug/DebugLinesBuilder.java b/core/src/main/java/org/teavm/backend/wasm/debug/DebugLinesBuilder.java index 9f577e05b..41759552b 100644 --- a/core/src/main/java/org/teavm/backend/wasm/debug/DebugLinesBuilder.java +++ b/core/src/main/java/org/teavm/backend/wasm/debug/DebugLinesBuilder.java @@ -28,6 +28,7 @@ public class DebugLinesBuilder extends DebugSectionBuilder implements DebugLines private String file; private int line = 1; private Deque 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(); diff --git a/core/src/main/java/org/teavm/backend/wasm/disasm/DisassemblyWriter.java b/core/src/main/java/org/teavm/backend/wasm/disasm/DisassemblyWriter.java index 6336368c9..eb3325bce 100644 --- a/core/src/main/java/org/teavm/backend/wasm/disasm/DisassemblyWriter.java +++ b/core/src/main/java/org/teavm/backend/wasm/disasm/DisassemblyWriter.java @@ -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(""); + printSingleDebugAnnotation("end debug line sequence"); ++currentSequenceIndex; currentCommandIndex = -1; lineInfoIndent = 0; @@ -122,45 +121,42 @@ public abstract class DisassemblyWriter { } var sequence = debugLines.sequences().get(currentSequenceIndex); - if (currentCommandIndex >= sequence.commands().size()) { - return; - } - 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; - } - } - - command.acceptVisitor(new LineInfoCommandVisitor() { - @Override - public void visit(LineInfoEnterCommand command) { - printSingleDebugAnnotation(" at " + command.method().fullName()); - ++lineInfoIndent; + while (currentCommandIndex < sequence.commands().size()) { + var command = sequence.commands().get(currentCommandIndex); + if (addressWithinSection < command.address()) { + break; } - @Override - public void visit(LineInfoExitCommand command) { - --lineInfoIndent; - } - - @Override - public void visit(LineInfoFileCommand command) { - if (command.file() == null) { - printSingleDebugAnnotation("at :" + command.line()); - } else { - printSingleDebugAnnotation(" at " + command.file().name() + ":" + command.line()); + command.acceptVisitor(new LineInfoCommandVisitor() { + @Override + public void visit(LineInfoEnterCommand command) { + printSingleDebugAnnotation("enter inline " + command.method().cls().name() + "." + + command.method().name()); + ++lineInfoIndent; } - } - @Override - public void visit(LineInfoLineCommand command) { - printSingleDebugAnnotation(" at " + command.line()); - } - }); + @Override + public void visit(LineInfoExitCommand command) { + --lineInfoIndent; + printSingleDebugAnnotation("exit inline"); + } + + @Override + public void visit(LineInfoFileCommand command) { + if (command.file() == null) { + printSingleDebugAnnotation("at :" + 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) { @@ -172,7 +168,7 @@ public abstract class DisassemblyWriter { out.print(" "); } startAnnotation(); - out.print("(;"); + out.print("(; "); write(text); out.print(" ;)"); endAnnotation(); diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java index d2682c930..5894ceb46 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java @@ -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);