public static BytecodeNode boxPrimitive(Class<?> type) { BytecodeBlock block = new BytecodeBlock().comment("box primitive"); if (type == long.class) { return block.invokeStatic(Long.class, "valueOf", Long.class, long.class); } if (type == double.class) { return block.invokeStatic(Double.class, "valueOf", Double.class, double.class); } if (type == boolean.class) { return block.invokeStatic(Boolean.class, "valueOf", Boolean.class, boolean.class); } if (type.isPrimitive()) { throw new UnsupportedOperationException("not yet implemented: " + type); } return NOP; }
public static BytecodeNode unboxPrimitive(Class<?> unboxedType) { BytecodeBlock block = new BytecodeBlock().comment("unbox primitive"); if (unboxedType == long.class) { return block.invokeVirtual(Long.class, "longValue", long.class); } if (unboxedType == double.class) { return block.invokeVirtual(Double.class, "doubleValue", double.class); } if (unboxedType == boolean.class) { return block.invokeVirtual(Boolean.class, "booleanValue", boolean.class); } throw new UnsupportedOperationException("not yet implemented: " + unboxedType); }
private static void generateGetSerializedType(ClassDefinition definition, List<StateField> fields, CallSiteBinder callSiteBinder) { BytecodeBlock body = definition.declareMethod(a(PUBLIC), "getSerializedType", type(Type.class)).getBody(); Type type; if (fields.size() > 1) { List<Type> types = fields.stream().map(StateField::getSqlType).collect(toImmutableList()); type = RowType.anonymous(types); } else if (fields.size() == 1) { type = getOnlyElement(fields).getSqlType(); } else { type = UNKNOWN; } body.comment("return %s", type.getTypeSignature()) .append(constantType(callSiteBinder, type)) .retObject(); }
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext context, Type returnType, List<RowExpression> arguments, Optional<Variable> outputBlockVariable) { Preconditions.checkArgument(arguments.size() == 3); Variable wasNull = context.wasNull(); BytecodeBlock condition = new BytecodeBlock() .append(context.generate(arguments.get(0), Optional.empty())) .comment("... and condition value was not null") .append(wasNull) .invokeStatic(CompilerOperations.class, "not", boolean.class, boolean.class) .invokeStatic(CompilerOperations.class, "and", boolean.class, boolean.class, boolean.class) .append(wasNull.set(constantFalse())); BytecodeBlock block = new BytecodeBlock() .append(new IfStatement() .condition(condition) .ifTrue(context.generate(arguments.get(1), Optional.empty())) .ifFalse(context.generate(arguments.get(2), Optional.empty()))); outputBlockVariable.ifPresent(output -> block.append(generateWrite(context, returnType, output))); return block; } }
@Override public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments, Optional<Variable> outputBlockVariable) { Preconditions.checkArgument(arguments.size() == 1); RowExpression argument = arguments.get(0); if (argument.getType().equals(UNKNOWN)) { return loadBoolean(true); } BytecodeNode value = generatorContext.generate(argument, Optional.empty()); // evaluate the expression, pop the produced value, and load the null flag Variable wasNull = generatorContext.wasNull(); BytecodeBlock block = new BytecodeBlock() .comment("is null") .append(value) .pop(argument.getType().getJavaType()) .append(wasNull); // clear the null flag block.append(wasNull.set(constantFalse())); outputBlockVariable.ifPresent(output -> block.append(generateWrite(generatorContext, returnType, output))); return block; } }
private static void generateConstructor( ClassDefinition classDefinition, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, Consumer<MethodDefinition> additionalStatements) { MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC)); BytecodeBlock body = constructorDefinition.getBody(); Variable thisVariable = constructorDefinition.getThis(); body.comment("super();") .append(thisVariable) .invokeConstructor(Object.class); additionalStatements.accept(constructorDefinition); cachedInstanceBinder.generateInitializations(thisVariable, body); body.ret(); }
@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(); }
private static void generateEvaluateFinal( ClassDefinition definition, List<FieldDefinition> stateFields, MethodHandle outputFunction, CallSiteBinder callSiteBinder) { Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod( a(PUBLIC), "evaluateFinal", type(void.class), out); BytecodeBlock body = method.getBody(); Variable thisVariable = method.getThis(); List<BytecodeExpression> states = new ArrayList<>(); for (FieldDefinition stateField : stateFields) { BytecodeExpression state = thisVariable.getField(stateField); states.add(state); } body.comment("output(state_0, state_1, ..., out)"); states.forEach(body::append); body.append(out); body.append(invoke(callSiteBinder.bind(outputFunction), "output")); body.ret(); }
.comment("if (wasNull)") .append(new IfStatement() .condition(wasNullVariable) .ifTrue(new BytecodeBlock() .comment("output.appendNull();") .pop(valueJavaType) .getVariable(outputBlockVariable) .pop()) .ifFalse(new BytecodeBlock() .comment("%s.%s(output, %s)", type.getTypeSignature(), methodName, valueJavaType.getSimpleName()) .putVariable(tempValue) .append(loadConstant(callSiteBinder.bind(type, Type.class)))
private static void generateGroupedEvaluateFinal( ClassDefinition definition, List<FieldDefinition> stateFields, MethodHandle outputFunction, CallSiteBinder callSiteBinder) { Parameter groupId = arg("groupId", int.class); Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateFinal", type(void.class), groupId, out); BytecodeBlock body = method.getBody(); Variable thisVariable = method.getThis(); List<BytecodeExpression> states = new ArrayList<>(); for (FieldDefinition stateField : stateFields) { BytecodeExpression state = thisVariable.getField(stateField); body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class))); states.add(state); } body.comment("output(state_0, state_1, ..., out)"); states.forEach(body::append); body.append(out); body.append(invoke(callSiteBinder.bind(outputFunction), "output")); body.ret(); }
return NOP; BytecodeBlock notNull = new BytecodeBlock().comment("box primitive"); Class<?> expectedCurrentStackType; if (type == Long.class) {
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; }
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; }
private void generateProjectMethod( ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, String methodName, RowExpression projection) { Parameter session = arg("session", ConnectorSession.class); Parameter cursor = arg("cursor", RecordCursor.class); Parameter output = arg("output", BlockBuilder.class); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(void.class), session, cursor, output); method.comment("Projection: %s", projection.toString()); Scope scope = method.getScope(); Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull"); RowExpressionCompiler compiler = new RowExpressionCompiler( callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(cursor), metadata.getFunctionRegistry(), compiledLambdaMap); method.getBody() .comment("boolean wasNull = false;") .putVariable(wasNullVariable, false) .comment("evaluate projection: " + projection.toString()) .append(compiler.compile(projection, scope, Optional.of(output))) .ret(); }
@Inject public EmbedVersion(ServerConfig serverConfig) { ClassDefinition classDefinition = new ClassDefinition( a(PUBLIC, FINAL), makeClassName(baseClassName(serverConfig)), type(Object.class), type(Runnable.class)); FieldDefinition field = classDefinition.declareField(a(PRIVATE), "runnable", Runnable.class); Parameter parameter = arg("runnable", type(Runnable.class)); MethodDefinition constructor = classDefinition.declareConstructor(a(PUBLIC), parameter); constructor.getBody() .comment("super(runnable);") .append(constructor.getThis()) .invokeConstructor(Object.class) .append(constructor.getThis()) .append(parameter) .putField(field) .ret(); MethodDefinition run = classDefinition.declareMethod(a(PUBLIC), "run", type(void.class)); run.getBody() .comment("runnable.run();") .append(run.getThis()) .getField(field) .invokeInterface(Runnable.class, "run", void.class) .ret(); Class<? extends Runnable> generatedClass = defineClass(classDefinition, Runnable.class, ImmutableMap.of(), getClass().getClassLoader()); this.runnableConstructor = constructorMethodHandle(generatedClass, Runnable.class); }
private static void generateConstructor( ClassDefinition classDefinition, FieldDefinition sessionField, CachedInstanceBinder cachedInstanceBinder) { Parameter sessionParameter = arg("session", ConnectorSession.class); MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), sessionParameter); BytecodeBlock body = constructorDefinition.getBody(); Variable thisVariable = constructorDefinition.getThis(); body.comment("super();") .append(thisVariable) .invokeConstructor(Object.class); body.append(thisVariable.setField(sessionField, sessionParameter)); cachedInstanceBinder.generateInitializations(thisVariable, body); body.ret(); }
List<Type> types = rowType.getTypeParameters(); block.comment("Create new RowBlockBuilder; beginBlockEntry;"); Variable blockBuilder = scope.createTempVariable(BlockBuilder.class); Variable singleRowBlockWriter = scope.createTempVariable(BlockBuilder.class); Type fieldType = types.get(i); Variable field = scope.createTempVariable(fieldType.getJavaType()); block.comment("Clean wasNull and Generate + " + i + "-th field of row"); block.append(context.wasNull().set(constantFalse())); block.append(context.generate(arguments.get(i), Optional.empty())); .ifFalse(constantType(binder, fieldType).writeValue(singleRowBlockWriter, field).pop())); block.comment("closeEntry; slice the SingleRowBlock; wasNull = false;"); block.append(blockBuilder.invoke("closeEntry", BlockBuilder.class).pop()); block.append(constantType(binder, rowType).invoke("getObject", Object.class, blockBuilder.cast(Block.class), constantInt(0))
.comment("%s.appendTo(channel_%s.get(outputChannel), blockPosition, pageBuilder.getBlockBuilder(outputChannelOffset + %s));", type.getClass(), outputChannel, pageBuilderOutputChannel) .append(typeExpression) .append(block)
public static BytecodeBlock unboxPrimitiveIfNecessary(Scope scope, Class<?> boxedType) { BytecodeBlock block = new BytecodeBlock(); LabelNode end = new LabelNode("end"); Class<?> unboxedType = Primitives.unwrap(boxedType); Variable wasNull = scope.getVariable("wasNull"); if (unboxedType.isPrimitive()) { LabelNode notNull = new LabelNode("notNull"); block.dup(boxedType) .ifNotNullGoto(notNull) .append(wasNull.set(constantTrue())) .comment("swap boxed null with unboxed default") .pop(boxedType) .pushJavaDefault(unboxedType) .gotoLabel(end) .visitLabel(notNull) .append(unboxPrimitive(unboxedType)); } else { block.dup(boxedType) .ifNotNullGoto(end) .append(wasNull.set(constantTrue())); } block.visitLabel(end); return block; }