CacheAst.java revision 1399:eea9202e8930
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 26package jdk.nashorn.internal.codegen; 27 28import java.util.ArrayDeque; 29import java.util.Collections; 30import java.util.Deque; 31import jdk.nashorn.internal.ir.FunctionNode; 32import jdk.nashorn.internal.ir.LexicalContext; 33import jdk.nashorn.internal.ir.Node; 34import jdk.nashorn.internal.ir.Statement; 35import jdk.nashorn.internal.ir.visitor.NodeVisitor; 36import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData; 37 38class CacheAst extends NodeVisitor<LexicalContext> { 39 private final Deque<RecompilableScriptFunctionData> dataStack = new ArrayDeque<>(); 40 41 private final Compiler compiler; 42 43 CacheAst(final Compiler compiler) { 44 super(new LexicalContext()); 45 this.compiler = compiler; 46 assert !compiler.isOnDemandCompilation(); 47 } 48 49 @Override 50 public boolean enterFunctionNode(final FunctionNode functionNode) { 51 final int id = functionNode.getId(); 52 // It isn't necessary to keep a stack of RecompilableScriptFunctionData, but then we'd need to do a 53 // potentially transitive lookup with compiler.getScriptFunctionData(id) for deeper functions; this way 54 // we keep it constant time. 55 dataStack.push(dataStack.isEmpty() ? compiler.getScriptFunctionData(id) : dataStack.peek().getScriptFunctionData(id)); 56 return true; 57 } 58 59 @Override 60 public Node leaveFunctionNode(final FunctionNode functionNode) { 61 final RecompilableScriptFunctionData data = dataStack.pop(); 62 if (functionNode.isSplit()) { 63 // NOTE: cache only split function ASTs from eager pass. Caching non-split functions would require 64 // some additional work, namely creating the concept of "uncacheable" function and reworking 65 // ApplySpecialization to ensure that functions undergoing apply-to-call transformations are not 66 // cacheable as well as recomputing Symbol.useCount when caching the eagerly parsed AST. 67 // Recomputing Symbol.useCount would be needed so it will only reflect uses from within the 68 // function being cached (and not reflect uses from its own nested functions or functions it is 69 // nested in). This is consistent with the count an on-demand recompilation of the function would 70 // produce. This is important as the decision to emit shared scope calls is based on this count, 71 // and if it is not matched between a previous version of the code and its deoptimizing rest-of 72 // compilation, it can result in rest-of not emitting a shared scope call where a previous version 73 // of the code (compiled from a cached eager pre-pass seeing higher (global) useCount) would emit 74 // it, causing a mismatch in stack shapes between previous code and its rest-of. 75 data.setCachedAst(functionNode); 76 } 77 78 if (!dataStack.isEmpty() && ((dataStack.peek().getFunctionFlags() & FunctionNode.IS_SPLIT) != 0)) { 79 // Return a function node with no body so that caching outer functions doesn't hold on to nested 80 // functions' bodies. Note we're doing this only for functions directly nested inside split 81 // functions, since we're only caching the split ones. It is not necessary to limit body removal 82 // to just these functions, but it's a cheap way to prevent unnecessary AST mutations. 83 return functionNode.setBody(lc, functionNode.getBody().setStatements(null, Collections.<Statement>emptyList())); 84 } 85 return functionNode; 86 } 87} 88