TypeEvaluator.java revision 971:c93b6091b11e
1/* 2 * Copyright (c) 2010, 2014, 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 static jdk.nashorn.internal.runtime.Property.NOT_CONFIGURABLE; 29import static jdk.nashorn.internal.runtime.Property.NOT_ENUMERABLE; 30import static jdk.nashorn.internal.runtime.Property.NOT_WRITABLE; 31 32import jdk.nashorn.internal.codegen.types.Type; 33import jdk.nashorn.internal.ir.AccessNode; 34import jdk.nashorn.internal.ir.Expression; 35import jdk.nashorn.internal.ir.IdentNode; 36import jdk.nashorn.internal.ir.IndexNode; 37import jdk.nashorn.internal.ir.Optimistic; 38import jdk.nashorn.internal.objects.NativeArray; 39import jdk.nashorn.internal.runtime.FindProperty; 40import jdk.nashorn.internal.runtime.JSType; 41import jdk.nashorn.internal.runtime.Property; 42import jdk.nashorn.internal.runtime.ScriptObject; 43import jdk.nashorn.internal.runtime.ScriptRuntime; 44 45/** 46 * Functionality for using a runtime scope to look up value types. 47 * Used during recompilation. 48 */ 49final class TypeEvaluator { 50 private final Compiler compiler; 51 private final ScriptObject runtimeScope; 52 53 TypeEvaluator(final Compiler compiler, final ScriptObject runtimeScope) { 54 this.compiler = compiler; 55 this.runtimeScope = runtimeScope; 56 } 57 58 Type getOptimisticType(final Optimistic node) { 59 assert compiler.useOptimisticTypes(); 60 61 final int programPoint = node.getProgramPoint(); 62 final Type validType = compiler.getInvalidatedProgramPointType(programPoint); 63 64 if (validType != null) { 65 return validType; 66 } 67 68 final Type mostOptimisticType = node.getMostOptimisticType(); 69 final Type evaluatedType = getEvaluatedType(node); 70 71 if (evaluatedType != null) { 72 if (evaluatedType.widerThan(mostOptimisticType)) { 73 final Type newValidType = evaluatedType.isObject() || evaluatedType.isBoolean() ? Type.OBJECT : evaluatedType; 74 // Update invalidatedProgramPoints so we don't re-evaluate the expression next time. This is a heuristic 75 // as we're doing a tradeoff. Re-evaluating expressions on each recompile takes time, but it might 76 // notice a widening in the type of the expression and thus prevent an unnecessary deoptimization later. 77 // We'll presume though that the types of expressions are mostly stable, so if we evaluated it in one 78 // compilation, we'll keep to that and risk a low-probability deoptimization if its type gets widened 79 // in the future. 80 compiler.addInvalidatedProgramPoint(node.getProgramPoint(), newValidType); 81 } 82 return evaluatedType; 83 } 84 return mostOptimisticType; 85 } 86 87 private static Type getPropertyType(final ScriptObject sobj, final String name) { 88 final FindProperty find = sobj.findProperty(name, true); 89 if (find == null) { 90 return null; 91 } 92 93 final Property property = find.getProperty(); 94 final Class<?> propertyClass = property.getCurrentType(); 95 if (propertyClass == null) { 96 // propertyClass == null means its value is Undefined. It is probably not initialized yet, so we won't make 97 // a type assumption yet. 98 return null; 99 } else if (propertyClass.isPrimitive()) { 100 return Type.typeFor(propertyClass); 101 } 102 103 final ScriptObject owner = find.getOwner(); 104 if (property.hasGetterFunction(owner)) { 105 // Can have side effects, so we can't safely evaluate it; since !propertyClass.isPrimitive(), it's Object. 106 return Type.OBJECT; 107 } 108 109 // Safely evaluate the property, and return the narrowest type for the actual value (e.g. Type.INT for a boxed 110 // integer). 111 final Object value = property.getObjectValue(owner, owner); 112 if (value == ScriptRuntime.UNDEFINED) { 113 return null; 114 } 115 return Type.typeFor(JSType.unboxedFieldType(value)); 116 } 117 118 /** 119 * Declares a symbol name as belonging to a non-scoped local variable during an on-demand compilation of a single 120 * function. This method will add an explicit Undefined binding for the local into the runtime scope if it's 121 * otherwise implicitly undefined so that when an expression is evaluated for the name, it won't accidentally find 122 * an unrelated value higher up the scope chain. It is only required to call this method when doing an optimistic 123 * on-demand compilation. 124 * @param symbolName the name of the symbol that is to be declared as being a non-scoped local variable. 125 */ 126 void declareLocalSymbol(final String symbolName) { 127 assert 128 compiler.useOptimisticTypes() && 129 compiler.isOnDemandCompilation() && 130 runtimeScope != null : 131 "useOptimistic=" + 132 compiler.useOptimisticTypes() + 133 " isOnDemand=" + 134 compiler.isOnDemandCompilation() + 135 " scope="+runtimeScope; 136 137 if (runtimeScope.findProperty(symbolName, false) == null) { 138 runtimeScope.addOwnProperty(symbolName, NOT_WRITABLE | NOT_ENUMERABLE | NOT_CONFIGURABLE, ScriptRuntime.UNDEFINED); 139 } 140 } 141 142 private Object evaluateSafely(final Expression expr) { 143 if (expr instanceof IdentNode) { 144 return runtimeScope == null ? null : evaluatePropertySafely(runtimeScope, ((IdentNode)expr).getName()); 145 } 146 147 if (expr instanceof AccessNode) { 148 final AccessNode accessNode = (AccessNode)expr; 149 final Object base = evaluateSafely(accessNode.getBase()); 150 if (!(base instanceof ScriptObject)) { 151 return null; 152 } 153 return evaluatePropertySafely((ScriptObject)base, accessNode.getProperty()); 154 } 155 156 return null; 157 } 158 159 private static Object evaluatePropertySafely(final ScriptObject sobj, final String name) { 160 final FindProperty find = sobj.findProperty(name, true); 161 if (find == null) { 162 return null; 163 } 164 final Property property = find.getProperty(); 165 final ScriptObject owner = find.getOwner(); 166 if (property.hasGetterFunction(owner)) { 167 // Possible side effects; can't evaluate safely 168 return null; 169 } 170 return property.getObjectValue(owner, owner); 171 } 172 173 174 private Type getEvaluatedType(final Optimistic expr) { 175 if (expr instanceof IdentNode) { 176 if (runtimeScope == null) { 177 return null; 178 } 179 return getPropertyType(runtimeScope, ((IdentNode)expr).getName()); 180 } 181 182 if (expr instanceof AccessNode) { 183 final AccessNode accessNode = (AccessNode)expr; 184 final Object base = evaluateSafely(accessNode.getBase()); 185 if (!(base instanceof ScriptObject)) { 186 return null; 187 } 188 return getPropertyType((ScriptObject)base, accessNode.getProperty()); 189 } 190 191 if (expr instanceof IndexNode) { 192 final IndexNode indexNode = (IndexNode)expr; 193 final Object base = evaluateSafely(indexNode.getBase()); 194 if(!(base instanceof NativeArray)) { 195 // We only know how to deal with NativeArray. TODO: maybe manage buffers too 196 return null; 197 } 198 // NOTE: optimistic array getters throw UnwarrantedOptimismException based on the type of their underlying 199 // array storage, not based on values of individual elements. Thus, a LongArrayData will throw UOE for every 200 // optimistic int linkage attempt, even if the long value being returned in the first invocation would be 201 // representable as int. That way, we can presume that the array's optimistic type is the most optimistic 202 // type for which an element getter has a chance of executing successfully. 203 return ((NativeArray)base).getArray().getOptimisticType(); 204 } 205 206 return null; 207 } 208} 209