nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/OptimisticLexicalContext.java
author attila
Wed, 28 Jan 2015 17:58:08 +0100
changeset 28690 78317797ab62
parent 25865 d38d876f1654
permissions -rw-r--r--
8067139: Finally blocks inlined incorrectly Reviewed-by: hannesw, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     1
/*
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     4
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    10
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    15
 * accompanied this code).
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    16
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    20
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    23
 * questions.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    24
 */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    25
package jdk.nashorn.internal.ir;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    26
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    27
import java.util.ArrayDeque;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    28
import java.util.ArrayList;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    29
import java.util.Collections;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    30
import java.util.Deque;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    31
import java.util.List;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    32
import jdk.nashorn.internal.codegen.types.Type;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    33
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    34
/**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    35
 * Lexical context that keeps track of optimistic assumptions (if any)
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    36
 * made during code generation. Used from Attr and FinalizeTypes
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    37
 */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    38
public class OptimisticLexicalContext extends LexicalContext {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    39
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    40
    private final boolean isEnabled;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    41
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    42
    class Assumption {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    43
        Symbol symbol;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    44
        Type   type;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    45
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    46
        Assumption(final Symbol symbol, final Type type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    47
            this.symbol = symbol;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    48
            this.type   = type;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    49
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    50
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    51
        public String toString() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    52
            return symbol.getName() + "=" + type;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    53
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    54
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    55
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    56
    /** Optimistic assumptions that could be made per function */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    57
    private final Deque<List<Assumption>> optimisticAssumptions = new ArrayDeque<>();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    58
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    59
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    60
     * Constructor
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    61
     * @param isEnabled are optimistic types enabled?
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    62
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    63
    public OptimisticLexicalContext(final boolean isEnabled) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    64
        super();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    65
        this.isEnabled = isEnabled;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    66
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    67
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    68
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    69
     * Are optimistic types enabled
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    70
     * @return true if optimistic types
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    71
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    72
    public boolean isEnabled() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    73
        return isEnabled;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    74
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    75
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    76
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    77
     * Log an optimistic assumption during codegen
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    78
     * TODO : different parameters and more info about the assumption for future profiling
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    79
     * needs
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    80
     * @param symbol symbol
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    81
     * @param type   type
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    82
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    83
    public void logOptimisticAssumption(final Symbol symbol, final Type type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    84
        if (isEnabled) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    85
            final List<Assumption> peek = optimisticAssumptions.peek();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    86
            peek.add(new Assumption(symbol, type));
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    87
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    88
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    89
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    90
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    91
     * Get the list of optimistic assumptions made
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    92
     * @return optimistic assumptions
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    93
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    94
    public List<Assumption> getOptimisticAssumptions() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    95
        return Collections.unmodifiableList(optimisticAssumptions.peek());
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    96
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    97
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    98
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    99
     * Does this method have optimistic assumptions made during codegen?
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   100
     * @return true if optimistic assumptions were made
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   101
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   102
    public boolean hasOptimisticAssumptions() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   103
        return !optimisticAssumptions.isEmpty() && !getOptimisticAssumptions().isEmpty();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   104
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   105
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   106
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   107
    public <T extends LexicalContextNode> T push(final T node) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   108
        if (isEnabled) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   109
            if(node instanceof FunctionNode) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   110
                optimisticAssumptions.push(new ArrayList<Assumption>());
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   111
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   112
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   113
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   114
        return super.push(node);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   115
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   116
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   117
    @Override
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 25865
diff changeset
   118
    public <T extends Node> T pop(final T node) {
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
   119
        final T popped = super.pop(node);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   120
        if (isEnabled) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   121
            if(node instanceof FunctionNode) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   122
                optimisticAssumptions.pop();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   123
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   124
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   125
        return popped;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   126
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   127
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   128
}