src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CodeGeneratorLexicalContext.java
author hannesw
Tue, 12 Dec 2017 15:38:18 +0100
changeset 48247 fa5a47cad0c9
parent 47216 71c04702a3d5
child 48380 597f69e5f1e3
permissions -rw-r--r--
8069338: Implement sharedScopeCall for optimistic types Reviewed-by: attila, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     1
/*
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     4
 *
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    10
 *
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    15
 * accompanied this code).
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    16
 *
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    20
 *
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    23
 * questions.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    24
 */
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    25
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    26
package jdk.nashorn.internal.codegen;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    27
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    28
import java.util.ArrayDeque;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    29
import java.util.Collection;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    30
import java.util.Collections;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    31
import java.util.Deque;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    32
import java.util.HashMap;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    33
import java.util.Map;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    34
import jdk.nashorn.internal.IntDeque;
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    35
import jdk.nashorn.internal.codegen.types.Type;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    36
import jdk.nashorn.internal.ir.Block;
29410
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
    37
import jdk.nashorn.internal.ir.Expression;
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    38
import jdk.nashorn.internal.ir.FunctionNode;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    39
import jdk.nashorn.internal.ir.LexicalContext;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    40
import jdk.nashorn.internal.ir.LexicalContextNode;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    41
import jdk.nashorn.internal.ir.Node;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    42
import jdk.nashorn.internal.ir.Symbol;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    43
import jdk.nashorn.internal.ir.WithNode;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    44
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    45
/**
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    46
 * A lexical context that also tracks if we have any dynamic scopes in the context. Such scopes can have new
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    47
 * variables introduced into them at run time - a with block or a function directly containing an eval call.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    48
 * Furthermore, this class keeps track of current discard state, which the current method emitter being used is,
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    49
 * the current compile unit, and local variable indexes
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    50
 */
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    51
final class CodeGeneratorLexicalContext extends LexicalContext {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    52
    private int dynamicScopeCount;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    53
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    54
    /** Map of shared scope call sites */
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    55
    private final Map<SharedScopeCall, SharedScopeCall> scopeCalls = new HashMap<>();
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    56
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    57
    /** Compile unit stack - every time we start a sub method (e.g. a split) we push one */
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    58
    private final Deque<CompileUnit> compileUnits = new ArrayDeque<>();
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    59
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    60
    /** Method emitter stack - every time we start a sub method (e.g. a split) we push one */
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    61
    private final Deque<MethodEmitter> methodEmitters = new ArrayDeque<>();
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    62
29410
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
    63
    /** The discard stack - whenever we evaluate an expression that will be discarded, we push it on this stack. Various
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
    64
     * implementations of expression code emitter can choose to emit code that'll discard the expression themselves, or
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
    65
     * ignore it in which case CodeGenerator.loadAndDiscard() will explicitly emit a pop instruction. */
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
    66
    private final Deque<Expression> discard = new ArrayDeque<>();
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
    67
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    68
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    69
    private final Deque<Map<String, Collection<Label>>> unwarrantedOptimismHandlers = new ArrayDeque<>();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    70
    private final Deque<StringBuilder> slotTypesDescriptors = new ArrayDeque<>();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    71
    private final IntDeque splitNodes = new IntDeque();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    72
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    73
    /** A stack tracking the next free local variable slot in the blocks. There's one entry for every block
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    74
     *  currently on the lexical context stack. */
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    75
    private int[] nextFreeSlots = new int[16];
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    76
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    77
    /** size of next free slot vector */
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    78
    private int nextFreeSlotsSize;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    79
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27361
diff changeset
    80
    private boolean isWithBoundary(final Object node) {
24749
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    81
        return node instanceof Block && !isEmpty() && peek() instanceof WithNode;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    82
    }
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    83
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    84
    @Override
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    85
    public <T extends LexicalContextNode> T push(final T node) {
24749
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    86
        if (isWithBoundary(node)) {
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    87
            dynamicScopeCount++;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    88
        } else if (node instanceof FunctionNode) {
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    89
            if (((FunctionNode)node).inDynamicContext()) {
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    90
                dynamicScopeCount++;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
    91
            }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    92
            splitNodes.push(0);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    93
        }
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    94
        return super.push(node);
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    95
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
    96
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    97
    void enterSplitNode() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
    98
        splitNodes.getAndIncrement();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
    99
        pushFreeSlots(methodEmitters.peek().getUsedSlotsWithLiveTemporaries());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   100
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   101
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   102
    void exitSplitNode() {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   103
        final int count = splitNodes.decrementAndGet();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   104
        assert count >= 0;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   105
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   106
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   107
    @Override
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27361
diff changeset
   108
    public <T extends Node> T pop(final T node) {
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   109
        final T popped = super.pop(node);
24749
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   110
        if (isWithBoundary(node)) {
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   111
            dynamicScopeCount--;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   112
            assert dynamicScopeCount >= 0;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   113
        } else if (node instanceof FunctionNode) {
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   114
            if (((FunctionNode)node).inDynamicContext()) {
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   115
                dynamicScopeCount--;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   116
                assert dynamicScopeCount >= 0;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24719
diff changeset
   117
            }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   118
            assert splitNodes.peek() == 0;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   119
            splitNodes.pop();
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   120
        }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   121
        return popped;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   122
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   123
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   124
    boolean inDynamicScope() {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   125
        return dynamicScopeCount > 0;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   126
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   127
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   128
    boolean inSplitNode() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   129
        return !splitNodes.isEmpty() && splitNodes.peek() > 0;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   130
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   131
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   132
    MethodEmitter pushMethodEmitter(final MethodEmitter newMethod) {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   133
        methodEmitters.push(newMethod);
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   134
        return newMethod;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   135
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   136
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   137
    MethodEmitter popMethodEmitter(final MethodEmitter oldMethod) {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   138
        assert methodEmitters.peek() == oldMethod;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   139
        methodEmitters.pop();
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   140
        return methodEmitters.isEmpty() ? null : methodEmitters.peek();
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   141
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   142
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   143
    void pushUnwarrantedOptimismHandlers() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   144
        unwarrantedOptimismHandlers.push(new HashMap<String, Collection<Label>>());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   145
        slotTypesDescriptors.push(new StringBuilder());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   146
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   147
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   148
    Map<String, Collection<Label>> getUnwarrantedOptimismHandlers() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   149
        return unwarrantedOptimismHandlers.peek();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   150
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   151
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   152
    Map<String, Collection<Label>> popUnwarrantedOptimismHandlers() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   153
        slotTypesDescriptors.pop();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   154
        return unwarrantedOptimismHandlers.pop();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   155
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   156
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   157
    CompileUnit pushCompileUnit(final CompileUnit newUnit) {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   158
        compileUnits.push(newUnit);
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   159
        return newUnit;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   160
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   161
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   162
    CompileUnit popCompileUnit(final CompileUnit oldUnit) {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   163
        assert compileUnits.peek() == oldUnit;
26506
7d55cc7fc301 8057703: More empty classes generated by Nashorn
lagergren
parents: 25865
diff changeset
   164
        final CompileUnit unit = compileUnits.pop();
7d55cc7fc301 8057703: More empty classes generated by Nashorn
lagergren
parents: 25865
diff changeset
   165
        assert unit.hasCode() : "compile unit popped without code";
7d55cc7fc301 8057703: More empty classes generated by Nashorn
lagergren
parents: 25865
diff changeset
   166
        unit.setUsed();
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   167
        return compileUnits.isEmpty() ? null : compileUnits.peek();
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   168
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   169
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   170
    boolean hasCompileUnits() {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   171
        return !compileUnits.isEmpty();
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   172
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   173
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   174
    Collection<SharedScopeCall> getScopeCalls() {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   175
        return Collections.unmodifiableCollection(scopeCalls.values());
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   176
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   177
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   178
    /**
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   179
     * Get a shared static method representing a dynamic scope callsite.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   180
     *
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   181
     * @param unit current compile unit
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   182
     * @param symbol the symbol
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   183
     * @param valueType the value type of the symbol
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   184
     * @param returnType the return type
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   185
     * @param paramTypes the parameter types
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   186
     * @param flags the callsite flags
48247
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   187
     * @param isOptimistic is this an optimistic call
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   188
     * @return an object representing a shared scope call
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   189
     */
48247
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   190
    SharedScopeCall getScopeCall(final CompileUnit unit, final Symbol symbol, final Type valueType,
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   191
                                 final Type returnType, final Type[] paramTypes, final int flags,
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   192
                                 final boolean isOptimistic) {
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   193
        final SharedScopeCall scopeCall = new SharedScopeCall(symbol, valueType, returnType, paramTypes, flags,
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   194
                isOptimistic);
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   195
        if (scopeCalls.containsKey(scopeCall)) {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   196
            return scopeCalls.get(scopeCall);
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   197
        }
22373
ca044992c73f 8030182: scopeCall with -1 as line number
sundar
parents: 17769
diff changeset
   198
        scopeCall.setClassAndName(unit, getCurrentFunction().uniqueName(":scopeCall"));
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   199
        scopeCalls.put(scopeCall, scopeCall);
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   200
        return scopeCall;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   201
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   202
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   203
    /**
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   204
     * Get a shared static method representing a dynamic scope get access.
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   205
     *
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   206
     * @param unit current compile unit
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   207
     * @param symbol the symbol
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   208
     * @param valueType the type of the variable
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   209
     * @param flags the callsite flags
48247
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   210
     * @param isOptimistic is this an optimistic get
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   211
     * @return an object representing a shared scope get
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   212
     */
48247
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   213
    SharedScopeCall getScopeGet(final CompileUnit unit, final Symbol symbol, final Type valueType, final int flags,
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   214
                                final boolean isOptimistic) {
fa5a47cad0c9 8069338: Implement sharedScopeCall for optimistic types
hannesw
parents: 47216
diff changeset
   215
        return getScopeCall(unit, symbol, valueType, valueType, null, flags, isOptimistic);
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   216
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   217
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   218
    void onEnterBlock(final Block block) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   219
        pushFreeSlots(assignSlots(block, isFunctionBody() ? 0 : getUsedSlotCount()));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   220
    }
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   221
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   222
    private void pushFreeSlots(final int freeSlots) {
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   223
        if (nextFreeSlotsSize == nextFreeSlots.length) {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   224
            final int[] newNextFreeSlots = new int[nextFreeSlotsSize * 2];
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   225
            System.arraycopy(nextFreeSlots, 0, newNextFreeSlots, 0, nextFreeSlotsSize);
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   226
            nextFreeSlots = newNextFreeSlots;
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   227
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   228
        nextFreeSlots[nextFreeSlotsSize++] = freeSlots;
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   229
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   230
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   231
    int getUsedSlotCount() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   232
        return nextFreeSlots[nextFreeSlotsSize - 1];
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   233
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   234
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   235
    void releaseSlots() {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   236
        --nextFreeSlotsSize;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   237
        final int undefinedFromSlot = nextFreeSlotsSize == 0 ? 0 : nextFreeSlots[nextFreeSlotsSize - 1];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   238
        if(!slotTypesDescriptors.isEmpty()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   239
            slotTypesDescriptors.peek().setLength(undefinedFromSlot);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   240
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   241
        methodEmitters.peek().undefineLocalVariables(undefinedFromSlot, false);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   242
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   243
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   244
    private int assignSlots(final Block block, final int firstSlot) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   245
        int fromSlot = firstSlot;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   246
        final MethodEmitter method = methodEmitters.peek();
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   247
        for (final Symbol symbol : block.getSymbols()) {
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   248
            if (symbol.hasSlot()) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   249
                symbol.setFirstSlot(fromSlot);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   250
                final int toSlot = fromSlot + symbol.slotCount();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   251
                method.defineBlockLocalVariable(fromSlot, toSlot);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   252
                fromSlot = toSlot;
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   253
            }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   254
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   255
        return fromSlot;
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   256
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   257
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   258
    static Type getTypeForSlotDescriptor(final char typeDesc) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   259
        // Recognizing both lowercase and uppercase as we're using both to signify symbol boundaries; see
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   260
        // MethodEmitter.markSymbolBoundariesInLvarTypesDescriptor().
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26506
diff changeset
   261
        switch (typeDesc) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   262
            case 'I':
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   263
            case 'i':
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   264
                return Type.INT;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   265
            case 'J':
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   266
            case 'j':
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   267
                return Type.LONG;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   268
            case 'D':
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   269
            case 'd':
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   270
                return Type.NUMBER;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   271
            case 'A':
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   272
            case 'a':
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   273
                return Type.OBJECT;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   274
            case 'U':
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   275
            case 'u':
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   276
                return Type.UNKNOWN;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   277
            default:
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   278
                throw new AssertionError();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   279
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   280
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22373
diff changeset
   281
29410
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   282
    void pushDiscard(final Expression expr) {
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   283
        discard.push(expr);
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   284
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   285
29410
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   286
    boolean popDiscardIfCurrent(final Expression expr) {
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   287
        if (isCurrentDiscard(expr)) {
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   288
            discard.pop();
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   289
            return true;
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   290
        }
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   291
        return false;
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   292
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   293
29410
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   294
    boolean isCurrentDiscard(final Expression expr) {
cdfd8fbb2b1d 8074484: More agressive value discarding
attila
parents: 28690
diff changeset
   295
        return discard.peek() == expr;
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   296
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   297
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   298
    int quickSlot(final Type type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   299
        return methodEmitters.peek().defineTemporaryLocalVariable(type.getSlots());
17769
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   300
    }
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   301
}
14ea7feaf658 8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
diff changeset
   302