From 1399045ff6fba4cc7717faa5582e62571128ba61 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 20 Nov 2022 08:42:32 +0100 Subject: [PATCH] Wasm: fix issues in DWARF generator --- .../wasm/generate/DwarfLinesGenerator.java | 18 +++++++++--------- .../wasm/generate/WasmGenerationVisitor.java | 7 +++++++ .../wasm/render/WasmBinaryRenderer.java | 4 ++-- .../render/WasmBinaryRenderingVisitor.java | 3 ++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/DwarfLinesGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/DwarfLinesGenerator.java index fc01c62a5..196852dbc 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/DwarfLinesGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/DwarfLinesGenerator.java @@ -173,8 +173,8 @@ class DwarfLinesGenerator { instructionsBlob.writeLEB(fileRef); changed = true; } - if (advanceTo(address, line)) { - changed = true; + if (line != this.line || this.address != address) { + changed = advanceTo(address, line); } if (changed) { instructionsBlob.writeByte(DW_LNS_COPY); @@ -186,23 +186,23 @@ class DwarfLinesGenerator { if (!sequenceStarted) { return; } - advanceTo(address, line); + if (this.address != address) { + advanceTo(address, line); + } instructionsBlob.writeByte(0); instructionsBlob.writeByte(1); instructionsBlob.writeByte(DW_LNE_END_SEQUENCE); this.line = 1; - this.file = 0; + this.file = 1; this.address = 0; sequenceStarted = false; } private boolean advanceTo(int address, int line) { - if (address == this.address && line == this.line) { - return false; - } int lineIncrement = line - this.line; int addressIncrement = address - this.address; - if (!tryEmitSpecial(lineIncrement, addressIncrement)) { + var result = !tryEmitSpecial(lineIncrement, addressIncrement); + if (result) { if (lineIncrement != 0) { instructionsBlob.writeByte(DW_LNS_ADVANCE_LINE); instructionsBlob.writeSLEB(lineIncrement); @@ -214,7 +214,7 @@ class DwarfLinesGenerator { } this.line = line; this.address = address; - return true; + return result; } private boolean tryEmitSpecial(int lineIncrement, int addressIncrement) { diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java index a4f4d8b7b..b0c26236c 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java @@ -903,21 +903,27 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { switch (expr.getMethod().getName()) { case "allocStack": generateAllocStack(expr.getArguments().get(0)); + result.setLocation(expr.getLocation()); return; case "releaseStack": generateReleaseStack(); + result.setLocation(expr.getLocation()); return; case "registerGCRoot": generateRegisterGcRoot(expr.getArguments().get(0), expr.getArguments().get(1)); + result.setLocation(expr.getLocation()); return; case "removeGCRoot": generateRemoveGcRoot(expr.getArguments().get(0)); + result.setLocation(expr.getLocation()); return; case "registerCallSite": generateRegisterCallSite(expr.getArguments().get(0)); + result.setLocation(expr.getLocation()); return; case "getExceptionHandlerId": generateGetHandlerId(); + result.setLocation(expr.getLocation()); return; } } @@ -941,6 +947,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { accept(argument); call.getArguments().add(result); } + call.setLocation(expr.getLocation()); result = call; } else if (expr.getType() == InvocationType.CONSTRUCTOR) { WasmBlock block = new WasmBlock(false); diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java index ac8dd6914..0051a0056 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java @@ -331,8 +331,8 @@ public class WasmBinaryRenderer { if (dwarfGenerator != null && function.getName() != null) { infoWriter.tag(getMethodAbbrev()); infoWriter.writeInt(dwarfGenerator.strings.stringRef(function.getName())); - infoWriter.writeInt(offset - 4); - infoWriter.writeInt(offset + code.getPosition()); + infoWriter.writeInt(offset); + infoWriter.writeInt(offset + code.getPosition() - 1); infoWriter.emptyTag(); } 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 ff3afc7e9..e7d2582e8 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 @@ -875,7 +875,8 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { } private void emitLocation(WasmExpression expression) { - if (dwarfGenerator == null || expression.getLocation() == null) { + if (dwarfGenerator == null || expression.getLocation() == null + || expression.getLocation().getFileName() == null) { return; } dwarfGenerator.lineNumber(writer.getPosition() + addressOffset, expression.getLocation().getFileName(),