Skip to content

Commit

Permalink
Issue #2130: measure iteration with step: generate code which special…
Browse files Browse the repository at this point in the history
… cases by == 1 to avoid incorrect results in situations of overflow.
  • Loading branch information
tombentley committed Jun 16, 2015
1 parent 960b8f3 commit e003496
Showing 1 changed file with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ abstract class IndexedAccessIterationOptimization extends ForStatementTransforma
protected final SyntheticName indexName;
protected final Tree.Term baseIterable;
protected final Tree.Term step;

protected final SyntheticName stepName;
IndexedAccessIterationOptimization(Tree.ForStatement stmt, Tree.Term baseIterable, Tree.Term step, Type elementType, String indexableName) {
this(stmt, baseIterable, step, elementType, indexableName, "length", "i");
}
Expand All @@ -1654,6 +1654,11 @@ abstract class IndexedAccessIterationOptimization extends ForStatementTransforma
super(stmt);
this.baseIterable = baseIterable;
this.step = step;
if (step != null) {
stepName = naming.alias("step");
} else {
stepName = null;
}
this.elementType = elementType;
this.indexableName = naming.alias(indexableName);
this.lengthName = naming.alias(lengthName);
Expand Down Expand Up @@ -1681,10 +1686,10 @@ protected ListBuffer<JCStatement> transformForClause() {
}

// int step = ...
final SyntheticName stepName;

if (this.step != null) {
JCExpression stepExpr = makeStepExpr();
stepName = naming.alias("step");

result.add(makeVar(FINAL, stepName,
makeIndexType(),
stepExpr));
Expand All @@ -1698,8 +1703,6 @@ protected ListBuffer<JCStatement> transformForClause() {
.build()),
null));

} else {
stepName = null;
}

// int i = 0;
Expand Down Expand Up @@ -2217,10 +2220,12 @@ protected JCExpression makeIndexedAccess() {
if (step == null && elementType.isExactly(typeFact().getIntegerType())) {
return make().Binary(JCTree.PLUS, indexName.makeIdent(), indexableName.makeIdent());
} else {
return make().Apply(null,
naming.makeSelect(makeJavaType(elementType, JT_NO_PRIMITIVES), "neighbour"),
List.<JCExpression>of(
indexableName.makeIdent(), indexName.makeIdent()));
return make().Conditional(make().Binary(JCTree.EQ, stepName.makeIdent(), make().Literal(1L)),
make().Binary(JCTree.PLUS, indexName.makeIdent(), indexableName.makeIdent()),
make().Apply(null,
naming.makeSelect(makeJavaType(elementType, JT_NO_PRIMITIVES), "neighbour"),
List.<JCExpression>of(
indexableName.makeIdent(), indexName.makeIdent())));
}
}

Expand Down Expand Up @@ -2250,10 +2255,12 @@ protected JCExpression makeIncrement(SyntheticName stepName) {
return make().Unary(JCTree.POSTINC, indexName.makeIdent());
} else {
return make().Assign(indexName.makeIdent(),
make().Apply(null,
naming.makeSelect(make().Type(syms().ceylonIntegerType), "neighbour"),
List.<JCExpression>of(
indexName.makeIdent(), stepName.makeIdent())));
make().Conditional(make().Binary(JCTree.EQ, stepName.makeIdent(), make().Literal(1L)),
make().Binary(JCTree.PLUS, indexName.makeIdent(), make().Literal(1L)),
make().Apply(null,
naming.makeSelect(make().Type(syms().ceylonIntegerType), "neighbour"),
List.<JCExpression>of(
indexName.makeIdent(), stepName.makeIdent()))));
}
}
}
Expand Down

0 comments on commit e003496

Please sign in to comment.