nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java
author chegar
Mon, 18 Aug 2014 10:59:40 +0100
changeset 26068 5488f52c2788
parent 26067 nashorn/src/jdk/nashorn/internal/codegen/Lower.java@b32ccc3a76c9
parent 25865 nashorn/src/jdk/nashorn/internal/codegen/Lower.java@d38d876f1654
child 26504 ed05e2f4c2db
permissions -rw-r--r--
Merge
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.codegen;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.EVAL;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    29
import static jdk.nashorn.internal.codegen.CompilerConstants.RETURN;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
    30
import static jdk.nashorn.internal.ir.Expression.isAlwaysTrue;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.util.ArrayList;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.util.Arrays;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
    34
import java.util.Collections;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.util.List;
18848
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
    36
import java.util.ListIterator;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    37
import jdk.nashorn.internal.ir.BaseNode;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import jdk.nashorn.internal.ir.BinaryNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import jdk.nashorn.internal.ir.Block;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    40
import jdk.nashorn.internal.ir.BlockLexicalContext;
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
    41
import jdk.nashorn.internal.ir.BlockStatement;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import jdk.nashorn.internal.ir.BreakNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import jdk.nashorn.internal.ir.CallNode;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
    44
import jdk.nashorn.internal.ir.CaseNode;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
import jdk.nashorn.internal.ir.CatchNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import jdk.nashorn.internal.ir.ContinueNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
import jdk.nashorn.internal.ir.EmptyNode;
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
    48
import jdk.nashorn.internal.ir.Expression;
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
    49
import jdk.nashorn.internal.ir.ExpressionStatement;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
import jdk.nashorn.internal.ir.ForNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
import jdk.nashorn.internal.ir.FunctionNode;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
    52
import jdk.nashorn.internal.ir.FunctionNode.CompilationState;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
import jdk.nashorn.internal.ir.IdentNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
import jdk.nashorn.internal.ir.IfNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
import jdk.nashorn.internal.ir.LabelNode;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
    56
import jdk.nashorn.internal.ir.LexicalContext;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
import jdk.nashorn.internal.ir.LiteralNode;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    58
import jdk.nashorn.internal.ir.LoopNode;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
import jdk.nashorn.internal.ir.Node;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
import jdk.nashorn.internal.ir.ReturnNode;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
    61
import jdk.nashorn.internal.ir.RuntimeNode;
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
    62
import jdk.nashorn.internal.ir.Statement;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
import jdk.nashorn.internal.ir.SwitchNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
import jdk.nashorn.internal.ir.Symbol;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
import jdk.nashorn.internal.ir.ThrowNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
import jdk.nashorn.internal.ir.TryNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
import jdk.nashorn.internal.ir.VarNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
import jdk.nashorn.internal.ir.WhileNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
import jdk.nashorn.internal.ir.WithNode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
import jdk.nashorn.internal.ir.visitor.NodeOperatorVisitor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
import jdk.nashorn.internal.parser.Token;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
import jdk.nashorn.internal.parser.TokenType;
20949
b230767d5d40 8026692: eval() throws NullPointerException with --compile-only
hannesw
parents: 20929
diff changeset
    74
import jdk.nashorn.internal.runtime.CodeInstaller;
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
    75
import jdk.nashorn.internal.runtime.Context;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
    76
import jdk.nashorn.internal.runtime.JSType;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
import jdk.nashorn.internal.runtime.Source;
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
    78
import jdk.nashorn.internal.runtime.logging.DebugLogger;
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
    79
import jdk.nashorn.internal.runtime.logging.Loggable;
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
    80
import jdk.nashorn.internal.runtime.logging.Logger;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
/**
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    83
 * Lower to more primitive operations. After lowering, an AST still has no symbols
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    84
 * and types, but several nodes have been turned into more low level constructs
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    85
 * and control flow termination criteria have been computed.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
 *
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    87
 * We do things like code copying/inlining of finallies here, as it is much
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    88
 * harder and context dependent to do any code copying after symbols have been
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    89
 * finalized.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
 */
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
    91
@Logger(name="lower")
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
    92
final class Lower extends NodeOperatorVisitor<BlockLexicalContext> implements Loggable {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
    94
    private final DebugLogger log;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
    95
20949
b230767d5d40 8026692: eval() throws NullPointerException with --compile-only
hannesw
parents: 20929
diff changeset
    96
    // needed only to get unique eval id
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 20949
diff changeset
    97
    private final CodeInstaller<?> installer;
20929
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 19100
diff changeset
    98
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
     * Constructor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
     */
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   102
    Lower(final Compiler compiler) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   103
        super(new BlockLexicalContext() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   104
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   105
            @Override
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
   106
            public List<Statement> popStatements() {
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
   107
                final List<Statement> newStatements = new ArrayList<>();
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   108
                boolean terminated = false;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   109
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
   110
                final List<Statement> statements = super.popStatements();
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
   111
                for (final Statement statement : statements) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   112
                    if (!terminated) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   113
                        newStatements.add(statement);
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
   114
                        if (statement.isTerminal() || statement instanceof BreakNode || statement instanceof ContinueNode) { //TODO hasGoto? But some Loops are hasGoto too - why?
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   115
                            terminated = true;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   116
                        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   117
                    } else {
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: 17752
diff changeset
   118
                        statement.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
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
   119
                            @Override
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
   120
                            public boolean enterVarNode(final VarNode varNode) {
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
   121
                                newStatements.add(varNode.setInit(null));
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
   122
                                return false;
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
   123
                            }
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
   124
                        });
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   125
                    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   126
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   127
                return newStatements;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   128
            }
18848
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   129
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   130
            @Override
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   131
            protected Block afterSetStatements(final Block block) {
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   132
                final List<Statement> stmts = block.getStatements();
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   133
                for(final ListIterator<Statement> li = stmts.listIterator(stmts.size()); li.hasPrevious();) {
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   134
                    final Statement stmt = li.previous();
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   135
                    // popStatements() guarantees that the only thing after a terminal statement are uninitialized
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   136
                    // VarNodes. We skip past those, and set the terminal state of the block to the value of the
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   137
                    // terminal state of the first statement that is not an uninitialized VarNode.
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   138
                    if(!(stmt instanceof VarNode && ((VarNode)stmt).getInit() == null)) {
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   139
                        return block.setIsTerminal(this, stmt.isTerminal());
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   140
                    }
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   141
                }
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   142
                return block.setIsTerminal(this, false);
e64943db2b06 8019809: return after break incorrectly sets the block as terminal
attila
parents: 17982
diff changeset
   143
            }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   144
        });
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   145
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   146
        this.installer = compiler.getCodeInstaller();
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   147
        this.log       = initLogger(compiler.getContext());
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   148
    }
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   149
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   150
    @Override
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   151
    public DebugLogger getLogger() {
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   152
        return log;
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   153
    }
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   154
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   155
    @Override
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   156
    public DebugLogger initLogger(final Context context) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   157
        return context.getLogger(this.getClass());
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   158
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   159
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   160
    @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   161
    public boolean enterBreakNode(final BreakNode breakNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   162
        addStatement(breakNode);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   163
        return false;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   164
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   165
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   166
    @Override
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   167
    public Node leaveCallNode(final CallNode callNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   168
        return checkEval(callNode.setFunction(markerFunction(callNode.getFunction())));
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   169
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   170
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   171
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   172
    public Node leaveCatchNode(final CatchNode catchNode) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   173
        return addStatement(catchNode);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   174
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   175
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   176
    @Override
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   177
    public boolean enterContinueNode(final ContinueNode continueNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   178
        addStatement(continueNode);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   179
        return false;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   180
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   181
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   182
    @Override
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   183
    public boolean enterEmptyNode(final EmptyNode emptyNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   184
        return false;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   185
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   186
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   187
    @Override
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   188
    public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   189
        final Expression expr = expressionStatement.getExpression();
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   190
        ExpressionStatement node = expressionStatement;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   191
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: 17752
diff changeset
   192
        final FunctionNode currentFunction = lc.getCurrentFunction();
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   193
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   194
        if (currentFunction.isProgram()) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   195
            if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   196
                node = expressionStatement.setExpression(
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   197
                    new BinaryNode(
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   198
                        Token.recast(
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   199
                            expressionStatement.getToken(),
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   200
                            TokenType.ASSIGN),
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   201
                        compilerConstant(RETURN),
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   202
                    expr));
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   203
            }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   204
        }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   205
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   206
        return addStatement(node);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   207
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   208
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   209
    @Override
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   210
    public Node leaveBlockStatement(final BlockStatement blockStatement) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   211
        return addStatement(blockStatement);
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   212
    }
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   213
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   214
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   215
    public Node leaveForNode(final ForNode forNode) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   216
        ForNode newForNode = forNode;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   217
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   218
        final Expression test = forNode.getTest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   219
        if (!forNode.isForIn() && isAlwaysTrue(test)) {
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: 17752
diff changeset
   220
            newForNode = forNode.setTest(lc, null);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   221
        }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   222
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   223
        newForNode = checkEscape(newForNode);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   224
        if(newForNode.isForIn()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   225
            // Wrap it in a block so its internally created iterator is restricted in scope
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   226
            addStatementEnclosedInBlock(newForNode);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   227
        } else {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   228
            addStatement(newForNode);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   229
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   230
        return newForNode;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   231
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   232
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   233
    @Override
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   234
    public Node leaveFunctionNode(final FunctionNode functionNode) {
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   235
        log.info("END FunctionNode: ", functionNode.getName());
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: 17752
diff changeset
   236
        return functionNode.setState(lc, CompilationState.LOWERED);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   237
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   238
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   239
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   240
    public Node leaveIfNode(final IfNode ifNode) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   241
        return addStatement(ifNode);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   242
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   243
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   244
    @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
   245
    public Node leaveIN(final BinaryNode binaryNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   246
        return new RuntimeNode(binaryNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   247
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   248
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   249
    @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
   250
    public Node leaveINSTANCEOF(final BinaryNode binaryNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   251
        return new RuntimeNode(binaryNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   252
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   253
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   254
    @Override
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   255
    public Node leaveLabelNode(final LabelNode labelNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   256
        return addStatement(labelNode);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   257
    }
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   258
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   259
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   260
    public Node leaveReturnNode(final ReturnNode returnNode) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   261
        addStatement(returnNode); //ReturnNodes are always terminal, marked as such in constructor
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   262
        return returnNode;
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   263
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   264
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   265
    @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
   266
    public Node leaveCaseNode(final CaseNode caseNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   267
        // Try to represent the case test as an integer
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   268
        final Node test = caseNode.getTest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   269
        if (test instanceof LiteralNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   270
            final LiteralNode<?> lit = (LiteralNode<?>)test;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   271
            if (lit.isNumeric() && !(lit.getValue() instanceof Integer)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   272
                if (JSType.isRepresentableAsInt(lit.getNumber())) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   273
                    return caseNode.setTest((Expression)LiteralNode.newInstance(lit, lit.getInt32()).accept(this));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   274
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   275
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   276
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   277
        return caseNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   278
    }
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   279
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   280
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   281
    public Node leaveSwitchNode(final SwitchNode switchNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   282
        if(!switchNode.isInteger()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   283
            // Wrap it in a block so its internally created tag is restricted in scope
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   284
            addStatementEnclosedInBlock(switchNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   285
        } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   286
            addStatement(switchNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   287
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   288
        return switchNode;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   289
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   290
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   291
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   292
    public Node leaveThrowNode(final ThrowNode throwNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   293
        return addStatement(throwNode); //ThrowNodes are always terminal, marked as such in constructor
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   294
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   295
17982
03696682b6e5 8015961: Several small code-gardening fixes
attila
parents: 17769
diff changeset
   296
    private static Node ensureUniqueNamesIn(final Node node) {
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: 17752
diff changeset
   297
        return node.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
17752
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   298
            @Override
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   299
            public Node leaveFunctionNode(final FunctionNode functionNode) {
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   300
                final String name = functionNode.getName();
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: 17752
diff changeset
   301
                return functionNode.setName(lc, lc.getCurrentFunction().uniqueName(name));
17752
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   302
            }
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   303
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   304
            @Override
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   305
            public Node leaveDefault(final Node labelledNode) {
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: 17752
diff changeset
   306
                return labelledNode.ensureUniqueLabels(lc);
17752
9d2d0e74a833 8013919: Original exception no longer thrown away when a finally rethrows
lagergren
parents: 17745
diff changeset
   307
            }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   308
        });
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   309
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   310
17982
03696682b6e5 8015961: Several small code-gardening fixes
attila
parents: 17769
diff changeset
   311
    private static List<Statement> copyFinally(final Block finallyBody) {
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
   312
        final List<Statement> newStatements = new ArrayList<>();
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
   313
        for (final Statement statement : finallyBody.getStatements()) {
17982
03696682b6e5 8015961: Several small code-gardening fixes
attila
parents: 17769
diff changeset
   314
            newStatements.add((Statement)ensureUniqueNamesIn(statement));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   315
            if (statement.hasTerminalFlags()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   316
                return newStatements;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   317
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   318
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   319
        return newStatements;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   320
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   321
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   322
    private Block catchAllBlock(final TryNode tryNode) {
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
   323
        final int  lineNumber = tryNode.getLineNumber();
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
   324
        final long token      = tryNode.getToken();
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
   325
        final int  finish     = tryNode.getFinish();
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   326
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   327
        final IdentNode exception = new IdentNode(token, finish, lc.getCurrentFunction().uniqueName(CompilerConstants.EXCEPTION_PREFIX.symbolName()));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   328
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   329
        final Block catchBody = new Block(token, finish, new ThrowNode(lineNumber, token, finish, new IdentNode(exception), true));
19100
62d400be5b44 8021244: Inconsistent stackmap with splitter threshold set very low
hannesw
parents: 18867
diff changeset
   330
        assert catchBody.isTerminal(); //ends with throw, so terminal
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   331
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   332
        final CatchNode catchAllNode  = new CatchNode(lineNumber, token, finish, new IdentNode(exception), null, catchBody, true);
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   333
        final Block     catchAllBlock = new Block(token, finish, catchAllNode);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   334
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   335
        //catchallblock -> catchallnode (catchnode) -> exception -> throw
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   336
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   337
        return (Block)catchAllBlock.accept(this); //not accepted. has to be accepted by lower
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   338
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   339
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   340
    private IdentNode compilerConstant(final CompilerConstants cc) {
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: 17752
diff changeset
   341
        final FunctionNode functionNode = lc.getCurrentFunction();
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17233
diff changeset
   342
        return new IdentNode(functionNode.getToken(), functionNode.getFinish(), cc.symbolName());
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   343
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   344
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
   345
    private static boolean isTerminal(final List<Statement> statements) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   346
        return !statements.isEmpty() && statements.get(statements.size() - 1).hasTerminalFlags();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   347
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   348
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   349
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   350
     * Splice finally code into all endpoints of a trynode
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   351
     * @param tryNode the try node
19100
62d400be5b44 8021244: Inconsistent stackmap with splitter threshold set very low
hannesw
parents: 18867
diff changeset
   352
     * @param rethrows list of rethrowing throw nodes from synthetic catch blocks
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   353
     * @param finallyBody the code in the original finally block
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   354
     * @return new try node after splicing finally code (same if nop)
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   355
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   356
    private Node spliceFinally(final TryNode tryNode, final List<ThrowNode> rethrows, final Block finallyBody) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   357
        assert tryNode.getFinallyBody() == null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   358
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   359
        final LexicalContext lowerLc = lc;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   360
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: 17752
diff changeset
   361
        final TryNode newTryNode = (TryNode)tryNode.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   362
            final List<Node> insideTry = new ArrayList<>();
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   363
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   364
            @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   365
            public boolean enterDefault(final Node node) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   366
                insideTry.add(node);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   367
                return true;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   368
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   369
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   370
            @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   371
            public boolean enterFunctionNode(final FunctionNode functionNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   372
                // do not enter function nodes - finally code should not be inlined into them
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   373
                return false;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   374
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   375
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   376
            @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   377
            public Node leaveThrowNode(final ThrowNode throwNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   378
                if (rethrows.contains(throwNode)) {
17982
03696682b6e5 8015961: Several small code-gardening fixes
attila
parents: 17769
diff changeset
   379
                    final List<Statement> newStatements = copyFinally(finallyBody);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   380
                    if (!isTerminal(newStatements)) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   381
                        newStatements.add(throwNode);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   382
                    }
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   383
                    return BlockStatement.createReplacement(throwNode, newStatements);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   384
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   385
                return throwNode;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   386
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   387
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   388
            @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   389
            public Node leaveBreakNode(final BreakNode breakNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   390
                return copy(breakNode, (Node)Lower.this.lc.getBreakable(breakNode.getLabelName()));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   391
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   392
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   393
            @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   394
            public Node leaveContinueNode(final ContinueNode continueNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   395
                return copy(continueNode, Lower.this.lc.getContinueTo(continueNode.getLabelName()));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   396
            }
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   397
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   398
            @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   399
            public Node leaveReturnNode(final ReturnNode returnNode) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   400
                final Expression expr  = returnNode.getExpression();
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
   401
                final List<Statement> newStatements = new ArrayList<>();
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   402
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   403
                final Expression resultNode;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   404
                if (expr != null) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   405
                    //we need to evaluate the result of the return in case it is complex while
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   406
                    //still in the try block, store it in a result value and return it afterwards
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   407
                    resultNode = new IdentNode(Lower.this.compilerConstant(RETURN));
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   408
                    newStatements.add(new ExpressionStatement(returnNode.getLineNumber(), returnNode.getToken(), returnNode.getFinish(), new BinaryNode(Token.recast(returnNode.getToken(), TokenType.ASSIGN), resultNode, expr)));
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   409
                    lowerLc.setFlag(lowerLc.getCurrentFunction(), FunctionNode.USES_RETURN_SYMBOL);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   410
                } else {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   411
                    resultNode = null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   412
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   413
17982
03696682b6e5 8015961: Several small code-gardening fixes
attila
parents: 17769
diff changeset
   414
                newStatements.addAll(copyFinally(finallyBody));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   415
                if (!isTerminal(newStatements)) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   416
                    newStatements.add(expr == null ? returnNode : returnNode.setExpression(resultNode));
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   417
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   418
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   419
                return BlockStatement.createReplacement(returnNode, lc.getCurrentBlock().getFinish(), newStatements);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   420
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   421
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
   422
            private Node copy(final Statement endpoint, final Node targetNode) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   423
                if (!insideTry.contains(targetNode)) {
17982
03696682b6e5 8015961: Several small code-gardening fixes
attila
parents: 17769
diff changeset
   424
                    final List<Statement> newStatements = copyFinally(finallyBody);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   425
                    if (!isTerminal(newStatements)) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   426
                        newStatements.add(endpoint);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   427
                    }
19100
62d400be5b44 8021244: Inconsistent stackmap with splitter threshold set very low
hannesw
parents: 18867
diff changeset
   428
                    return BlockStatement.createReplacement(endpoint, tryNode.getFinish(), newStatements);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   429
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   430
                return endpoint;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   431
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   432
        });
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   433
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   434
        addStatement(newTryNode);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   435
        for (final Node statement : finallyBody.getStatements()) {
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
   436
            addStatement((Statement)statement);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   437
        }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   438
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   439
        return newTryNode;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   440
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   441
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   442
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   443
    public Node leaveTryNode(final TryNode tryNode) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   444
        final Block finallyBody = tryNode.getFinallyBody();
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   445
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   446
        if (finallyBody == null) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   447
            return addStatement(ensureUnconditionalCatch(tryNode));
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   448
        }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   449
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   450
        /*
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   451
         * create a new trynode
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   452
         *    if we have catches:
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   453
         *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   454
         *    try            try
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   455
         *       x              try
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   456
         *    catch               x
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   457
         *       y              catch
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   458
         *    finally z           y
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   459
         *                   catchall
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   460
         *                        rethrow
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   461
         *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   462
         *   otheriwse
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   463
         *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   464
         *   try              try
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   465
         *      x               x
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   466
         *   finally          catchall
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   467
         *      y               rethrow
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   468
         *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   469
         *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   470
         *   now splice in finally code wherever needed
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   471
         *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   472
         */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   473
        TryNode newTryNode;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   474
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   475
        final Block catchAll = catchAllBlock(tryNode);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   476
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   477
        final List<ThrowNode> rethrows = new ArrayList<>();
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: 17752
diff changeset
   478
        catchAll.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   479
            @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   480
            public boolean enterThrowNode(final ThrowNode throwNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   481
                rethrows.add(throwNode);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   482
                return true;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   483
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   484
        });
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   485
        assert rethrows.size() == 1;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   486
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   487
        if (tryNode.getCatchBlocks().isEmpty()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   488
            newTryNode = tryNode.setFinallyBody(null);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   489
        } else {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   490
            final Block outerBody = new Block(tryNode.getToken(), tryNode.getFinish(), ensureUnconditionalCatch(tryNode.setFinallyBody(null)));
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   491
            newTryNode = tryNode.setBody(outerBody).setCatchBlocks(null);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   492
        }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   493
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   494
        newTryNode = newTryNode.setCatchBlocks(Arrays.asList(catchAll)).setFinallyBody(null);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   495
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   496
        /*
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   497
         * Now that the transform is done, we have to go into the try and splice
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   498
         * the finally block in front of any statement that is outside the try
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   499
         */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   500
        return spliceFinally(newTryNode, rethrows, finallyBody);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   501
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   502
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   503
    private TryNode ensureUnconditionalCatch(final TryNode tryNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   504
        final List<CatchNode> catches = tryNode.getCatches();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   505
        if(catches == null || catches.isEmpty() || catches.get(catches.size() - 1).getExceptionCondition() == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   506
            return tryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   507
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   508
        // If the last catch block is conditional, add an unconditional rethrow block
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   509
        final List<Block> newCatchBlocks = new ArrayList<>(tryNode.getCatchBlocks());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   510
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   511
        newCatchBlocks.add(catchAllBlock(tryNode));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   512
        return tryNode.setCatchBlocks(newCatchBlocks);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   513
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   514
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   515
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   516
    public Node leaveVarNode(final VarNode varNode) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   517
        addStatement(varNode);
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: 17752
diff changeset
   518
        if (varNode.getFlag(VarNode.IS_LAST_FUNCTION_DECLARATION) && lc.getCurrentFunction().isProgram()) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   519
            new ExpressionStatement(varNode.getLineNumber(), varNode.getToken(), varNode.getFinish(), new IdentNode(varNode.getName())).accept(this);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   520
        }
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   521
        return varNode;
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   522
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   523
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   524
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   525
    public Node leaveWhileNode(final WhileNode whileNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   526
        final Expression test = whileNode.getTest();
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   527
        final Block body = whileNode.getBody();
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   528
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   529
        if (isAlwaysTrue(test)) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   530
            //turn it into a for node without a test.
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   531
            final ForNode forNode = (ForNode)new ForNode(whileNode.getLineNumber(), whileNode.getToken(), whileNode.getFinish(), body, ForNode.IS_FOR).accept(this);
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: 17752
diff changeset
   532
            lc.replace(whileNode, forNode);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   533
            return forNode;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   534
        }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   535
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   536
         return addStatement(checkEscape(whileNode));
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   537
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   538
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   539
    @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   540
    public Node leaveWithNode(final WithNode withNode) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   541
        return addStatement(withNode);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   542
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   543
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   544
    /**
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   545
     * Given a function node that is a callee in a CallNode, replace it with
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   546
     * the appropriate marker function. This is used by {@link CodeGenerator}
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   547
     * for fast scope calls
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   548
     *
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   549
     * @param function function called by a CallNode
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   550
     * @return transformed node to marker function or identity if not ident/access/indexnode
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   551
     */
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   552
    private static Expression markerFunction(final Expression function) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   553
        if (function instanceof IdentNode) {
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   554
            return ((IdentNode)function).setIsFunction();
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   555
        } else if (function instanceof BaseNode) {
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   556
            return ((BaseNode)function).setIsFunction();
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   557
        }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   558
        return function;
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   559
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   560
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   561
    /**
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   562
     * Calculate a synthetic eval location for a node for the stacktrace, for example src#17<eval>
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   563
     * @param node a node
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   564
     * @return eval location
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   565
     */
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17233
diff changeset
   566
    private String evalLocation(final IdentNode node) {
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: 17752
diff changeset
   567
        final Source source = lc.getCurrentFunction().getSource();
20929
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 19100
diff changeset
   568
        final int pos = node.position();
20949
b230767d5d40 8026692: eval() throws NullPointerException with --compile-only
hannesw
parents: 20929
diff changeset
   569
        // Code installer is null when running with --compile-only, use 0 as id in that case
b230767d5d40 8026692: eval() throws NullPointerException with --compile-only
hannesw
parents: 20929
diff changeset
   570
        final long id = installer == null ? 0 : installer.getUniqueEvalId();
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   571
        return new StringBuilder().
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17233
diff changeset
   572
            append(source.getName()).
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   573
            append('#').
20929
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 19100
diff changeset
   574
            append(source.getLine(pos)).
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 19100
diff changeset
   575
            append(':').
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 19100
diff changeset
   576
            append(source.getColumn(pos)).
f2bd18181940 8026167: Class cache/reuse of 'eval' scripts results in ClassCastException in some cases.
sundar
parents: 19100
diff changeset
   577
            append("<eval>@").
20949
b230767d5d40 8026692: eval() throws NullPointerException with --compile-only
hannesw
parents: 20929
diff changeset
   578
            append(id).
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   579
            toString();
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   580
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   581
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   582
    /**
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   583
     * Check whether a call node may be a call to eval. In that case we
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   584
     * clone the args in order to create the following construct in
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   585
     * {@link CodeGenerator}
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   586
     *
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   587
     * <pre>
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   588
     * if (calledFuntion == buildInEval) {
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   589
     *    eval(cloned arg);
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   590
     * } else {
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   591
     *    cloned arg;
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   592
     * }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   593
     * </pre>
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   594
     *
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   595
     * @param callNode call node to check if it's an eval
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   596
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   597
    private CallNode checkEval(final CallNode callNode) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   598
        if (callNode.getFunction() instanceof IdentNode) {
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   599
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   600
            final List<Expression> args = callNode.getArgs();
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   601
            final IdentNode callee = (IdentNode)callNode.getFunction();
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   602
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   603
            // 'eval' call with at least one argument
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   604
            if (args.size() >= 1 && EVAL.symbolName().equals(callee.getName())) {
25423
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25244
diff changeset
   605
                final List<Expression> evalArgs = new ArrayList<>(args.size());
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25244
diff changeset
   606
                for(final Expression arg: args) {
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25244
diff changeset
   607
                    evalArgs.add((Expression)ensureUniqueNamesIn(arg).accept(this));
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25244
diff changeset
   608
                }
ca63828cf996 8047067: all eval arguments need to be copied in Lower
attila
parents: 25244
diff changeset
   609
                return callNode.setEvalArgs(new CallNode.EvalArgs(evalArgs, evalLocation(callee)));
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   610
            }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   611
        }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   612
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   613
        return callNode;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   614
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   615
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   616
    /**
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   617
     * Helper that given a loop body makes sure that it is not terminal if it
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   618
     * has a continue that leads to the loop header or to outer loops' loop
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   619
     * headers. This means that, even if the body ends with a terminal
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   620
     * statement, we cannot tag it as terminal
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   621
     *
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   622
     * @param loopBody the loop body to check
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   623
     * @return true if control flow may escape the loop
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   624
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   625
    private static boolean controlFlowEscapes(final LexicalContext lex, final Block loopBody) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   626
        final List<Node> escapes = new ArrayList<>();
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   627
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: 17752
diff changeset
   628
        loopBody.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   629
            @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   630
            public Node leaveBreakNode(final BreakNode node) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   631
                escapes.add(node);
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   632
                return node;
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   633
            }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   634
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   635
            @Override
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents: 16524
diff changeset
   636
            public Node leaveContinueNode(final ContinueNode node) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   637
                // all inner loops have been popped.
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   638
                if (lex.contains(lex.getContinueTo(node.getLabelName()))) {
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   639
                    escapes.add(node);
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   640
                }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   641
                return node;
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   642
            }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   643
        });
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   644
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   645
        return !escapes.isEmpty();
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   646
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   647
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   648
    @SuppressWarnings("unchecked")
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   649
    private <T extends LoopNode> T checkEscape(final T loopNode) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   650
        final boolean escapes = controlFlowEscapes(lc, loopNode.getBody());
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   651
        if (escapes) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   652
            return (T)loopNode.
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   653
                setBody(lc, loopNode.getBody().setIsTerminal(lc, false)).
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   654
                setControlFlowEscapes(lc, escapes);
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   655
        }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   656
        return loopNode;
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   657
    }
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   658
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   659
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
   660
    private Node addStatement(final Statement statement) {
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: 17752
diff changeset
   661
        lc.appendStatement(statement);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   662
        return statement;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   665
    private void addStatementEnclosedInBlock(final Statement stmt) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   666
        BlockStatement b = BlockStatement.createReplacement(stmt, Collections.<Statement>singletonList(stmt));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   667
        if(stmt.isTerminal()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   668
            b = b.setBlock(b.getBlock().setIsTerminal(null, true));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   669
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   670
        addStatement(b);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   671
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   672
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   673
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   674
     * An internal expression has a symbol that is tagged internal. Check if
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   675
     * this is such a node
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   676
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   677
     * @param expression expression to check for internal symbol
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   678
     * @return true if internal, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   679
     */
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18848
diff changeset
   680
    private static boolean isInternalExpression(final Expression expression) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   681
        if (!(expression instanceof IdentNode)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   682
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   683
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24745
diff changeset
   684
        final Symbol symbol = ((IdentNode)expression).getSymbol();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   685
        return symbol != null && symbol.isInternal();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   686
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   687
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
    /**
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   689
     * Is this an assignment to the special variable that hosts scripting eval
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   690
     * results, i.e. __return__?
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   691
     *
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   692
     * @param expression expression to check whether it is $evalresult = X
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   693
     * @return true if an assignment to eval result, false otherwise
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   694
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   695
    private static boolean isEvalResultAssignment(final Node expression) {
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24719
diff changeset
   696
        final Node e = expression;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   697
        if (e instanceof BinaryNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   698
            final Node lhs = ((BinaryNode)e).lhs();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   699
            if (lhs instanceof IdentNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   700
                return ((IdentNode)lhs).getName().equals(RETURN.symbolName());
16206
83069fa0935b 8006529: Methods always get callee - it should be conditional
attila
parents: 16201
diff changeset
   701
            }
83069fa0935b 8006529: Methods always get callee - it should be conditional
attila
parents: 16201
diff changeset
   702
        }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   703
        return false;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   704
    }
16201
889ddb179cdf 8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents: 16188
diff changeset
   705
}