aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/codegen/FindScopeDepths.java
blob: 431244dc3c69dfbcb6035e843b1855990b4468b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.internal.codegen;

import static jdk.nashorn.internal.runtime.logging.DebugLogger.quote;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import jdk.nashorn.internal.codegen.ObjectClassGenerator.AllocatorDescriptor;
import jdk.nashorn.internal.ir.Block;
import jdk.nashorn.internal.ir.FunctionNode;
import jdk.nashorn.internal.ir.FunctionNode.CompilationState;
import jdk.nashorn.internal.ir.IdentNode;
import jdk.nashorn.internal.ir.LexicalContext;
import jdk.nashorn.internal.ir.Node;
import jdk.nashorn.internal.ir.Symbol;
import jdk.nashorn.internal.ir.WithNode;
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
import jdk.nashorn.internal.runtime.logging.DebugLogger;
import jdk.nashorn.internal.runtime.logging.Loggable;
import jdk.nashorn.internal.runtime.logging.Logger;

/**
 * Establishes depth of scope for non local symbols at the start of method.
 * If this is a recompilation, the previous data from eager compilation is
 * stored in the RecompilableScriptFunctionData and is transferred to the
 * FunctionNode being compiled
 */
@Logger(name="scopedepths")
final class FindScopeDepths extends NodeVisitor<LexicalContext> implements Loggable {

    private final Compiler compiler;
    private final Map<Integer, Map<Integer, RecompilableScriptFunctionData>> fnIdToNestedFunctions = new HashMap<>();
    private final Map<Integer, Map<String, Integer>> externalSymbolDepths = new HashMap<>();
    private final Map<Integer, Set<String>> internalSymbols = new HashMap<>();
    private final Set<Block> withBodies = new HashSet<>();

    private final DebugLogger log;

    private int dynamicScopeCount;

    FindScopeDepths(final Compiler compiler) {
        super(new LexicalContext());
        this.compiler = compiler;
        this.log      = initLogger(compiler.getContext());
    }

    @Override
    public DebugLogger getLogger() {
        return log;
    }

    @Override
    public DebugLogger initLogger(final Context context) {
        return context.getLogger(this.getClass());
    }

    static int findScopesToStart(final LexicalContext lc, final FunctionNode fn, final Block block) {
        final Block bodyBlock = findBodyBlock(lc, fn, block);
        final Iterator<Block> iter = lc.getBlocks(block);
        Block b = iter.next();
        int scopesToStart = 0;
        while (true) {
            if (b.needsScope()) {
                scopesToStart++;
            }
            if (b == bodyBlock) {
                break;
            }
            b = iter.next();
        }
        return scopesToStart;
    }

    static int findInternalDepth(final LexicalContext lc, final FunctionNode fn, final Block block, final Symbol symbol) {
        final Block bodyBlock = findBodyBlock(lc, fn, block);
        final Iterator<Block> iter = lc.getBlocks(block);
        Block b = iter.next();
        int scopesToStart = 0;
        while (true) {
            if (definedInBlock(b, symbol)) {
                return scopesToStart;
            }
            if (b.needsScope()) {
                scopesToStart++;
            }
            if (b == bodyBlock) {
                break; //don't go past body block, but process it
            }
            b = iter.next();
        }
        return -1;
    }

    private static boolean definedInBlock(final Block block, final Symbol symbol) {
        if (symbol.isGlobal()) {
            if (block.isGlobalScope()) {
                return true;
            }
            //globals cannot be defined anywhere else
            return false;
        }
        return block.getExistingSymbol(symbol.getName()) == symbol;
    }

    static Block findBodyBlock(final LexicalContext lc, final FunctionNode fn, final Block block) {
        final Iterator<Block> iter = lc.getBlocks(block);
        while (iter.hasNext()) {
            final Block next = iter.next();
            if (fn.getBody() == next) {
                return next;
            }
        }
        return null;
    }

    private static Block findGlobalBlock(final LexicalContext lc, final Block block) {
        final Iterator<Block> iter = lc.getBlocks(block);
        Block globalBlock = null;
        while (iter.hasNext()) {
            globalBlock = iter.next();
        }
        return globalBlock;
    }

    private static boolean isDynamicScopeBoundary(final FunctionNode fn) {
        return fn.needsDynamicScope();
    }

    private boolean isDynamicScopeBoundary(final Block block) {
        return withBodies.contains(block);
    }

    @Override
    public boolean enterFunctionNode(final FunctionNode functionNode) {
        if (compiler.isOnDemandCompilation()) {
            return true;
        }

        if (isDynamicScopeBoundary(functionNode)) {
            increaseDynamicScopeCount(functionNode);
        }

        final int fnId = functionNode.getId();
        Map<Integer, RecompilableScriptFunctionData> nestedFunctions = fnIdToNestedFunctions.get(fnId);
        if (nestedFunctions == null) {
            nestedFunctions = new HashMap<>();
            fnIdToNestedFunctions.put(fnId, nestedFunctions);
        }

        return true;
    }

    //external symbols hold the scope depth of sc11 from global at the start of the method
    @Override
    public Node leaveFunctionNode(final FunctionNode functionNode) {
        final String name = functionNode.getName();
        FunctionNode newFunctionNode = functionNode.setState(lc, CompilationState.SCOPE_DEPTHS_COMPUTED);

        if (compiler.isOnDemandCompilation()) {
            final RecompilableScriptFunctionData data = compiler.getScriptFunctionData(newFunctionNode.getId());
            if (data.inDynamicContext()) {
                log.fine("Reviving scriptfunction ", quote(name), " as defined in previous (now lost) dynamic scope.");
                newFunctionNode = newFunctionNode.setInDynamicContext(lc);
            }
            return newFunctionNode;
        }

        if (inDynamicScope()) {
            log.fine("Tagging ", quote(name), " as defined in dynamic scope");
            newFunctionNode = newFunctionNode.setInDynamicContext(lc);
        }

        //create recompilable scriptfunctiondata
        final int fnId = newFunctionNode.getId();
        final Map<Integer, RecompilableScriptFunctionData> nestedFunctions = fnIdToNestedFunctions.remove(fnId);

        assert nestedFunctions != null;
        // Generate the object class and property map in case this function is ever used as constructor
        final RecompilableScriptFunctionData data = new RecompilableScriptFunctionData(
                newFunctionNode,
                compiler.getCodeInstaller(),
                new AllocatorDescriptor(newFunctionNode.getThisProperties()),
                nestedFunctions,
                externalSymbolDepths.get(fnId),
                internalSymbols.get(fnId),
                compiler.removeSerializedAst(fnId));

        if (lc.getOutermostFunction() != newFunctionNode) {
            final FunctionNode parentFn = lc.getParentFunction(newFunctionNode);
            if (parentFn != null) {
                fnIdToNestedFunctions.get(parentFn.getId()).put(fnId, data);
            }
        } else {
            compiler.setData(data);
        }

        if (isDynamicScopeBoundary(functionNode)) {
            decreaseDynamicScopeCount(functionNode);
        }

        return newFunctionNode;
    }

    private boolean inDynamicScope() {
        return dynamicScopeCount > 0;
    }

    private void increaseDynamicScopeCount(final Node node) {
        assert dynamicScopeCount >= 0;
        ++dynamicScopeCount;
        if (log.isEnabled()) {
            log.finest(quote(lc.getCurrentFunction().getName()), " ++dynamicScopeCount = ", dynamicScopeCount, " at: ", node, node.getClass());
        }
    }

    private void decreaseDynamicScopeCount(final Node node) {
        --dynamicScopeCount;
        assert dynamicScopeCount >= 0;
        if (log.isEnabled()) {
            log.finest(quote(lc.getCurrentFunction().getName()), " --dynamicScopeCount = ", dynamicScopeCount, " at: ", node, node.getClass());
        }
    }

    @Override
    public boolean enterWithNode(final WithNode node) {
        withBodies.add(node.getBody());
        return true;
    }

    @Override
    public boolean enterBlock(final Block block) {
        if (compiler.isOnDemandCompilation()) {
            return true;
        }

        if (isDynamicScopeBoundary(block)) {
            increaseDynamicScopeCount(block);
        }

        if (!lc.isFunctionBody()) {
            return true;
        }

        //the below part only happens on eager compilation when we have the entire hierarchy
        //block is a function body
        final FunctionNode fn = lc.getCurrentFunction();

        //get all symbols that are referenced inside this function body
        final Set<Symbol> symbols = new HashSet<>();
        block.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
            @Override
            public final boolean enterDefault(final Node node) {
                if (!compiler.isOnDemandCompilation()) {
                    if (node instanceof IdentNode) {
                        final Symbol symbol = ((IdentNode)node).getSymbol();
                        if (symbol != null && symbol.isScope()) {
                            //if this is an internal symbol, skip it.
                            symbols.add(symbol);
                        }
                    }
                }
                return true;
            }
        });

        final Map<String, Integer> internals = new HashMap<>();

        final Block globalBlock = findGlobalBlock(lc, block);
        final Block bodyBlock   = findBodyBlock(lc, fn, block);

        assert globalBlock != null;
        assert bodyBlock   != null;

        for (final Symbol symbol : symbols) {
            Iterator<Block> iter;

            final int internalDepth = findInternalDepth(lc, fn, block, symbol);
            final boolean internal = internalDepth >= 0;
            if (internal) {
                internals.put(symbol.getName(), internalDepth);
            }

            // if not internal, we have to continue walking until we reach the top. We
            // start outside the body and each new scope adds a depth count. When we
            // find the symbol, we store its depth count
            if (!internal) {
                int depthAtStart = 0;
                //not internal - keep looking.
                iter = lc.getAncestorBlocks(bodyBlock);
                while (iter.hasNext()) {
                    final Block b2 = iter.next();
                    if (definedInBlock(b2, symbol)) {
                        addExternalSymbol(fn, symbol, depthAtStart);
                        break;
                    }
                    if (b2.needsScope()) {
                        depthAtStart++;
                    }
                }
            }
        }

        addInternalSymbols(fn, internals.keySet());

        if (log.isEnabled()) {
            log.info(fn.getName() + " internals=" + internals + " externals=" + externalSymbolDepths.get(fn.getId()));
        }

        return true;
    }

    @Override
    public Node leaveBlock(final Block block) {
        if (compiler.isOnDemandCompilation()) {
            return block;
        }
        if (isDynamicScopeBoundary(block)) {
            decreaseDynamicScopeCount(block);
        }
        return block;
    }

    private void addInternalSymbols(final FunctionNode functionNode, final Set<String> symbols) {
        final int fnId = functionNode.getId();
        assert internalSymbols.get(fnId) == null || internalSymbols.get(fnId).equals(symbols); //e.g. cloned finally block
        internalSymbols.put(fnId, symbols);
    }

    private void addExternalSymbol(final FunctionNode functionNode, final Symbol symbol, final int depthAtStart) {
        final int fnId = functionNode.getId();
        Map<String, Integer> depths = externalSymbolDepths.get(fnId);
        if (depths == null) {
            depths = new HashMap<>();
            externalSymbolDepths.put(fnId, depths);
        }
        depths.put(symbol.getName(), depthAtStart);
    }

}