From 86003b45ca7a2132286c6f982a23f9c425f11b27 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 24 Aug 2014 17:37:57 +0400 Subject: [PATCH] Fixes generation of CFG debug information --- .../main/java/org/teavm/model/BasicBlock.java | 4 +-- .../model/util/LocationGraphBuilder.java | 2 +- .../java/org/teavm/parsing/ProgramParser.java | 33 +++++++++---------- 3 files changed, 18 insertions(+), 21 deletions(-) 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 1ada9dc84..066df2651 100644 --- a/teavm-core/src/main/java/org/teavm/model/BasicBlock.java +++ b/teavm-core/src/main/java/org/teavm/model/BasicBlock.java @@ -173,9 +173,7 @@ public class BasicBlock implements BasicBlockReader { @Override public void readInstruction(int index, InstructionReader reader) { Instruction insn = instructions.get(index); - if (insn.getLocation() != null) { - reader.location(insn.getLocation()); - } + reader.location(insn.getLocation()); insn.acceptVisitor(new InstructionReadVisitor(reader)); } diff --git a/teavm-core/src/main/java/org/teavm/model/util/LocationGraphBuilder.java b/teavm-core/src/main/java/org/teavm/model/util/LocationGraphBuilder.java index 96341b79d..1339dd985 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/LocationGraphBuilder.java +++ b/teavm-core/src/main/java/org/teavm/model/util/LocationGraphBuilder.java @@ -66,7 +66,7 @@ class LocationGraphBuilder { for (Instruction insn : block.getInstructions()) { if (insn.getLocation() != null) { if (!started) { - step.startLocations.add(location); + step.startLocations.add(insn.getLocation()); } started = true; if (blockLocations[step.block] == null) { diff --git a/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java b/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java index 556046c9e..cf8a534f5 100644 --- a/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java +++ b/teavm-core/src/main/java/org/teavm/parsing/ProgramParser.java @@ -45,9 +45,6 @@ public class ProgramParser implements VariableDebugInformation { private int minLocal; private Program program; private String currentClassName; - private int currentLineNumber; - private boolean lineNumberChanged; - private InstructionLocation lastInsnLocation; private Map> localVariableMap = new HashMap<>(); private Map> variableDebugNames = new HashMap<>(); @@ -88,8 +85,6 @@ public class ProgramParser implements VariableDebugInformation { } public Program parse(MethodNode method, String className) { - currentLineNumber = -1; - lineNumberChanged = true; program = new Program(); this.currentClassName = className; InsnList instructions = method.instructions; @@ -103,7 +98,7 @@ public class ProgramParser implements VariableDebugInformation { insn.setTarget(program.basicBlockAt(1)); program.basicBlockAt(0).getInstructions().add(insn); doAnalyze(method); - assemble(); + assemble(method); for (int i = 0; i < program.basicBlockCount(); ++i) { BasicBlock block = program.basicBlockAt(i); for (int j = 0; j < block.getTryCatchBlocks().size(); ++j) { @@ -266,9 +261,11 @@ public class ProgramParser implements VariableDebugInformation { } } - private void assemble() { + private void assemble(MethodNode methodNode) { BasicBlock basicBlock = null; Map accumulatedDebugNames = new HashMap<>(); + Integer lastLineNumber = null; + InstructionLocation lastLocation = null; for (int i = 0; i < basicBlocks.size(); ++i) { BasicBlock newBasicBlock = basicBlocks.get(i); if (newBasicBlock != null) { @@ -296,7 +293,19 @@ public class ProgramParser implements VariableDebugInformation { } accumulatedDebugNames.putAll(debugNames); } + AbstractInsnNode insnNode = methodNode.instructions.get(i); + if (insnNode instanceof LabelNode) { + Label label = ((LabelNode)insnNode).getLabel(); + Integer lineNumber = lineNumbers.get(label); + if (lineNumber != null && !lineNumber.equals(lastLineNumber)) { + lastLineNumber = lineNumber; + lastLocation = new InstructionLocation(fileName, lastLineNumber); + } + } if (builtInstructions != null) { + for (Instruction insn : builtInstructions) { + insn.setLocation(lastLocation); + } basicBlock.getInstructions().addAll(builtInstructions); } } @@ -347,11 +356,6 @@ public class ProgramParser implements VariableDebugInformation { } private void addInstruction(Instruction insn) { - if (lineNumberChanged) { - lastInsnLocation = new InstructionLocation(fileName, currentLineNumber); - lineNumberChanged = false; - } - insn.setLocation(lastInsnLocation); builder.add(insn); } @@ -602,11 +606,6 @@ public class ProgramParser implements VariableDebugInformation { @Override public void visitLabel(Label label) { - Integer lineNumber = lineNumbers.get(label); - if (lineNumber != null) { - currentLineNumber = lineNumber; - lineNumberChanged = true; - } } private void emitBranching(BranchingCondition condition, int value, int target) {