Wasm: fix issues in DWARF generator

This commit is contained in:
Alexey Andreev 2022-11-20 08:42:32 +01:00
parent 7f38697d50
commit 1399045ff6
4 changed files with 20 additions and 12 deletions

View File

@ -173,8 +173,8 @@ class DwarfLinesGenerator {
instructionsBlob.writeLEB(fileRef); instructionsBlob.writeLEB(fileRef);
changed = true; changed = true;
} }
if (advanceTo(address, line)) { if (line != this.line || this.address != address) {
changed = true; changed = advanceTo(address, line);
} }
if (changed) { if (changed) {
instructionsBlob.writeByte(DW_LNS_COPY); instructionsBlob.writeByte(DW_LNS_COPY);
@ -186,23 +186,23 @@ class DwarfLinesGenerator {
if (!sequenceStarted) { if (!sequenceStarted) {
return; return;
} }
advanceTo(address, line); if (this.address != address) {
advanceTo(address, line);
}
instructionsBlob.writeByte(0); instructionsBlob.writeByte(0);
instructionsBlob.writeByte(1); instructionsBlob.writeByte(1);
instructionsBlob.writeByte(DW_LNE_END_SEQUENCE); instructionsBlob.writeByte(DW_LNE_END_SEQUENCE);
this.line = 1; this.line = 1;
this.file = 0; this.file = 1;
this.address = 0; this.address = 0;
sequenceStarted = false; sequenceStarted = false;
} }
private boolean advanceTo(int address, int line) { private boolean advanceTo(int address, int line) {
if (address == this.address && line == this.line) {
return false;
}
int lineIncrement = line - this.line; int lineIncrement = line - this.line;
int addressIncrement = address - this.address; int addressIncrement = address - this.address;
if (!tryEmitSpecial(lineIncrement, addressIncrement)) { var result = !tryEmitSpecial(lineIncrement, addressIncrement);
if (result) {
if (lineIncrement != 0) { if (lineIncrement != 0) {
instructionsBlob.writeByte(DW_LNS_ADVANCE_LINE); instructionsBlob.writeByte(DW_LNS_ADVANCE_LINE);
instructionsBlob.writeSLEB(lineIncrement); instructionsBlob.writeSLEB(lineIncrement);
@ -214,7 +214,7 @@ class DwarfLinesGenerator {
} }
this.line = line; this.line = line;
this.address = address; this.address = address;
return true; return result;
} }
private boolean tryEmitSpecial(int lineIncrement, int addressIncrement) { private boolean tryEmitSpecial(int lineIncrement, int addressIncrement) {

View File

@ -903,21 +903,27 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
switch (expr.getMethod().getName()) { switch (expr.getMethod().getName()) {
case "allocStack": case "allocStack":
generateAllocStack(expr.getArguments().get(0)); generateAllocStack(expr.getArguments().get(0));
result.setLocation(expr.getLocation());
return; return;
case "releaseStack": case "releaseStack":
generateReleaseStack(); generateReleaseStack();
result.setLocation(expr.getLocation());
return; return;
case "registerGCRoot": case "registerGCRoot":
generateRegisterGcRoot(expr.getArguments().get(0), expr.getArguments().get(1)); generateRegisterGcRoot(expr.getArguments().get(0), expr.getArguments().get(1));
result.setLocation(expr.getLocation());
return; return;
case "removeGCRoot": case "removeGCRoot":
generateRemoveGcRoot(expr.getArguments().get(0)); generateRemoveGcRoot(expr.getArguments().get(0));
result.setLocation(expr.getLocation());
return; return;
case "registerCallSite": case "registerCallSite":
generateRegisterCallSite(expr.getArguments().get(0)); generateRegisterCallSite(expr.getArguments().get(0));
result.setLocation(expr.getLocation());
return; return;
case "getExceptionHandlerId": case "getExceptionHandlerId":
generateGetHandlerId(); generateGetHandlerId();
result.setLocation(expr.getLocation());
return; return;
} }
} }
@ -941,6 +947,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
accept(argument); accept(argument);
call.getArguments().add(result); call.getArguments().add(result);
} }
call.setLocation(expr.getLocation());
result = call; result = call;
} else if (expr.getType() == InvocationType.CONSTRUCTOR) { } else if (expr.getType() == InvocationType.CONSTRUCTOR) {
WasmBlock block = new WasmBlock(false); WasmBlock block = new WasmBlock(false);

View File

@ -331,8 +331,8 @@ public class WasmBinaryRenderer {
if (dwarfGenerator != null && function.getName() != null) { if (dwarfGenerator != null && function.getName() != null) {
infoWriter.tag(getMethodAbbrev()); infoWriter.tag(getMethodAbbrev());
infoWriter.writeInt(dwarfGenerator.strings.stringRef(function.getName())); infoWriter.writeInt(dwarfGenerator.strings.stringRef(function.getName()));
infoWriter.writeInt(offset - 4); infoWriter.writeInt(offset);
infoWriter.writeInt(offset + code.getPosition()); infoWriter.writeInt(offset + code.getPosition() - 1);
infoWriter.emptyTag(); infoWriter.emptyTag();
} }

View File

@ -875,7 +875,8 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
} }
private void emitLocation(WasmExpression expression) { private void emitLocation(WasmExpression expression) {
if (dwarfGenerator == null || expression.getLocation() == null) { if (dwarfGenerator == null || expression.getLocation() == null
|| expression.getLocation().getFileName() == null) {
return; return;
} }
dwarfGenerator.lineNumber(writer.getPosition() + addressOffset, expression.getLocation().getFileName(), dwarfGenerator.lineNumber(writer.getPosition() + addressOffset, expression.getLocation().getFileName(),