1 /*
2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.nashorn.internal.codegen;
27 
28 import java.util.ArrayDeque;
29 import java.util.Collections;
30 import java.util.Deque;
31 import jdk.nashorn.internal.ir.FunctionNode;
32 import jdk.nashorn.internal.ir.Node;
33 import jdk.nashorn.internal.ir.Statement;
34 import jdk.nashorn.internal.ir.visitor.SimpleNodeVisitor;
35 import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
36 
37 class CacheAst extends SimpleNodeVisitor {
38     private final Deque<RecompilableScriptFunctionData> dataStack = new ArrayDeque<>();
39 
40     private final Compiler compiler;
41 
CacheAst(final Compiler compiler)42     CacheAst(final Compiler compiler) {
43         this.compiler = compiler;
44         assert !compiler.isOnDemandCompilation();
45     }
46 
47     @Override
enterFunctionNode(final FunctionNode functionNode)48     public boolean enterFunctionNode(final FunctionNode functionNode) {
49         final int id = functionNode.getId();
50         // It isn't necessary to keep a stack of RecompilableScriptFunctionData, but then we'd need to do a
51         // potentially transitive lookup with compiler.getScriptFunctionData(id) for deeper functions; this way
52         // we keep it constant time.
53         dataStack.push(dataStack.isEmpty() ? compiler.getScriptFunctionData(id) : dataStack.peek().getScriptFunctionData(id));
54         return true;
55     }
56 
57     @Override
leaveFunctionNode(final FunctionNode functionNode)58     public Node leaveFunctionNode(final FunctionNode functionNode) {
59         final RecompilableScriptFunctionData data = dataStack.pop();
60         if (functionNode.isSplit()) {
61             // NOTE: cache only split function ASTs from eager pass. Caching non-split functions would require
62             // some additional work, namely creating the concept of "uncacheable" function and reworking
63             // ApplySpecialization to ensure that functions undergoing apply-to-call transformations are not
64             // cacheable as well as recomputing Symbol.useCount when caching the eagerly parsed AST.
65             // Recomputing Symbol.useCount would be needed so it will only reflect uses from within the
66             // function being cached (and not reflect uses from its own nested functions or functions it is
67             // nested in). This is consistent with the count an on-demand recompilation of the function would
68             // produce. This is important as the decision to emit shared scope calls is based on this count,
69             // and if it is not matched between a previous version of the code and its deoptimizing rest-of
70             // compilation, it can result in rest-of not emitting a shared scope call where a previous version
71             // of the code (compiled from a cached eager pre-pass seeing higher (global) useCount) would emit
72             // it, causing a mismatch in stack shapes between previous code and its rest-of.
73             data.setCachedAst(functionNode);
74         }
75 
76         if (!dataStack.isEmpty() && ((dataStack.peek().getFunctionFlags() & FunctionNode.IS_SPLIT) != 0)) {
77             // Return a function node with no body so that caching outer functions doesn't hold on to nested
78             // functions' bodies. Note we're doing this only for functions directly nested inside split
79             // functions, since we're only caching the split ones. It is not necessary to limit body removal
80             // to just these functions, but it's a cheap way to prevent unnecessary AST mutations.
81             return functionNode.setBody(lc, functionNode.getBody().setStatements(null, Collections.<Statement>emptyList()));
82         }
83         return functionNode;
84     }
85 }
86