Search in sources :

Example 16 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class LambdaBytecodeGenerator method compileLambdaProvider.

public static Class<? extends LambdaProvider> compileLambdaProvider(LambdaDefinitionExpression lambdaExpression, Metadata metadata, Class<?> lambdaInterface) {
    ClassDefinition lambdaProviderClassDefinition = new ClassDefinition(a(PUBLIC, Access.FINAL), makeClassName("LambdaProvider"), type(Object.class), type(LambdaProvider.class));
    FieldDefinition sessionField = lambdaProviderClassDefinition.declareField(a(PRIVATE), "session", ConnectorSession.class);
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(lambdaProviderClassDefinition, callSiteBinder);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, lambdaExpression, metadata);
    MethodDefinition method = lambdaProviderClassDefinition.declareMethod(a(PUBLIC), "getLambda", type(Object.class), ImmutableList.of());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    scope.declareVariable("wasNull", body, constantFalse());
    scope.declareVariable("session", body, method.getThis().getField(sessionField));
    RowExpressionCompiler rowExpressionCompiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, variableReferenceCompiler(ImmutableMap.of()), metadata, compiledLambdaMap);
    BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, scope, callSiteBinder, cachedInstanceBinder, metadata.getFunctionAndTypeManager());
    body.append(generateLambda(generatorContext, ImmutableList.of(), compiledLambdaMap.get(lambdaExpression), lambdaInterface)).retObject();
    // constructor
    Parameter sessionParameter = arg("session", ConnectorSession.class);
    MethodDefinition constructorDefinition = lambdaProviderClassDefinition.declareConstructor(a(PUBLIC), sessionParameter);
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable constructorThisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(constructorThisVariable).invokeConstructor(Object.class).append(constructorThisVariable.setField(sessionField, sessionParameter));
    cachedInstanceBinder.generateInitializations(constructorThisVariable, constructorBody);
    constructorBody.ret();
    return defineClass(lambdaProviderClassDefinition, LambdaProvider.class, callSiteBinder.getBindings(), AccumulatorCompiler.class.getClassLoader());
}
Also used : Variable(io.airlift.bytecode.Variable) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) LambdaProvider(io.prestosql.operator.aggregation.LambdaProvider) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) AccumulatorCompiler(io.prestosql.operator.aggregation.AccumulatorCompiler) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 17 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class LambdaBytecodeGenerator method defineLambdaMethod.

private static CompiledLambda defineLambdaMethod(RowExpressionCompiler innerExpressionCompiler, ClassDefinition classDefinition, String methodName, List<Parameter> inputParameters, LambdaDefinitionExpression lambda) {
    checkCondition(inputParameters.size() <= 254, NOT_SUPPORTED, "Too many arguments for lambda expression");
    Class<?> returnType = Primitives.wrap(lambda.getBody().getType().getJavaType());
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(returnType), inputParameters);
    Scope scope = method.getScope();
    Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
    BytecodeNode compiledBody = innerExpressionCompiler.compile(lambda.getBody(), scope);
    method.getBody().putVariable(wasNull, false).append(compiledBody).append(boxPrimitiveIfNecessary(scope, returnType)).ret(returnType);
    Handle lambdaAsmHandle = new Handle(Opcodes.H_INVOKEVIRTUAL, method.getThis().getType().getClassName(), method.getName(), method.getMethodDescriptor(), false);
    return new CompiledLambda(lambdaAsmHandle, method.getReturnType(), method.getParameterTypes());
}
Also used : Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeNode(io.airlift.bytecode.BytecodeNode) Handle(org.objectweb.asm.Handle)

Example 18 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class CursorProcessorCompiler method generateMethods.

@Override
public void generateMethods(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, RowExpression filter, List<RowExpression> projections) {
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
    generateProcessMethod(classDefinition, projections.size());
    Map<LambdaDefinitionExpression, CompiledLambda> filterCompiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, filter, "filter");
    generateFilterMethod(classDefinition, callSiteBinder, cachedInstanceBinder, filterCompiledLambdaMap, filter);
    for (int i = 0; i < projections.size(); i++) {
        String methodName = "project_" + i;
        Map<LambdaDefinitionExpression, CompiledLambda> projectCompiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, projections.get(i), methodName);
        generateProjectMethod(classDefinition, callSiteBinder, cachedInstanceBinder, projectCompiledLambdaMap, methodName, projections.get(i));
    }
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC));
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    cachedInstanceBinder.generateInitializations(thisVariable, constructorBody);
    constructorBody.ret();
}
Also used : CompiledLambda(io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 19 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class CursorProcessorCompiler method generateProcessMethod.

private static void generateProcessMethod(ClassDefinition classDefinition, int projections) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter yieldSignal = arg("yieldSignal", DriverYieldSignal.class);
    Parameter cursor = arg("cursor", RecordCursor.class);
    Parameter pageBuilder = arg("pageBuilder", PageBuilder.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "process", type(CursorProcessorOutput.class), session, yieldSignal, cursor, pageBuilder);
    Scope scope = method.getScope();
    Variable completedPositionsVariable = scope.declareVariable(int.class, "completedPositions");
    Variable finishedVariable = scope.declareVariable(boolean.class, "finished");
    method.getBody().comment("int completedPositions = 0;").putVariable(completedPositionsVariable, 0).comment("boolean finished = false;").putVariable(finishedVariable, false);
    // while loop loop body
    LabelNode done = new LabelNode("done");
    WhileLoop whileLoop = new WhileLoop().condition(constantTrue()).body(new BytecodeBlock().comment("if (pageBuilder.isFull() || yieldSignal.isSet()) return new CursorProcessorOutput(completedPositions, false);").append(new IfStatement().condition(or(pageBuilder.invoke("isFull", boolean.class), yieldSignal.invoke("isSet", boolean.class))).ifTrue(jump(done))).comment("if (!cursor.advanceNextPosition()) return new CursorProcessorOutput(completedPositions, true);").append(new IfStatement().condition(cursor.invoke("advanceNextPosition", boolean.class)).ifFalse(new BytecodeBlock().putVariable(finishedVariable, true).gotoLabel(done))).comment("do the projection").append(createProjectIfStatement(classDefinition, method, session, cursor, pageBuilder, projections)).comment("completedPositions++;").incrementVariable(completedPositionsVariable, (byte) 1));
    method.getBody().append(whileLoop).visitLabel(done).append(newInstance(CursorProcessorOutput.class, completedPositionsVariable, finishedVariable).ret());
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) WhileLoop(io.airlift.bytecode.control.WhileLoop) CursorProcessorOutput(io.prestosql.operator.project.CursorProcessorOutput)

Example 20 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class JoinCompiler method generateCompareSortChannelPositionsMethod.

private static void generateCompareSortChannelPositionsMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> types, List<FieldDefinition> channelFields, Optional<Integer> sortChannel) {
    Parameter leftBlockIndex = arg("leftBlockIndex", int.class);
    Parameter leftBlockPosition = arg("leftBlockPosition", int.class);
    Parameter rightBlockIndex = arg("rightBlockIndex", int.class);
    Parameter rightBlockPosition = arg("rightBlockPosition", int.class);
    MethodDefinition compareMethod = classDefinition.declareMethod(a(PUBLIC), "compareSortChannelPositions", type(int.class), leftBlockIndex, leftBlockPosition, rightBlockIndex, rightBlockPosition);
    if (!sortChannel.isPresent()) {
        compareMethod.getBody().append(newInstance(UnsupportedOperationException.class)).throwObject();
        return;
    }
    Variable thisVariable = compareMethod.getThis();
    int index = sortChannel.get();
    BytecodeExpression type = constantType(callSiteBinder, types.get(index));
    BytecodeExpression leftBlock = thisVariable.getField(channelFields.get(index)).invoke("get", Object.class, leftBlockIndex).cast(Block.class);
    BytecodeExpression rightBlock = thisVariable.getField(channelFields.get(index)).invoke("get", Object.class, rightBlockIndex).cast(Block.class);
    BytecodeNode comparison = type.invoke("compareTo", int.class, leftBlock, leftBlockPosition, rightBlock, rightBlockPosition).ret();
    compareMethod.getBody().append(comparison);
}
Also used : Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) BytecodeNode(io.airlift.bytecode.BytecodeNode) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Aggregations

MethodDefinition (io.airlift.bytecode.MethodDefinition)126 Parameter (io.airlift.bytecode.Parameter)104 Variable (io.airlift.bytecode.Variable)102 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)83 Scope (io.airlift.bytecode.Scope)53 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)52 IfStatement (io.airlift.bytecode.control.IfStatement)45 ClassDefinition (io.airlift.bytecode.ClassDefinition)32 FieldDefinition (io.airlift.bytecode.FieldDefinition)28 BytecodeNode (io.airlift.bytecode.BytecodeNode)24 ImmutableList (com.google.common.collect.ImmutableList)21 ArrayList (java.util.ArrayList)20 LabelNode (io.airlift.bytecode.instruction.LabelNode)19 ForLoop (io.airlift.bytecode.control.ForLoop)17 Type (io.trino.spi.type.Type)15 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)13 List (java.util.List)13 Block (io.prestosql.spi.block.Block)12 BlockBuilder (io.prestosql.spi.block.BlockBuilder)12 SqlTypeBytecodeExpression.constantType (io.trino.sql.gen.SqlTypeBytecodeExpression.constantType)12