Skip to content

Commit

Permalink
Remove line number tracking from CompileContext
Browse files Browse the repository at this point in the history
Only output line number byte code if line number changes during generation
  • Loading branch information
dain committed Apr 8, 2015
1 parent fd8a88e commit 84f5799
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
Expand Up @@ -61,6 +61,7 @@ public class Block
private final List<ByteCodeNode> nodes = new ArrayList<>(); private final List<ByteCodeNode> nodes = new ArrayList<>();


private String description; private String description;
private int currentLineNumber = -1;


public Block(CompilerContext context) public Block(CompilerContext context)
{ {
Expand Down Expand Up @@ -995,14 +996,12 @@ public Block putObjectArrayElement()
return this; return this;
} }


public Block visitLineNumber(int line) public Block visitLineNumber(int currentLineNumber)
{ {
if (line <= 0) { checkArgument(currentLineNumber >= 0, "currentLineNumber must be positive");
context.cleanLineNumber(); if (this.currentLineNumber != currentLineNumber) {
} nodes.add(new LineNumberNode(currentLineNumber));
else if (!context.hasVisitedLine(line)) { this.currentLineNumber = currentLineNumber;
nodes.add(new LineNumberNode(line));
context.visitLine(line);
} }
return this; return this;
} }
Expand Down
Expand Up @@ -40,8 +40,6 @@ public class CompilerContext
private final LabelNode variableStartLabel = new LabelNode("VariableStart"); private final LabelNode variableStartLabel = new LabelNode("VariableStart");
private final LabelNode variableEndLabel = new LabelNode("VariableEnd"); private final LabelNode variableEndLabel = new LabelNode("VariableEnd");


private Integer currentLine;

public CompilerContext() public CompilerContext()
{ {
} }
Expand Down Expand Up @@ -119,21 +117,6 @@ public IterationScope peekIterationScope(int level)
return Iterators.get(iterationScopes.iterator(), level - 1, null); return Iterators.get(iterationScopes.iterator(), level - 1, null);
} }


public boolean hasVisitedLine(Integer line)
{
return this.currentLine != null && line.intValue() == this.currentLine.intValue();
}

public void cleanLineNumber()
{
this.currentLine = null;
}

public void visitLine(int currentLine)
{
this.currentLine = currentLine;
}

public LabelNode getVariableStartLabel() public LabelNode getVariableStartLabel()
{ {
return variableStartLabel; return variableStartLabel;
Expand Down
Expand Up @@ -15,4 +15,15 @@


public class MethodGenerationContext public class MethodGenerationContext
{ {
private int currentLineNumber = -1;

public boolean updateLineNumber(int lineNumber)
{
if (lineNumber == currentLineNumber) {
return false;
}

currentLineNumber = lineNumber;
return true;
}
} }
Expand Up @@ -38,7 +38,9 @@ public LineNumberNode(int lineNumber)
@Override @Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) public void accept(MethodVisitor visitor, MethodGenerationContext generationContext)
{ {
visitor.visitLineNumber(lineNumber, label.getLabel()); if (generationContext.updateLineNumber(lineNumber)) {
visitor.visitLineNumber(lineNumber, label.getLabel());
}
} }


public int getLineNumber() public int getLineNumber()
Expand Down

0 comments on commit 84f5799

Please sign in to comment.