NashornCallSiteDescriptor.java revision 1481:e6bb9489faac
1/* 2 * Copyright (c) 2010, 2013, 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.runtime.linker; 27 28import java.lang.invoke.MethodHandles; 29import java.lang.invoke.MethodHandles.Lookup; 30import java.lang.invoke.MethodType; 31import java.security.AccessControlContext; 32import java.security.AccessController; 33import java.security.PrivilegedAction; 34import java.util.concurrent.ConcurrentHashMap; 35import java.util.concurrent.ConcurrentMap; 36import jdk.internal.dynalink.CallSiteDescriptor; 37import jdk.internal.dynalink.support.AbstractCallSiteDescriptor; 38import jdk.nashorn.internal.ir.debug.NashornTextifier; 39import jdk.nashorn.internal.runtime.AccessControlContextFactory; 40import jdk.nashorn.internal.runtime.ScriptRuntime; 41 42/** 43 * Nashorn-specific implementation of Dynalink's {@link CallSiteDescriptor}. The reason we have our own subclass is that 44 * we can have a more compact representation, as we know that we're always only using {@code "dyn:*"} operations; also 45 * we're storing flags in an additional primitive field. 46 */ 47public final class NashornCallSiteDescriptor extends AbstractCallSiteDescriptor<NashornCallSiteDescriptor> { 48 /** Flags that the call site references a scope variable (it's an identifier reference or a var declaration, not a 49 * property access expression. */ 50 public static final int CALLSITE_SCOPE = 1 << 0; 51 /** Flags that the call site is in code that uses ECMAScript strict mode. */ 52 public static final int CALLSITE_STRICT = 1 << 1; 53 /** Flags that a property getter or setter call site references a scope variable that is located at a known distance 54 * in the scope chain. Such getters and setters can often be linked more optimally using these assumptions. */ 55 public static final int CALLSITE_FAST_SCOPE = 1 << 2; 56 /** Flags that a callsite type is optimistic, i.e. we might get back a wider return value than encoded in the 57 * descriptor, and in that case we have to throw an UnwarrantedOptimismException */ 58 public static final int CALLSITE_OPTIMISTIC = 1 << 3; 59 /** Is this really an apply that we try to call as a call? */ 60 public static final int CALLSITE_APPLY_TO_CALL = 1 << 4; 61 /** Does this a callsite for a variable declaration? */ 62 public static final int CALLSITE_DECLARE = 1 << 5; 63 64 /** Flags that the call site is profiled; Contexts that have {@code "profile.callsites"} boolean property set emit 65 * code where call sites have this flag set. */ 66 public static final int CALLSITE_PROFILE = 1 << 6; 67 /** Flags that the call site is traced; Contexts that have {@code "trace.callsites"} property set emit code where 68 * call sites have this flag set. */ 69 public static final int CALLSITE_TRACE = 1 << 7; 70 /** Flags that the call site linkage miss (and thus, relinking) is traced; Contexts that have the keyword 71 * {@code "miss"} in their {@code "trace.callsites"} property emit code where call sites have this flag set. */ 72 public static final int CALLSITE_TRACE_MISSES = 1 << 8; 73 /** Flags that entry/exit to/from the method linked at call site are traced; Contexts that have the keyword 74 * {@code "enterexit"} in their {@code "trace.callsites"} property emit code where call sites have this flag set. */ 75 public static final int CALLSITE_TRACE_ENTEREXIT = 1 << 9; 76 /** Flags that values passed as arguments to and returned from the method linked at call site are traced; Contexts 77 * that have the keyword {@code "values"} in their {@code "trace.callsites"} property emit code where call sites 78 * have this flag set. */ 79 public static final int CALLSITE_TRACE_VALUES = 1 << 10; 80 81 //we could have more tracing flags here, for example CALLSITE_TRACE_SCOPE, but bits are a bit precious 82 //right now given the program points 83 84 /** 85 * Number of bits the program point is shifted to the left in the flags (lowest bit containing a program point). 86 * Always one larger than the largest flag shift. Note that introducing a new flag halves the number of program 87 * points we can have. 88 * TODO: rethink if we need the various profile/trace flags or the linker can use the Context instead to query its 89 * trace/profile settings. 90 */ 91 public static final int CALLSITE_PROGRAM_POINT_SHIFT = 11; 92 93 /** 94 * Maximum program point value. 21 bits should be enough for anyone 95 */ 96 public static final int MAX_PROGRAM_POINT_VALUE = (1 << 32 - CALLSITE_PROGRAM_POINT_SHIFT) - 1; 97 98 /** 99 * Flag mask to get the program point flags 100 */ 101 public static final int FLAGS_MASK = (1 << CALLSITE_PROGRAM_POINT_SHIFT) - 1; 102 103 private static final ClassValue<ConcurrentMap<NashornCallSiteDescriptor, NashornCallSiteDescriptor>> canonicals = 104 new ClassValue<ConcurrentMap<NashornCallSiteDescriptor,NashornCallSiteDescriptor>>() { 105 @Override 106 protected ConcurrentMap<NashornCallSiteDescriptor, NashornCallSiteDescriptor> computeValue(final Class<?> type) { 107 return new ConcurrentHashMap<>(); 108 } 109 }; 110 111 private static final AccessControlContext GET_LOOKUP_PERMISSION_CONTEXT = 112 AccessControlContextFactory.createAccessControlContext(CallSiteDescriptor.GET_LOOKUP_PERMISSION); 113 114 private final MethodHandles.Lookup lookup; 115 private final String operator; 116 private final String operand; 117 private final MethodType methodType; 118 private final int flags; 119 120 /** 121 * Function used by {@link NashornTextifier} to represent call site flags in 122 * human readable form 123 * @param flags call site flags 124 * @return human readable form of this callsite descriptor 125 */ 126 public static String toString(final int flags) { 127 final StringBuilder sb = new StringBuilder(); 128 if ((flags & CALLSITE_SCOPE) != 0) { 129 if ((flags & CALLSITE_FAST_SCOPE) != 0) { 130 sb.append("fastscope "); 131 } else { 132 assert (flags & CALLSITE_FAST_SCOPE) == 0 : "can't be fastscope without scope"; 133 sb.append("scope "); 134 } 135 if ((flags & CALLSITE_DECLARE) != 0) { 136 sb.append("declare "); 137 } 138 } 139 if ((flags & CALLSITE_APPLY_TO_CALL) != 0) { 140 sb.append("apply2call "); 141 } 142 if ((flags & CALLSITE_STRICT) != 0) { 143 sb.append("strict "); 144 } 145 return sb.length() == 0 ? "" : " " + sb.toString().trim(); 146 } 147 148 /** 149 * Retrieves a Nashorn call site descriptor with the specified values. Since call site descriptors are immutable 150 * this method is at liberty to retrieve canonicalized instances (although it is not guaranteed it will do so). 151 * @param lookup the lookup describing the script 152 * @param name the name at the call site, e.g. {@code "dyn:getProp|getElem|getMethod:color"}. 153 * @param methodType the method type at the call site 154 * @param flags Nashorn-specific call site flags 155 * @return a call site descriptor with the specified values. 156 */ 157 public static NashornCallSiteDescriptor get(final MethodHandles.Lookup lookup, final String name, 158 final MethodType methodType, final int flags) { 159 final String[] tokenizedName = CallSiteDescriptor.tokenizeName(name); 160 assert tokenizedName.length >= 2; 161 assert "dyn".equals(tokenizedName[0]); 162 assert tokenizedName[1] != null; 163 // TODO: see if we can move mangling/unmangling into Dynalink 164 return get(lookup, tokenizedName[1], tokenizedName.length == 3 ? tokenizedName[2].intern() : null, 165 methodType, flags); 166 } 167 168 private static NashornCallSiteDescriptor get(final MethodHandles.Lookup lookup, final String operator, final String operand, final MethodType methodType, final int flags) { 169 final NashornCallSiteDescriptor csd = new NashornCallSiteDescriptor(lookup, operator, operand, methodType, flags); 170 // Many of these call site descriptors are identical (e.g. every getter for a property color will be 171 // "dyn:getProp:color(Object)Object", so it makes sense canonicalizing them. 172 final ConcurrentMap<NashornCallSiteDescriptor, NashornCallSiteDescriptor> classCanonicals = canonicals.get(lookup.lookupClass()); 173 final NashornCallSiteDescriptor canonical = classCanonicals.putIfAbsent(csd, csd); 174 return canonical != null ? canonical : csd; 175 } 176 177 private NashornCallSiteDescriptor(final MethodHandles.Lookup lookup, final String operator, final String operand, 178 final MethodType methodType, final int flags) { 179 this.lookup = lookup; 180 this.operator = operator; 181 this.operand = operand; 182 this.methodType = methodType; 183 this.flags = flags; 184 } 185 186 @Override 187 public int getNameTokenCount() { 188 return operand == null ? 2 : 3; 189 } 190 191 @Override 192 public String getNameToken(final int i) { 193 switch(i) { 194 case 0: return "dyn"; 195 case 1: return operator; 196 case 2: 197 if(operand != null) { 198 return operand; 199 } 200 break; 201 default: 202 break; 203 } 204 throw new IndexOutOfBoundsException(String.valueOf(i)); 205 } 206 207 @Override 208 public Lookup getLookup() { 209 return CallSiteDescriptor.checkLookup(lookup); 210 } 211 212 static Lookup getLookupPrivileged(final CallSiteDescriptor csd) { 213 if (csd instanceof NashornCallSiteDescriptor) { 214 return ((NashornCallSiteDescriptor)csd).lookup; 215 } 216 return AccessController.doPrivileged((PrivilegedAction<Lookup>)()->csd.getLookup(), 217 GET_LOOKUP_PERMISSION_CONTEXT); 218 } 219 220 @Override 221 protected boolean equalsInKind(final NashornCallSiteDescriptor csd) { 222 return super.equalsInKind(csd) && flags == csd.flags; 223 } 224 225 @Override 226 public MethodType getMethodType() { 227 return methodType; 228 } 229 230 /** 231 * Returns the operator (e.g. {@code "getProp"}) in this call site descriptor's name. Equivalent to 232 * {@code getNameToken(CallSiteDescriptor.OPERATOR)}. The returned operator can be composite. 233 * @return the operator in this call site descriptor's name. 234 */ 235 public String getOperator() { 236 return operator; 237 } 238 239 /** 240 * Returns the first operator in this call site descriptor's name. E.g. if this call site descriptor has a composite 241 * operation {@code "getProp|getMethod|getElem"}, it will return {@code "getProp"}. Nashorn - being a ECMAScript 242 * engine - does not distinguish between property, element, and method namespace; ECMAScript objects just have one 243 * single property namespace for all these, therefore it is largely irrelevant what the composite operation is 244 * structured like; if the first operation can't be satisfied, neither can the others. The first operation is 245 * however sometimes used to slightly alter the semantics; for example, a distinction between {@code "getProp"} and 246 * {@code "getMethod"} being the first operation can translate into whether {@code "__noSuchProperty__"} or 247 * {@code "__noSuchMethod__"} will be executed in case the property is not found. 248 * @return the first operator in this call site descriptor's name. 249 */ 250 public String getFirstOperator() { 251 final int delim = operator.indexOf(CallSiteDescriptor.OPERATOR_DELIMITER); 252 return delim == -1 ? operator : operator.substring(0, delim); 253 } 254 255 /** 256 * Returns the named operand in this descriptor's name. Equivalent to 257 * {@code getNameToken(CallSiteDescriptor.NAME_OPERAND)}. E.g. for operation {@code "dyn:getProp:color"}, returns 258 * {@code "color"}. For call sites without named operands (e.g. {@code "dyn:new"}) returns null. 259 * @return the named operand in this descriptor's name. 260 */ 261 public String getOperand() { 262 return operand; 263 } 264 265 /** 266 * If this is a dyn:call or dyn:new, this returns function description from callsite. 267 * Caller has to make sure this is a dyn:call or dyn:new call site. 268 * 269 * @return function description if available (or null) 270 */ 271 public String getFunctionDescription() { 272 assert getFirstOperator().equals("call") || getFirstOperator().equals("new"); 273 return getNameTokenCount() > 2? getNameToken(2) : null; 274 } 275 276 /** 277 * If this is a dyn:call or dyn:new, this returns function description from callsite. 278 * Caller has to make sure this is a dyn:call or dyn:new call site. 279 * 280 * @param desc call site descriptor 281 * @return function description if available (or null) 282 */ 283 public static String getFunctionDescription(final CallSiteDescriptor desc) { 284 return desc instanceof NashornCallSiteDescriptor ? 285 ((NashornCallSiteDescriptor)desc).getFunctionDescription() : null; 286 } 287 288 289 /** 290 * Returns the error message to be used when dyn:call or dyn:new is used on a non-function. 291 * 292 * @param obj object on which dyn:call or dyn:new is used 293 * @return error message 294 */ 295 public String getFunctionErrorMessage(final Object obj) { 296 final String funcDesc = getFunctionDescription(); 297 return funcDesc != null? funcDesc : ScriptRuntime.safeToString(obj); 298 } 299 300 /** 301 * Returns the error message to be used when dyn:call or dyn:new is used on a non-function. 302 * 303 * @param desc call site descriptor 304 * @param obj object on which dyn:call or dyn:new is used 305 * @return error message 306 */ 307 public static String getFunctionErrorMessage(final CallSiteDescriptor desc, final Object obj) { 308 return desc instanceof NashornCallSiteDescriptor ? 309 ((NashornCallSiteDescriptor)desc).getFunctionErrorMessage(obj) : 310 ScriptRuntime.safeToString(obj); 311 } 312 313 /** 314 * Returns the Nashorn-specific flags for this call site descriptor. 315 * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a 316 * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code 317 * generated outside of Nashorn. 318 * @return the Nashorn-specific flags for the call site, or 0 if the passed descriptor is not a Nashorn call site 319 * descriptor. 320 */ 321 public static int getFlags(final CallSiteDescriptor desc) { 322 return desc instanceof NashornCallSiteDescriptor ? ((NashornCallSiteDescriptor)desc).flags : 0; 323 } 324 325 /** 326 * Returns true if this descriptor has the specified flag set, see {@code CALLSITE_*} constants in this class. 327 * @param flag the tested flag 328 * @return true if the flag is set, false otherwise 329 */ 330 private boolean isFlag(final int flag) { 331 return (flags & flag) != 0; 332 } 333 334 /** 335 * Returns true if this descriptor has the specified flag set, see {@code CALLSITE_*} constants in this class. 336 * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a 337 * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code 338 * generated outside of Nashorn. 339 * @param flag the tested flag 340 * @return true if the flag is set, false otherwise (it will be false if the descriptor is not a Nashorn call site 341 * descriptor). 342 */ 343 private static boolean isFlag(final CallSiteDescriptor desc, final int flag) { 344 return (getFlags(desc) & flag) != 0; 345 } 346 347 /** 348 * Returns true if this descriptor is a Nashorn call site descriptor and has the {@link #CALLSITE_SCOPE} flag set. 349 * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a 350 * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code 351 * generated outside of Nashorn. 352 * @return true if the descriptor is a Nashorn call site descriptor, and the flag is set, false otherwise. 353 */ 354 public static boolean isScope(final CallSiteDescriptor desc) { 355 return isFlag(desc, CALLSITE_SCOPE); 356 } 357 358 /** 359 * Returns true if this descriptor is a Nashorn call site descriptor and has the {@link #CALLSITE_FAST_SCOPE} flag set. 360 * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a 361 * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code 362 * generated outside of Nashorn. 363 * @return true if the descriptor is a Nashorn call site descriptor, and the flag is set, false otherwise. 364 */ 365 public static boolean isFastScope(final CallSiteDescriptor desc) { 366 return isFlag(desc, CALLSITE_FAST_SCOPE); 367 } 368 369 /** 370 * Returns true if this descriptor is a Nashorn call site descriptor and has the {@link #CALLSITE_STRICT} flag set. 371 * @param desc the descriptor. It can be any kind of a call site descriptor, not necessarily a 372 * {@code NashornCallSiteDescriptor}. This allows for graceful interoperability when linking Nashorn with code 373 * generated outside of Nashorn. 374 * @return true if the descriptor is a Nashorn call site descriptor, and the flag is set, false otherwise. 375 */ 376 public static boolean isStrict(final CallSiteDescriptor desc) { 377 return isFlag(desc, CALLSITE_STRICT); 378 } 379 380 /** 381 * Returns true if this is an apply call that we try to call as 382 * a "call" 383 * @param desc descriptor 384 * @return true if apply to call 385 */ 386 public static boolean isApplyToCall(final CallSiteDescriptor desc) { 387 return isFlag(desc, CALLSITE_APPLY_TO_CALL); 388 } 389 390 /** 391 * Is this an optimistic call site 392 * @param desc descriptor 393 * @return true if optimistic 394 */ 395 public static boolean isOptimistic(final CallSiteDescriptor desc) { 396 return isFlag(desc, CALLSITE_OPTIMISTIC); 397 } 398 399 /** 400 * Does this callsite contain a declaration for its target? 401 * @param desc descriptor 402 * @return true if contains declaration 403 */ 404 public static boolean isDeclaration(final CallSiteDescriptor desc) { 405 return isFlag(desc, CALLSITE_DECLARE); 406 } 407 408 /** 409 * Returns true if {@code flags} has the {@link #CALLSITE_STRICT} bit set. 410 * @param flags the flags 411 * @return true if the flag is set, false otherwise. 412 */ 413 public static boolean isStrictFlag(final int flags) { 414 return (flags & CALLSITE_STRICT) != 0; 415 } 416 417 /** 418 * Returns true if {@code flags} has the {@link #CALLSITE_SCOPE} bit set. 419 * @param flags the flags 420 * @return true if the flag is set, false otherwise. 421 */ 422 public static boolean isScopeFlag(final int flags) { 423 return (flags & CALLSITE_SCOPE) != 0; 424 } 425 426 /** 427 * Get a program point from a descriptor (must be optimistic) 428 * @param desc descriptor 429 * @return program point 430 */ 431 public static int getProgramPoint(final CallSiteDescriptor desc) { 432 assert isOptimistic(desc) : "program point requested from non-optimistic descriptor " + desc; 433 return getFlags(desc) >> CALLSITE_PROGRAM_POINT_SHIFT; 434 } 435 436 boolean isProfile() { 437 return isFlag(CALLSITE_PROFILE); 438 } 439 440 boolean isTrace() { 441 return isFlag(CALLSITE_TRACE); 442 } 443 444 boolean isTraceMisses() { 445 return isFlag(CALLSITE_TRACE_MISSES); 446 } 447 448 boolean isTraceEnterExit() { 449 return isFlag(CALLSITE_TRACE_ENTEREXIT); 450 } 451 452 boolean isTraceObjects() { 453 return isFlag(CALLSITE_TRACE_VALUES); 454 } 455 456 boolean isOptimistic() { 457 return isFlag(CALLSITE_OPTIMISTIC); 458 } 459 460 @Override 461 public CallSiteDescriptor changeMethodType(final MethodType newMethodType) { 462 return get(lookup, operator, operand, newMethodType, flags); 463 } 464 465 466 @Override 467 protected boolean lookupEquals(final NashornCallSiteDescriptor other) { 468 return AbstractCallSiteDescriptor.lookupsEqual(lookup, other.lookup); 469 } 470 471 @Override 472 protected int lookupHashCode() { 473 return AbstractCallSiteDescriptor.lookupHashCode(lookup); 474 } 475 476 @Override 477 protected String lookupToString() { 478 return lookup.toString(); 479 } 480} 481