Skip to content

Commit

Permalink
Merge ForLoop and ForLoopBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Apr 8, 2015
1 parent c4ad555 commit e94186a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 104 deletions.
Expand Up @@ -339,10 +339,10 @@ public Void visitFor(ByteCodeNode parent, ForLoop forLoop)
}
printLine("for {");
indentLevel++;
forLoop.getInitialize().accept(forLoop, this);
forLoop.getCondition().accept(forLoop, this);
forLoop.getUpdate().accept(forLoop, this);
forLoop.getBody().accept(forLoop, this);
visitNestedNode("initialize", forLoop.initialize(), forLoop);
visitNestedNode("condition", forLoop.condition(), forLoop);
visitNestedNode("update", forLoop.update(), forLoop);
visitNestedNode("body", forLoop.body(), forLoop);
indentLevel--;
printLine("}");
return null;
Expand Down
Expand Up @@ -23,89 +23,29 @@

import java.util.List;

import static com.facebook.presto.byteCode.ByteCodeNodes.buildBlock;
import static com.google.common.base.Preconditions.checkState;

public class ForLoop
implements FlowControl
{
public static ForLoopBuilder forLoopBuilder()
{
return new ForLoopBuilder();
}

public static class ForLoopBuilder
{
private final LabelNode continueLabel = new LabelNode("continue");
private final LabelNode endLabel = new LabelNode("end");

private String comment;
private ByteCodeNode initialize;
private ByteCodeNode condition;
private ByteCodeNode update;
private ByteCodeNode body;

public ForLoopBuilder comment(String format, Object... args)
{
this.comment = String.format(format, args);
return this;
}

public ForLoopBuilder initialize(ByteCodeNode initialize)
{
this.initialize = buildBlock(initialize, "initialize");
return this;
}

public ForLoopBuilder condition(ByteCodeNode condition)
{
this.condition = buildBlock(condition, "condition");
return this;
}

public ForLoopBuilder update(ByteCodeNode update)
{
this.update = buildBlock(update, "update");
return this;
}
private final String comment;
private final Block initialize = new Block();
private final Block condition = new Block();
private final Block update = new Block();
private final Block body = new Block();

public ForLoopBuilder body(ByteCodeNode body)
{
this.body = buildBlock(body, "body");
return this;
}
private final LabelNode beginLabel = new LabelNode("beginLabel");
private final LabelNode continueLabel = new LabelNode("continue");
private final LabelNode endLabel = new LabelNode("end");

public ForLoop build()
{
ForLoop forLoop = new ForLoop(comment, initialize, condition, update, body, continueLabel, endLabel);
return forLoop;
}
public ForLoop()
{
this.comment = null;
}

private final String comment;
private final ByteCodeNode initialize;
private final ByteCodeNode condition;
private final ByteCodeNode update;
private final ByteCodeNode body;

private final LabelNode beginLabel = new LabelNode("beginLabel");
private final LabelNode continueLabel;
private final LabelNode endLabel;

private ForLoop(String comment,
ByteCodeNode initialize,
ByteCodeNode condition,
ByteCodeNode update,
ByteCodeNode body,
LabelNode continueLabel,
LabelNode endLabel)
{
this.comment = comment;
this.initialize = initialize;
this.condition = condition;
this.update = update;
this.body = body;
this.continueLabel = continueLabel;
this.endLabel = endLabel;
public ForLoop(String format, Object... args)
{
this.comment = String.format(format, args);
}

@Override
Expand All @@ -114,41 +54,89 @@ public String getComment()
return comment;
}

public ByteCodeNode getInitialize()
public LabelNode getContinueLabel()
{
return continueLabel;
}

public LabelNode getEndLabel()
{
return endLabel;
}

public Block initialize()
{
return initialize;
}

public ByteCodeNode getCondition()
public ForLoop initialize(ByteCodeNode node)
{
checkState(initialize.isEmpty(), "initialize already set");
initialize.append(node);
return this;
}

public Block condition()
{
return condition;
}

public ByteCodeNode getUpdate()
public ForLoop condition(ByteCodeNode node)
{
checkState(condition.isEmpty(), "condition already set");
condition.append(node);
return this;
}

public Block update()
{
return update;
}

public ByteCodeNode getBody()
public ForLoop update(ByteCodeNode node)
{
checkState(update.isEmpty(), "update already set");
update.append(node);
return this;
}

public Block body()
{
return body;
}

public ForLoop body(ByteCodeNode node)
{
checkState(body.isEmpty(), "body already set");
body.append(node);
return this;
}

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext)
{
Block block = new Block()
.append(initialize)
.visitLabel(beginLabel)
.append(condition)
.ifZeroGoto(endLabel);
checkState(!condition.isEmpty(), "ForLoop does not have a condition set");

Block block = new Block();

block.append(new Block()
.setDescription("initialize")
.append(initialize));

block.visitLabel(beginLabel)
.append(new Block()
.setDescription("condition")
.append(condition))
.ifFalseGoto(endLabel);

if (body != null) {
block.append(body);
}
block.append(new Block()
.setDescription("body")
.append(body));

block.visitLabel(continueLabel)
.append(update)
.append(new Block()
.setDescription("update")
.append(update))
.gotoLabel(beginLabel)
.visitLabel(endLabel);

Expand Down
Expand Up @@ -335,15 +335,14 @@ else if (metadata.getParameterType() == NULLABLE_INPUT_CHANNEL) {
.ifTrue(loopBody);
}

block.append(new ForLoop.ForLoopBuilder()
block.append(new ForLoop()
.initialize(new Block(context).putVariable(positionVariable, 0))
.condition(new Block(context)
.getVariable(positionVariable)
.getVariable(rowsVariable)
.invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class))
.update(new Block(context).incrementVariable(positionVariable, (byte) 1))
.body(loopBody)
.build());
.body(loopBody));

return block;
}
Expand Down Expand Up @@ -592,15 +591,14 @@ private static Block generateBlockNonNullPositionForLoop(CompilerContext context
.invokeInterface(com.facebook.presto.spi.block.Block.class, "isNull", boolean.class, int.class))
.ifFalse(loopBody);

block.append(new ForLoop.ForLoopBuilder()
block.append(new ForLoop()
.initialize(new Block(context).putVariable(positionVariable, 0))
.condition(new Block(context)
.getVariable(positionVariable)
.getVariable(rowsVariable)
.invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class))
.update(new Block(context).incrementVariable(positionVariable, (byte) 1))
.body(ifStatement)
.build());
.body(ifStatement));

return block;
}
Expand Down
Expand Up @@ -43,7 +43,6 @@
import static com.facebook.presto.byteCode.NamedParameterDefinition.arg;
import static com.facebook.presto.byteCode.OpCode.NOP;
import static com.facebook.presto.byteCode.ParameterizedType.type;
import static com.facebook.presto.byteCode.control.ForLoop.ForLoopBuilder;
import static com.facebook.presto.sql.gen.ByteCodeUtils.generateWrite;
import static java.lang.String.format;

Expand Down Expand Up @@ -95,7 +94,7 @@ private void generateProcessMethod(ClassDefinition classDefinition, int projecti
// for loop loop body
//
LabelNode done = new LabelNode("done");
ForLoopBuilder forLoop = ForLoop.forLoopBuilder()
ForLoop forLoop = new ForLoop()
.initialize(NOP)
.condition(new Block(context)
.comment("completedPositions < count")
Expand Down Expand Up @@ -160,7 +159,7 @@ private void generateProcessMethod(ClassDefinition classDefinition, int projecti
forLoopBody.append(ifStatement);

method.getBody()
.append(forLoop.build())
.append(forLoop)
.visitLabel(done)
.comment("return completedPositions;")
.getVariable(completedPositionsVariable)
Expand Down
Expand Up @@ -49,7 +49,6 @@
import static com.facebook.presto.byteCode.NamedParameterDefinition.arg;
import static com.facebook.presto.byteCode.OpCode.NOP;
import static com.facebook.presto.byteCode.ParameterizedType.type;
import static com.facebook.presto.byteCode.control.ForLoop.ForLoopBuilder;
import static com.facebook.presto.sql.gen.ByteCodeUtils.generateWrite;
import static com.facebook.presto.sql.gen.ByteCodeUtils.loadConstant;
import static java.lang.String.format;
Expand Down Expand Up @@ -120,7 +119,7 @@ private void generateProcessMethod(ClassDefinition classDefinition, RowExpressio

Block loopBody = new Block(context);

ForLoopBuilder loop = ForLoop.forLoopBuilder()
ForLoop loop = new ForLoop()
.initialize(NOP)
.condition(new Block(context)
.comment("position < end")
Expand Down Expand Up @@ -189,7 +188,7 @@ private void generateProcessMethod(ClassDefinition classDefinition, RowExpressio
loopBody.append(filterBlock);

method.getBody()
.append(loop.build())
.append(loop)
.visitLabel(done)
.comment("return position;")
.getVariable(positionVariable)
Expand Down

0 comments on commit e94186a

Please sign in to comment.