CallSiteBinder binder = new CallSiteBinder(); BytecodeExpression createBlockBuilder = blockBuilderVariable.set( constantType(binder, elementType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), constantInt(stackTypes.size()))); body.append(createBlockBuilder); IfStatement ifStatement = new IfStatement() .condition(equal(argument, constantNull(stackTypes.get(i)))) .ifTrue(blockBuilderVariable.invoke("appendNull", BlockBuilder.class).pop()) .ifFalse(constantType(binder, elementType).writeValue(blockBuilderVariable, argument.cast(elementType.getJavaType()))); body.append(ifStatement); body.append(blockBuilderVariable.invoke("build", Block.class).ret());
body.append(columnarRow.set( invokeStatic(ColumnarRow.class, "toColumnarRow", ColumnarRow.class, scope.getVariable("block")))); for (int i = 0; i < stateCount; i++) { Variable columnBlock = scope.declareVariable(Block.class, "columnBlock_" + i); body.append(columnBlock.set( columnarRow.invoke("getField", Block.class, constantInt(i)))); block.add(columnBlock); FieldDefinition stateFactoryField = stateFieldAndDescriptors.get(i).getStateFactoryField(); body.comment(format("scratchState_%s = stateFactory[%s].createSingleState();", i, i)) .append(thisVariable.getField(stateFactoryField)) .invokeInterface(AccumulatorStateFactory.class, "createSingleState", Object.class) .checkCast(scratchStates.get(i).getType()) .putVariable(scratchStates.get(i)); if (grouped) { Variable groupIdsBlock = scope.getVariable("groupIdsBlock"); loopBody.append(thisVariable.getField(stateField).invoke("setGroupId", void.class, groupIdsBlock.invoke("getGroupId", long.class, position))); loopBody.append(thisVariable.getField(stateField)); loopBody.append(thisVariable.getField(stateSerializerField).invoke("deserialize", void.class, block.get(i), position, scratchStates.get(i).cast(Object.class))); loopBody.append(scratchStates.get(i)); loopBody.append(scope.getThis().getField(lambdaProviderFields.get(i)) .invoke("getLambda", Object.class) .cast(lambdaInterfaces.get(i)));
private static void generateGetEstimatedSize(ClassDefinition definition, List<FieldDefinition> stateFields) { MethodDefinition method = definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)); Variable estimatedSize = method.getScope().declareVariable(long.class, "estimatedSize"); method.getBody().append(estimatedSize.set(constantLong(0L))); for (FieldDefinition stateField : stateFields) { method.getBody() .append(estimatedSize.set( BytecodeExpressions.add( estimatedSize, method.getThis().getField(stateField).invoke("getEstimatedSize", long.class)))); } method.getBody().append(estimatedSize.ret()); }
private static void generateEnsureCapacity(Scope scope, List<FieldDefinition> stateFields, BytecodeBlock block) { Variable groupIdsBlock = scope.getVariable("groupIdsBlock"); for (FieldDefinition stateField : stateFields) { BytecodeExpression state = scope.getThis().getField(stateField); block.append(state.invoke("ensureCapacity", void.class, groupIdsBlock.invoke("getGroupCount", long.class))); } }
BytecodeBlock body = method.getBody(); Variable from = scope.declareVariable("from", body, thisVariable.getField(nextIndexOrPosition)); Variable to = scope.declareVariable("to", body, add(thisVariable.getField(selectedPositions).invoke("getOffset", int.class), thisVariable.getField(selectedPositions).invoke("size", int.class))); Variable positions = scope.declareVariable(int[].class, "positions"); Variable index = scope.declareVariable(int.class, "index"); .condition(thisVariable.getField(selectedPositions).invoke("isList", boolean.class)); body.append(ifStatement); .append(positions.set(thisVariable.getField(selectedPositions).invoke("getPositions", int[].class))) .append(new ForLoop("positions loop") .initialize(index.set(from)) .condition(lessThan(index, to)) .update(index.increment()) .body(new BytecodeBlock() .append(thisVariable.invoke("evaluate", void.class, thisVariable.getField(session), thisVariable.getField(page), positions.getElement(index)))))); .initialize(index.set(from)) .condition(lessThan(index, to)) .update(index.increment()) .body(new BytecodeBlock() .append(thisVariable.invoke("evaluate", void.class, thisVariable.getField(session), thisVariable.getField(page), index)))); .append(thisVariable.setField(result, thisVariable.getField(blockBuilder).invoke("build", Block.class))) .push(true) .retBoolean();
.condition(lessThan(thisVariable.getField(selectedPositionsField).length(), positionCount)) .ifTrue(thisVariable.setField(selectedPositionsField, newArray(type(boolean[].class), positionCount)))); Variable selectedPositions = scope.declareVariable("selectedPositions", body, thisVariable.getField(selectedPositionsField)); Variable position = scope.declareVariable(int.class, "position"); body.append(new ForLoop() .initialize(position.set(constantInt(0))) .condition(lessThan(position, positionCount)) .update(position.increment()) .body(selectedPositions.setElement(position, thisVariable.invoke("filter", boolean.class, session, page, position))));
body.append(thisVariable.setField( stateFieldAndDescriptors.get(i).getStateSerializerField(), stateDescriptors.invoke("get", Object.class, constantInt(i)) .cast(AccumulatorStateDescriptor.class) .invoke("getSerializer", AccumulatorStateSerializer.class))); body.append(thisVariable.setField( stateFieldAndDescriptors.get(i).getStateFactoryField(), stateDescriptors.invoke("get", Object.class, constantInt(i)) body.append(thisVariable.setField( lambdaProviderFields.get(i), lambdaProviders.invoke("get", Object.class, constantInt(i)) body.append(thisVariable.setField(inputChannelsField, generateRequireNotNull(inputChannels))); body.append(thisVariable.setField(maskChannelField, generateRequireNotNull(maskChannel))); BytecodeExpression stateFactory = thisVariable.getField(stateFieldAndDescriptor.getStateFactoryField()); body.append(thisVariable.setField(stateField, stateFactory.invoke(createState, Object.class).cast(stateField.getType())));
private static void generateEvaluateIntermediate(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors) { Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod( a(PUBLIC), "evaluateIntermediate", type(void.class), out); Variable thisVariable = method.getThis(); BytecodeBlock body = method.getBody(); if (stateFieldAndDescriptors.size() == 1) { BytecodeExpression stateSerializer = thisVariable.getField(getOnlyElement(stateFieldAndDescriptors).getStateSerializerField()); BytecodeExpression state = thisVariable.getField(getOnlyElement(stateFieldAndDescriptors).getStateField()); body.append(stateSerializer.invoke("serialize", void.class, state.cast(Object.class), out)) .ret(); } else { Variable rowBuilder = method.getScope().declareVariable(BlockBuilder.class, "rowBuilder"); body.append(rowBuilder.set(out.invoke("beginBlockEntry", BlockBuilder.class))); for (int i = 0; i < stateFieldAndDescriptors.size(); i++) { BytecodeExpression stateSerializer = thisVariable.getField(stateFieldAndDescriptors.get(i).getStateSerializerField()); BytecodeExpression state = thisVariable.getField(stateFieldAndDescriptors.get(i).getStateField()); body.append(stateSerializer.invoke("serialize", void.class, state.cast(Object.class), rowBuilder)); } body.append(out.invoke("closeEntry", BlockBuilder.class).pop()) .ret(); } }
/** * Defines the read method for an union. */ private void defineReadUnionMethod() { Parameter protocol = arg("protocol", TProtocolReader.class); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "read", structType, protocol) .addException(Exception.class); // ProtocolReader reader = new ProtocolReader(protocol); Variable reader = method.getScope().declareVariable(type(ProtocolReader.class), "reader"); method.getBody().append(reader.set(newInstance(ProtocolReader.class, protocol))); // field id field. Variable fieldId = method.getScope().declareVariable(short.class, "fieldId"); method.getBody().append(fieldId.set(defaultValue(fieldId.getType()))); // read all of the data in to local variables Map<Short, Variable> unionData = readSingleFieldValue(method, reader, fieldId); // build the struct Variable result = buildUnion(method, fieldId, unionData); // return the instance method.getBody().append(result.ret()); }
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; } }
"f_" + field.getName()); structData.put(field.getId(), variable); method.getBody().append(variable.set(defaultValue(variable.getType()))); method.getBody().append(reader.invoke("readStructBegin", void.class)); .condition(reader.invoke("nextField", boolean.class)); .expression(reader.invoke("getFieldId", short.class)); .append(reader.invoke("skipFieldData", void.class))); method.getBody().append(reader.invoke("readStructEnd", void.class));
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; }
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, Optional.empty()); 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()); }
.append(thisVariable.getField(maskChannelField)) .append(page) .invokeStatic(type(AggregationUtils.class), "pageBlockGetter", type(Function.class, Integer.class, Block.class), type(Page.class)) body.comment("%s = page.getBlock(inputChannels.get(%d));", parameterVariables.get(i).getName(), i) .append(page) .append(thisVariable.getField(inputChannelsField)) .push(i) .invokeInterface(List.class, "get", Object.class, int.class)
public void enterScope(Scope scope) { requireNonNull(scope, "scope is null"); checkArgument(!allEnteredScopes.contains(scope), "scope has already been entered"); allEnteredScopes.add(scope); ScopeContext scopeContext = new ScopeContext(scope); scopes.addLast(scopeContext); for (Variable variable : scopeContext.getVariables()) { checkArgument(!"this".equals(variable.getName()) || nextSlot == 0, "The 'this' variable must be in slot 0"); variableSlots.put(variable, nextSlot); nextSlot += Type.getType(variable.getType().getType()).getSize(); } scopeContext.getStartLabel().accept(methodVisitor, this); }