Search in sources :

Example 1 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class IfStatement method accept.

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
    checkState(!condition.isEmpty(), "IfStatement does not have a condition set");
    checkState(!ifTrue.isEmpty() || !ifFalse.isEmpty(), "IfStatement does not have a true or false block set");
    BytecodeBlock block = new BytecodeBlock();
    // if !condition goto false;
    block.append(new BytecodeBlock().setDescription("condition").append(condition));
    block.ifFalseGoto(falseLabel);
    if (!ifTrue.isEmpty()) {
        block.append(new BytecodeBlock().setDescription("ifTrue").append(ifTrue));
    }
    if (!ifFalse.isEmpty()) {
        // close true case by skipping to end
        block.gotoLabel(outLabel);
        block.visitLabel(falseLabel);
        block.append(new BytecodeBlock().setDescription("ifFalse").append(ifFalse));
        block.visitLabel(outLabel);
    } else {
        block.visitLabel(falseLabel);
    }
    block.accept(visitor, generationContext);
}
Also used : BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 2 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class TryCatch method accept.

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
    LabelNode tryStart = new LabelNode("tryStart");
    LabelNode tryEnd = new LabelNode("tryEnd");
    LabelNode handler = new LabelNode("handler");
    LabelNode done = new LabelNode("done");
    BytecodeBlock block = new BytecodeBlock();
    // try block
    block.visitLabel(tryStart).append(tryNode).visitLabel(tryEnd).gotoLabel(done);
    // handler block
    block.visitLabel(handler).append(catchNode);
    // all done
    block.visitLabel(done);
    block.accept(visitor, generationContext);
    visitor.visitTryCatchBlock(tryStart.getLabel(), tryEnd.getLabel(), handler.getLabel(), exceptionName);
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 3 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class WhileLoop method accept.

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
    checkState(!condition.isEmpty(), "WhileLoop does not have a condition set");
    BytecodeBlock block = new BytecodeBlock().visitLabel(continueLabel).append(condition).ifZeroGoto(endLabel).append(body).gotoLabel(continueLabel).visitLabel(endLabel);
    block.accept(visitor, generationContext);
}
Also used : BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 4 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class AndBytecodeExpression method getBytecode.

@Override
public BytecodeNode getBytecode(MethodGenerationContext generationContext) {
    LabelNode falseLabel = new LabelNode("false");
    LabelNode endLabel = new LabelNode("end");
    return new BytecodeBlock().append(left).ifFalseGoto(falseLabel).append(right).ifFalseGoto(falseLabel).push(true).gotoLabel(endLabel).visitLabel(falseLabel).push(false).visitLabel(endLabel);
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 5 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class AbstractMinMaxBy method generateInputMethod.

private void generateInputMethod(ClassDefinition definition, CallSiteBinder binder, MethodHandle compareMethod, Type keyType, Type valueType, Class<?> stateClass) {
    Parameter state = arg("state", stateClass);
    Parameter value = arg("value", Block.class);
    Parameter key = arg("key", Block.class);
    Parameter position = arg("position", int.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "input", type(void.class), state, value, key, position);
    if (keyType.equals(UNKNOWN)) {
        method.getBody().ret();
        return;
    }
    SqlTypeBytecodeExpression keySqlType = constantType(binder, keyType);
    BytecodeBlock ifBlock = new BytecodeBlock().append(state.invoke("setFirst", void.class, keySqlType.getValue(key, position))).append(state.invoke("setFirstNull", void.class, constantBoolean(false))).append(state.invoke("setSecondNull", void.class, value.invoke("isNull", boolean.class, position)));
    if (!valueType.equals(UNKNOWN)) {
        SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType);
        ifBlock.append(new IfStatement().condition(value.invoke("isNull", boolean.class, position)).ifFalse(state.invoke("setSecond", void.class, valueSqlType.getValue(value, position))));
    }
    method.getBody().append(new IfStatement().condition(or(state.invoke("isFirstNull", boolean.class), and(not(key.invoke("isNull", boolean.class, position)), loadConstant(binder, compareMethod, MethodHandle.class).invoke("invokeExact", boolean.class, keySqlType.getValue(key, position), state.invoke("getFirst", keyType.getJavaType()))))).ifTrue(ifBlock)).ret();
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) Signature.orderableTypeParameter(com.facebook.presto.metadata.Signature.orderableTypeParameter) SqlTypeBytecodeExpression(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)82 Variable (com.facebook.presto.bytecode.Variable)57 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)43 IfStatement (com.facebook.presto.bytecode.control.IfStatement)42 Parameter (com.facebook.presto.bytecode.Parameter)38 Scope (com.facebook.presto.bytecode.Scope)33 Block (com.facebook.presto.spi.block.Block)23 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)18 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)18 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)17 ImmutableList (com.google.common.collect.ImmutableList)16 ForLoop (com.facebook.presto.bytecode.control.ForLoop)12 Type (com.facebook.presto.spi.type.Type)12 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)11 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)11 LazyBlock (com.facebook.presto.spi.block.LazyBlock)11 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)11 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)10 List (java.util.List)9 RowExpression (com.facebook.presto.sql.relational.RowExpression)7