nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java
author sundar
Mon, 14 Sep 2015 16:13:10 +0530
changeset 32692 8f60bd284bf4
parent 31549 b627094c5649
child 32888 24f99be3d5ab
permissions -rw-r--r--
8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes Reviewed-by: attila, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     1
/*
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     4
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    10
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    15
 * accompanied this code).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    16
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    20
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    23
 * questions.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    24
 */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    25
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    26
package jdk.nashorn.internal.codegen;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    27
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.RETURN;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    29
import static jdk.nashorn.internal.ir.Expression.isAlwaysFalse;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    30
import static jdk.nashorn.internal.ir.Expression.isAlwaysTrue;
27969
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
    31
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    32
import java.util.ArrayDeque;
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
    33
import java.util.ArrayList;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    34
import java.util.Collections;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    35
import java.util.Deque;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    36
import java.util.HashSet;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    37
import java.util.IdentityHashMap;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    38
import java.util.Iterator;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    39
import java.util.LinkedList;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    40
import java.util.List;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    41
import java.util.Map;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    42
import java.util.Set;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    43
import jdk.nashorn.internal.codegen.types.Type;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    44
import jdk.nashorn.internal.ir.AccessNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    45
import jdk.nashorn.internal.ir.BinaryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    46
import jdk.nashorn.internal.ir.Block;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    47
import jdk.nashorn.internal.ir.BreakNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    48
import jdk.nashorn.internal.ir.BreakableNode;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
    49
import jdk.nashorn.internal.ir.CallNode;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    50
import jdk.nashorn.internal.ir.CaseNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    51
import jdk.nashorn.internal.ir.CatchNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    52
import jdk.nashorn.internal.ir.ContinueNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    53
import jdk.nashorn.internal.ir.Expression;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
    54
import jdk.nashorn.internal.ir.ExpressionStatement;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    55
import jdk.nashorn.internal.ir.ForNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    56
import jdk.nashorn.internal.ir.FunctionNode;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
    57
import jdk.nashorn.internal.ir.GetSplitState;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    58
import jdk.nashorn.internal.ir.IdentNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    59
import jdk.nashorn.internal.ir.IfNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    60
import jdk.nashorn.internal.ir.IndexNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    61
import jdk.nashorn.internal.ir.JoinPredecessor;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    62
import jdk.nashorn.internal.ir.JoinPredecessorExpression;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    63
import jdk.nashorn.internal.ir.JumpStatement;
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
    64
import jdk.nashorn.internal.ir.JumpToInlinedFinally;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    65
import jdk.nashorn.internal.ir.LabelNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    66
import jdk.nashorn.internal.ir.LexicalContext;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    67
import jdk.nashorn.internal.ir.LexicalContextNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    68
import jdk.nashorn.internal.ir.LiteralNode;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
    69
import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    70
import jdk.nashorn.internal.ir.LocalVariableConversion;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    71
import jdk.nashorn.internal.ir.LoopNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    72
import jdk.nashorn.internal.ir.Node;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
    73
import jdk.nashorn.internal.ir.ObjectNode;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    74
import jdk.nashorn.internal.ir.PropertyNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    75
import jdk.nashorn.internal.ir.ReturnNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    76
import jdk.nashorn.internal.ir.RuntimeNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    77
import jdk.nashorn.internal.ir.RuntimeNode.Request;
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26889
diff changeset
    78
import jdk.nashorn.internal.ir.SplitReturn;
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
    79
import jdk.nashorn.internal.ir.Statement;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    80
import jdk.nashorn.internal.ir.SwitchNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    81
import jdk.nashorn.internal.ir.Symbol;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    82
import jdk.nashorn.internal.ir.TernaryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    83
import jdk.nashorn.internal.ir.ThrowNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    84
import jdk.nashorn.internal.ir.TryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    85
import jdk.nashorn.internal.ir.UnaryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    86
import jdk.nashorn.internal.ir.VarNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    87
import jdk.nashorn.internal.ir.WhileNode;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
    88
import jdk.nashorn.internal.ir.WithNode;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    89
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    90
import jdk.nashorn.internal.parser.TokenType;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    91
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    92
/**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    93
 * Calculates types for local variables. For purposes of local variable type calculation, the only types used are
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    94
 * Undefined, boolean, int, long, double, and Object. The calculation eagerly widens types of local variable to their
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    95
 * widest at control flow join points.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    96
 * TODO: investigate a more sophisticated solution that uses use/def information to only widens the type of a local
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    97
 * variable to its widest used type after the join point. That would eliminate some widenings of undefined variables to
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    98
 * object, most notably those used only in loops. We need a full liveness analysis for that. Currently, we can establish
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    99
 * per-type liveness, which eliminates most of unwanted dead widenings.
27972
8ec664fdf8da 8066236: RuntimeNode forces copy creation on visitation
attila
parents: 27970
diff changeset
   100
 * NOTE: the way this class is implemented, it actually processes the AST in two passes. The first pass is top-down and
8ec664fdf8da 8066236: RuntimeNode forces copy creation on visitation
attila
parents: 27970
diff changeset
   101
 * implemented in {@code enterXxx} methods. This pass does not mutate the AST (except for one occurrence, noted below),
8ec664fdf8da 8066236: RuntimeNode forces copy creation on visitation
attila
parents: 27970
diff changeset
   102
 * as being able to find relevant labels for control flow joins is sensitive to their reference identity, and mutated
8ec664fdf8da 8066236: RuntimeNode forces copy creation on visitation
attila
parents: 27970
diff changeset
   103
 * label-carrying nodes will create copies of their labels. A second bottom-up pass applying the changes is implemented
8ec664fdf8da 8066236: RuntimeNode forces copy creation on visitation
attila
parents: 27970
diff changeset
   104
 * in the separate visitor sitting in {@link #leaveFunctionNode(FunctionNode)}. This visitor will also instantiate new
8ec664fdf8da 8066236: RuntimeNode forces copy creation on visitation
attila
parents: 27970
diff changeset
   105
 * instances of the calculator to be run on nested functions (when not lazy compiling).
8ec664fdf8da 8066236: RuntimeNode forces copy creation on visitation
attila
parents: 27970
diff changeset
   106
 *
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   107
 */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   108
final class LocalVariableTypesCalculator extends NodeVisitor<LexicalContext>{
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   109
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   110
    private static class JumpOrigin {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   111
        final JoinPredecessor node;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   112
        final Map<Symbol, LvarType> types;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   113
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   114
        JumpOrigin(final JoinPredecessor node, final Map<Symbol, LvarType> types) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   115
            this.node = node;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   116
            this.types = types;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   117
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   118
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   119
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   120
    private static class JumpTarget {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   121
        private final List<JumpOrigin> origins = new LinkedList<>();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   122
        private Map<Symbol, LvarType> types = Collections.emptyMap();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   123
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   124
        void addOrigin(final JoinPredecessor originNode, final Map<Symbol, LvarType> originTypes) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   125
            origins.add(new JumpOrigin(originNode, originTypes));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   126
            this.types = getUnionTypes(this.types, originTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   127
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   128
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   129
    private enum LvarType {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   130
        UNDEFINED(Type.UNDEFINED),
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   131
        BOOLEAN(Type.BOOLEAN),
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   132
        INT(Type.INT),
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   133
        LONG(Type.LONG),
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   134
        DOUBLE(Type.NUMBER),
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   135
        OBJECT(Type.OBJECT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   136
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   137
        private final Type type;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   138
        private final TypeHolderExpression typeExpression;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   139
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   140
        private LvarType(final Type type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   141
            this.type = type;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   142
            this.typeExpression = new TypeHolderExpression(type);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   143
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   144
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   145
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   146
    /**
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   147
     * A bogus Expression subclass that only reports its type. Used to interrogate BinaryNode and UnaryNode about their
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   148
     * types by creating temporary copies of them and replacing their operands with instances of these. An alternative
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   149
     * solution would be to add BinaryNode.getType(Type lhsType, Type rhsType) and UnaryNode.getType(Type exprType)
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   150
     * methods. For the time being though, this is easier to implement and is in fact fairly clean. It does result in
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   151
     * generation of higher number of temporary short lived nodes, though.
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   152
     */
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   153
    private static class TypeHolderExpression extends Expression {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   154
        private static final long serialVersionUID = 1L;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   155
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   156
        private final Type type;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   157
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   158
        TypeHolderExpression(final Type type) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   159
            super(0L, 0, 0);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   160
            this.type = type;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   161
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   162
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   163
        @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   164
        public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   165
            throw new AssertionError();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   166
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   167
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   168
        @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   169
        public Type getType() {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   170
            return type;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   171
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   172
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   173
        @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   174
        public void toString(final StringBuilder sb, final boolean printType) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   175
            throw new AssertionError();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   176
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   177
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   178
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   179
    private static final Map<Type, LvarType> TO_LVAR_TYPE = new IdentityHashMap<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   180
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   181
    static {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   182
        for(final LvarType lvarType: LvarType.values()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   183
            TO_LVAR_TYPE.put(lvarType.type, lvarType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   184
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   185
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   186
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   187
    @SuppressWarnings("unchecked")
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   188
    private static IdentityHashMap<Symbol, LvarType> cloneMap(final Map<Symbol, LvarType> map) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   189
        return (IdentityHashMap<Symbol, LvarType>)((IdentityHashMap<?,?>)map).clone();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   190
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   191
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   192
    private LocalVariableConversion createConversion(final Symbol symbol, final LvarType branchLvarType,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   193
            final Map<Symbol, LvarType> joinLvarTypes, final LocalVariableConversion next) {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   194
        final LvarType targetType = joinLvarTypes.get(symbol);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   195
        assert targetType != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   196
        if(targetType == branchLvarType) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   197
            return next;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   198
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   199
        // NOTE: we could naively just use symbolIsUsed(symbol, branchLvarType) here, but that'd be wrong. While
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   200
        // technically a conversion will read the value of the symbol with that type, but it will also write it to a new
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   201
        // type, and that type might be dead (we can't know yet). For this reason, we don't treat conversion reads as
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   202
        // real uses until we know their target type is live. If we didn't do this, and just did a symbolIsUsed here,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   203
        // we'd introduce false live variables which could nevertheless turn into dead ones in a subsequent
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   204
        // deoptimization, causing a shift in the list of live locals that'd cause erroneous restoration of
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   205
        // continuations (since RewriteException's byteCodeSlots carries an array and not a name-value map).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   206
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   207
        symbolIsConverted(symbol, branchLvarType, targetType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   208
        return new LocalVariableConversion(symbol, branchLvarType.type, targetType.type, next);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   209
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   210
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   211
    private static Map<Symbol, LvarType> getUnionTypes(final Map<Symbol, LvarType> types1, final Map<Symbol, LvarType> types2) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   212
        if(types1 == types2 || types1.isEmpty()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   213
            return types2;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   214
        } else if(types2.isEmpty()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   215
            return types1;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   216
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   217
        final Set<Symbol> commonSymbols = new HashSet<>(types1.keySet());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   218
        commonSymbols.retainAll(types2.keySet());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   219
        // We have a chance of returning an unmodified set if both sets have the same keys and one is strictly wider
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   220
        // than the other.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   221
        final int commonSize = commonSymbols.size();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   222
        final int types1Size = types1.size();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   223
        final int types2Size = types2.size();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   224
        if(commonSize == types1Size && commonSize == types2Size) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   225
            boolean matches1 = true, matches2 = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   226
            Map<Symbol, LvarType> union = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   227
            for(final Symbol symbol: commonSymbols) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   228
                final LvarType type1 = types1.get(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   229
                final LvarType type2 = types2.get(symbol);
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   230
                final LvarType widest = widestLvarType(type1, type2);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   231
                if(widest != type1 && matches1) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   232
                    matches1 = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   233
                    if(!matches2) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   234
                        union = cloneMap(types1);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   235
                    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   236
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   237
                if (widest != type2 && matches2) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   238
                    matches2 = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   239
                    if(!matches1) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   240
                        union = cloneMap(types2);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   241
                    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   242
                }
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   243
                if(!(matches1 || matches2)) {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   244
                    assert union != null;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   245
                    union.put(symbol, widest);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   246
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   247
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   248
            return matches1 ? types1 : matches2 ? types2 : union;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   249
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   250
        // General case
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   251
        final Map<Symbol, LvarType> union;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   252
        if(types1Size > types2Size) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   253
            union = cloneMap(types1);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   254
            union.putAll(types2);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   255
        } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   256
            union = cloneMap(types2);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   257
            union.putAll(types1);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   258
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   259
        for(final Symbol symbol: commonSymbols) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   260
            final LvarType type1 = types1.get(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   261
            final LvarType type2 = types2.get(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   262
            union.put(symbol, widestLvarType(type1,  type2));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   263
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   264
        return union;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   265
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   266
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   267
    private static void symbolIsUsed(final Symbol symbol, final LvarType type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   268
        if(type != LvarType.UNDEFINED) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   269
            symbol.setHasSlotFor(type.type);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   270
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   271
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   272
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   273
    private static class SymbolConversions {
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30392
diff changeset
   274
        private static final byte I2L = 1 << 0;
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30392
diff changeset
   275
        private static final byte I2D = 1 << 1;
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30392
diff changeset
   276
        private static final byte I2O = 1 << 2;
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30392
diff changeset
   277
        private static final byte L2D = 1 << 3;
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30392
diff changeset
   278
        private static final byte L2O = 1 << 4;
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 30392
diff changeset
   279
        private static final byte D2O = 1 << 5;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   280
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   281
        private byte conversions;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   282
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   283
        void recordConversion(final LvarType from, final LvarType to) {
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27206
diff changeset
   284
            switch (from) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   285
            case UNDEFINED:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   286
                return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   287
            case INT:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   288
            case BOOLEAN:
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27206
diff changeset
   289
                switch (to) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   290
                case LONG:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   291
                    recordConversion(I2L);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   292
                    return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   293
                case DOUBLE:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   294
                    recordConversion(I2D);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   295
                    return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   296
                case OBJECT:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   297
                    recordConversion(I2O);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   298
                    return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   299
                default:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   300
                    illegalConversion(from, to);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   301
                    return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   302
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   303
            case LONG:
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27206
diff changeset
   304
                switch (to) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   305
                case DOUBLE:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   306
                    recordConversion(L2D);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   307
                    return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   308
                case OBJECT:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   309
                    recordConversion(L2O);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   310
                    return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   311
                default:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   312
                    illegalConversion(from, to);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   313
                    return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   314
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   315
            case DOUBLE:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   316
                if(to == LvarType.OBJECT) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   317
                    recordConversion(D2O);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   318
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   319
                return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   320
            default:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   321
                illegalConversion(from, to);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   322
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   323
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   324
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   325
        private static void illegalConversion(final LvarType from, final LvarType to) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   326
            throw new AssertionError("Invalid conversion from " + from + " to " + to);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   327
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   328
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   329
        void recordConversion(final byte convFlag) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   330
            conversions = (byte)(conversions | convFlag);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   331
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   332
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   333
        boolean hasConversion(final byte convFlag) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   334
            return (conversions & convFlag) != 0;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   335
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   336
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   337
        void calculateTypeLiveness(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   338
            if(symbol.hasSlotFor(Type.OBJECT)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   339
                if(hasConversion(D2O)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   340
                    symbol.setHasSlotFor(Type.NUMBER);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   341
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   342
                if(hasConversion(L2O)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   343
                    symbol.setHasSlotFor(Type.LONG);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   344
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   345
                if(hasConversion(I2O)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   346
                    symbol.setHasSlotFor(Type.INT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   347
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   348
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   349
            if(symbol.hasSlotFor(Type.NUMBER)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   350
                if(hasConversion(L2D)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   351
                    symbol.setHasSlotFor(Type.LONG);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   352
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   353
                if(hasConversion(I2D)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   354
                    symbol.setHasSlotFor(Type.INT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   355
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   356
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   357
            if(symbol.hasSlotFor(Type.LONG)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   358
                if(hasConversion(I2L)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   359
                    symbol.setHasSlotFor(Type.INT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   360
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   361
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   362
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   363
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   364
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   365
    private void symbolIsConverted(final Symbol symbol, final LvarType from, final LvarType to) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   366
        SymbolConversions conversions = symbolConversions.get(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   367
        if(conversions == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   368
            conversions = new SymbolConversions();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   369
            symbolConversions.put(symbol, conversions);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   370
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   371
        conversions.recordConversion(from, to);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   372
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   373
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   374
    private static LvarType toLvarType(final Type type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   375
        assert type != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   376
        final LvarType lvarType = TO_LVAR_TYPE.get(type);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   377
        if(lvarType != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   378
            return lvarType;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   379
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   380
        assert type.isObject();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   381
        return LvarType.OBJECT;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   382
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   383
    private static LvarType widestLvarType(final LvarType t1, final LvarType t2) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   384
        if(t1 == t2) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   385
            return t1;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   386
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   387
        // Undefined or boolean to anything always widens to object.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   388
        if(t1.ordinal() < LvarType.INT.ordinal() || t2.ordinal() < LvarType.INT.ordinal()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   389
            return LvarType.OBJECT;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   390
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   391
        // NOTE: we allow "widening" of long to double even though it can lose precision. ECMAScript doesn't have an
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   392
        // Int64 type anyway, so this loss of precision is actually more conformant to the specification...
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   393
        return LvarType.values()[Math.max(t1.ordinal(), t2.ordinal())];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   394
    }
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   395
    private final Compiler compiler;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   396
    private final Map<Label, JumpTarget> jumpTargets = new IdentityHashMap<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   397
    // Local variable type mapping at the currently evaluated point. No map instance is ever modified; setLvarType() always
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   398
    // allocates a new map. Immutability of maps allows for cheap snapshots by just keeping the reference to the current
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   399
    // value.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   400
    private Map<Symbol, LvarType> localVariableTypes = new IdentityHashMap<>();
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   401
    // Stack for evaluated expression types.
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   402
    private final Deque<LvarType> typeStack = new ArrayDeque<>();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   403
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   404
    // Whether the current point in the AST is reachable code
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   405
    private boolean reachable = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   406
    // Return type of the function
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   407
    private Type returnType = Type.UNKNOWN;
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
   408
    // Synthetic return node that we must insert at the end of the function if it's end is reachable.
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
   409
    private ReturnNode syntheticReturn;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   410
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   411
    private boolean alreadyEnteredTopLevelFunction;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   412
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   413
    // LvarType and conversion information gathered during the top-down pass; applied to nodes in the bottom-up pass.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   414
    private final Map<JoinPredecessor, LocalVariableConversion> localVariableConversions = new IdentityHashMap<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   415
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   416
    private final Map<IdentNode, LvarType> identifierLvarTypes = new IdentityHashMap<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   417
    private final Map<Symbol, SymbolConversions> symbolConversions = new IdentityHashMap<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   418
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   419
    // Stack of open labels for starts of catch blocks, one for every currently traversed try block; for inserting
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   420
    // control flow edges to them. Note that we currently don't insert actual control flow edges, but instead edges that
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   421
    // help us with type calculations. This means that some operations that can result in an exception being thrown
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   422
    // aren't considered (function calls, side effecting property getters and setters etc.), while some operations that
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   423
    // don't result in control flow transfers do originate an edge to the catch blocks (namely, assignments to local
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   424
    // variables).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   425
    private final Deque<Label> catchLabels = new ArrayDeque<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   426
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   427
    LocalVariableTypesCalculator(final Compiler compiler) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   428
        super(new LexicalContext());
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   429
        this.compiler = compiler;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   430
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   431
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   432
    private JumpTarget createJumpTarget(final Label label) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   433
        assert !jumpTargets.containsKey(label);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   434
        final JumpTarget jumpTarget = new JumpTarget();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   435
        jumpTargets.put(label, jumpTarget);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   436
        return jumpTarget;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   437
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   438
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   439
    private void doesNotContinueSequentially() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   440
        reachable = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   441
        localVariableTypes = Collections.emptyMap();
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   442
        assertTypeStackIsEmpty();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   443
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   444
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   445
    private boolean pushExpressionType(final Expression expr) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   446
        typeStack.push(toLvarType(expr.getType()));
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   447
        return false;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   448
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   449
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   450
    @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   451
    public boolean enterAccessNode(final AccessNode accessNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   452
        visitExpression(accessNode.getBase());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   453
        return pushExpressionType(accessNode);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   454
    }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   455
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   456
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   457
    public boolean enterBinaryNode(final BinaryNode binaryNode) {
27969
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   458
        // NOTE: regardless of operator's lexical associativity, lhs is always evaluated first.
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   459
        final Expression lhs = binaryNode.lhs();
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   460
        final LvarType lhsType;
30392
dc4a419b2982 8079362: Enforce best practices for Node token API usage
attila
parents: 29404
diff changeset
   461
        if (!(lhs instanceof IdentNode && binaryNode.isTokenType(TokenType.ASSIGN))) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   462
            lhsType = visitExpression(lhs);
27969
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   463
        } else {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   464
            // Can't visit IdentNode on LHS of a simple assignment, as visits imply use, and this is def.
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   465
            // The type is irrelevant, as only RHS is used to determine the type anyway.
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   466
            lhsType = LvarType.UNDEFINED;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   467
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   468
27969
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   469
        final boolean isLogical = binaryNode.isLogical();
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   470
        final Label joinLabel = isLogical ? new Label("") : null;
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   471
        if(isLogical) {
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   472
            jumpToLabel((JoinPredecessor)lhs, joinLabel);
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   473
        }
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   474
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   475
        final Expression rhs = binaryNode.rhs();
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   476
        final LvarType rhsType = visitExpression(rhs);
27969
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   477
        if(isLogical) {
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   478
            jumpToLabel((JoinPredecessor)rhs, joinLabel);
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   479
        }
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   480
        joinOnLabel(joinLabel);
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
   481
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   482
        final LvarType type = toLvarType(binaryNode.setOperands(lhsType.typeExpression, rhsType.typeExpression).getType());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   483
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   484
        if(binaryNode.isAssignment() && lhs instanceof IdentNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   485
            if(binaryNode.isSelfModifying()) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   486
                onSelfAssignment((IdentNode)lhs, type);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   487
            } else {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   488
                onAssignment((IdentNode)lhs, type);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   489
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   490
        }
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   491
        typeStack.push(type);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   492
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   493
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   494
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   495
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   496
    public boolean enterBlock(final Block block) {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   497
        for(final Symbol symbol: block.getSymbols()) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   498
            if(symbol.isBytecodeLocal() && getLocalVariableTypeOrNull(symbol) == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   499
                setType(symbol, LvarType.UNDEFINED);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   500
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   501
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   502
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   503
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   504
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   505
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   506
    public boolean enterBreakNode(final BreakNode breakNode) {
26889
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 26766
diff changeset
   507
        return enterJumpStatement(breakNode);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   508
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   509
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   510
    @Override
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   511
    public boolean enterCallNode(final CallNode callNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   512
        visitExpression(callNode.getFunction());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   513
        visitExpressions(callNode.getArgs());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   514
        final CallNode.EvalArgs evalArgs = callNode.getEvalArgs();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   515
        if (evalArgs != null) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   516
            visitExpressions(evalArgs.getArgs());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   517
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   518
        return pushExpressionType(callNode);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   519
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   520
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   521
    @Override
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   522
    public boolean enterContinueNode(final ContinueNode continueNode) {
26889
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 26766
diff changeset
   523
        return enterJumpStatement(continueNode);
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 26766
diff changeset
   524
    }
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 26766
diff changeset
   525
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 26766
diff changeset
   526
    private boolean enterJumpStatement(final JumpStatement jump) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   527
        if(!reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   528
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   529
        }
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   530
        assertTypeStackIsEmpty();
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
   531
        jumpToLabel(jump, jump.getTargetLabel(lc), getBreakTargetTypes(jump.getPopScopeLimit(lc)));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   532
        doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   533
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   534
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   535
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   536
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   537
    protected boolean enterDefault(final Node node) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   538
        return reachable;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   539
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   540
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   541
    private void enterDoWhileLoop(final WhileNode loopNode) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   542
        assertTypeStackIsEmpty();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   543
        final JoinPredecessorExpression test = loopNode.getTest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   544
        final Block body = loopNode.getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   545
        final Label continueLabel = loopNode.getContinueLabel();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   546
        final Label breakLabel = loopNode.getBreakLabel();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   547
        final Map<Symbol, LvarType> beforeLoopTypes = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   548
        final Label repeatLabel = new Label("");
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   549
        for(;;) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   550
            jumpToLabel(loopNode, repeatLabel, beforeLoopTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   551
            final Map<Symbol, LvarType> beforeRepeatTypes = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   552
            body.accept(this);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   553
            if(reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   554
                jumpToLabel(body, continueLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   555
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   556
            joinOnLabel(continueLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   557
            if(!reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   558
                break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   559
            }
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   560
            visitExpressionOnEmptyStack(test);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   561
            jumpToLabel(test, breakLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   562
            if(isAlwaysFalse(test)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   563
                break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   564
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   565
            jumpToLabel(test, repeatLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   566
            joinOnLabel(repeatLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   567
            if(localVariableTypes.equals(beforeRepeatTypes)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   568
                break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   569
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   570
            resetJoinPoint(continueLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   571
            resetJoinPoint(breakLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   572
            resetJoinPoint(repeatLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   573
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   574
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   575
        if(isAlwaysTrue(test)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   576
            doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   577
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   578
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   579
        leaveBreakable(loopNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   580
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   581
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   582
    @Override
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   583
    public boolean enterExpressionStatement(final ExpressionStatement expressionStatement) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   584
        if (reachable) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   585
            visitExpressionOnEmptyStack(expressionStatement.getExpression());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   586
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   587
        return false;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   588
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   589
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   590
    private void assertTypeStackIsEmpty() {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   591
        assert typeStack.isEmpty();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   592
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   593
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   594
    @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   595
    protected Node leaveDefault(final Node node) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   596
        assert !(node instanceof Expression); // All expressions were handled
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   597
        assert !(node instanceof Statement) || typeStack.isEmpty(); // No statements leave with a non-empty stack
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   598
        return node;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   599
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   600
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   601
    private LvarType visitExpressionOnEmptyStack(final Expression expr) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   602
        assertTypeStackIsEmpty();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   603
        return visitExpression(expr);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   604
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   605
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   606
    private LvarType visitExpression(final Expression expr) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   607
        final int stackSize = typeStack.size();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   608
        expr.accept(this);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   609
        assert typeStack.size() == stackSize + 1;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   610
        return typeStack.pop();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   611
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   612
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   613
    private void visitExpressions(final List<Expression> exprs) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   614
        for(final Expression expr: exprs) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   615
            if (expr != null) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   616
                visitExpression(expr);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   617
            }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   618
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   619
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   620
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   621
    @Override
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   622
    public boolean enterForNode(final ForNode forNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   623
        if(!reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   624
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   625
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   626
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   627
        final Expression init = forNode.getInit();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   628
        if(forNode.isForIn()) {
26507
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   629
            final JoinPredecessorExpression iterable = forNode.getModify();
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   630
            visitExpression(iterable);
26507
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   631
            enterTestFirstLoop(forNode, null, init,
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   632
                    // If we're iterating over property names, and we can discern from the runtime environment
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   633
                    // of the compilation that the object being iterated over must use strings for property
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   634
                    // names (e.g., it is a native JS object or array), then we'll not bother trying to treat
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   635
                    // the property names optimistically.
26766
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
   636
                    !compiler.useOptimisticTypes() || (!forNode.isForEach() && compiler.hasStringPropertyIterator(iterable.getExpression())));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   637
        } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   638
            if(init != null) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   639
                visitExpressionOnEmptyStack(init);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   640
            }
26507
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   641
            enterTestFirstLoop(forNode, forNode.getModify(), null, false);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   642
        }
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   643
        assertTypeStackIsEmpty();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   644
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   645
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   646
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   647
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   648
    public boolean enterFunctionNode(final FunctionNode functionNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   649
        if(alreadyEnteredTopLevelFunction) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   650
            typeStack.push(LvarType.OBJECT);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   651
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   652
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   653
        int pos = 0;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   654
        if(!functionNode.isVarArg()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   655
            for (final IdentNode param : functionNode.getParameters()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   656
                final Symbol symbol = param.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   657
                // Parameter is not necessarily bytecode local as it can be scoped due to nested context use, but it
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   658
                // must have a slot if we aren't in a function with vararg signature.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   659
                assert symbol.hasSlot();
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   660
                final Type callSiteParamType = compiler.getParamType(functionNode, pos);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   661
                final LvarType paramType = callSiteParamType == null ? LvarType.OBJECT : toLvarType(callSiteParamType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   662
                setType(symbol, paramType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   663
                // Make sure parameter slot for its incoming value is not marked dead. NOTE: this is a heuristic. Right
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   664
                // now, CodeGenerator.expandParameters() relies on the fact that every parameter's final slot width will
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   665
                // be at least the same as incoming width, therefore even if a parameter is never read, we'll still keep
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   666
                // its slot.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   667
                symbolIsUsed(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   668
                setIdentifierLvarType(param, paramType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   669
                pos++;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   670
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   671
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   672
        setCompilerConstantAsObject(functionNode, CompilerConstants.THIS);
25238
28476bdc25ce 8047728: (function(x){var o={x:0}; with(o){delete x} return o.x})() evaluates to 0 instead of undefined
sundar
parents: 25234
diff changeset
   673
25240
f92c14b1ca11 8047959: bindings created for declarations in eval code are not mutable
sundar
parents: 25238
diff changeset
   674
        // TODO: coarse-grained. If we wanted to solve it completely precisely,
25238
28476bdc25ce 8047728: (function(x){var o={x:0}; with(o){delete x} return o.x})() evaluates to 0 instead of undefined
sundar
parents: 25234
diff changeset
   675
        // we'd also need to push/pop its type when handling WithNode (so that
28476bdc25ce 8047728: (function(x){var o={x:0}; with(o){delete x} return o.x})() evaluates to 0 instead of undefined
sundar
parents: 25234
diff changeset
   676
        // it can go back to undefined after a 'with' block.
28476bdc25ce 8047728: (function(x){var o={x:0}; with(o){delete x} return o.x})() evaluates to 0 instead of undefined
sundar
parents: 25234
diff changeset
   677
        if(functionNode.hasScopeBlock() || functionNode.needsParentScope()) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   678
            setCompilerConstantAsObject(functionNode, CompilerConstants.SCOPE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   679
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   680
        if(functionNode.needsCallee()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   681
            setCompilerConstantAsObject(functionNode, CompilerConstants.CALLEE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   682
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   683
        if(functionNode.needsArguments()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   684
            setCompilerConstantAsObject(functionNode, CompilerConstants.ARGUMENTS);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   685
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   686
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   687
        alreadyEnteredTopLevelFunction = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   688
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   689
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   690
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   691
    @Override
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   692
    public boolean enterGetSplitState(final GetSplitState getSplitState) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   693
        return pushExpressionType(getSplitState);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   694
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   695
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   696
    @Override
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   697
    public boolean enterIdentNode(final IdentNode identNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   698
        final Symbol symbol = identNode.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   699
        if(symbol.isBytecodeLocal()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   700
            symbolIsUsed(symbol);
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   701
            final LvarType type = getLocalVariableType(symbol);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   702
            setIdentifierLvarType(identNode, type);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   703
            typeStack.push(type);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   704
        } else {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   705
            pushExpressionType(identNode);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   706
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   707
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   708
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   709
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   710
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   711
    public boolean enterIfNode(final IfNode ifNode) {
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   712
        processIfNode(ifNode);
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   713
        return false;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   714
    }
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   715
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   716
    private void processIfNode(final IfNode ifNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   717
        if(!reachable) {
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   718
            return;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   719
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   720
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   721
        final Expression test = ifNode.getTest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   722
        final Block pass = ifNode.getPass();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   723
        final Block fail = ifNode.getFail();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   724
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   725
        visitExpressionOnEmptyStack(test);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   726
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   727
        final Map<Symbol, LvarType> passLvarTypes;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   728
        final boolean reachableFromPass;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   729
        final boolean isTestAlwaysTrue = isAlwaysTrue(test);
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   730
        if(isAlwaysFalse(test)) {
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   731
            passLvarTypes = null;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   732
            reachableFromPass = false;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   733
        } else {
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   734
            final Map<Symbol, LvarType> afterTestLvarTypes = localVariableTypes;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   735
            pass.accept(this);
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   736
            assertTypeStackIsEmpty();
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   737
            if (isTestAlwaysTrue) {
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   738
                return;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   739
            }
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   740
            passLvarTypes = localVariableTypes;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   741
            reachableFromPass = reachable;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   742
            localVariableTypes = afterTestLvarTypes;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   743
            reachable = true;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   744
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   745
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   746
        // If we get here, then we need to consider the case where pass block is not executed
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   747
        assert !isTestAlwaysTrue;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   748
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   749
        if (fail != null) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   750
            fail.accept(this);
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   751
            assertTypeStackIsEmpty();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   752
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   753
29404
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   754
        if(reachable) {
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   755
            if(reachableFromPass) {
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   756
                final Map<Symbol, LvarType> failLvarTypes = localVariableTypes;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   757
                localVariableTypes = getUnionTypes(passLvarTypes, failLvarTypes);
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   758
                setConversion(pass, passLvarTypes, localVariableTypes);
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   759
                // IfNode itself is associated with conversions that might need to be performed after the test if
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   760
                // there's no else branch. E.g.
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   761
                // if(x = 1, cond) { x = 1.0 } must widen "x = 1" to a double.
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   762
                setConversion(fail != null ? fail : ifNode, failLvarTypes, localVariableTypes);
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   763
            }
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   764
        } else if (reachableFromPass) {
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   765
            assert passLvarTypes != null;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   766
            localVariableTypes = passLvarTypes;
d9023e6faff1 8074487: Static analysis of IfNode should consider terminating branches
attila
parents: 29283
diff changeset
   767
            reachable = true;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   768
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   769
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   770
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   771
    @Override
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   772
    public boolean enterIndexNode(final IndexNode indexNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   773
        visitExpression(indexNode.getBase());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   774
        visitExpression(indexNode.getIndex());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   775
        return pushExpressionType(indexNode);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   776
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   777
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   778
    @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   779
    public boolean enterJoinPredecessorExpression(final JoinPredecessorExpression joinExpr) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   780
        final Expression expr = joinExpr.getExpression();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   781
        if (expr != null) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   782
            expr.accept(this);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   783
        } else {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   784
            typeStack.push(LvarType.UNDEFINED);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   785
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   786
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   787
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   788
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   789
    @Override
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
   790
    public boolean enterJumpToInlinedFinally(final JumpToInlinedFinally jumpToInlinedFinally) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
   791
        return enterJumpStatement(jumpToInlinedFinally);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
   792
    }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
   793
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
   794
    @Override
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   795
    public boolean enterLiteralNode(final LiteralNode<?> literalNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   796
        if (literalNode instanceof ArrayLiteralNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   797
            final List<Expression> expressions = ((ArrayLiteralNode)literalNode).getElementExpressions();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   798
            if (expressions != null) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   799
                visitExpressions(expressions);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   800
            }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   801
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   802
        pushExpressionType(literalNode);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   803
        return false;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   804
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   805
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   806
    @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   807
    public boolean enterObjectNode(final ObjectNode objectNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   808
        for(final PropertyNode propertyNode: objectNode.getElements()) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   809
            // Avoid falsely adding property keys to the control flow graph
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   810
            final Expression value = propertyNode.getValue();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   811
            if (value != null) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   812
                visitExpression(value);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   813
            }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   814
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   815
        return pushExpressionType(objectNode);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   816
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   817
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   818
    @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   819
    public boolean enterPropertyNode(final PropertyNode propertyNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   820
        // Property nodes are only accessible through object literals, and we handled that case above
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   821
        throw new AssertionError();
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   822
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   823
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   824
    @Override
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   825
    public boolean enterReturnNode(final ReturnNode returnNode) {
26766
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
   826
        if(!reachable) {
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
   827
            return false;
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
   828
        }
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
   829
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   830
        final Expression returnExpr = returnNode.getExpression();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   831
        final Type returnExprType;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   832
        if(returnExpr != null) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   833
            returnExprType = visitExpressionOnEmptyStack(returnExpr).type;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   834
        } else {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   835
            assertTypeStackIsEmpty();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   836
            returnExprType = Type.UNDEFINED;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   837
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   838
        returnType = Type.widestReturnType(returnType, returnExprType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   839
        doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   840
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   841
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   842
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   843
    @Override
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   844
    public boolean enterRuntimeNode(final RuntimeNode runtimeNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   845
        visitExpressions(runtimeNode.getArgs());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   846
        return pushExpressionType(runtimeNode);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   847
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   848
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   849
    @Override
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26889
diff changeset
   850
    public boolean enterSplitReturn(final SplitReturn splitReturn) {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26889
diff changeset
   851
        doesNotContinueSequentially();
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26889
diff changeset
   852
        return false;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   853
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   854
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   855
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   856
    public boolean enterSwitchNode(final SwitchNode switchNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   857
        if(!reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   858
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   859
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   860
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   861
        visitExpressionOnEmptyStack(switchNode.getExpression());
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   862
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   863
        final List<CaseNode> cases = switchNode.getCases();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   864
        if(cases.isEmpty()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   865
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   866
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   867
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   868
        // Control flow is different for all-integer cases where we dispatch by switch table, and for all other cases
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   869
        // where we do sequential comparison. Note that CaseNode objects act as join points.
27970
7b0048b90967 8066225: NPE in MethodEmitter with duplicate integer switch cases
attila
parents: 27969
diff changeset
   870
        final boolean isInteger = switchNode.isUniqueInteger();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   871
        final Label breakLabel = switchNode.getBreakLabel();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   872
        final boolean hasDefault = switchNode.getDefaultCase() != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   873
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   874
        boolean tagUsed = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   875
        for(final CaseNode caseNode: cases) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   876
            final Expression test = caseNode.getTest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   877
            if(!isInteger && test != null) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   878
                visitExpressionOnEmptyStack(test);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   879
                if(!tagUsed) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   880
                    symbolIsUsed(switchNode.getTag(), LvarType.OBJECT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   881
                    tagUsed = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   882
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   883
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   884
            // CaseNode carries the conversions that need to be performed on its entry from the test.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   885
            // CodeGenerator ensures these are only emitted when arriving on the branch and not through a
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   886
            // fallthrough.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   887
            jumpToLabel(caseNode, caseNode.getBody().getEntryLabel());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   888
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   889
        if(!hasDefault) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   890
            // No default case means we can arrive at the break label without entering any cases. In that case
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   891
            // SwitchNode will carry the conversions that need to be performed before it does that jump.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   892
            jumpToLabel(switchNode, breakLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   893
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   894
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   895
        // All cases are arrived at through jumps
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   896
        doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   897
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   898
        Block previousBlock = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   899
        for(final CaseNode caseNode: cases) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   900
            final Block body = caseNode.getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   901
            final Label entryLabel = body.getEntryLabel();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   902
            if(previousBlock != null && reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   903
                jumpToLabel(previousBlock, entryLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   904
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   905
            joinOnLabel(entryLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   906
            assert reachable == true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   907
            body.accept(this);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   908
            previousBlock = body;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   909
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   910
        if(previousBlock != null && reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   911
            jumpToLabel(previousBlock, breakLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   912
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   913
        leaveBreakable(switchNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   914
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   915
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   916
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   917
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   918
    public boolean enterTernaryNode(final TernaryNode ternaryNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   919
        final Expression test = ternaryNode.getTest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   920
        final Expression trueExpr = ternaryNode.getTrueExpression();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   921
        final Expression falseExpr = ternaryNode.getFalseExpression();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   922
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   923
        visitExpression(test);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   924
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   925
        final Map<Symbol, LvarType> testExitLvarTypes = localVariableTypes;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   926
        final LvarType trueType;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   927
        if(!isAlwaysFalse(test)) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   928
            trueType = visitExpression(trueExpr);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   929
        } else {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   930
            trueType = null;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   931
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   932
        final Map<Symbol, LvarType> trueExitLvarTypes = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   933
        localVariableTypes = testExitLvarTypes;
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   934
        final LvarType falseType;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   935
        if(!isAlwaysTrue(test)) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   936
            falseType = visitExpression(falseExpr);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   937
        } else {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   938
            falseType = null;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   939
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   940
        final Map<Symbol, LvarType> falseExitLvarTypes = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   941
        localVariableTypes = getUnionTypes(trueExitLvarTypes, falseExitLvarTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   942
        setConversion((JoinPredecessor)trueExpr, trueExitLvarTypes, localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   943
        setConversion((JoinPredecessor)falseExpr, falseExitLvarTypes, localVariableTypes);
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   944
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   945
        typeStack.push(trueType != null ? falseType != null ? widestLvarType(trueType, falseType) : trueType : assertNotNull(falseType));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   946
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   947
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   948
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   949
    private static <T> T assertNotNull(final T t) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   950
        assert t != null;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   951
        return t;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   952
    }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   953
26507
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   954
    private void enterTestFirstLoop(final LoopNode loopNode, final JoinPredecessorExpression modify,
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   955
            final Expression iteratorValues, final boolean iteratorValuesAreObject) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   956
        final JoinPredecessorExpression test = loopNode.getTest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   957
        if(isAlwaysFalse(test)) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   958
            visitExpressionOnEmptyStack(test);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   959
            return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   960
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   961
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   962
        final Label continueLabel = loopNode.getContinueLabel();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   963
        final Label breakLabel = loopNode.getBreakLabel();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   964
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   965
        final Label repeatLabel = modify == null ? continueLabel : new Label("");
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   966
        final Map<Symbol, LvarType> beforeLoopTypes = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   967
        for(;;) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   968
            jumpToLabel(loopNode, repeatLabel, beforeLoopTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   969
            final Map<Symbol, LvarType> beforeRepeatTypes = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   970
            if(test != null) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   971
                visitExpressionOnEmptyStack(test);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   972
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   973
            if(!isAlwaysTrue(test)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   974
                jumpToLabel(test, breakLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   975
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   976
            if(iteratorValues instanceof IdentNode) {
26507
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   977
                final IdentNode ident = (IdentNode)iteratorValues;
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   978
                // Receives iterator values; the optimistic type of the iterator values is tracked on the
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   979
                // identifier, but we override optimism if it's known that the object being iterated over will
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   980
                // never have primitive property names.
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   981
                onAssignment(ident, iteratorValuesAreObject ? LvarType.OBJECT :
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
   982
                    toLvarType(compiler.getOptimisticType(ident)));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   983
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   984
            final Block body = loopNode.getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   985
            body.accept(this);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   986
            if(reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   987
                jumpToLabel(body, continueLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   988
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   989
            joinOnLabel(continueLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   990
            if(!reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   991
                break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   992
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   993
            if(modify != null) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
   994
                visitExpressionOnEmptyStack(modify);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   995
                jumpToLabel(modify, repeatLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   996
                joinOnLabel(repeatLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   997
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   998
            if(localVariableTypes.equals(beforeRepeatTypes)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   999
                break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1000
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1001
            // Reset the join points and repeat the analysis
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1002
            resetJoinPoint(continueLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1003
            resetJoinPoint(breakLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1004
            resetJoinPoint(repeatLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1005
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1006
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1007
        if(isAlwaysTrue(test) && iteratorValues == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1008
            doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1009
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1010
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1011
        leaveBreakable(loopNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1012
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1013
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1014
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1015
    public boolean enterThrowNode(final ThrowNode throwNode) {
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1016
        if(!reachable) {
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1017
            return false;
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1018
        }
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1019
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1020
        visitExpressionOnEmptyStack(throwNode.getExpression());
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1021
        jumpToCatchBlock(throwNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1022
        doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1023
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1024
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1025
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1026
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1027
    public boolean enterTryNode(final TryNode tryNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1028
        if(!reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1029
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1030
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1031
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1032
        // This is the label for the join point at the entry of the catch blocks.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1033
        final Label catchLabel = new Label("");
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1034
        catchLabels.push(catchLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1035
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1036
        // Presume that even the start of the try block can immediately go to the catch
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1037
        jumpToLabel(tryNode, catchLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1038
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1039
        final Block body = tryNode.getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1040
        body.accept(this);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1041
        catchLabels.pop();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1042
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1043
        // Final exit label for the whole try/catch construct (after the try block and after all catches).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1044
        final Label endLabel = new Label("");
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1045
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1046
        boolean canExit = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1047
        if(reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1048
            jumpToLabel(body, endLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1049
            canExit = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1050
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1051
        doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1052
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1053
        for (final Block inlinedFinally : tryNode.getInlinedFinallies()) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1054
            final Block finallyBody = TryNode.getLabelledInlinedFinallyBlock(inlinedFinally);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1055
            joinOnLabel(finallyBody.getEntryLabel());
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1056
            // NOTE: the jump to inlined finally can end up in dead code, so it is not necessarily reachable.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1057
            if (reachable) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1058
                finallyBody.accept(this);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1059
                // All inlined finallies end with a jump or a return
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1060
                assert !reachable;
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1061
            }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1062
        }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1063
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1064
        joinOnLabel(catchLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1065
        for(final CatchNode catchNode: tryNode.getCatches()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1066
            final IdentNode exception = catchNode.getException();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1067
            onAssignment(exception, LvarType.OBJECT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1068
            final Expression condition = catchNode.getExceptionCondition();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1069
            if(condition != null) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1070
                visitExpression(condition);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1071
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1072
            final Map<Symbol, LvarType> afterConditionTypes = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1073
            final Block catchBody = catchNode.getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1074
            // TODO: currently, we consider that the catch blocks are always reachable from the try block as currently
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1075
            // we lack enough analysis to prove that no statement before a break/continue/return in the try block can
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1076
            // throw an exception.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1077
            reachable = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1078
            catchBody.accept(this);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1079
            final Symbol exceptionSymbol = exception.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1080
            if(reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1081
                localVariableTypes = cloneMap(localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1082
                localVariableTypes.remove(exceptionSymbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1083
                jumpToLabel(catchBody, endLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1084
                canExit = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1085
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1086
            localVariableTypes = cloneMap(afterConditionTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1087
            localVariableTypes.remove(exceptionSymbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1088
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1089
        // NOTE: if we had one or more conditional catch blocks with no unconditional catch block following them, then
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1090
        // there will be an unconditional rethrow, so the join point can never be reached from the last
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1091
        // conditionExpression.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1092
        doesNotContinueSequentially();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1093
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1094
        if(canExit) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1095
            joinOnLabel(endLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1096
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1097
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1098
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1099
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1100
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1101
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1102
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1103
    public boolean enterUnaryNode(final UnaryNode unaryNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1104
        final Expression expr = unaryNode.getExpression();
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1105
        final LvarType unaryType = toLvarType(unaryNode.setExpression(visitExpression(expr).typeExpression).getType());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1106
        if(unaryNode.isSelfModifying() && expr instanceof IdentNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1107
            onSelfAssignment((IdentNode)expr, unaryType);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1108
        }
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1109
        typeStack.push(unaryType);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1110
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1111
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1112
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1113
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1114
    public boolean enterVarNode(final VarNode varNode) {
26766
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
  1115
        if (!reachable) {
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
  1116
            return false;
523242aab20f 8058561: NPE in LocalVariableTypesCalculator
attila
parents: 26507
diff changeset
  1117
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1118
        final Expression init = varNode.getInit();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1119
        if(init != null) {
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1120
            onAssignment(varNode.getName(), visitExpression(init));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1121
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1122
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1123
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1124
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1125
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1126
    public boolean enterWhileNode(final WhileNode whileNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1127
        if(!reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1128
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1129
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1130
        if(whileNode.isDoWhile()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1131
            enterDoWhileLoop(whileNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1132
        } else {
26507
9d6e3ec59878 8034954: Optimistic iteration in for-in and for-each
attila
parents: 25865
diff changeset
  1133
            enterTestFirstLoop(whileNode, null, null, false);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1134
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1135
        return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1136
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1137
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1138
    @Override
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1139
    public boolean enterWithNode(final WithNode withNode) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1140
        if (reachable) {
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1141
            visitExpression(withNode.getExpression());
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1142
            withNode.getBody().accept(this);
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1143
        }
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1144
        return false;
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1145
    };
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1146
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1147
    private Map<Symbol, LvarType> getBreakTargetTypes(final LexicalContextNode target) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1148
        // Remove symbols defined in the the blocks that are being broken out of.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1149
        Map<Symbol, LvarType> types = localVariableTypes;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1150
        for(final Iterator<LexicalContextNode> it = lc.getAllNodes(); it.hasNext();) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1151
            final LexicalContextNode node = it.next();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1152
            if(node instanceof Block) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1153
                for(final Symbol symbol: ((Block)node).getSymbols()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1154
                    if(localVariableTypes.containsKey(symbol)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1155
                        if(types == localVariableTypes) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1156
                            types = cloneMap(localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1157
                        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1158
                        types.remove(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1159
                    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1160
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1161
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1162
            if(node == target) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1163
                break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1164
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1165
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1166
        return types;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1167
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1168
27969
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1169
    /**
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1170
     * Returns the current type of the local variable represented by the symbol. This is the most strict of all
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1171
     * {@code getLocalVariableType*} methods, as it will throw an assertion if the type is null. Therefore, it is only
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1172
     * safe to be invoked on symbols known to be bytecode locals, and only after they have been initialized.
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1173
     * Regardless, it is recommended to use this method in majority of cases, as because of its strictness it is the
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1174
     * best suited for catching missing type calculation bugs early.
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1175
     * @param symbol a symbol representing a bytecode local variable.
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1176
     * @return the current type of the local variable represented by the symbol
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1177
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1178
    private LvarType getLocalVariableType(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1179
        final LvarType type = getLocalVariableTypeOrNull(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1180
        assert type != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1181
        return type;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1182
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1183
27969
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1184
    /**
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1185
     * Gets the type for a variable represented by a symbol, or null if the type is not know. This is the least strict
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1186
     * of all local variable type getters, and as such its use is discouraged except in initialization scenarios (where
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1187
     * a just-defined symbol might still be null).
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1188
     * @param symbol the symbol
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1189
     * @return the current type for the symbol, or null if the type is not known either because the symbol has not been
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1190
     * initialized, or because the symbol does not represent a bytecode local variable.
c03b64ccbd0f 8066227: CodeGenerator load unitialized slot
attila
parents: 27361
diff changeset
  1191
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1192
    private LvarType getLocalVariableTypeOrNull(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1193
        return localVariableTypes.get(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1194
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1195
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1196
    private JumpTarget getOrCreateJumpTarget(final Label label) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1197
        JumpTarget jumpTarget = jumpTargets.get(label);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1198
        if(jumpTarget == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1199
            jumpTarget = createJumpTarget(label);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1200
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1201
        return jumpTarget;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1202
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1203
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1204
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1205
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1206
     * If there's a join point associated with a label, insert the join point into the flow.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1207
     * @param label the label to insert a join point for.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1208
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1209
    private void joinOnLabel(final Label label) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1210
        final JumpTarget jumpTarget = jumpTargets.remove(label);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1211
        if(jumpTarget == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1212
            return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1213
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1214
        assert !jumpTarget.origins.isEmpty();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1215
        reachable = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1216
        localVariableTypes = getUnionTypes(jumpTarget.types, localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1217
        for(final JumpOrigin jumpOrigin: jumpTarget.origins) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1218
            setConversion(jumpOrigin.node, jumpOrigin.types, localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1219
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1220
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1221
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1222
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1223
     * If we're in a try/catch block, add an edge from the specified node to the try node's pre-catch label.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1224
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1225
    private void jumpToCatchBlock(final JoinPredecessor jumpOrigin) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1226
        final Label currentCatchLabel = catchLabels.peek();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1227
        if(currentCatchLabel != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1228
            jumpToLabel(jumpOrigin, currentCatchLabel);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1229
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1230
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1231
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1232
    private void jumpToLabel(final JoinPredecessor jumpOrigin, final Label label) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1233
        jumpToLabel(jumpOrigin, label, localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1234
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1235
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1236
    private void jumpToLabel(final JoinPredecessor jumpOrigin, final Label label, final Map<Symbol, LvarType> types) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1237
        getOrCreateJumpTarget(label).addOrigin(jumpOrigin, types);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1238
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1239
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1240
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1241
    public Node leaveBlock(final Block block) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1242
        if(lc.isFunctionBody()) {
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1243
            if(reachable) {
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1244
                // reachable==true means we can reach the end of the function without an explicit return statement. We
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1245
                // need to insert a synthetic one then. This logic used to be in Lower.leaveBlock(), but Lower's
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1246
                // reachability analysis (through Terminal.isTerminal() flags) is not precise enough so
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1247
                // Lower$BlockLexicalContext.afterSetStatements will sometimes think the control flow terminates even
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1248
                // when it didn't. Example: function() { switch((z)) { default: {break; } throw x; } }.
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1249
                createSyntheticReturn(block);
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1250
                assert !reachable;
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1251
            }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1252
            // We must calculate the return type here (and not in leaveFunctionNode) as it can affect the liveness of
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1253
            // the :return symbol and thus affect conversion type liveness calculations for it.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1254
            calculateReturnType();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1255
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1256
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1257
        boolean cloned = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1258
        for(final Symbol symbol: block.getSymbols()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1259
            // Undefine the symbol outside the block
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1260
            if(localVariableTypes.containsKey(symbol)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1261
                if(!cloned) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1262
                    localVariableTypes = cloneMap(localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1263
                    cloned = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1264
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1265
                localVariableTypes.remove(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1266
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1267
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1268
            if(symbol.hasSlot()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1269
                final SymbolConversions conversions = symbolConversions.get(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1270
                if(conversions != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1271
                    // Potentially make some currently dead types live if they're needed as a source of a type
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1272
                    // conversion at a join.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1273
                    conversions.calculateTypeLiveness(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1274
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1275
                if(symbol.slotCount() == 0) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1276
                    // This is a local variable that is never read. It won't need a slot.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1277
                    symbol.setNeedsSlot(false);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1278
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1279
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1280
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1281
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1282
        if(reachable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1283
            // TODO: this is totally backwards. Block should not be breakable, LabelNode should be breakable.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1284
            final LabelNode labelNode = lc.getCurrentBlockLabelNode();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1285
            if(labelNode != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1286
                jumpToLabel(labelNode, block.getBreakLabel());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1287
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1288
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1289
        leaveBreakable(block);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1290
        return block;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1291
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1292
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1293
    private void calculateReturnType() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1294
        // NOTE: if return type is unknown, then the function does not explicitly return a value. Such a function under
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1295
        // ECMAScript rules returns Undefined, which has Type.OBJECT. We might consider an optimization in the future
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1296
        // where we can return void functions.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1297
        if(returnType.isUnknown()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1298
            returnType = Type.OBJECT;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1299
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1300
    }
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1301
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1302
    private void createSyntheticReturn(final Block body) {
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1303
        final FunctionNode functionNode = lc.getCurrentFunction();
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1304
        final long token = functionNode.getToken();
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1305
        final int finish = functionNode.getFinish();
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1306
        final List<Statement> statements = body.getStatements();
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1307
        final int lineNumber = statements.isEmpty() ? functionNode.getLineNumber() : statements.get(statements.size() - 1).getLineNumber();
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1308
        final IdentNode returnExpr;
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1309
        if(functionNode.isProgram()) {
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1310
            returnExpr = new IdentNode(token, finish, RETURN.symbolName()).setSymbol(getCompilerConstantSymbol(functionNode, RETURN));
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1311
        } else {
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1312
            returnExpr = null;
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1313
        }
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1314
        syntheticReturn = new ReturnNode(lineNumber, token, finish, returnExpr);
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1315
        syntheticReturn.accept(this);
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1316
    }
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1317
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1318
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1319
     * Leave a breakable node. If there's a join point associated with its break label (meaning there was at least one
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1320
     * break statement to the end of the node), insert the join point into the flow.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1321
     * @param breakable the breakable node being left.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1322
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1323
    private void leaveBreakable(final BreakableNode breakable) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1324
        joinOnLabel(breakable.getBreakLabel());
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1325
        assertTypeStackIsEmpty();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1326
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1327
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1328
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1329
    public Node leaveFunctionNode(final FunctionNode functionNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1330
        // Sets the return type of the function and also performs the bottom-up pass of applying type and conversion
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1331
        // information to nodes as well as doing the calculation on nested functions as required.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1332
        FunctionNode newFunction = functionNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1333
        final NodeVisitor<LexicalContext> applyChangesVisitor = new NodeVisitor<LexicalContext>(new LexicalContext()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1334
            private boolean inOuterFunction = true;
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
  1335
            private final Deque<JoinPredecessor> joinPredecessors = new ArrayDeque<>();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1336
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1337
            @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
  1338
            protected boolean enterDefault(final Node node) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1339
                if(!inOuterFunction) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1340
                    return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1341
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1342
                if(node instanceof JoinPredecessor) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1343
                    joinPredecessors.push((JoinPredecessor)node);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1344
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1345
                return inOuterFunction;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1346
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1347
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1348
            @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1349
            public boolean enterFunctionNode(final FunctionNode fn) {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
  1350
                if(compiler.isOnDemandCompilation()) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1351
                    // Only calculate nested function local variable types if we're doing eager compilation
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1352
                    return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1353
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1354
                inOuterFunction = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1355
                return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1356
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1357
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1358
            @SuppressWarnings("fallthrough")
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1359
            @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
  1360
            public Node leaveBinaryNode(final BinaryNode binaryNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1361
                if(binaryNode.isComparison()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1362
                    final Expression lhs = binaryNode.lhs();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1363
                    final Expression rhs = binaryNode.rhs();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1364
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1365
                    final TokenType tt = binaryNode.tokenType();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1366
                    switch (tt) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1367
                    case EQ_STRICT:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1368
                    case NE_STRICT:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1369
                        // Specialize comparison with undefined
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1370
                        final Expression undefinedNode = createIsUndefined(binaryNode, lhs, rhs,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1371
                                tt == TokenType.EQ_STRICT ? Request.IS_UNDEFINED : Request.IS_NOT_UNDEFINED);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1372
                        if(undefinedNode != binaryNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1373
                            return undefinedNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1374
                        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1375
                        // Specialize comparison of boolean with non-boolean
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1376
                        if (lhs.getType().isBoolean() != rhs.getType().isBoolean()) {
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 28690
diff changeset
  1377
                            return new RuntimeNode(binaryNode);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1378
                        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1379
                        // fallthrough
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1380
                    default:
29283
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 28690
diff changeset
  1381
                        if (lhs.getType().isObject() && rhs.getType().isObject()) {
fb47e4d25a9f 8035712: Restore some of the RuntimeCallSite specializations
attila
parents: 28690
diff changeset
  1382
                            return new RuntimeNode(binaryNode);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1383
                        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1384
                    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1385
                } else if(binaryNode.isOptimisticUndecidedType()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1386
                    // At this point, we can assign a static type to the optimistic binary ADD operator as now we know
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1387
                    // the types of its operands.
25829
1a5e1de71e57 8051439: Wrong type calculated for ADD operator with undefined operand
attila
parents: 25249
diff changeset
  1388
                    return binaryNode.decideType();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1389
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1390
                return binaryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1391
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1392
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1393
            @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1394
            protected Node leaveDefault(final Node node) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1395
                if(node instanceof JoinPredecessor) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1396
                    final JoinPredecessor original = joinPredecessors.pop();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1397
                    assert original.getClass() == node.getClass() : original.getClass().getName() + "!=" + node.getClass().getName();
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1398
                    final JoinPredecessor newNode = setLocalVariableConversion(original, (JoinPredecessor)node);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1399
                    if (newNode instanceof LexicalContextNode) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1400
                        lc.replace((LexicalContextNode)node, (LexicalContextNode)newNode);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1401
                    }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 28130
diff changeset
  1402
                    return (Node)newNode;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1403
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1404
                return node;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1405
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1406
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1407
            @Override
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1408
            public Node leaveBlock(final Block block) {
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1409
                if(inOuterFunction && syntheticReturn != null && lc.isFunctionBody()) {
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1410
                    final ArrayList<Statement> stmts = new ArrayList<>(block.getStatements());
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1411
                    stmts.add((ReturnNode)syntheticReturn.accept(this));
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1412
                    return block.setStatements(lc, stmts);
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1413
                }
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1414
                return super.leaveBlock(block);
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1415
            }
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1416
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25240
diff changeset
  1417
            @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
  1418
            public Node leaveFunctionNode(final FunctionNode nestedFunctionNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1419
                inOuterFunction = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1420
                final FunctionNode newNestedFunction = (FunctionNode)nestedFunctionNode.accept(
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
  1421
                        new LocalVariableTypesCalculator(compiler));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1422
                lc.replace(nestedFunctionNode, newNestedFunction);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1423
                return newNestedFunction;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1424
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1425
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1426
            @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
  1427
            public Node leaveIdentNode(final IdentNode identNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1428
                final IdentNode original = (IdentNode)joinPredecessors.pop();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1429
                final Symbol symbol = identNode.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1430
                if(symbol == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1431
                    assert identNode.isPropertyName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1432
                    return identNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1433
                } else if(symbol.hasSlot()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1434
                    assert !symbol.isScope() || symbol.isParam(); // Only params can be slotted and scoped.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1435
                    assert original.getName().equals(identNode.getName());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1436
                    final LvarType lvarType = identifierLvarTypes.remove(original);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1437
                    if(lvarType != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1438
                        return setLocalVariableConversion(original, identNode.setType(lvarType.type));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1439
                    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1440
                    // If there's no type, then the identifier must've been in unreachable code. In that case, it can't
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1441
                    // have assigned conversions either.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1442
                    assert localVariableConversions.get(original) == null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1443
                } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1444
                    assert identIsDeadAndHasNoLiveConversions(original);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1445
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1446
                return identNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1447
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1448
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1449
            @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
  1450
            public Node leaveLiteralNode(final LiteralNode<?> literalNode) {
25234
e2f9df6b8797 8047078: Fuzzing bug discovered when ArrayLiteralNodes weren't immutable
lagergren
parents: 24759
diff changeset
  1451
                //for e.g. ArrayLiteralNodes the initial types may have been narrowed due to the
e2f9df6b8797 8047078: Fuzzing bug discovered when ArrayLiteralNodes weren't immutable
lagergren
parents: 24759
diff changeset
  1452
                //introduction of optimistic behavior - hence ensure that all literal nodes are
e2f9df6b8797 8047078: Fuzzing bug discovered when ArrayLiteralNodes weren't immutable
lagergren
parents: 24759
diff changeset
  1453
                //reinitialized
e2f9df6b8797 8047078: Fuzzing bug discovered when ArrayLiteralNodes weren't immutable
lagergren
parents: 24759
diff changeset
  1454
                return literalNode.initialize(lc);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1455
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1456
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1457
            @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1458
            public Node leaveRuntimeNode(final RuntimeNode runtimeNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1459
                final Request request = runtimeNode.getRequest();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1460
                final boolean isEqStrict = request == Request.EQ_STRICT;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1461
                if(isEqStrict || request == Request.NE_STRICT) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1462
                    return createIsUndefined(runtimeNode, runtimeNode.getArgs().get(0), runtimeNode.getArgs().get(1),
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1463
                            isEqStrict ? Request.IS_UNDEFINED : Request.IS_NOT_UNDEFINED);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1464
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1465
                return runtimeNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1466
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1467
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1468
            @SuppressWarnings("unchecked")
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1469
            private <T extends JoinPredecessor> T setLocalVariableConversion(final JoinPredecessor original, final T jp) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1470
                // NOTE: can't use Map.remove() as our copy-on-write AST semantics means some nodes appear twice (in
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1471
                // finally blocks), so we need to be able to access conversions for them multiple times.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1472
                return (T)jp.setLocalVariableConversion(lc, localVariableConversions.get(original));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1473
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1474
        };
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1475
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1476
        newFunction = newFunction.setBody(lc, (Block)newFunction.getBody().accept(applyChangesVisitor));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1477
        newFunction = newFunction.setReturnType(lc, returnType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1478
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1479
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1480
        newFunction = newFunction.setParameters(lc, newFunction.visitParameters(applyChangesVisitor));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1481
        return newFunction;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1482
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1483
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1484
    private static Expression createIsUndefined(final Expression parent, final Expression lhs, final Expression rhs, final Request request) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1485
        if (isUndefinedIdent(lhs) || isUndefinedIdent(rhs)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1486
            return new RuntimeNode(parent, request, lhs, rhs);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1487
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1488
        return parent;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1489
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1490
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1491
    private static boolean isUndefinedIdent(final Expression expr) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1492
        return expr instanceof IdentNode && "undefined".equals(((IdentNode)expr).getName());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1493
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1494
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1495
    private boolean identIsDeadAndHasNoLiveConversions(final IdentNode identNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1496
        final LocalVariableConversion conv = localVariableConversions.get(identNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1497
        return conv == null || !conv.isLive();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1498
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1499
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1500
    private void onAssignment(final IdentNode identNode, final LvarType type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1501
        final Symbol symbol = identNode.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1502
        assert symbol != null : identNode.getName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1503
        if(!symbol.isBytecodeLocal()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1504
            return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1505
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1506
        assert type != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1507
        final LvarType finalType;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1508
        if(type == LvarType.UNDEFINED && getLocalVariableType(symbol) != LvarType.UNDEFINED) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1509
            // Explicit assignment of a known undefined local variable to a local variable that is not undefined will
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1510
            // materialize that undefined in the assignment target. Note that assigning known undefined to known
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1511
            // undefined will *not* initialize the variable, e.g. "var x; var y = x;" compiles to no-op.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1512
            finalType = LvarType.OBJECT;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1513
            symbol.setFlag(Symbol.HAS_OBJECT_VALUE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1514
        } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1515
            finalType = type;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1516
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1517
        setType(symbol, finalType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1518
        // Explicit assignment of an undefined value. Make sure the variable can store an object
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1519
        // TODO: if we communicated the fact to codegen with a flag on the IdentNode that the value was already
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1520
        // undefined before the assignment, we could just ignore it. In general, we could ignore an assignment if we
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1521
        // know that the value assigned is the same as the current value of the variable, but we'd need constant
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1522
        // propagation for that.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1523
        setIdentifierLvarType(identNode, finalType);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1524
        // For purposes of type calculation, we consider an assignment to a local variable to be followed by
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1525
        // the catch nodes of the current (if any) try block. This will effectively enforce that narrower
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1526
        // assignments to a local variable in a try block will also have to store a widened value as well. Code
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1527
        // within the try block will be able to keep loading the narrower value, but after the try block only
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1528
        // the widest value will remain live.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1529
        // Rationale for this is that if there's an use for that variable in any of the catch blocks, or
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1530
        // following the catch blocks, they must use the widest type.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1531
        // Example:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1532
        /*
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1533
            Originally:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1534
            ===========
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1535
            var x;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1536
            try {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1537
              x = 1; <-- stores into int slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1538
              f(x); <-- loads the int slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1539
              x = 3.14 <-- stores into the double slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1540
              f(x); <-- loads the double slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1541
              x = 1; <-- stores into int slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1542
              f(x); <-- loads the int slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1543
            } finally {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1544
              f(x); <-- loads the double slot for x, but can be reached by a path where x is int, so we need
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1545
                           to go back and ensure that double values are also always stored along with int
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1546
                           values.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1547
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1548
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1549
            After correction:
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1550
            =================
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1551
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1552
            var x;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1553
            try {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1554
              x = 1; <-- stores into both int and double slots for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1555
              f(x); <-- loads the int slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1556
              x = 3.14 <-- stores into the double slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1557
              f(x); <-- loads the double slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1558
              x = 1; <-- stores into both int and double slots for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1559
              f(x); <-- loads the int slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1560
            } finally {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1561
              f(x); <-- loads the double slot for x
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1562
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1563
         */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1564
        jumpToCatchBlock(identNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1565
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1566
28130
433d6755c5f8 8067774: Use a stack of types when calculating local variable types
attila
parents: 27972
diff changeset
  1567
    private void onSelfAssignment(final IdentNode identNode, final LvarType type) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1568
        final Symbol symbol = identNode.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1569
        assert symbol != null : identNode.getName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1570
        if(!symbol.isBytecodeLocal()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1571
            return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1572
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1573
        // Self-assignment never produce either a boolean or undefined
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1574
        assert type != null && type != LvarType.UNDEFINED && type != LvarType.BOOLEAN;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1575
        setType(symbol, type);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1576
        jumpToCatchBlock(identNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1577
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1578
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1579
    private void resetJoinPoint(final Label label) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1580
        jumpTargets.remove(label);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1581
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1582
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1583
    private void setCompilerConstantAsObject(final FunctionNode functionNode, final CompilerConstants cc) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1584
        final Symbol symbol = getCompilerConstantSymbol(functionNode, cc);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1585
        setType(symbol, LvarType.OBJECT);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1586
        // never mark compiler constants as dead
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1587
        symbolIsUsed(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1588
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1589
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1590
    private static Symbol getCompilerConstantSymbol(final FunctionNode functionNode, final CompilerConstants cc) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1591
        return functionNode.getBody().getExistingSymbol(cc.symbolName());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1592
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1593
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1594
    private void setConversion(final JoinPredecessor node, final Map<Symbol, LvarType> branchLvarTypes, final Map<Symbol, LvarType> joinLvarTypes) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1595
        if(node == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1596
            return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1597
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1598
        if(branchLvarTypes.isEmpty() || joinLvarTypes.isEmpty()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1599
            localVariableConversions.remove(node);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1600
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1601
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1602
        LocalVariableConversion conversion = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1603
        if(node instanceof IdentNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1604
            // conversions on variable assignment in try block are special cases, as they only apply to the variable
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1605
            // being assigned and all other conversions should be ignored.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1606
            final Symbol symbol = ((IdentNode)node).getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1607
            conversion = createConversion(symbol, branchLvarTypes.get(symbol), joinLvarTypes, null);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1608
        } else {
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
  1609
            for(final Map.Entry<Symbol, LvarType> entry: branchLvarTypes.entrySet()) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1610
                final Symbol symbol = entry.getKey();
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
  1611
                final LvarType branchLvarType = entry.getValue();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1612
                conversion = createConversion(symbol, branchLvarType, joinLvarTypes, conversion);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1613
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1614
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1615
        if(conversion != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1616
            localVariableConversions.put(node, conversion);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1617
        } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1618
            localVariableConversions.remove(node);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1619
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1620
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1621
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1622
    private void setIdentifierLvarType(final IdentNode identNode, final LvarType type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1623
        assert type != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1624
        identifierLvarTypes.put(identNode, type);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1625
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1626
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1627
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1628
     * Marks a local variable as having a specific type from this point onward. Invoked by stores to local variables.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1629
     * @param symbol the symbol representing the variable
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1630
     * @param type the type
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1631
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1632
    private void setType(final Symbol symbol, final LvarType type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1633
        if(getLocalVariableTypeOrNull(symbol) == type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1634
            return;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1635
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1636
        assert symbol.hasSlot();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1637
        assert !symbol.isGlobal();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1638
        localVariableTypes = localVariableTypes.isEmpty() ? new IdentityHashMap<Symbol, LvarType>() : cloneMap(localVariableTypes);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1639
        localVariableTypes.put(symbol, type);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1640
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1641
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1642
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1643
     * Set a flag in the symbol marking it as needing to be able to store a value of a particular type. Every symbol for
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1644
     * a local variable will be assigned between 1 and 6 local variable slots for storing all types it is known to need
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1645
     * to store.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1646
     * @param symbol the symbol
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1647
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1648
    private void symbolIsUsed(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1649
        symbolIsUsed(symbol, getLocalVariableType(symbol));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1650
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1651
}