Package com.facebook.presto.byteCode

Examples of com.facebook.presto.byteCode.Block


        RowExpression second = arguments.get(1);

        LabelNode notMatch = new LabelNode("notMatch");

        // push first arg on the stack
        Block block = new Block(context)
                .comment("check if first arg is null")
                .append(generatorContext.generate(first))
                .append(ByteCodeUtils.ifWasNullPopAndGoto(context, notMatch, void.class));

        Type firstType = first.getType();
        Type secondType = second.getType();

        // this is a hack! We shouldn't be determining type coercions at this point, but there's no way
        // around it in the current expression AST
        Type commonType = FunctionRegistry.getCommonSuperType(firstType, secondType).get();

        FunctionBinding castFirst = generatorContext.getBootstrapBinder().bindCastOperator(
                generatorContext.generateGetSession(),
                new Block(context).dup(firstType.getJavaType()),
                firstType,
                commonType);

        FunctionBinding castSecond = generatorContext.getBootstrapBinder().bindCastOperator(
                generatorContext.generateGetSession(),
                generatorContext.generate(second),
                secondType,
                commonType);

        // if (equal(cast(first as <common type>), cast(second as <common type>))
        FunctionBinding functionBinding = generatorContext.getBootstrapBinder().bindOperator(
                OperatorType.EQUAL,
                generatorContext.generateGetSession(),
                ImmutableList.of(
                        invoke(context, castFirst, "cast(first)"),
                        invoke(context, castSecond, "cast(second)")),
                ImmutableList.of(firstType, secondType));

        MethodType methodType = functionBinding.getCallSite().type();
        Class<?> unboxedReturnType = Primitives.unwrap(methodType.returnType());

        LabelNode end = new LabelNode("end");

        Block equalsCall = new Block(context)
                .setDescription("invoke")
                .comment("equal");
        ArrayList<Class<?>> stackTypes = new ArrayList<>();
        for (int i = 0; i < functionBinding.getArguments().size(); i++) {
            equalsCall.append(functionBinding.getArguments().get(i));
            stackTypes.add(methodType.parameterType(i));
            equalsCall.append(ByteCodeUtils.ifWasNullPopAndGoto(context, end, unboxedReturnType, Lists.reverse(stackTypes)));
        }

        equalsCall.invokeDynamic(functionBinding.getName(), functionBinding.getCallSite().type(), functionBinding.getBindingId())
            .visitLabel(end);

        Block conditionBlock = new Block(context)
                .append(equalsCall)
                .append(ByteCodeUtils.ifWasNullClearPopAndGoto(context, notMatch, void.class, boolean.class));

        // if first and second are equal, return null
        Block trueBlock = new Block(context)
                .putVariable("wasNull", true)
                .pop(first.getType().getJavaType())
                .pushJavaDefault(first.getType().getJavaType());

        // else return first (which is still on the stack
View Full Code Here


        List<ByteCodeNode> arguments = functionBinding.getArguments();
        MethodType methodType = functionBinding.getCallSite().type();
        Class<?> unboxedReturnType = Primitives.unwrap(methodType.returnType());

        LabelNode end = new LabelNode("end");
        Block block = new Block(context)
                .setDescription("invoke")
                .comment(comment);
        ArrayList<Class<?>> stackTypes = new ArrayList<>();
        for (int i = 0; i < arguments.size(); i++) {
            block.append(arguments.get(i));
            stackTypes.add(methodType.parameterType(i));
            block.append(ifWasNullPopAndGoto(context, end, unboxedReturnType, Lists.reverse(stackTypes)));
        }
        block.invokeDynamic(functionBinding.getName(), methodType, functionBinding.getBindingId());

        block.visitLabel(end);

        return block;
    }
View Full Code Here

                OperatorType.EQUAL,
                generator.generateGetSession(),
                ImmutableList.<ByteCodeNode>of(NOP, NOP),
                ImmutableList.of(leftType, rightType));

        ByteCodeNode equalsCall = new Block(context).comment("equals(%s, %s)", leftType, rightType)
                .invokeDynamic(functionBinding.getName(), functionBinding.getCallSite().type(), functionBinding.getBindingId());

        Block block = new Block(context)
                .comment("IS DISTINCT FROM")
                .comment("left")
                .append(generator.generate(left))
                .append(new IfStatement(context,
                        new Block(context).getVariable("wasNull"),
                        new Block(context)
                                .pop(leftType.getJavaType())
                                .putVariable("wasNull", false)
                                .comment("right is not null")
                                .append(generator.generate(right))
                                .pop(rightType.getJavaType())
                                .getVariable("wasNull")
                                .invokeStatic(CompilerOperations.class, "not", boolean.class, boolean.class),
                        new Block(context)
                                .comment("right")
                                .append(generator.generate(right))
                                .append(new IfStatement(context,
                                        new Block(context).getVariable("wasNull"),
                                        new Block(context)
                                                .pop(leftType.getJavaType())
                                                .pop(rightType.getJavaType())
                                                .push(true),
                                        new Block(context)
                                                .append(equalsCall)
                                                .invokeStatic(CompilerOperations.class, "not", boolean.class, boolean.class)))))
                .putVariable("wasNull", false);

        return block;
View Full Code Here

        for (RowExpression expression : arguments) {
            operands.add(generatorContext.generate(expression));
        }

        CompilerContext context = generatorContext.getContext();
        ByteCodeNode nullValue = new Block(context)
                .putVariable("wasNull", true)
                .pushJavaDefault(returnType.getJavaType());

        // reverse list because current if statement builder doesn't support if/else so we need to build the if statements bottom up
        for (ByteCodeNode operand : Lists.reverse(operands)) {
            Block condition = new Block(context)
                    .append(operand)
                    .getVariable("wasNull");

            // if value was null, pop the null value, clear the null flag, and process the next operand
            Block nullBlock = new Block(context)
                    .pop(returnType.getJavaType())
                    .putVariable("wasNull", false)
                    .append(nullValue);

            nullValue = new IfStatement(context, condition, nullBlock, NOP);
View Full Code Here

    private void generateConstructor(ClassDefinition classDefinition,
            List<Integer> joinChannels,
            List<FieldDefinition> channelFields,
            List<FieldDefinition> joinChannelFields)
    {
        Block constructor = classDefinition.declareConstructor(new CompilerContext(bootstrapMethod),
                a(PUBLIC),
                arg("channels", type(List.class, type(List.class, com.facebook.presto.spi.block.Block.class))))
                .getBody()
                .comment("super();")
                .pushThis()
                .invokeConstructor(Object.class);

        constructor.comment("Set channel fields");
        for (int index = 0; index < channelFields.size(); index++) {
            constructor
                    .pushThis()
                    .getVariable("channels")
                    .push(index)
                    .invokeInterface(List.class, "get", Object.class, int.class)
                    .checkCast(type(List.class, com.facebook.presto.spi.block.Block.class))
                    .putField(channelFields.get(index));
        }

        constructor.comment("Set join channel fields");
        for (int index = 0; index < joinChannelFields.size(); index++) {
            constructor
                    .pushThis()
                    .getVariable("channels")
                    .push(joinChannels.get(index))
                    .invokeInterface(List.class, "get", Object.class, int.class)
                    .checkCast(type(List.class, com.facebook.presto.spi.block.Block.class))
                    .putField(joinChannelFields.get(index));
        }

        constructor.ret();
    }
View Full Code Here

                .retInt();
    }

    private void generateAppendToMethod(ClassDefinition classDefinition, List<FieldDefinition> channelFields)
    {
        Block appendToBody = classDefinition.declareMethod(new CompilerContext(bootstrapMethod),
                a(PUBLIC),
                "appendTo",
                type(void.class),
                arg("blockIndex", int.class),
                arg("blockPosition", int.class),
                arg("pageBuilder", PageBuilder.class),
                arg("outputChannelOffset", int.class))
                .getBody();

        for (int index = 0; index < channelFields.size(); index++) {
            appendToBody.pushThis()
                    .getField(channelFields.get(index))
                    .getVariable("blockIndex")
                    .invokeInterface(List.class, "get", Object.class, int.class)
                    .checkCast(com.facebook.presto.spi.block.Block.class)
                    .getVariable("blockPosition")
                    .getVariable("pageBuilder")
                    .getVariable("outputChannelOffset")
                    .push(index)
                    .append(OpCodes.IADD)
                    .invokeVirtual(PageBuilder.class, "getBlockBuilder", BlockBuilder.class, int.class)
                    .invokeInterface(com.facebook.presto.spi.block.Block.class, "appendTo", void.class, int.class, BlockBuilder.class);
        }
        appendToBody.ret();
    }
View Full Code Here

    }

    private static <T> void generateDeserialize(ClassDefinition definition, Class<T> clazz, List<StateField> fields)
    {
        CompilerContext compilerContext = new CompilerContext(null);
        Block deserializerBody = definition.declareMethod(compilerContext, a(PUBLIC), "deserialize", type(void.class), arg("block", com.facebook.presto.spi.block.Block.class), arg("index", int.class), arg("state", Object.class)).getBody();

        if (fields.size() == 1) {
            generatePrimitiveDeserializer(deserializerBody, getSetter(clazz, fields.get(0)));
        }
        else {
            LocalVariableDefinition slice = compilerContext.declareVariable(Slice.class, "slice");
            deserializerBody.comment("Slice slice = block.getSlice(index);")
                    .getVariable("block")
                    .getVariable("index")
                    .invokeInterface(com.facebook.presto.spi.block.Block.class, "getSlice", Slice.class, int.class)
                    .putVariable(slice);

            for (StateField field : fields) {
                generateDeserializeFromSlice(deserializerBody, slice, getSetter(clazz, field), offsetOfField(field, fields));
            }
        }
        deserializerBody.ret();
    }
View Full Code Here

    }

    private static <T> void generateSerialize(ClassDefinition definition, Class<T> clazz, List<StateField> fields)
    {
        CompilerContext compilerContext = new CompilerContext(null);
        Block serializerBody = definition.declareMethod(compilerContext, a(PUBLIC), "serialize", type(void.class), arg("state", Object.class), arg("out", BlockBuilder.class)).getBody();

        if (fields.size() == 1) {
            generatePrimitiveSerializer(serializerBody, getGetter(clazz, fields.get(0)));
        }
        else {
            LocalVariableDefinition slice = compilerContext.declareVariable(Slice.class, "slice");
            int size = serializedSizeOf(clazz);
            serializerBody.comment("Slice slice = Slices.allocate(%d);", size)
                    .push(size)
                    .invokeStatic(Slices.class, "allocate", Slice.class, int.class)
                    .putVariable(slice);

            for (StateField field : fields) {
                generateSerializeFieldToSlice(serializerBody, slice, getGetter(clazz, field), offsetOfField(field, fields));
            }
            serializerBody.comment("out.appendSlice(slice);")
                    .getVariable("out")
                    .getVariable(slice)
                    .invokeInterface(BlockBuilder.class, "appendSlice", BlockBuilder.class, Slice.class);
        }
        serializerBody.ret();
    }
View Full Code Here

                typeFromPathName("Single" + clazz.getSimpleName() + "_" + CLASS_ID.incrementAndGet()),
                type(AbstractAccumulatorState.class),
                type(clazz));

        // Generate constructor
        Block constructor = definition.declareConstructor(new CompilerContext(null), a(PUBLIC))
                .getBody()
                .pushThis()
                .invokeConstructor(AbstractAccumulatorState.class);

        // Generate fields
        List<StateField> fields = enumerateFields(clazz);
        for (StateField field : fields) {
            generateField(definition, constructor, field);
        }

        constructor.ret();

        return defineClass(definition, clazz, classLoader);
    }
View Full Code Here

                type(GroupedAccumulator.class));

        List<StateField> fields = enumerateFields(clazz);

        // Create constructor
        Block constructor = definition.declareConstructor(new CompilerContext(null), a(PUBLIC))
                .getBody()
                .pushThis()
                .invokeConstructor(AbstractGroupedAccumulatorState.class);
        // Create ensureCapacity
        Block ensureCapacity = definition.declareMethod(new CompilerContext(null), a(PUBLIC), "ensureCapacity", type(void.class), arg("size", long.class)).getBody();

        // Generate fields, constructor, and ensureCapacity
        List<FieldDefinition> fieldDefinitions = new ArrayList<>();
        for (StateField field : fields) {
            fieldDefinitions.add(generateGroupedField(definition, constructor, ensureCapacity, field));
        }

        constructor.ret();
        ensureCapacity.ret();

        // Generate getEstimatedSize
        Block getEstimatedSize = definition.declareMethod(new CompilerContext(null), a(PUBLIC), "getEstimatedSize", type(long.class))
                .getBody()
                .comment("long size = 0;")
                .push(0L);
        for (FieldDefinition field : fieldDefinitions) {
            getEstimatedSize
                    .comment("size += %s.sizeOf();", field.getName())
                    .pushThis()
                    .getField(field)
                    .invokeVirtual(field.getType(), "sizeOf", type(long.class))
                    .longAdd();
        }
        getEstimatedSize.comment("return size;");
        getEstimatedSize.retLong();

        return defineClass(definition, clazz, classLoader);
    }
View Full Code Here

TOP

Related Classes of com.facebook.presto.byteCode.Block

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.