From 62281f696cd4f333c44fb854ed0a41935ac8b50f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 27 Jul 2014 23:39:56 +0400 Subject: [PATCH] Fixes bugs in debug information generator and writer --- .../org/teavm/debugging/DebugInformationBuilder.java | 10 ++++++++-- .../org/teavm/debugging/DebugInformationEmitter.java | 3 +++ .../org/teavm/debugging/DebugInformationWriter.java | 6 +++++- .../teavm/debugging/DummyDebugInformationEmitter.java | 5 +++++ .../src/main/java/org/teavm/javascript/Decompiler.java | 2 +- .../src/main/java/org/teavm/model/BasicBlock.java | 6 +++++- .../org/teavm/model/util/InstructionStringifier.java | 2 -- teavm-core/src/main/java/org/teavm/vm/TeaVM.java | 1 + 8 files changed, 28 insertions(+), 7 deletions(-) diff --git a/teavm-core/src/main/java/org/teavm/debugging/DebugInformationBuilder.java b/teavm-core/src/main/java/org/teavm/debugging/DebugInformationBuilder.java index 8c7d8df98..bc8baaa99 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/DebugInformationBuilder.java +++ b/teavm-core/src/main/java/org/teavm/debugging/DebugInformationBuilder.java @@ -39,6 +39,7 @@ public class DebugInformationBuilder implements DebugInformationEmitter { return locationProvider; } + @Override public void setLocationProvider(LocationProvider locationProvider) { this.locationProvider = locationProvider; } @@ -114,8 +115,9 @@ public class DebugInformationBuilder implements DebugInformationEmitter { fileDesc.methodMap = fileDescProto.methodMap.toArray(new MethodReference[0]); fileDesc.generatedLocations = new GeneratedLocation[fileDescProto.generatedLocations.size()][]; for (int i = 0; i < fileDescProto.generatedLocations.size(); ++i) { - fileDesc.generatedLocations[i] = fileDescProto.generatedLocations.get(index) - .toArray(new GeneratedLocation[0]); + List locations = fileDescProto.generatedLocations.get(index); + fileDesc.generatedLocations[i] = locations != null ? + locations.toArray(new GeneratedLocation[0]) : null; } } } @@ -132,6 +134,10 @@ public class DebugInformationBuilder implements DebugInformationEmitter { line - generatedLocations.size() + 1, null)); } List existingLocations = generatedLocations.get(line); + if (existingLocations == null) { + existingLocations = new ArrayList<>(); + generatedLocations.set(line, existingLocations); + } existingLocations.add(location); } diff --git a/teavm-core/src/main/java/org/teavm/debugging/DebugInformationEmitter.java b/teavm-core/src/main/java/org/teavm/debugging/DebugInformationEmitter.java index c22a846a4..df2d3f678 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/DebugInformationEmitter.java +++ b/teavm-core/src/main/java/org/teavm/debugging/DebugInformationEmitter.java @@ -15,6 +15,7 @@ */ package org.teavm.debugging; +import org.teavm.codegen.LocationProvider; import org.teavm.model.MethodReference; /** @@ -22,6 +23,8 @@ import org.teavm.model.MethodReference; * @author Alexey Andreev */ public interface DebugInformationEmitter { + void setLocationProvider(LocationProvider locationProvider); + void emitLocation(String fileName, int line); void emitMethod(MethodReference method); diff --git a/teavm-core/src/main/java/org/teavm/debugging/DebugInformationWriter.java b/teavm-core/src/main/java/org/teavm/debugging/DebugInformationWriter.java index f6abcc6da..f4b83a6db 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/DebugInformationWriter.java +++ b/teavm-core/src/main/java/org/teavm/debugging/DebugInformationWriter.java @@ -110,7 +110,11 @@ class DebugInformationWriter { private void writeNumber(int number) throws IOException { do { - number = (number << 1) | (number >>> 31); + if (number < 0) { + number = (-number << 1) | 1; + } else { + number = number << 1; + } byte b = (byte)(number & 0x7F); if ((number & 0xFFFFFF80) != 0) { b |= 0x80; diff --git a/teavm-core/src/main/java/org/teavm/debugging/DummyDebugInformationEmitter.java b/teavm-core/src/main/java/org/teavm/debugging/DummyDebugInformationEmitter.java index 38cff54b4..1bfa50782 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/DummyDebugInformationEmitter.java +++ b/teavm-core/src/main/java/org/teavm/debugging/DummyDebugInformationEmitter.java @@ -15,6 +15,7 @@ */ package org.teavm.debugging; +import org.teavm.codegen.LocationProvider; import org.teavm.model.MethodReference; @@ -30,4 +31,8 @@ public class DummyDebugInformationEmitter implements DebugInformationEmitter { @Override public void emitMethod(MethodReference method) { } + + @Override + public void setLocationProvider(LocationProvider locationProvider) { + } } diff --git a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java index 065dd1a81..a7e186502 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java @@ -232,7 +232,7 @@ public class Decompiler { InstructionLocation lastLocation = null; NodeLocation nodeLocation = null; for (Instruction insn : generator.currentBlock.getInstructions()) { - if (lastLocation != insn.getLocation()) { + if (insn.getLocation() != null && lastLocation != insn.getLocation()) { lastLocation = insn.getLocation(); nodeLocation = new NodeLocation(lastLocation.getFileName(), lastLocation.getLine()); } diff --git a/teavm-core/src/main/java/org/teavm/model/BasicBlock.java b/teavm-core/src/main/java/org/teavm/model/BasicBlock.java index 62d493133..1ada9dc84 100644 --- a/teavm-core/src/main/java/org/teavm/model/BasicBlock.java +++ b/teavm-core/src/main/java/org/teavm/model/BasicBlock.java @@ -172,7 +172,11 @@ public class BasicBlock implements BasicBlockReader { @Override public void readInstruction(int index, InstructionReader reader) { - instructions.get(index).acceptVisitor(new InstructionReadVisitor(reader)); + Instruction insn = instructions.get(index); + if (insn.getLocation() != null) { + reader.location(insn.getLocation()); + } + insn.acceptVisitor(new InstructionReadVisitor(reader)); } @Override diff --git a/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java b/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java index be6d17556..f32559396 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java +++ b/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java @@ -37,8 +37,6 @@ public class InstructionStringifier implements InstructionReader { if (location != null) { sb.append("at " + (location.getFileName() != null ? location.getFileName() : "") + ":" + (location.getLine() >= 0 ? String.valueOf(location.getLine()) : "")); - } else { - sb.append(""); } this.location = location; } diff --git a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java index 24e0570e7..cad245984 100644 --- a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java +++ b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java @@ -351,6 +351,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository { if (debugEmitter != null) { renderer.setDebugEmitter(debugEmitter); } + debugEmitter.setLocationProvider(sourceWriter); for (Map.Entry entry : methodInjectors.entrySet()) { renderer.addInjector(entry.getKey(), entry.getValue()); }