nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/Block.java
author attila
Wed, 28 Jan 2015 17:58:08 +0100
changeset 28690 78317797ab62
parent 27204 06ec78f29a56
child 29407 3fd4ede1581e
permissions -rw-r--r--
8067139: Finally blocks inlined incorrectly Reviewed-by: hannesw, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.ir;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import java.io.PrintWriter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import java.util.ArrayList;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    30
import java.util.Arrays;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.util.Collections;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
    32
import java.util.Comparator;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    33
import java.util.LinkedHashMap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.util.List;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    35
import java.util.Map;
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16234
diff changeset
    36
import jdk.nashorn.internal.codegen.Label;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    37
import jdk.nashorn.internal.ir.annotations.Immutable;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
/**
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
    41
 * IR representation for a list of statements.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
 */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    43
@Immutable
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 24759
diff changeset
    44
public class Block extends Node implements BreakableNode, Terminal, Flags<Block> {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 27102
diff changeset
    45
    private static final long serialVersionUID = 1L;
06ec78f29a56 8059843: Make AST serializable
attila
parents: 27102
diff changeset
    46
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
    /** List of statements */
17524
703643aeb0d6 8013914: Removed explicit LineNumberNodes that were too brittle when code moves around, and also introduced unnecessary footprint. Introduced the Statement node and fixed dead code elimination issues that were discovered by the absense of labels for LineNumberNodes.
lagergren
parents: 17523
diff changeset
    48
    protected final List<Statement> statements;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    50
    /** Symbol table - keys must be returned in the order they were put in. */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    51
    protected final Map<String, Symbol> symbols;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    /** Entry label. */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19100
diff changeset
    54
    private final Label entryLabel;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
    56
    /** Break label. */
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
    57
    private final Label breakLabel;
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
    58
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    59
    /** Does the block/function need a new scope? */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    60
    protected final int flags;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    61
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
    62
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
    63
     * @see JoinPredecessor
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
    64
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
    65
    private final LocalVariableConversion conversion;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
    66
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    67
    /** Flag indicating that this block needs scope */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    68
    public static final int NEEDS_SCOPE = 1 << 0;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    70
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    71
     * Is this block tagged as terminal based on its contents
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    72
     * (usually the last statement)
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    73
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    74
    public static final int IS_TERMINAL = 1 << 2;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
    /**
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
    77
     * Is this block the eager global scope - i.e. the original program. This isn't true for the
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
    78
     * outermost level of recompiles
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
    79
     */
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
    80
    public static final int IS_GLOBAL_SCOPE = 1 << 3;
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
    81
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
    82
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
     *
27102
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
    85
     * @param token      The first token of the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
    86
     * @param finish     The index of the last character
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
    87
     * @param flags      The flags of the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
    88
     * @param statements All statements in the block
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
     */
27102
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
    90
    public Block(final long token, final int finish, final int flags, final Statement... statements) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
    91
        super(token, finish);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    93
        this.statements = Arrays.asList(statements);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    94
        this.symbols    = new LinkedHashMap<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
        this.entryLabel = new Label("block_entry");
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
    96
        this.breakLabel = new Label("block_break");
19100
62d400be5b44 8021244: Inconsistent stackmap with splitter threshold set very low
hannesw
parents: 19095
diff changeset
    97
        final int len = statements.length;
27102
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
    98
        final int terminalFlags = len > 0 && statements[len - 1].hasTerminalFlags() ? IS_TERMINAL : 0;
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
    99
        this.flags = terminalFlags | flags;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   100
        this.conversion = null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
    /**
27102
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   104
     * Constructs a new block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   105
     *
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   106
     * @param token The first token of the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   107
     * @param finish The index of the last character
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   108
     * @param statements All statements in the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   109
     */
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   110
    public Block(final long token, final int finish, final Statement...statements){
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   111
        this(token, finish, 0, statements);
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   112
    }
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   113
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   114
    /**
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   115
     * Constructs a new block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   116
     *
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   117
     * @param token The first token of the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   118
     * @param finish The index of the last character
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   119
     * @param statements All statements in the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   120
     */
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   121
    public Block(final long token, final int finish, final List<Statement> statements){
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   122
        this(token, finish, 0, statements);
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   123
    }
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   124
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   125
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   126
     * Constructor
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
     *
27102
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   128
     * @param token      The first token of the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   129
     * @param finish     The index of the last character
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   130
     * @param flags      The flags of the block
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   131
     * @param statements All statements in the block
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
     */
27102
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   133
    public Block(final long token, final int finish, final int flags, final List<Statement> statements) {
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   134
        this(token, finish, flags, statements.toArray(new Statement[statements.size()]));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   135
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   137
    private Block(final Block block, final int finish, final List<Statement> statements, final int flags, final Map<String, Symbol> symbols, final LocalVariableConversion conversion) {
27102
c64b3468d51d 8012518: Reengineer Parser.java to make it play well with the copy-on-write IR.
lagergren
parents: 26503
diff changeset
   138
        super(block, finish);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   139
        this.statements = statements;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   140
        this.flags      = flags;
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   141
        this.symbols    = new LinkedHashMap<>(symbols); //todo - symbols have no dependencies on any IR node and can as far as we understand it be shallow copied now
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16234
diff changeset
   142
        this.entryLabel = new Label(block.entryLabel);
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   143
        this.breakLabel = new Label(block.breakLabel);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   144
        this.conversion = conversion;
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   145
    }
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   146
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   147
    /**
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   148
     * Is this block the outermost eager global scope - i.e. the primordial program?
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   149
     * Used for global anchor point for scope depth computation for recompilation code
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   150
     * @return true if outermost eager global scope
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   151
     */
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   152
    public boolean isGlobalScope() {
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   153
        return getFlag(IS_GLOBAL_SCOPE);
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   154
    }
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   155
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   156
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   157
     * Clear the symbols in the block.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   158
     * TODO: make this immutable.
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   159
     */
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   160
    public void clearSymbols() {
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   161
        symbols.clear();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
    @Override
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   165
    public Node ensureUniqueLabels(final LexicalContext lc) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   166
        return Node.replaceInLexicalContext(lc, this, new Block(this, finish, statements, flags, symbols, conversion));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
     * Assist in IR navigation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
     * @param visitor IR navigating visitor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
     * @return new or same node
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    @Override
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents: 17524
diff changeset
   176
    public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   177
        if (visitor.enterBlock(this)) {
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25244
diff changeset
   178
            return visitor.leaveBlock(setStatements(lc, Node.accept(visitor, statements)));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   185
     * Get a copy of the list for all the symbols defined in this block
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16240
diff changeset
   186
     * @return symbol iterator
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16240
diff changeset
   187
     */
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   188
    public List<Symbol> getSymbols() {
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   189
        return Collections.unmodifiableList(new ArrayList<>(symbols.values()));
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16240
diff changeset
   190
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16240
diff changeset
   191
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16240
diff changeset
   192
    /**
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   193
     * Retrieves an existing symbol defined in the current block.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   194
     * @param name the name of the symbol
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   195
     * @return an existing symbol with the specified name defined in the current block, or null if this block doesn't
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   196
     * define a symbol with this name.T
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
     */
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   198
    public Symbol getExistingSymbol(final String name) {
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   199
        return symbols.get(name);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
     * Test if this block represents a <tt>catch</tt> block in a <tt>try</tt> statement.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
     * This is used by the Splitter as catch blocks are not be subject to splitting.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
     * @return true if this block represents a catch block in a try statement.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
    public boolean isCatchBlock() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
        return statements.size() == 1 && statements.get(0) instanceof CatchNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
    @Override
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   213
    public void toString(final StringBuilder sb, final boolean printType) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
        for (final Node statement : statements) {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   215
            statement.toString(sb, printType);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
            sb.append(';');
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
    /**
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   221
     * Print symbols in block in alphabetical order, sorted on name
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   222
     * Used for debugging, see the --print-symbols flag
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
     * @param stream print writer to output symbols to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
     * @return true if symbols were found
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
    public boolean printSymbols(final PrintWriter stream) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   229
        final List<Symbol> values = new ArrayList<>(symbols.values());
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   230
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   231
        Collections.sort(values, new Comparator<Symbol>() {
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   232
            @Override
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   233
            public int compare(final Symbol s0, final Symbol s1) {
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   234
                return s0.getName().compareTo(s1.getName());
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   235
            }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16168
diff changeset
   236
        });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
        for (final Symbol symbol : values) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
            symbol.print(stream);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
        return !values.isEmpty();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   246
     * Tag block as terminal or non terminal
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   247
     * @param lc          lexical context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   248
     * @param isTerminal is block terminal
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   249
     * @return same block, or new if flag changed
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   251
    public Block setIsTerminal(final LexicalContext lc, final boolean isTerminal) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   252
        return isTerminal ? setFlag(lc, IS_TERMINAL) : clearFlag(lc, IS_TERMINAL);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   253
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   254
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   255
    @Override
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24725
diff changeset
   256
    public int getFlags() {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24725
diff changeset
   257
        return flags;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24725
diff changeset
   258
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24725
diff changeset
   259
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 24759
diff changeset
   260
    /**
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 24759
diff changeset
   261
     * Is this a terminal block, i.e. does it end control flow like ending with a throw or return?
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 24759
diff changeset
   262
     *
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 24759
diff changeset
   263
     * @return true if this node statement is terminal
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 24759
diff changeset
   264
     */
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24725
diff changeset
   265
    @Override
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   266
    public boolean isTerminal() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   267
        return getFlag(IS_TERMINAL);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
     * Get the entry label for this block
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
     * @return the entry label
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
    public Label getEntryLabel() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
        return entryLabel;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   278
    @Override
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   279
    public Label getBreakLabel() {
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   280
        return breakLabel;
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   281
    }
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   282
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   283
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   284
    public Block setLocalVariableConversion(final LexicalContext lc, final LocalVariableConversion conversion) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   285
        if(this.conversion == conversion) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   286
            return this;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   287
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   288
        return Node.replaceInLexicalContext(lc, this, new Block(this, finish, statements, flags, symbols, conversion));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   289
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   290
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   291
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   292
    public LocalVariableConversion getLocalVariableConversion() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   293
        return conversion;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   294
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   295
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
     * Get the list of statements in this block
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
     * @return a list of statements
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
     */
17524
703643aeb0d6 8013914: Removed explicit LineNumberNodes that were too brittle when code moves around, and also introduced unnecessary footprint. Introduced the Statement node and fixed dead code elimination issues that were discovered by the absense of labels for LineNumberNodes.
lagergren
parents: 17523
diff changeset
   301
    public List<Statement> getStatements() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
        return Collections.unmodifiableList(statements);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
    /**
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   306
     * Returns the number of statements in the block.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   307
     * @return the number of statements in the block.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   308
     */
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   309
    public int getStatementCount() {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   310
        return statements.size();
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   311
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   312
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 25865
diff changeset
   313
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   314
     * Returns the line number of the first statement in the block.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   315
     * @return the line number of the first statement in the block, or -1 if the block has no statements.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   316
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   317
    public int getFirstStatementLineNumber() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   318
        if(statements == null || statements.isEmpty()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   319
            return -1;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   320
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   321
        return statements.get(0).getLineNumber();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   322
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   323
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   324
    /**
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   325
     * Returns the last statement in the block.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   326
     * @return the last statement in the block, or null if the block has no statements.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   327
     */
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   328
    public Statement getLastStatement() {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   329
        return statements.isEmpty() ? null : statements.get(statements.size() - 1);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   330
    }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   331
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   332
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
     * Reset the statement list for this block
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
     *
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   335
     * @param lc lexical context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   336
     * @param statements new statement list
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   337
     * @return new block if statements changed, identity of statements == block.statements
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
     */
17524
703643aeb0d6 8013914: Removed explicit LineNumberNodes that were too brittle when code moves around, and also introduced unnecessary footprint. Introduced the Statement node and fixed dead code elimination issues that were discovered by the absense of labels for LineNumberNodes.
lagergren
parents: 17523
diff changeset
   339
    public Block setStatements(final LexicalContext lc, final List<Statement> statements) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   340
        if (this.statements == statements) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   341
            return this;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   342
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   343
        int lastFinish = 0;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   344
        if (!statements.isEmpty()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   345
            lastFinish = statements.get(statements.size() - 1).getFinish();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   346
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   347
        return Node.replaceInLexicalContext(lc, this, new Block(this, Math.max(finish, lastFinish), statements, flags, symbols, conversion));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
     * Add or overwrite an existing symbol in the block
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
     *
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   353
     * @param lc     get lexical context
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
     * @param symbol symbol
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
     */
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   356
    public void putSymbol(final LexicalContext lc, final Symbol symbol) {
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   357
        symbols.put(symbol.getName(), symbol);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
     * Check whether scope is necessary for this Block
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
     * @return true if this function needs a scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
    public boolean needsScope() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   366
        return (flags & NEEDS_SCOPE) == NEEDS_SCOPE;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   367
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   368
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   369
    @Override
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   370
    public Block setFlags(final LexicalContext lc, final int flags) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   371
        if (this.flags == flags) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   372
            return this;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   373
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   374
        return Node.replaceInLexicalContext(lc, this, new Block(this, finish, statements, flags, symbols, conversion));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   375
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   376
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   377
    @Override
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   378
    public Block clearFlag(final LexicalContext lc, final int flag) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   379
        return setFlags(lc, flags & ~flag);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   380
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   381
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   382
    @Override
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   383
    public Block setFlag(final LexicalContext lc, final int flag) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   384
        return setFlags(lc, flags | flag);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   385
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   386
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   387
    @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   388
    public boolean getFlag(final int flag) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   389
        return (flags & flag) == flag;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    /**
16168
f0c208287983 8005976: Break out AccessSpecializer into one pass before CodeGenerator instead of iterative applications from CodeGenerator
lagergren
parents: 16160
diff changeset
   393
     * Set the needs scope flag.
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   394
     * @param lc lexicalContext
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   395
     * @return new block if state changed, otherwise this
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   397
    public Block setNeedsScope(final LexicalContext lc) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   398
        if (needsScope()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   399
            return this;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   400
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   401
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   402
        return Node.replaceInLexicalContext(lc, this, new Block(this, finish, statements, flags | NEEDS_SCOPE, symbols, conversion));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   405
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   406
     * Computationally determine the next slot for this block,
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   407
     * indexed from 0. Use this as a relative base when computing
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   408
     * frames
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   409
     * @return next slot
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   410
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   411
    public int nextSlot() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   412
        int next = 0;
17518
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   413
        for (final Symbol symbol : getSymbols()) {
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   414
            if (symbol.hasSlot()) {
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   415
                next += symbol.slotCount();
2225a4f929c0 8013477: Node.setSymbol needs to be copy on write - enable IR snapshots for recompilation based on callsite type specialization. [not enabled by default, hidden by a flag for now]
lagergren
parents: 17233
diff changeset
   416
            }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   417
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   418
        return next;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   419
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   420
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   421
    @Override
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   422
    public boolean isBreakableWithoutLabel() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   423
        return false;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16523
diff changeset
   424
    }
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   425
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   426
    @Override
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   427
    public List<Label> getLabels() {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   428
        return Collections.unmodifiableList(Arrays.asList(entryLabel, breakLabel));
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   429
    }
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   430
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   431
    @Override
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   432
    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   433
        return Acceptor.accept(this, visitor);
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 17769
diff changeset
   434
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
}