Package com.facebook.presto.byteCode

Examples of com.facebook.presto.byteCode.Block


        LabelNode defaultLabel = new LabelNode("default");

        ByteCodeNode switchBlock;
        if (constantValues.size() < 1000) {
            Block switchCaseBlocks = new Block(context);
            LookupSwitchBuilder switchBuilder = lookupSwitchBuilder();
            for (Entry<Integer, Collection<ByteCodeNode>> bucket : hashBuckets.asMap().entrySet()) {
                LabelNode label = new LabelNode("inHash" + bucket.getKey());
                switchBuilder.addCase(bucket.getKey(), label);
                Collection<ByteCodeNode> testValues = bucket.getValue();

                Block caseBlock = buildInCase(context, type, label, match, defaultLabel, testValues, false);
                switchCaseBlocks
                        .append(caseBlock.setDescription("case " + bucket.getKey()));
            }
            switchBuilder.defaultCase(defaultLabel);

            switchBlock = new Block(context)
                    .comment("lookupSwitch(hashCode(<stackValue>))")
                    .dup(javaType)
                    .invokeDynamic(hashCodeFunction.getName(), hashCodeFunction.getCallSite().type(), hashCodeFunction.getBindingId())
                    .append(switchBuilder.build())
                    .append(switchCaseBlocks);
        }
        else {
            // for huge IN lists, use a Set
            FunctionBinding functionBinding = bootstrapFunctionBinder.bindFunction(
                    "in",
                    getSessionByteCode,
                    ImmutableList.<ByteCodeNode>of(),
                    new InFunctionBinder(javaType, constantValues));

            switchBlock = new Block(context)
                    .comment("inListSet.contains(<stackValue>)")
                    .append(new IfStatement(context,
                            new Block(context).dup(javaType).invokeDynamic(functionBinding.getName(), functionBinding.getCallSite().type(), functionBinding.getBindingId()),
                            jump(match),
                            NOP));
        }

        Block defaultCaseBlock = buildInCase(context, type, defaultLabel, match, noMatch, defaultBucket.build(), true).setDescription("default");

        Block block = new Block(context)
                .comment(node.toString())
                .append(value)
                .append(ifWasNullPopAndGoto(context, end, boolean.class, javaType))
                .append(switchBlock)
                .append(defaultCaseBlock);

        Block matchBlock = new Block(context)
                .setDescription("match")
                .visitLabel(match)
                .pop(javaType)
                .putVariable("wasNull", false)
                .push(true)
                .gotoLabel(end);
        block.append(matchBlock);

        Block noMatchBlock = new Block(context)
                .setDescription("noMatch")
                .visitLabel(noMatch)
                .pop(javaType)
                .push(false)
                .gotoLabel(end);
View Full Code Here


        Variable caseWasNull = null;
        if (checkForNulls) {
            caseWasNull = context.createTempVariable(boolean.class);
        }

        Block caseBlock = new Block(context)
                .visitLabel(caseLabel);

        if (checkForNulls) {
            caseBlock.putVariable(caseWasNull.getLocalVariableDefinition(), false);
        }

        LabelNode elseLabel = new LabelNode("else");
        Block elseBlock = new Block(context)
                .visitLabel(elseLabel);

        if (checkForNulls) {
            elseBlock.getVariable(caseWasNull.getLocalVariableDefinition())
                    .putVariable("wasNull");
        }

        elseBlock.gotoLabel(noMatchLabel);

        FunctionBinding equalsFunction = bootstrapFunctionBinder.bindOperator(
                OperatorType.EQUAL,
                getSessionByteCode,
                ImmutableList.<ByteCodeNode>of(NOP, NOP),
                ImmutableList.of(type, type));

        ByteCodeNode elseNode = elseBlock;
        for (ByteCodeNode testNode : testValues) {
            LabelNode testLabel = new LabelNode("test");
            IfStatementBuilder test = ifStatementBuilder(context);

            Block condition = new Block(context)
                    .visitLabel(testLabel)
                    .dup(type.getJavaType())
                    .append(testNode);

            if (checkForNulls) {
                condition.getVariable("wasNull")
                        .putVariable(caseWasNull.getLocalVariableDefinition())
                        .append(ifWasNullPopAndGoto(context, elseLabel, void.class, type.getJavaType(), type.getJavaType()));
            }
            condition.invokeDynamic(equalsFunction.getName(), equalsFunction.getCallSite().type(), equalsFunction.getBindingId());
            test.condition(condition);

            test.ifTrue(new Block(context).gotoLabel(matchLabel));
            test.ifFalse(elseNode);

            elseNode = test.build();
            elseLabel = testLabel;
        }
View Full Code Here

            LabelNode label,
            Class<?> returnType,
            List<? extends Class<?>> stackArgsToPop,
            boolean clearNullFlag)
    {
        Block nullCheck = new Block(context)
                .setDescription("ifWasNullGoto")
                .getVariable("wasNull");

        String clearComment = null;
        if (clearNullFlag) {
            nullCheck.putVariable("wasNull", false);
            clearComment = "clear wasNull";
        }

        Block isNull = new Block(context);
        for (Class<?> parameterType : stackArgsToPop) {
            isNull.pop(parameterType);
        }

        isNull.pushJavaDefault(returnType);
        String loadDefaultComment = null;
        if (returnType != void.class) {
            loadDefaultComment = format("loadJavaDefault(%s)", returnType.getName());
        }

        isNull.gotoLabel(label);

        String popComment = null;
        if (!stackArgsToPop.isEmpty()) {
            popComment = format("pop(%s)", Joiner.on(", ").join(stackArgsToPop));
        }
View Full Code Here

        return new IfStatement(context, comment, nullCheck, isNull, NOP);
    }

    private ByteCodeNode typedNull(CompilerContext context, Class<?> type)
    {
        return new Block(context).putVariable("wasNull", true).pushJavaDefault(type);
    }
View Full Code Here

            FieldDefinition lookupSourceField,
            List<FieldDefinition> cursorFields,
            List<FieldDefinition> probeChannelFields,
            FieldDefinition probeCursorsArrayField)
    {
        Block constructor = classDefinition.declareConstructor(new CompilerContext(bootstrapMethod),
                a(PUBLIC),
                arg("lookupSource", LookupSource.class),
                arg("page", Page.class))
                .getBody()
                .comment("super();")
                .pushThis()
                .invokeConstructor(Object.class);

        constructor.comment("this.lookupSource = lookupSource;");
        constructor
                .pushThis()
                .getVariable("lookupSource")
                .putField(lookupSourceField);

        constructor.comment("Set cursor fields");
        for (int index = 0; index < cursorFields.size(); index++) {
            constructor
                    .pushThis()
                    .getVariable("page")
                    .push(index)
                    .invokeVirtual(Page.class, "getBlock", com.facebook.presto.spi.block.Block.class, int.class)
                    .invokeInterface(com.facebook.presto.spi.block.Block.class, "cursor", BlockCursor.class)
                    .putField(cursorFields.get(index));
        }

        constructor.comment("Set probe channel fields");
        for (int index = 0; index < probeChannelFields.size(); index++) {
            constructor
                    .pushThis()
                    .pushThis()
                    .getField(cursorFields.get(probeChannels.get(index)))
                    .putField(probeChannelFields.get(index));
        }

        constructor.comment("this.probeCursors = new BlockCursor[<probeChannelCount>];");
        constructor
                .pushThis()
                .push(probeChannelFields.size())
                .newArray(BlockCursor.class)
                .putField(probeCursorsArrayField);
        for (int index = 0; index < probeChannelFields.size(); index++) {
            constructor
                    .pushThis()
                    .getField(probeCursorsArrayField)
                    .push(index)
                    .pushThis()
                    .getField(probeChannelFields.get(index))
                    .putObjectArrayElement();
        }

        constructor.ret();
    }
View Full Code Here

                .retInt();
    }

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

        for (int index = 0; index < cursorFields.size(); index++) {
            appendToBody
                    .pushThis()
                    .getField(cursorFields.get(index))
                    .getVariable("pageBuilder")
                    .push(index)
                    .invokeVirtual(PageBuilder.class, "getBlockBuilder", BlockBuilder.class, int.class)
                    .invokeInterface(BlockCursor.class, "appendTo", void.class, BlockBuilder.class);
        }
        appendToBody.ret();
    }
View Full Code Here

    }

    private void generateAdvanceNextPosition(ClassDefinition classDefinition, List<FieldDefinition> cursorFields)
    {
        CompilerContext compilerContext = new CompilerContext(bootstrapMethod);
        Block advanceNextPositionBody = classDefinition.declareMethod(compilerContext,
                a(PUBLIC),
                "advanceNextPosition",
                type(boolean.class))
                .getBody();
        LocalVariableDefinition advancedVariable = compilerContext.declareVariable(boolean.class, "advanced");

        advanceNextPositionBody
                .pushThis()
                .getField(cursorFields.get(0))
                .invokeInterface(BlockCursor.class, "advanceNextPosition", boolean.class)
                .putVariable(advancedVariable);

        for (int index = 1; index < cursorFields.size(); index++) {
            advanceNextPositionBody
                    .pushThis()
                    .getField(cursorFields.get(index))
                    .invokeInterface(BlockCursor.class, "advanceNextPosition", boolean.class)
                    .getVariable(advancedVariable)
                    .invokeStatic(JoinProbeCompiler.class, "checkState", void.class, boolean.class, boolean.class);
        }

        advanceNextPositionBody
                .getVariable(advancedVariable)
                .retBoolean();
    }
View Full Code Here

                "getCurrentJoinPosition",
                type(long.class))
                .getBody()
                .append(new IfStatement(
                        compilerContext,
                        new Block(compilerContext).pushThis().invokeVirtual(classDefinition.getType(), "currentRowContainsNull", type(boolean.class)),
                        new Block(compilerContext).push(-1L).retLong(),
                        null
                ))
                .pushThis()
                .getField(lookupSourceField)
                .pushThis()
View Full Code Here

                .retLong();
    }

    private void generateCurrentRowContainsNull(ClassDefinition classDefinition, List<FieldDefinition> probeCursorFields)
    {
        Block body = classDefinition.declareMethod(new CompilerContext(bootstrapMethod),
                a(PRIVATE),
                "currentRowContainsNull",
                type(boolean.class))
                .getBody();

        for (FieldDefinition probeCursorField : probeCursorFields) {
            LabelNode checkNextField = new LabelNode("checkNextField");
            body
                    .pushThis()
                    .getField(probeCursorField)
                    .invokeInterface(BlockCursor.class, "isNull", boolean.class)
                    .ifFalseGoto(checkNextField)
                    .push(true)
                    .retBoolean()
                    .visitLabel(checkNextField);
        }

        body.push(false).retInt();
    }
View Full Code Here

        for (int i = 0; i < sortChannels.size(); i++) {
            int sortChannel = sortChannels.get(i);
            SortOrder sortOrder = sortOrders.get(i);

            Block block = new Block(compilerContext)
                    .setDescription("compare channel " + sortChannel + " " + sortOrder);

            block.comment("push leftBlock -- pagesIndex.getChannel(sortChannel).get(leftBlockIndex)")
                    .getVariable("pagesIndex")
                    .push(sortChannel)
                    .invokeVirtual(PagesIndex.class, "getChannel", ObjectArrayList.class, int.class)
                    .getVariable(leftBlockIndex)
                    .invokeVirtual(ObjectArrayList.class, "get", Object.class, int.class)
                    .checkCast(RandomAccessBlock.class);

            block.comment("push sortOrder")
                    .getStaticField(SortOrder.class, sortOrder.name(), SortOrder.class);

            block.comment("push leftBlockPosition")
                    .getVariable(leftBlockPosition);

            block.comment("push rightBlock -- pagesIndex.getChannel(sortChannel).get(rightBlockIndex)")
                    .getVariable("pagesIndex")
                    .push(sortChannel)
                    .invokeVirtual(PagesIndex.class, "getChannel", ObjectArrayList.class, int.class)
                    .getVariable(rightBlockIndex)
                    .invokeVirtual(ObjectArrayList.class, "get", Object.class, int.class)
                    .checkCast(RandomAccessBlock.class);

            block.comment("push rightBlockPosition")
                    .getVariable(rightBlockPosition);

            block.comment("invoke compareTo")
                    .invokeInterface(RandomAccessBlock.class, "compareTo", int.class, SortOrder.class, int.class, RandomAccessBlock.class, int.class);

            LabelNode equal = new LabelNode("equal");
            block.comment("if (compare != 0) return compare")
                    .dup()
                    .ifZeroGoto(equal)
                    .retInt()
                    .visitLabel(equal)
                    .pop(int.class);
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.