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