SortOrder sortOrder = sortOrders.get(i); BytecodeBlock block = new BytecodeBlock() .setDescription("compare channel " + sortChannel + " " + sortOrder); .invoke("getBlock", Block.class, constantInt(sortChannel)); block.append(getStatic(SortOrder.class, sortOrder.name()) .invoke("compareBlockValue", int.class, block.comment("if (compare != 0) return compare") .dup() .ifZeroGoto(equal) .retInt() .visitLabel(equal) .pop(int.class); compareToMethod.getBody().append(block); .push(0) .retInt();
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(); }
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; }
public BytecodeNode produceBlockAndPosition() { BytecodeBlock blockAndPosition = new BytecodeBlock(); blockAndPosition.append(block); blockAndPosition.append(position); return blockAndPosition; } }
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); }
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 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; }
BytecodeBlock block = new BytecodeBlock() .comment("OR") .setDescription("OR"); block.append(left); ifLeftIsNull.ifTrue(new BytecodeBlock() .comment("clear the null flag, pop left value off stack, and push left null flag on the stack (true)") .append(wasNull.set(constantFalse())) .pop(arguments.get(0).getType().getJavaType()) // discard left value .push(true)); ifLeftIsNull.ifFalse(new BytecodeBlock() .comment("if left is true, push true, and goto end") .ifFalseGoto(leftIsFalse) .push(true) .gotoLabel(end) .comment("left was false; push left null flag on the stack (false)") .visitLabel(leftIsFalse) .push(false)); block.append(ifLeftIsNull); block.append(right); .comment("right was null, pop the right value off the stack; wasNull flag remains set to TRUE") .pop(arguments.get(1).getType().getJavaType()); .comment("if right is true, pop left null flag off stack, push true and goto end")
BytecodeBlock block = new BytecodeBlock() .comment("check if first arg is null") .append(generatorContext.generate(first, Optional.empty())) .append(ifWasNullPopAndGoto(scope, notMatch, void.class)) .dup(first.getType().getJavaType()) .putVariable(firstValue); cast(generatorContext, generatorContext.generate(second, Optional.empty()), secondType, equalsSignature.getArgumentTypes().get(1)))); BytecodeBlock conditionBlock = new BytecodeBlock() .append(equalsCall) .append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, notMatch, void.class, boolean.class)); BytecodeBlock trueBlock = new BytecodeBlock() .append(generatorContext.wasNull().set(constantTrue())) .pop(first.getType().getJavaType()) .pushJavaDefault(first.getType().getJavaType()); block.append(new IfStatement() .condition(conditionBlock) .ifTrue(trueBlock) .ifFalse(notMatch)); outputBlockVariable.ifPresent(output -> block.append(generateWrite(generatorContext, returnType, output))); return block;
.comment("boolean wasNull = false;") .putVariable(wasNullVariable, false) .comment("evaluate filter: " + filter) .append(compiler.compile(filter, scope, Optional.empty())) .comment("if (wasNull) return false;") .getVariable(wasNullVariable) .ifFalseGoto(end) .pop(boolean.class) .push(false) .visitLabel(end) .retBoolean();
switchBlock = new BytecodeBlock() .comment("lookupSwitch(<stackValue>))") .append(new IfStatement() .condition(invokeStatic(InCodeGenerator.class, "isInteger", boolean.class, value)) .ifFalse(new BytecodeBlock() .gotoLabel(defaultLabel))) .append(expression.set(value.cast(int.class))) .append(switchBuilder.build()); break; case HASH_SWITCH: .getCallSiteBinder() .bind(hashCodeFunction); switchBlock = new BytecodeBlock() .comment("lookupSwitch(hashCode(<stackValue>))") .getVariable(value) .append(invoke(hashCodeBinding, hashCodeSignature)) .invokeStatic(Long.class, "hashCode", int.class, long.class) .putVariable(expression) .append(switchBuilder.build()); break; case SET_CONTAINS: switchBlock = new BytecodeBlock() .comment("inListSet.contains(<stackValue>)") .append(new IfStatement() .condition(new BytecodeBlock() .comment("value") .getVariable(value)
BytecodeBlock block = new BytecodeBlock().comment("DEREFERENCE").setDescription("DEREFERENCE"); Variable wasNull = generator.wasNull(); Variable rowBlock = generator.getScope().createTempVariable(Block.class); block.putVariable(wasNull, false); block.append(generator.generate(arguments.get(0), Optional.empty())).putVariable(rowBlock); LabelNode end = new LabelNode("end"); ifRowBlockIsNull.ifTrue() .comment("if row block is null, push null to the stack and goto 'end' label (return)") .putVariable(wasNull, true) .pushJavaDefault(javaType) .gotoLabel(end); block.append(ifRowBlockIsNull); .comment("call rowBlock.isNull(index)") .append(rowBlock) .push(index) .invokeInterface(Block.class, "isNull", boolean.class, int.class); .comment("if the field is null, push null to stack") .putVariable(wasNull, true) .pushJavaDefault(javaType); .comment("otherwise call type.getTYPE(rowBlock, index)") .append(value) .putVariable(wasNull, false);
body.comment("masksBlock = maskChannel.map(page.blockGetter()).orElse(null);") .append(thisVariable.getField(maskChannelField)) .append(page) .invokeStatic(type(AggregationUtils.class), "pageBlockGetter", type(Function.class, Integer.class, Block.class), type(Page.class)) .invokeVirtual(Optional.class, "map", Optional.class, Function.class) .pushNull() .invokeVirtual(Optional.class, "orElse", Object.class, Object.class) .checkCast(Block.class) .putVariable(masksBlock); 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) .checkCast(Integer.class) .invokeVirtual(Integer.class, "intValue", int.class) .invokeVirtual(Page.class, "getBlock", Block.class, int.class) .putVariable(parameterVariables.get(i)); grouped); body.append(block); body.ret();
BytecodeBlock write = new BytecodeBlock(); write.getVariable(writer); write.push(field.getName()); write.push(field.getId()); write.append(method.getThis().getField(codecField)); write.append(getFieldValue(method, field)); write.dup(); write.ifNullGoto(fieldIsNull); write.invokeStatic(field.getCoercion().get().getToThrift()); write.dup(); write.ifNullGoto(fieldIsNull); write.invokeVirtual(writeMethod); write.gotoLabel(fieldEnd); write.visitLabel(fieldIsNull); write.pop(); write.pop();
body.append(wasNull.set(constantFalse())); body.append(new IfStatement("if isNull is true...") .condition(isNull) .ifTrue(new BytecodeBlock() .push(true) .gotoLabel(end))); body.push(true) .gotoLabel(end); IfStatement ifNullField = new IfStatement("if the field is null..."); ifNullField.condition(value.invoke("isNull", boolean.class, constantInt(i))) .ifTrue(new BytecodeBlock() .push(true) .gotoLabel(end)); .ifTrue(new BytecodeBlock() .push(true) .gotoLabel(end))); body.append(ifNullField); body.push(false); body.visitLabel(end) .retBoolean(); BytecodeBlock constructorBody = constructorDefinition.getBody();
.comment("super();") .append(thisVariable) .invokeConstructor(Object.class); constructor.comment("this.size = INSTANCE_SIZE") .append(thisVariable.setField(sizeField, getStatic(instanceSizeField))); constructor.comment("Set channel fields"); constructor.append(thisVariable.setField(channelFields.get(index), channel)); BytecodeBlock loopBody = new BytecodeBlock(); constructor.comment("for(blockIndex = 0; blockIndex < channel.size(); blockIndex++) { size += channel.get(i).getRetainedSizeInBytes() }") .append(new ForLoop() .initialize(blockIndex.set(constantInt(0))) .condition(new BytecodeBlock() .append(blockIndex) .append(channel.invoke("size", int.class)) .invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)) .update(new BytecodeBlock().incrementVariable(blockIndex, (byte) 1)) .body(loopBody)); loopBody.append(thisVariable) .append(thisVariable) .getField(sizeField) .append( channel.invoke("get", Object.class, blockIndex) .cast(type(Block.class))
private static void generateHashRowMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes) { Parameter position = arg("position", int.class); Parameter page = arg("blocks", Page.class); MethodDefinition hashRowMethod = classDefinition.declareMethod(a(PUBLIC), "hashRow", type(long.class), position, page); Variable resultVariable = hashRowMethod.getScope().declareVariable(long.class, "result"); hashRowMethod.getBody().push(0L).putVariable(resultVariable); for (int index = 0; index < joinChannelTypes.size(); index++) { BytecodeExpression type = constantType(callSiteBinder, joinChannelTypes.get(index)); BytecodeExpression block = page.invoke("getBlock", Block.class, constantInt(index)); hashRowMethod .getBody() .getVariable(resultVariable) .push(31L) .append(OpCode.LMUL) .append(typeHashCode(type, block, position)) .append(OpCode.LADD) .putVariable(resultVariable); } hashRowMethod .getBody() .getVariable(resultVariable) .retLong(); }
BytecodeBlock block = new BytecodeBlock() .append(page) .invokeVirtual(Page.class, "getPositionCount", int.class) .putVariable(rowsVariable) .initializeVariable(positionVariable); Variable variableDefinition = parameterVariables.get(i); loopBody = new IfStatement("if(!%s.isNull(position))", variableDefinition.getName()) .condition(new BytecodeBlock() .getVariable(variableDefinition) .getVariable(positionVariable) .invokeInterface(Block.class, "isNull", boolean.class, int.class)) .ifFalse(loopBody); .condition(new BytecodeBlock() .getVariable(masksBlock) .getVariable(positionVariable) .invokeStatic(CompilerOperations.class, "testMask", boolean.class, Block.class, int.class)) .ifTrue(loopBody); block.append(new ForLoop() .initialize(new BytecodeBlock().putVariable(positionVariable, 0)) .condition(new BytecodeBlock() .getVariable(positionVariable) .getVariable(rowsVariable) .invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)) .update(new BytecodeBlock().incrementVariable(positionVariable, (byte) 1)) .body(loopBody));
@Override public BytecodeNode getBytecode(MethodGenerationContext generationContext) { LabelNode falseLabel = new LabelNode("false"); LabelNode endLabel = new LabelNode("end"); return new BytecodeBlock() .append(left) .ifFalseGoto(falseLabel) .append(right) .ifFalseGoto(falseLabel) .push(true) .gotoLabel(endLabel) .visitLabel(falseLabel) .push(false) .visitLabel(endLabel); }
@Override public BytecodeNode getBytecode(MethodGenerationContext generationContext) { LabelNode trueLabel = new LabelNode("true"); LabelNode endLabel = new LabelNode("end"); return new BytecodeBlock() .append(value) .ifTrueGoto(trueLabel) .push(true) .gotoLabel(endLabel) .visitLabel(trueLabel) .push(false) .visitLabel(endLabel); }