use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class JoinFilterFunctionCompiler method generateMethods.
private void generateMethods(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, RowExpression filter, int leftBlocksSize) {
CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
FieldDefinition sessionField = classDefinition.declareField(a(PRIVATE, FINAL), "session", ConnectorSession.class);
Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, filter);
generateFilterMethod(classDefinition, callSiteBinder, cachedInstanceBinder, compiledLambdaMap, filter, leftBlocksSize, sessionField);
generateConstructor(classDefinition, sessionField, cachedInstanceBinder);
}
use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class LambdaBytecodeGenerator method compileLambdaProvider.
public static Class<? extends Supplier<Object>> compileLambdaProvider(LambdaDefinitionExpression lambdaExpression, FunctionManager functionManager, Class<?> lambdaInterface) {
ClassDefinition lambdaProviderClassDefinition = new ClassDefinition(a(PUBLIC, Access.FINAL), makeClassName("LambdaProvider"), type(Object.class), type(Supplier.class, Object.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, functionManager);
MethodDefinition method = lambdaProviderClassDefinition.declareMethod(a(PUBLIC), "get", 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()), functionManager, compiledLambdaMap);
BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, scope, callSiteBinder, cachedInstanceBinder, functionManager);
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();
// noinspection unchecked
return (Class<? extends Supplier<Object>>) defineClass(lambdaProviderClassDefinition, Supplier.class, callSiteBinder.getBindings(), AccumulatorCompiler.class.getClassLoader());
}
use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class StateCompiler method generateGroupedField.
private static FieldDefinition generateGroupedField(ClassDefinition definition, MethodDefinition constructor, MethodDefinition ensureCapacity, StateField stateField) {
Class<?> bigArrayType = getBigArrayType(stateField.getType());
FieldDefinition field = definition.declareField(a(PRIVATE), UPPER_CAMEL.to(LOWER_CAMEL, stateField.getName()) + "Values", bigArrayType);
// Generate getter
MethodDefinition getter = definition.declareMethod(a(PUBLIC), stateField.getGetterName(), type(stateField.getType()));
getter.getBody().append(getter.getThis().getField(field).invoke("get", stateField.getType(), getter.getThis().invoke("getGroupId", long.class)).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().getField(field).invoke("set", void.class, setter.getThis().invoke("getGroupId", long.class), value)).ret();
Scope ensureCapacityScope = ensureCapacity.getScope();
ensureCapacity.getBody().append(ensureCapacity.getThis().getField(field).invoke("ensureCapacity", void.class, ensureCapacityScope.getVariable("size")));
// Initialize field in constructor
constructor.getBody().append(constructor.getThis().setField(field, newInstance(field.getType(), stateField.initialValueExpression())));
return field;
}
use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class StateCompiler method generateCopy.
private static void generateCopy(ClassDefinition definition, List<StateField> fields, List<FieldDefinition> fieldDefinitions) {
MethodDefinition copy = definition.declareMethod(a(PUBLIC), "copy", type(AccumulatorState.class));
Variable thisVariable = copy.getThis();
BytecodeBlock body = copy.getBody();
List<BytecodeExpression> fieldCopyExpressions = new ArrayList<>();
for (int i = 0; i < fields.size(); i++) {
Optional<BytecodeExpression> fieldCopy = copyField(thisVariable, fieldDefinitions.get(i), fields.get(i).getType());
if (fieldCopy.isEmpty()) {
body.append(newInstance(UnsupportedOperationException.class, constantString(format("copy not supported for %s (cannot copy field of type %s)", definition.getName(), fields.get(i).getType())))).throwObject();
return;
}
fieldCopyExpressions.add(fieldCopy.get());
}
Variable instanceCopy = copy.getScope().declareVariable(definition.getType(), "instanceCopy");
body.append(instanceCopy.set(newInstance(definition.getType())));
for (int i = 0; i < fieldDefinitions.size(); i++) {
FieldDefinition fieldDefinition = fieldDefinitions.get(i);
Class<?> type = fields.get(i).getType();
if (type == long.class || type == double.class || type == boolean.class || type == byte.class || type == int.class) {
body.append(instanceCopy.setField(fieldDefinition, fieldCopyExpressions.get(i)));
} else {
body.append(new IfStatement("if field value is null").condition(isNull(thisVariable.getField(fieldDefinition))).ifTrue(instanceCopy.setField(fieldDefinition, thisVariable.getField(fieldDefinition))).ifFalse(instanceCopy.setField(fieldDefinition, fieldCopyExpressions.get(i))));
}
}
copy.getBody().append(instanceCopy.ret());
}
use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class StateCompiler 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;
}
Aggregations