Java Code Examples for jdk.nashorn.internal.ir.Block#getStatements()

The following examples show how to use jdk.nashorn.internal.ir.Block#getStatements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IRTranslator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private boolean handleBlock(final Block block, final boolean sortStats) {
    // FIXME: revisit this!
    if (block.isSynthetic()) {
        final int statCount = block.getStatementCount();
        switch (statCount) {
            case 0: {
                final EmptyNode emptyNode = new EmptyNode(-1, block.getToken(), block.getFinish());
                curStat = new EmptyStatementTreeImpl(emptyNode);
                return false;
            }
            case 1: {
                curStat = translateStat(block.getStatements().get(0));
                return false;
            }
            default: {
                // fall through
                break;
            }
        }
    }

    final List<? extends Statement> stats = block.getStatements();
    curStat = new BlockTreeImpl(block,
        translateStats(sortStats? getOrderedStatements(stats) : stats));
    return false;
}
 
Example 2
Source File: AssignSymbols.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode createSyntheticInitializers(final FunctionNode functionNode) {
    final List<VarNode> syntheticInitializers = new ArrayList<>(2);

    // Must visit the new var nodes in the context of the body. We could also just set the new statements into the
    // block and then revisit the entire block, but that seems to be too much double work.
    final Block body = functionNode.getBody();
    lc.push(body);
    try {
        if (functionNode.usesSelfSymbol()) {
            // "var fn = :callee"
            syntheticInitializers.add(createSyntheticInitializer(functionNode.getIdent(), CALLEE, functionNode));
        }

        if (functionNode.needsArguments()) {
            // "var arguments = :arguments"
            syntheticInitializers.add(createSyntheticInitializer(createImplicitIdentifier(ARGUMENTS_VAR.symbolName()),
                    ARGUMENTS, functionNode));
        }

        if (syntheticInitializers.isEmpty()) {
            return functionNode;
        }

        for(final ListIterator<VarNode> it = syntheticInitializers.listIterator(); it.hasNext();) {
            it.set((VarNode)it.next().accept(this));
        }
    } finally {
        lc.pop(body);
    }

    final List<Statement> stmts = body.getStatements();
    final List<Statement> newStatements = new ArrayList<>(stmts.size() + syntheticInitializers.size());
    newStatements.addAll(syntheticInitializers);
    newStatements.addAll(stmts);
    return functionNode.setBody(lc, body.setStatements(lc, newStatements));
}
 
Example 3
Source File: LocalVariableTypesCalculator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void createSyntheticReturn(final Block body) {
    final FunctionNode functionNode = lc.getCurrentFunction();
    final long token = functionNode.getToken();
    final int finish = functionNode.getFinish();
    final List<Statement> statements = body.getStatements();
    final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
    final IdentNode returnExpr;
    if(functionNode.isProgram()) {
        returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
    } else {
        returnExpr = null;
    }
    syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
    syntheticReturn.accept(this);
}
 
Example 4
Source File: Lower.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Block createFinallyBlock(final Block finallyBody) {
    final List<Statement> newStatements = new ArrayList<>();
    for (final Statement statement : finallyBody.getStatements()) {
        newStatements.add(statement);
        if (statement.hasTerminalFlags()) {
            break;
        }
    }
    return finallyBody.setStatements(null, newStatements);
}
 
Example 5
Source File: Lower.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Block createFinallyBlock(final Block finallyBody) {
    final List<Statement> newStatements = new ArrayList<>();
    for (final Statement statement : finallyBody.getStatements()) {
        newStatements.add(statement);
        if (statement.hasTerminalFlags()) {
            break;
        }
    }
    return finallyBody.setStatements(null, newStatements);
}
 
Example 6
Source File: Splitter.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Split a block into sub methods.
 *
 * @param block Block or function to split.
 *
 * @return new weight for the resulting block.
 */
private Block splitBlock(final Block block, final FunctionNode function) {

    final List<Statement> splits = new ArrayList<>();
    List<Statement> statements = new ArrayList<>();
    long statementsWeight = 0;

    for (final Statement statement : block.getStatements()) {
        final long weight = WeighNodes.weigh(statement, weightCache);

        if (statementsWeight + weight >= SPLIT_THRESHOLD || statement.isTerminal()) {
            if (!statements.isEmpty()) {
                splits.add(createBlockSplitNode(block, function, statements, statementsWeight));
                statements = new ArrayList<>();
                statementsWeight = 0;
            }
        }

        if (statement.isTerminal()) {
            splits.add(statement);
        } else {
            statements.add(statement);
            statementsWeight += weight;
        }
    }

    if (!statements.isEmpty()) {
        splits.add(createBlockSplitNode(block, function, statements, statementsWeight));
    }

    return block.setStatements(lc, splits);
}
 
Example 7
Source File: AssignSymbols.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode createSyntheticInitializers(final FunctionNode functionNode) {
    final List<VarNode> syntheticInitializers = new ArrayList<>(2);

    // Must visit the new var nodes in the context of the body. We could also just set the new statements into the
    // block and then revisit the entire block, but that seems to be too much double work.
    final Block body = functionNode.getBody();
    lc.push(body);
    try {
        if (functionNode.usesSelfSymbol()) {
            // "var fn = :callee"
            syntheticInitializers.add(createSyntheticInitializer(functionNode.getIdent(), CALLEE, functionNode));
        }

        if (functionNode.needsArguments()) {
            // "var arguments = :arguments"
            syntheticInitializers.add(createSyntheticInitializer(createImplicitIdentifier(ARGUMENTS_VAR.symbolName()),
                    ARGUMENTS, functionNode));
        }

        if (syntheticInitializers.isEmpty()) {
            return functionNode;
        }

        for(final ListIterator<VarNode> it = syntheticInitializers.listIterator(); it.hasNext();) {
            it.set((VarNode)it.next().accept(this));
        }
    } finally {
        lc.pop(body);
    }

    final List<Statement> stmts = body.getStatements();
    final List<Statement> newStatements = new ArrayList<>(stmts.size() + syntheticInitializers.size());
    newStatements.addAll(syntheticInitializers);
    newStatements.addAll(stmts);
    return functionNode.setBody(lc, body.setStatements(lc, newStatements));
}
 
Example 8
Source File: LocalVariableTypesCalculator.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private void createSyntheticReturn(final Block body) {
    final FunctionNode functionNode = lc.getCurrentFunction();
    final long token = functionNode.getToken();
    final int finish = functionNode.getFinish();
    final List<Statement> statements = body.getStatements();
    final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
    final IdentNode returnExpr;
    if(functionNode.isProgram()) {
        returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
    } else {
        returnExpr = null;
    }
    syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
    syntheticReturn.accept(this);
}
 
Example 9
Source File: Lower.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Block prependFinally(final Block finallyBlock, final Statement statement) {
    final Block inlinedFinally = ensureUniqueNamesIn(finallyBlock);
    if (isTerminalFinally(finallyBlock)) {
        return inlinedFinally;
    }
    final List<Statement> stmts = inlinedFinally.getStatements();
    final List<Statement> newStmts = new ArrayList<>(stmts.size() + 1);
    newStmts.addAll(stmts);
    newStmts.add(statement);
    return new Block(inlinedFinally.getToken(), statement.getFinish(), newStmts);
}
 
Example 10
Source File: Splitter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Split a block into sub methods.
 *
 * @param block Block or function to split.
 *
 * @return new weight for the resulting block.
 */
private Block splitBlock(final Block block, final FunctionNode function) {

    final List<Statement> splits = new ArrayList<>();
    List<Statement> statements = new ArrayList<>();
    long statementsWeight = 0;

    for (final Statement statement : block.getStatements()) {
        final long weight = WeighNodes.weigh(statement, weightCache);

        if (statementsWeight + weight >= SPLIT_THRESHOLD || statement.isTerminal()) {
            if (!statements.isEmpty()) {
                splits.add(createBlockSplitNode(block, function, statements, statementsWeight));
                statements = new ArrayList<>();
                statementsWeight = 0;
            }
        }

        if (statement.isTerminal()) {
            splits.add(statement);
        } else {
            statements.add(statement);
            statementsWeight += weight;
        }
    }

    if (!statements.isEmpty()) {
        splits.add(createBlockSplitNode(block, function, statements, statementsWeight));
    }

    return block.setStatements(lc, splits);
}
 
Example 11
Source File: AssignSymbols.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private FunctionNode createSyntheticInitializers(final FunctionNode functionNode) {
    final List<VarNode> syntheticInitializers = new ArrayList<>(2);

    // Must visit the new var nodes in the context of the body. We could also just set the new statements into the
    // block and then revisit the entire block, but that seems to be too much double work.
    final Block body = functionNode.getBody();
    lc.push(body);
    try {
        if (functionNode.usesSelfSymbol()) {
            // "var fn = :callee"
            syntheticInitializers.add(createSyntheticInitializer(functionNode.getIdent(), CALLEE, functionNode));
        }

        if (functionNode.needsArguments()) {
            // "var arguments = :arguments"
            syntheticInitializers.add(createSyntheticInitializer(createImplicitIdentifier(ARGUMENTS_VAR.symbolName()),
                    ARGUMENTS, functionNode));
        }

        if (syntheticInitializers.isEmpty()) {
            return functionNode;
        }

        for(final ListIterator<VarNode> it = syntheticInitializers.listIterator(); it.hasNext();) {
            it.set((VarNode)it.next().accept(this));
        }
    } finally {
        lc.pop(body);
    }

    final List<Statement> stmts = body.getStatements();
    final List<Statement> newStatements = new ArrayList<>(stmts.size() + syntheticInitializers.size());
    newStatements.addAll(syntheticInitializers);
    newStatements.addAll(stmts);
    return functionNode.setBody(lc, body.setStatements(lc, newStatements));
}
 
Example 12
Source File: Splitter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Split a block into sub methods.
 *
 * @param block Block or function to split.
 *
 * @return new weight for the resulting block.
 */
private Block splitBlock(final Block block, final FunctionNode function) {

    final List<Statement> splits = new ArrayList<>();
    List<Statement> statements = new ArrayList<>();
    long statementsWeight = 0;

    for (final Statement statement : block.getStatements()) {
        final long weight = WeighNodes.weigh(statement, weightCache);

        if (statementsWeight + weight >= SPLIT_THRESHOLD || statement.isTerminal()) {
            if (!statements.isEmpty()) {
                splits.add(createBlockSplitNode(block, function, statements, statementsWeight));
                statements = new ArrayList<>();
                statementsWeight = 0;
            }
        }

        if (statement.isTerminal()) {
            splits.add(statement);
        } else {
            statements.add(statement);
            statementsWeight += weight;
        }
    }

    if (!statements.isEmpty()) {
        splits.add(createBlockSplitNode(block, function, statements, statementsWeight));
    }

    return block.setStatements(lc, splits);
}
 
Example 13
Source File: Lower.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static Block prependFinally(final Block finallyBlock, final Statement statement) {
    final Block inlinedFinally = ensureUniqueNamesIn(finallyBlock);
    if (isTerminalFinally(finallyBlock)) {
        return inlinedFinally;
    }
    final List<Statement> stmts = inlinedFinally.getStatements();
    final List<Statement> newStmts = new ArrayList<>(stmts.size() + 1);
    newStmts.addAll(stmts);
    newStmts.add(statement);
    return new Block(inlinedFinally.getToken(), statement.getFinish(), newStmts);
}
 
Example 14
Source File: Lower.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Block prependFinally(final Block finallyBlock, final Statement statement) {
    final Block inlinedFinally = ensureUniqueNamesIn(finallyBlock);
    if (isTerminalFinally(finallyBlock)) {
        return inlinedFinally;
    }
    final List<Statement> stmts = inlinedFinally.getStatements();
    final List<Statement> newStmts = new ArrayList<>(stmts.size() + 1);
    newStmts.addAll(stmts);
    newStmts.add(statement);
    return new Block(inlinedFinally.getToken(), statement.getFinish(), newStmts);
}
 
Example 15
Source File: LocalVariableTypesCalculator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void createSyntheticReturn(final Block body) {
    final FunctionNode functionNode = lc.getCurrentFunction();
    final long token = functionNode.getToken();
    final int finish = functionNode.getFinish();
    final List<Statement> statements = body.getStatements();
    final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
    final IdentNode returnExpr;
    if(functionNode.isProgram()) {
        returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
    } else {
        returnExpr = null;
    }
    syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
    syntheticReturn.accept(this);
}
 
Example 16
Source File: Lower.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static List<Statement> copyFinally(final Block finallyBody) {
    final List<Statement> newStatements = new ArrayList<>();
    for (final Statement statement : finallyBody.getStatements()) {
        newStatements.add((Statement)ensureUniqueNamesIn(statement));
        if (statement.hasTerminalFlags()) {
            return newStatements;
        }
    }
    return newStatements;
}
 
Example 17
Source File: Lower.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor.
 */
Lower(final CodeInstaller<?> installer) {
    super(new BlockLexicalContext() {

        @Override
        public List<Statement> popStatements() {
            final List<Statement> newStatements = new ArrayList<>();
            boolean terminated = false;

            final List<Statement> statements = super.popStatements();
            for (final Statement statement : statements) {
                if (!terminated) {
                    newStatements.add(statement);
                    if (statement.isTerminal() || statement instanceof BreakNode || statement instanceof ContinueNode) { //TODO hasGoto? But some Loops are hasGoto too - why?
                        terminated = true;
                    }
                } else {
                    statement.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
                        @Override
                        public boolean enterVarNode(final VarNode varNode) {
                            newStatements.add(varNode.setInit(null));
                            return false;
                        }
                    });
                }
            }
            return newStatements;
        }

        @Override
        protected Block afterSetStatements(final Block block) {
            final List<Statement> stmts = block.getStatements();
            for(final ListIterator<Statement> li = stmts.listIterator(stmts.size()); li.hasPrevious();) {
                final Statement stmt = li.previous();
                // popStatements() guarantees that the only thing after a terminal statement are uninitialized
                // VarNodes. We skip past those, and set the terminal state of the block to the value of the
                // terminal state of the first statement that is not an uninitialized VarNode.
                if(!(stmt instanceof VarNode && ((VarNode)stmt).getInit() == null)) {
                    return block.setIsTerminal(this, stmt.isTerminal());
                }
            }
            return block.setIsTerminal(this, false);
        }
    });
    this.installer = installer;
}
 
Example 18
Source File: Lower.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor.
 */
Lower(final Compiler compiler) {
    super(new BlockLexicalContext() {

        @Override
        public List<Statement> popStatements() {
            final List<Statement> newStatements = new ArrayList<>();
            boolean terminated = false;

            final List<Statement> statements = super.popStatements();
            for (final Statement statement : statements) {
                if (!terminated) {
                    newStatements.add(statement);
                    if (statement.isTerminal() || statement instanceof JumpStatement) { //TODO hasGoto? But some Loops are hasGoto too - why?
                        terminated = true;
                    }
                } else {
                    FoldConstants.extractVarNodesFromDeadCode(statement, newStatements);
                }
            }
            return newStatements;
        }

        @Override
        protected Block afterSetStatements(final Block block) {
            final List<Statement> stmts = block.getStatements();
            for(final ListIterator<Statement> li = stmts.listIterator(stmts.size()); li.hasPrevious();) {
                final Statement stmt = li.previous();
                // popStatements() guarantees that the only thing after a terminal statement are uninitialized
                // VarNodes. We skip past those, and set the terminal state of the block to the value of the
                // terminal state of the first statement that is not an uninitialized VarNode.
                if(!(stmt instanceof VarNode && ((VarNode)stmt).getInit() == null)) {
                    return block.setIsTerminal(this, stmt.isTerminal());
                }
            }
            return block.setIsTerminal(this, false);
        }
    });

    this.log = initLogger(compiler.getContext());
    this.es6 = compiler.getScriptEnvironment()._es6;
    this.source = compiler.getSource();
}
 
Example 19
Source File: Lower.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor.
 */
Lower() {
    super(new BlockLexicalContext() {

        @Override
        public List<Statement> popStatements() {
            final List<Statement> newStatements = new ArrayList<>();
            boolean terminated = false;

            final List<Statement> statements = super.popStatements();
            for (final Statement statement : statements) {
                if (!terminated) {
                    newStatements.add(statement);
                    if (statement.isTerminal() || statement instanceof BreakNode || statement instanceof ContinueNode) { //TODO hasGoto? But some Loops are hasGoto too - why?
                        terminated = true;
                    }
                } else {
                    statement.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
                        @Override
                        public boolean enterVarNode(final VarNode varNode) {
                            newStatements.add(varNode.setInit(null));
                            return false;
                        }
                    });
                }
            }
            return newStatements;
        }

        @Override
        protected Block afterSetStatements(final Block block) {
            final List<Statement> stmts = block.getStatements();
            for(final ListIterator<Statement> li = stmts.listIterator(stmts.size()); li.hasPrevious();) {
                final Statement stmt = li.previous();
                // popStatements() guarantees that the only thing after a terminal statement are uninitialized
                // VarNodes. We skip past those, and set the terminal state of the block to the value of the
                // terminal state of the first statement that is not an uninitialized VarNode.
                if(!(stmt instanceof VarNode && ((VarNode)stmt).getInit() == null)) {
                    return block.setIsTerminal(this, stmt.isTerminal());
                }
            }
            return block.setIsTerminal(this, false);
        }
    });
}
 
Example 20
Source File: PrintVisitor.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean enterBlock(final Block block) {
    sb.append(' ');
    sb.append('{');

    indent += TABWIDTH;

    final List<Statement> statements = block.getStatements();

    for (final Statement statement : statements) {
        if (printLineNumbers) {
            final int lineNumber = statement.getLineNumber();
            sb.append('\n');
            if (lineNumber != lastLineNumber) {
                indent();
                sb.append("[|").append(lineNumber).append("|];").append('\n');
            }
            lastLineNumber = lineNumber;
        }
        indent();

        statement.accept(this);

        int  lastIndex = sb.length() - 1;
        char lastChar  = sb.charAt(lastIndex);
        while (Character.isWhitespace(lastChar) && lastIndex >= 0) {
            lastChar = sb.charAt(--lastIndex);
        }

        if (lastChar != '}' && lastChar != ';') {
            sb.append(';');
        }

        if (statement.hasGoto()) {
            sb.append(" [GOTO]");
        }

        if (statement.isTerminal()) {
            sb.append(" [TERMINAL]");
        }
    }

    indent -= TABWIDTH;

    sb.append(EOLN);
    indent();
    sb.append('}');
    printLocalVariableConversion(block);

    return false;
}