use of io.airlift.bytecode.instruction.LabelNode in project hetu-core by openlookeng.
the class BytecodeUtils method generateInvocation.
public static BytecodeNode generateInvocation(Scope scope, String name, BuiltInScalarFunctionImplementation function, Optional<BytecodeNode> instance, List<BytecodeNode> arguments, CallSiteBinder binder) {
LabelNode end = new LabelNode("end");
BytecodeBlock block = new BytecodeBlock().setDescription("invoke " + name);
List<Class<?>> stackTypes = new ArrayList<>();
if (function.getInstanceFactory().isPresent()) {
checkArgument(instance.isPresent());
}
// Index of current parameter in the MethodHandle
int currentParameterIndex = 0;
// Index of parameter (without @IsNull) in Presto function
int realParameterIndex = 0;
// Go through all the choices in the function and then pick the best one
List<ScalarImplementationChoice> choices = function.getAllChoices();
ScalarImplementationChoice bestChoice = null;
for (ScalarImplementationChoice currentChoice : choices) {
boolean isValid = true;
for (int i = 0; i < arguments.size(); i++) {
if (currentChoice.getArgumentProperty(i).getArgumentType() != VALUE_TYPE) {
continue;
}
if (!(arguments.get(i) instanceof InputReferenceNode) && currentChoice.getArgumentProperty(i).getNullConvention() == NullConvention.BLOCK_AND_POSITION) {
isValid = false;
break;
}
}
if (isValid) {
bestChoice = currentChoice;
}
}
checkState(bestChoice != null, "None of the scalar function implementation choices are valid");
Binding binding = binder.bind(bestChoice.getMethodHandle());
MethodType methodType = binding.getType();
Class<?> returnType = methodType.returnType();
Class<?> unboxedReturnType = Primitives.unwrap(returnType);
boolean boundInstance = false;
while (currentParameterIndex < methodType.parameterArray().length) {
Class<?> type = methodType.parameterArray()[currentParameterIndex];
stackTypes.add(type);
if (bestChoice.getInstanceFactory().isPresent() && !boundInstance) {
checkState(type.equals(bestChoice.getInstanceFactory().get().type().returnType()), "Mismatched type for instance parameter");
block.append(instance.get());
boundInstance = true;
} else if (type == ConnectorSession.class) {
block.append(scope.getVariable("session"));
} else {
ArgumentProperty argumentProperty = bestChoice.getArgumentProperty(realParameterIndex);
switch(argumentProperty.getArgumentType()) {
case VALUE_TYPE:
// Apply null convention for value type argument
switch(argumentProperty.getNullConvention()) {
case RETURN_NULL_ON_NULL:
block.append(arguments.get(realParameterIndex));
checkArgument(!Primitives.isWrapperType(type), "Non-nullable argument must not be primitive wrapper type");
block.append(ifWasNullPopAndGoto(scope, end, unboxedReturnType, Lists.reverse(stackTypes)));
break;
case USE_NULL_FLAG:
block.append(arguments.get(realParameterIndex));
block.append(scope.getVariable("wasNull"));
block.append(scope.getVariable("wasNull").set(constantFalse()));
stackTypes.add(boolean.class);
currentParameterIndex++;
break;
case USE_BOXED_TYPE:
block.append(arguments.get(realParameterIndex));
block.append(boxPrimitiveIfNecessary(scope, type));
block.append(scope.getVariable("wasNull").set(constantFalse()));
break;
case BLOCK_AND_POSITION:
InputReferenceNode inputReferenceNode = (InputReferenceNode) arguments.get(realParameterIndex);
block.append(inputReferenceNode.produceBlockAndPosition());
stackTypes.add(int.class);
currentParameterIndex++;
break;
default:
throw new UnsupportedOperationException(format("Unsupported null convention: %s", argumentProperty.getNullConvention()));
}
break;
case FUNCTION_TYPE:
block.append(arguments.get(realParameterIndex));
break;
default:
throw new UnsupportedOperationException(format("Unsupported argument type: %s", argumentProperty.getArgumentType()));
}
realParameterIndex++;
}
currentParameterIndex++;
}
block.append(invoke(binding, name));
if (function.isNullable()) {
block.append(unboxPrimitiveIfNecessary(scope, returnType));
}
block.visitLabel(end);
return block;
}
use of io.airlift.bytecode.instruction.LabelNode in project hetu-core by openlookeng.
the class AndCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(FunctionHandle functionHandle, BytecodeGeneratorContext generator, Type returnType, List<RowExpression> arguments) {
Preconditions.checkArgument(arguments.size() == 2);
Variable wasNull = generator.wasNull();
BytecodeBlock block = new BytecodeBlock().comment("AND").setDescription("AND");
BytecodeNode left = generator.generate(arguments.get(0));
BytecodeNode right = generator.generate(arguments.get(1));
block.append(left);
IfStatement ifLeftIsNull = new IfStatement("if left wasNull...").condition(wasNull);
LabelNode end = new LabelNode("end");
ifLeftIsNull.ifTrue().comment("clear the null flag, pop left value off stack, and push left null flag on the stack (true)").append(wasNull.set(constantFalse())).pop(// discard left value
arguments.get(0).getType().getJavaType()).push(true);
LabelNode leftIsTrue = new LabelNode("leftIsTrue");
ifLeftIsNull.ifFalse().comment("if left is false, push false, and goto end").ifTrueGoto(leftIsTrue).push(false).gotoLabel(end).comment("left was true; push left null flag on the stack (false)").visitLabel(leftIsTrue).push(false);
block.append(ifLeftIsNull);
// At this point we know the left expression was either NULL or TRUE. The stack contains a single boolean
// value for this expression which indicates if the left value was NULL.
// eval right!
block.append(right);
IfStatement ifRightIsNull = new IfStatement("if right wasNull...");
ifRightIsNull.condition().append(wasNull);
// this leaves a single boolean on the stack which is ignored since the value in NULL
ifRightIsNull.ifTrue().comment("right was null, pop the right value off the stack; wasNull flag remains set to TRUE").pop(arguments.get(1).getType().getJavaType());
LabelNode rightIsTrue = new LabelNode("rightIsTrue");
ifRightIsNull.ifFalse().comment("if right is false, pop left null flag off stack, push false and goto end").ifTrueGoto(rightIsTrue).pop(boolean.class).push(false).gotoLabel(end).comment("right was true; store left null flag (on stack) in wasNull variable, and push true").visitLabel(rightIsTrue).putVariable(wasNull).push(true);
block.append(ifRightIsNull).visitLabel(end);
return block;
}
use of io.airlift.bytecode.instruction.LabelNode in project hetu-core by openlookeng.
the class BetweenCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(FunctionHandle functionHandle, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
checkArgument(functionHandle == null, "functionHandle is null here");
RowExpression value = arguments.get(0);
RowExpression min = arguments.get(1);
RowExpression max = arguments.get(2);
Variable firstValue = generatorContext.getScope().createTempVariable(value.getType().getJavaType());
VariableReferenceExpression valueReference = createTempVariableReferenceExpression(firstValue, value.getType());
SpecialForm newExpression = new SpecialForm(BETWEEN_AND, BOOLEAN, call(GREATER_THAN_OR_EQUAL.getFunctionName().getObjectName(), generatorContext.getFunctionManager().resolveOperatorFunctionHandle(GREATER_THAN_OR_EQUAL, TypeSignatureProvider.fromTypes(value.getType(), min.getType())), BOOLEAN, valueReference, min), call(LESS_THAN_OR_EQUAL.getFunctionName().getObjectName(), generatorContext.getFunctionManager().resolveOperatorFunctionHandle(LESS_THAN_OR_EQUAL, TypeSignatureProvider.fromTypes(value.getType(), min.getType())), BOOLEAN, valueReference, max));
LabelNode done = new LabelNode("done");
// push value arg on the stack
BytecodeBlock block = new BytecodeBlock().comment("check if value is null").append(generatorContext.generate(value)).append(ifWasNullPopAndGoto(generatorContext.getScope(), done, boolean.class, value.getType().getJavaType())).putVariable(firstValue).append(generatorContext.generate(newExpression)).visitLabel(done);
return block;
}
use of io.airlift.bytecode.instruction.LabelNode in project trino by trinodb.
the class AbstractGreatestLeast method generate.
private Class<?> generate(List<Class<?>> javaTypes, MethodHandle compareMethod) {
Signature signature = getFunctionMetadata().getSignature();
checkCondition(javaTypes.size() <= 127, NOT_SUPPORTED, "Too many arguments for function call %s()", signature.getName());
String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(javaTypeName + "$" + signature.getName()), type(Object.class));
definition.declareDefaultConstructor(a(PRIVATE));
List<Parameter> parameters = IntStream.range(0, javaTypes.size()).mapToObj(i -> arg("arg" + i, javaTypes.get(i))).collect(toImmutableList());
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), signature.getName(), type(wrap(javaTypes.get(0))), parameters);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
CallSiteBinder binder = new CallSiteBinder();
Variable value = scope.declareVariable(wrap(javaTypes.get(0)), "value");
BytecodeExpression nullValue = constantNull(wrap(javaTypes.get(0)));
body.append(value.set(nullValue));
LabelNode done = new LabelNode("done");
compareMethod = compareMethod.asType(methodType(boolean.class, compareMethod.type().wrap().parameterList()));
for (int i = 0; i < javaTypes.size(); i++) {
Parameter parameter = parameters.get(i);
BytecodeExpression invokeCompare = invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binder.bind(compareMethod).getBindingId()), "compare", boolean.class, parameter, value);
body.append(new IfStatement().condition(isNull(parameter)).ifTrue(new BytecodeBlock().append(value.set(nullValue)).gotoLabel(done)));
body.append(new IfStatement().condition(or(isNull(value), invokeCompare)).ifTrue(value.set(parameter)));
}
body.visitLabel(done);
body.append(value.ret());
return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
use of io.airlift.bytecode.instruction.LabelNode in project trino by trinodb.
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's 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());
}
Aggregations