Search in sources :

Example 6 with FieldDefinition

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

the class PageFunctionCompiler method definePageProjectWorkClass.

private ClassDefinition definePageProjectWorkClass(RowExpression projection, CallSiteBinder callSiteBinder, Optional<String> classNameSuffix) {
    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), generateProjectionWorkClassName(classNameSuffix), type(Object.class), type(Work.class));
    FieldDefinition blockBuilderField = classDefinition.declareField(a(PRIVATE), "blockBuilder", BlockBuilder.class);
    FieldDefinition sessionField = classDefinition.declareField(a(PRIVATE), "session", ConnectorSession.class);
    FieldDefinition pageField = classDefinition.declareField(a(PRIVATE), "page", Page.class);
    FieldDefinition selectedPositionsField = classDefinition.declareField(a(PRIVATE), "selectedPositions", SelectedPositions.class);
    FieldDefinition nextIndexOrPositionField = classDefinition.declareField(a(PRIVATE), "nextIndexOrPosition", int.class);
    FieldDefinition resultField = classDefinition.declareField(a(PRIVATE), "result", Block.class);
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
    // process
    generateProcessMethod(classDefinition, blockBuilderField, sessionField, pageField, selectedPositionsField, nextIndexOrPositionField, resultField);
    // getResult
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "getResult", type(Object.class), ImmutableList.of());
    method.getBody().append(method.getThis().getField(resultField)).ret(Object.class);
    // evaluate
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, projection);
    generateEvaluateMethod(classDefinition, callSiteBinder, cachedInstanceBinder, compiledLambdaMap, projection, blockBuilderField);
    // constructor
    Parameter blockBuilder = arg("blockBuilder", BlockBuilder.class);
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter selectedPositions = arg("selectedPositions", SelectedPositions.class);
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), blockBuilder, session, page, selectedPositions);
    BytecodeBlock body = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    body.comment("super();").append(thisVariable).invokeConstructor(Object.class).append(thisVariable.setField(blockBuilderField, blockBuilder)).append(thisVariable.setField(sessionField, session)).append(thisVariable.setField(pageField, page)).append(thisVariable.setField(selectedPositionsField, selectedPositions)).append(thisVariable.setField(nextIndexOrPositionField, selectedPositions.invoke("getOffset", int.class))).append(thisVariable.setField(resultField, constantNull(Block.class)));
    cachedInstanceBinder.generateInitializations(thisVariable, body);
    body.ret();
    return classDefinition;
}
Also used : CompiledLambda(io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Work(io.prestosql.operator.Work) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) ClassDefinition(io.airlift.bytecode.ClassDefinition) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 7 with FieldDefinition

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

the class StateCompiler method generateSingleStateClass.

private static <T> Class<? extends T> generateSingleStateClass(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("Single" + clazz.getSimpleName()), type(Object.class), type(clazz), type(Restorable.class));
    FieldDefinition instanceSize = generateInstanceSize(definition);
    // Add getter for class size
    definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)).getBody().getStaticField(instanceSize).retLong();
    // Generate constructor
    MethodDefinition constructor = definition.declareConstructor(a(PUBLIC));
    constructor.getBody().append(constructor.getThis()).invokeConstructor(Object.class);
    // Generate fields
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    for (StateField field : fields) {
        generateField(definition, constructor, field);
    }
    constructor.getBody().ret();
    Parameter captureSerdeProvider = arg("serdeProvider", BlockEncodingSerdeProvider.class);
    MethodDefinition capture = definition.declareMethod(a(PUBLIC), "capture", type(Object.class), captureSerdeProvider);
    Variable myState = capture.getScope().declareVariable(List.class, "myState");
    capture.getBody().append(myState.set(newInstance(ArrayList.class)));
    Variable captureBlockEncodingSerde = capture.getScope().declareVariable(BlockEncodingSerde.class, "captureBlockEncodingSerde");
    capture.getBody().append(captureBlockEncodingSerde.set(captureSerdeProvider.invoke("getBlockEncodingSerde", BlockEncodingSerde.class)));
    Variable obj = capture.getScope().declareVariable(Object.class, "obj");
    for (int i = 1; i < definition.getFields().size(); i++) {
        capture.getBody().append(obj.set(invokeStatic(SnapshotUtils.class, "captureHelper", Object.class, capture.getThis().getField(definition.getFields().get(i)).cast(Object.class), captureSerdeProvider)));
        capture.getBody().append(myState.invoke("add", boolean.class, obj));
    }
    capture.getBody().append(myState.ret());
    Parameter state = arg("state", Object.class);
    Parameter restoreSerdeProvider = arg("serdeProvider", BlockEncodingSerdeProvider.class);
    MethodDefinition restore = definition.declareMethod(a(PUBLIC), "restore", type(void.class), state, restoreSerdeProvider);
    Variable restoreBlockEncodingSerde = restore.getScope().declareVariable(BlockEncodingSerde.class, "restoreBlockEncodingSerde");
    restore.getBody().append(restoreBlockEncodingSerde.set(restoreSerdeProvider.invoke("getBlockEncodingSerde", BlockEncodingSerde.class)));
    Variable restoreState = restore.getScope().declareVariable(List.class, "restoreState");
    restore.getBody().append(restoreState.set(state.cast(List.class)));
    Variable value = restore.getScope().declareVariable(Object.class, "value");
    for (int i = 0; i < fields.size(); i++) {
        restore.getBody().append(value.set(restoreState.invoke("get", Object.class, constantInt(i))));
        restore.getBody().append(value.set(invokeStatic(SnapshotUtils.class, "restoreHelper", Object.class, value, constantClass(fields.get(i).getType()), restoreSerdeProvider)));
        restore.getBody().append(restore.getThis().invoke(fields.get(i).getSetterName(), void.class, value.cast(fields.get(i).getType())));
    }
    restore.getBody().ret();
    return defineClass(definition, clazz, classLoader);
}
Also used : Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Parameter(io.airlift.bytecode.Parameter) ClassDefinition(io.airlift.bytecode.ClassDefinition) Restorable(io.prestosql.spi.snapshot.Restorable)

Example 8 with FieldDefinition

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

the class StateCompiler method generateField.

private static void generateField(ClassDefinition definition, MethodDefinition constructor, StateField stateField) {
    FieldDefinition field = definition.declareField(a(PRIVATE), UPPER_CAMEL.to(LOWER_CAMEL, stateField.getName()) + "Value", stateField.getType());
    // Generate getter
    MethodDefinition getter = definition.declareMethod(a(PUBLIC), stateField.getGetterName(), type(stateField.getType()));
    getter.getBody().append(getter.getThis().getField(field).ret());
    // Generate setter
    Parameter value = arg("value", stateField.getType());
    MethodDefinition setter = definition.declareMethod(a(PUBLIC), stateField.getSetterName(), type(void.class), value);
    setter.getBody().append(setter.getThis().setField(field, value)).ret();
    constructor.getBody().append(constructor.getThis().setField(field, stateField.initialValueExpression()));
}
Also used : MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Parameter(io.airlift.bytecode.Parameter)

Example 9 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.

the class JoinCompiler method generateInstanceSize.

private static FieldDefinition generateInstanceSize(ClassDefinition definition) {
    // Store instance size in static field
    FieldDefinition instanceSize = definition.declareField(a(PRIVATE, STATIC, FINAL), "INSTANCE_SIZE", long.class);
    definition.getClassInitializer().getBody().comment("INSTANCE_SIZE = ClassLayout.parseClass(%s.class).instanceSize()", definition.getName()).push(definition.getType()).invokeStatic(ClassLayout.class, "parseClass", ClassLayout.class, Class.class).invokeVirtual(ClassLayout.class, "instanceSize", int.class).intToLong().putStaticField(instanceSize);
    return instanceSize;
}
Also used : FieldDefinition(io.airlift.bytecode.FieldDefinition) ClassLayout(org.openjdk.jol.info.ClassLayout) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass)

Example 10 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.

the class JoinCompiler method generateIsPositionNull.

private static void generateIsPositionNull(ClassDefinition classDefinition, List<FieldDefinition> joinChannelFields) {
    Parameter blockIndex = arg("blockIndex", int.class);
    Parameter blockPosition = arg("blockPosition", int.class);
    MethodDefinition isPositionNullMethod = classDefinition.declareMethod(a(PUBLIC), "isPositionNull", type(boolean.class), blockIndex, blockPosition);
    for (FieldDefinition joinChannelField : joinChannelFields) {
        BytecodeExpression block = isPositionNullMethod.getThis().getField(joinChannelField).invoke("get", Object.class, blockIndex).cast(Block.class);
        IfStatement ifStatement = new IfStatement();
        ifStatement.condition(block.invoke("isNull", boolean.class, blockPosition));
        ifStatement.ifTrue(constantTrue().ret());
        isPositionNullMethod.getBody().append(ifStatement);
    }
    isPositionNullMethod.getBody().append(constantFalse().ret());
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Aggregations

FieldDefinition (io.airlift.bytecode.FieldDefinition)50 MethodDefinition (io.airlift.bytecode.MethodDefinition)27 Variable (io.airlift.bytecode.Variable)25 Parameter (io.airlift.bytecode.Parameter)20 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)19 ArrayList (java.util.ArrayList)17 ClassDefinition (io.airlift.bytecode.ClassDefinition)15 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)14 Scope (io.airlift.bytecode.Scope)6 IfStatement (io.airlift.bytecode.control.IfStatement)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 LambdaDefinitionExpression (io.prestosql.spi.relation.LambdaDefinitionExpression)4 AccumulatorState (io.trino.spi.function.AccumulatorState)4 LambdaDefinitionExpression (io.trino.sql.relational.LambdaDefinitionExpression)4 List (java.util.List)4 CompiledLambda (io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda)3 GroupedAccumulatorState (io.trino.spi.function.GroupedAccumulatorState)3 CompiledLambda (io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda)3 CompilerUtils.defineClass (io.trino.util.CompilerUtils.defineClass)3