1 /* 2 * Copyright (c) 2008, 2018, 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 java.lang.invoke; 27 28 import jdk.internal.misc.Unsafe; 29 import jdk.internal.vm.annotation.ForceInline; 30 import jdk.internal.vm.annotation.Stable; 31 import sun.invoke.util.ValueConversions; 32 import sun.invoke.util.VerifyAccess; 33 import sun.invoke.util.VerifyType; 34 import sun.invoke.util.Wrapper; 35 36 import java.lang.ref.WeakReference; 37 import java.util.Arrays; 38 import java.util.Objects; 39 40 import static java.lang.invoke.LambdaForm.*; 41 import static java.lang.invoke.LambdaForm.Kind.*; 42 import static java.lang.invoke.MethodHandleNatives.Constants.*; 43 import static java.lang.invoke.MethodHandleStatics.UNSAFE; 44 import static java.lang.invoke.MethodHandleStatics.newInternalError; 45 import static java.lang.invoke.MethodTypeForm.*; 46 47 /** 48 * The flavor of method handle which implements a constant reference 49 * to a class member. 50 * @author jrose 51 */ 52 class DirectMethodHandle extends MethodHandle { 53 final MemberName member; 54 55 // Constructors and factory methods in this class *must* be package scoped or private. 56 private DirectMethodHandle(MethodType mtype, LambdaForm form, MemberName member) { 57 super(mtype, form); 58 if (!member.isResolved()) throw new InternalError(); 59 60 if (member.getDeclaringClass().isInterface() && 61 member.getReferenceKind() == REF_invokeInterface && 62 member.isMethod() && !member.isAbstract()) { 63 // Check for corner case: invokeinterface of Object method 64 MemberName m = new MemberName(Object.class, member.getName(), member.getMethodType(), member.getReferenceKind()); 65 m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null); 66 if (m != null && m.isPublic()) { 67 assert(member.getReferenceKind() == m.getReferenceKind()); // else this.form is wrong 68 member = m; 69 } 70 } 71 72 this.member = member; 73 } 74 75 // Factory methods: 76 static DirectMethodHandle make(byte refKind, Class<?> refc, MemberName member, Class<?> callerClass) { 77 MethodType mtype = member.getMethodOrFieldType(); 78 if (!member.isStatic()) { 79 if (!member.getDeclaringClass().isAssignableFrom(refc) || member.isConstructor()) 80 throw new InternalError(member.toString()); 81 mtype = mtype.insertParameterTypes(0, refc); 82 } 83 if (!member.isField()) { 84 switch (refKind) { 85 case REF_invokeSpecial: { 86 member = member.asSpecial(); 87 // if caller is an interface we need to adapt to get the 88 // receiver check inserted 89 LambdaForm lform = preparedLambdaForm(member, callerClass.isInterface()); 90 Class<?> checkClass = refc; // Class to use for receiver type check 91 if (callerClass != null) { 92 checkClass = callerClass; // potentially strengthen to caller class 93 } 94 return new Special(mtype, lform, member, checkClass); 95 } 96 case REF_invokeInterface: { 97 // we always adapt 'special' when dealing with interfaces 98 LambdaForm lform = preparedLambdaForm(member, true); 99 return new Interface(mtype, lform, member, refc); 100 } 101 default: { 102 LambdaForm lform = preparedLambdaForm(member); 103 return new DirectMethodHandle(mtype, lform, member); 104 } 105 } 106 } else { 107 LambdaForm lform = preparedFieldLambdaForm(member); 108 if (member.isStatic()) { 109 long offset = MethodHandleNatives.staticFieldOffset(member); 110 Object base = MethodHandleNatives.staticFieldBase(member); 111 return new StaticAccessor(mtype, lform, member, base, offset); 112 } else { 113 long offset = MethodHandleNatives.objectFieldOffset(member); 114 assert(offset == (int)offset); 115 return new Accessor(mtype, lform, member, (int)offset); 116 } 117 } 118 } 119 static DirectMethodHandle make(Class<?> refc, MemberName member) { 120 byte refKind = member.getReferenceKind(); 121 if (refKind == REF_invokeSpecial) 122 refKind = REF_invokeVirtual; 123 return make(refKind, refc, member, null /* no callerClass context */); 124 } 125 static DirectMethodHandle make(MemberName member) { 126 if (member.isConstructor()) 127 return makeAllocator(member); 128 return make(member.getDeclaringClass(), member); 129 } 130 private static DirectMethodHandle makeAllocator(MemberName ctor) { 131 assert(ctor.isConstructor() && ctor.getName().equals("<init>")); 132 Class<?> instanceClass = ctor.getDeclaringClass(); 133 ctor = ctor.asConstructor(); 134 assert(ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor; 135 MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass); 136 LambdaForm lform = preparedLambdaForm(ctor); 137 MemberName init = ctor.asSpecial(); 138 assert(init.getMethodType().returnType() == void.class); 139 return new Constructor(mtype, lform, ctor, init, instanceClass); 140 } 141 142 @Override 143 BoundMethodHandle rebind() { 144 return BoundMethodHandle.makeReinvoker(this); 145 } 146 147 @Override 148 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 149 assert(this.getClass() == DirectMethodHandle.class); // must override in subclasses 150 return new DirectMethodHandle(mt, lf, member); 151 } 152 153 @Override 154 String internalProperties() { 155 return "\n& DMH.MN="+internalMemberName(); 156 } 157 158 //// Implementation methods. 159 @Override 160 @ForceInline 161 MemberName internalMemberName() { 162 return member; 163 } 164 165 private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory(); 166 167 /** 168 * Create a LF which can invoke the given method. 169 * Cache and share this structure among all methods with 170 * the same basicType and refKind. 171 */ 172 private static LambdaForm preparedLambdaForm(MemberName m, boolean adaptToSpecialIfc) { 173 assert(m.isInvocable()) : m; // call preparedFieldLambdaForm instead 174 MethodType mtype = m.getInvocationType().basicType(); 175 assert(!m.isMethodHandleInvoke()) : m; 176 int which; 177 // MemberName.getReferenceKind may be different from the 'kind' passed to 178 // DMH.make. Specifically private/final methods that use a direct call 179 // have been adapted to REF_invokeSpecial, even though the actual 180 // invocation mode may be invokevirtual or invokeinterface. 181 switch (m.getReferenceKind()) { 182 case REF_invokeVirtual: which = LF_INVVIRTUAL; break; 183 case REF_invokeStatic: which = LF_INVSTATIC; break; 184 case REF_invokeSpecial: which = LF_INVSPECIAL; break; 185 case REF_invokeInterface: which = LF_INVINTERFACE; break; 186 case REF_newInvokeSpecial: which = LF_NEWINVSPECIAL; break; 187 default: throw new InternalError(m.toString()); 188 } 189 if (which == LF_INVSTATIC && shouldBeInitialized(m)) { 190 // precompute the barrier-free version: 191 preparedLambdaForm(mtype, which); 192 which = LF_INVSTATIC_INIT; 193 } 194 if (which == LF_INVSPECIAL && adaptToSpecialIfc) { 195 which = LF_INVSPECIAL_IFC; 196 } 197 LambdaForm lform = preparedLambdaForm(mtype, which); 198 maybeCompile(lform, m); 199 assert(lform.methodType().dropParameterTypes(0, 1) 200 .equals(m.getInvocationType().basicType())) 201 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); 202 return lform; 203 } 204 205 private static LambdaForm preparedLambdaForm(MemberName m) { 206 return preparedLambdaForm(m, false); 207 } 208 209 private static LambdaForm preparedLambdaForm(MethodType mtype, int which) { 210 LambdaForm lform = mtype.form().cachedLambdaForm(which); 211 if (lform != null) return lform; 212 lform = makePreparedLambdaForm(mtype, which); 213 return mtype.form().setCachedLambdaForm(which, lform); 214 } 215 216 static LambdaForm makePreparedLambdaForm(MethodType mtype, int which) { 217 boolean needsInit = (which == LF_INVSTATIC_INIT); 218 boolean doesAlloc = (which == LF_NEWINVSPECIAL); 219 boolean needsReceiverCheck = (which == LF_INVINTERFACE || 220 which == LF_INVSPECIAL_IFC); 221 222 String linkerName; 223 LambdaForm.Kind kind; 224 switch (which) { 225 case LF_INVVIRTUAL: linkerName = "linkToVirtual"; kind = DIRECT_INVOKE_VIRTUAL; break; 226 case LF_INVSTATIC: linkerName = "linkToStatic"; kind = DIRECT_INVOKE_STATIC; break; 227 case LF_INVSTATIC_INIT:linkerName = "linkToStatic"; kind = DIRECT_INVOKE_STATIC_INIT; break; 228 case LF_INVSPECIAL_IFC:linkerName = "linkToSpecial"; kind = DIRECT_INVOKE_SPECIAL_IFC; break; 229 case LF_INVSPECIAL: linkerName = "linkToSpecial"; kind = DIRECT_INVOKE_SPECIAL; break; 230 case LF_INVINTERFACE: linkerName = "linkToInterface"; kind = DIRECT_INVOKE_INTERFACE; break; 231 case LF_NEWINVSPECIAL: linkerName = "linkToSpecial"; kind = DIRECT_NEW_INVOKE_SPECIAL; break; 232 default: throw new InternalError("which="+which); 233 } 234 235 MethodType mtypeWithArg = mtype.appendParameterTypes(MemberName.class); 236 if (doesAlloc) 237 mtypeWithArg = mtypeWithArg 238 .insertParameterTypes(0, Object.class) // insert newly allocated obj 239 .changeReturnType(void.class); // <init> returns void 240 MemberName linker = new MemberName(MethodHandle.class, linkerName, mtypeWithArg, REF_invokeStatic); 241 try { 242 linker = IMPL_NAMES.resolveOrFail(REF_invokeStatic, linker, null, NoSuchMethodException.class); 243 } catch (ReflectiveOperationException ex) { 244 throw newInternalError(ex); 245 } 246 final int DMH_THIS = 0; 247 final int ARG_BASE = 1; 248 final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); 249 int nameCursor = ARG_LIMIT; 250 final int NEW_OBJ = (doesAlloc ? nameCursor++ : -1); 251 final int GET_MEMBER = nameCursor++; 252 final int CHECK_RECEIVER = (needsReceiverCheck ? nameCursor++ : -1); 253 final int LINKER_CALL = nameCursor++; 254 Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType()); 255 assert(names.length == nameCursor); 256 if (doesAlloc) { 257 // names = { argx,y,z,... new C, init method } 258 names[NEW_OBJ] = new Name(getFunction(NF_allocateInstance), names[DMH_THIS]); 259 names[GET_MEMBER] = new Name(getFunction(NF_constructorMethod), names[DMH_THIS]); 260 } else if (needsInit) { 261 names[GET_MEMBER] = new Name(getFunction(NF_internalMemberNameEnsureInit), names[DMH_THIS]); 262 } else { 263 names[GET_MEMBER] = new Name(getFunction(NF_internalMemberName), names[DMH_THIS]); 264 } 265 assert(findDirectMethodHandle(names[GET_MEMBER]) == names[DMH_THIS]); 266 Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, GET_MEMBER+1, Object[].class); 267 if (needsReceiverCheck) { 268 names[CHECK_RECEIVER] = new Name(getFunction(NF_checkReceiver), names[DMH_THIS], names[ARG_BASE]); 269 outArgs[0] = names[CHECK_RECEIVER]; 270 } 271 assert(outArgs[outArgs.length-1] == names[GET_MEMBER]); // look, shifted args! 272 int result = LAST_RESULT; 273 if (doesAlloc) { 274 assert(outArgs[outArgs.length-2] == names[NEW_OBJ]); // got to move this one 275 System.arraycopy(outArgs, 0, outArgs, 1, outArgs.length-2); 276 outArgs[0] = names[NEW_OBJ]; 277 result = NEW_OBJ; 278 } 279 names[LINKER_CALL] = new Name(linker, outArgs); 280 LambdaForm lform = new LambdaForm(ARG_LIMIT, names, result, kind); 281 282 // This is a tricky bit of code. Don't send it through the LF interpreter. 283 lform.compileToBytecode(); 284 return lform; 285 } 286 287 /* assert */ static Object findDirectMethodHandle(Name name) { 288 if (name.function.equals(getFunction(NF_internalMemberName)) || 289 name.function.equals(getFunction(NF_internalMemberNameEnsureInit)) || 290 name.function.equals(getFunction(NF_constructorMethod))) { 291 assert(name.arguments.length == 1); 292 return name.arguments[0]; 293 } 294 return null; 295 } 296 297 private static void maybeCompile(LambdaForm lform, MemberName m) { 298 if (lform.vmentry == null && VerifyAccess.isSamePackage(m.getDeclaringClass(), MethodHandle.class)) 299 // Help along bootstrapping... 300 lform.compileToBytecode(); 301 } 302 303 /** Static wrapper for DirectMethodHandle.internalMemberName. */ 304 @ForceInline 305 /*non-public*/ static Object internalMemberName(Object mh) { 306 return ((DirectMethodHandle)mh).member; 307 } 308 309 /** Static wrapper for DirectMethodHandle.internalMemberName. 310 * This one also forces initialization. 311 */ 312 /*non-public*/ static Object internalMemberNameEnsureInit(Object mh) { 313 DirectMethodHandle dmh = (DirectMethodHandle)mh; 314 dmh.ensureInitialized(); 315 return dmh.member; 316 } 317 318 /*non-public*/ static 319 boolean shouldBeInitialized(MemberName member) { 320 switch (member.getReferenceKind()) { 321 case REF_invokeStatic: 322 case REF_getStatic: 323 case REF_putStatic: 324 case REF_newInvokeSpecial: 325 break; 326 default: 327 // No need to initialize the class on this kind of member. 328 return false; 329 } 330 Class<?> cls = member.getDeclaringClass(); 331 if (cls == ValueConversions.class || 332 cls == MethodHandleImpl.class || 333 cls == Invokers.class) { 334 // These guys have lots of <clinit> DMH creation but we know 335 // the MHs will not be used until the system is booted. 336 return false; 337 } 338 if (VerifyAccess.isSamePackage(MethodHandle.class, cls) || 339 VerifyAccess.isSamePackage(ValueConversions.class, cls)) { 340 // It is a system class. It is probably in the process of 341 // being initialized, but we will help it along just to be safe. 342 if (UNSAFE.shouldBeInitialized(cls)) { 343 UNSAFE.ensureClassInitialized(cls); 344 } 345 return false; 346 } 347 return UNSAFE.shouldBeInitialized(cls); 348 } 349 350 private static class EnsureInitialized extends ClassValue<WeakReference<Thread>> { 351 @Override 352 protected WeakReference<Thread> computeValue(Class<?> type) { 353 UNSAFE.ensureClassInitialized(type); 354 if (UNSAFE.shouldBeInitialized(type)) 355 // If the previous call didn't block, this can happen. 356 // We are executing inside <clinit>. 357 return new WeakReference<>(Thread.currentThread()); 358 return null; 359 } 360 static final EnsureInitialized INSTANCE = new EnsureInitialized(); 361 } 362 363 private void ensureInitialized() { 364 if (checkInitialized(member)) { 365 // The coast is clear. Delete the <clinit> barrier. 366 if (member.isField()) 367 updateForm(preparedFieldLambdaForm(member)); 368 else 369 updateForm(preparedLambdaForm(member)); 370 } 371 } 372 private static boolean checkInitialized(MemberName member) { 373 Class<?> defc = member.getDeclaringClass(); 374 WeakReference<Thread> ref = EnsureInitialized.INSTANCE.get(defc); 375 if (ref == null) { 376 return true; // the final state 377 } 378 Thread clinitThread = ref.get(); 379 // Somebody may still be running defc.<clinit>. 380 if (clinitThread == Thread.currentThread()) { 381 // If anybody is running defc.<clinit>, it is this thread. 382 if (UNSAFE.shouldBeInitialized(defc)) 383 // Yes, we are running it; keep the barrier for now. 384 return false; 385 } else { 386 // We are in a random thread. Block. 387 UNSAFE.ensureClassInitialized(defc); 388 } 389 assert(!UNSAFE.shouldBeInitialized(defc)); 390 // put it into the final state 391 EnsureInitialized.INSTANCE.remove(defc); 392 return true; 393 } 394 395 /*non-public*/ static void ensureInitialized(Object mh) { 396 ((DirectMethodHandle)mh).ensureInitialized(); 397 } 398 399 /** This subclass represents invokespecial instructions. */ 400 static class Special extends DirectMethodHandle { 401 private final Class<?> caller; 402 private Special(MethodType mtype, LambdaForm form, MemberName member, Class<?> caller) { 403 super(mtype, form, member); 404 this.caller = caller; 405 } 406 @Override 407 boolean isInvokeSpecial() { 408 return true; 409 } 410 @Override 411 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 412 return new Special(mt, lf, member, caller); 413 } 414 Object checkReceiver(Object recv) { 415 if (!caller.isInstance(recv)) { 416 String msg = String.format("Receiver class %s is not a subclass of caller class %s", 417 recv.getClass().getName(), caller.getName()); 418 throw new IncompatibleClassChangeError(msg); 419 } 420 return recv; 421 } 422 } 423 424 /** This subclass represents invokeinterface instructions. */ 425 static class Interface extends DirectMethodHandle { 426 private final Class<?> refc; 427 private Interface(MethodType mtype, LambdaForm form, MemberName member, Class<?> refc) { 428 super(mtype, form, member); 429 assert refc.isInterface() : refc; 430 this.refc = refc; 431 } 432 @Override 433 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 434 return new Interface(mt, lf, member, refc); 435 } 436 @Override 437 Object checkReceiver(Object recv) { 438 if (!refc.isInstance(recv)) { 439 String msg = String.format("Receiver class %s does not implement the requested interface %s", 440 recv.getClass().getName(), refc.getName()); 441 throw new IncompatibleClassChangeError(msg); 442 } 443 return recv; 444 } 445 } 446 447 /** Used for interface receiver type checks, by Interface and Special modes. */ 448 Object checkReceiver(Object recv) { 449 throw new InternalError("Should only be invoked on a subclass"); 450 } 451 452 453 /** This subclass handles constructor references. */ 454 static class Constructor extends DirectMethodHandle { 455 final MemberName initMethod; 456 final Class<?> instanceClass; 457 458 private Constructor(MethodType mtype, LambdaForm form, MemberName constructor, 459 MemberName initMethod, Class<?> instanceClass) { 460 super(mtype, form, constructor); 461 this.initMethod = initMethod; 462 this.instanceClass = instanceClass; 463 assert(initMethod.isResolved()); 464 } 465 @Override 466 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 467 return new Constructor(mt, lf, member, initMethod, instanceClass); 468 } 469 } 470 471 /*non-public*/ static Object constructorMethod(Object mh) { 472 Constructor dmh = (Constructor)mh; 473 return dmh.initMethod; 474 } 475 476 /*non-public*/ static Object allocateInstance(Object mh) throws InstantiationException { 477 Constructor dmh = (Constructor)mh; 478 return UNSAFE.allocateInstance(dmh.instanceClass); 479 } 480 481 /** This subclass handles non-static field references. */ 482 static class Accessor extends DirectMethodHandle { 483 final Class<?> fieldType; 484 final int fieldOffset; 485 private Accessor(MethodType mtype, LambdaForm form, MemberName member, 486 int fieldOffset) { 487 super(mtype, form, member); 488 this.fieldType = member.getFieldType(); 489 this.fieldOffset = fieldOffset; 490 } 491 492 @Override Object checkCast(Object obj) { 493 return fieldType.cast(obj); 494 } 495 @Override 496 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 497 return new Accessor(mt, lf, member, fieldOffset); 498 } 499 } 500 501 @ForceInline 502 /*non-public*/ static long fieldOffset(Object accessorObj) { 503 // Note: We return a long because that is what Unsafe.getObject likes. 504 // We store a plain int because it is more compact. 505 return ((Accessor)accessorObj).fieldOffset; 506 } 507 508 @ForceInline 509 /*non-public*/ static Object checkBase(Object obj) { 510 // Note that the object's class has already been verified, 511 // since the parameter type of the Accessor method handle 512 // is either member.getDeclaringClass or a subclass. 513 // This was verified in DirectMethodHandle.make. 514 // Therefore, the only remaining check is for null. 515 // Since this check is *not* guaranteed by Unsafe.getInt 516 // and its siblings, we need to make an explicit one here. 517 return Objects.requireNonNull(obj); 518 } 519 520 /** This subclass handles static field references. */ 521 static class StaticAccessor extends DirectMethodHandle { 522 private final Class<?> fieldType; 523 private final Object staticBase; 524 private final long staticOffset; 525 526 private StaticAccessor(MethodType mtype, LambdaForm form, MemberName member, 527 Object staticBase, long staticOffset) { 528 super(mtype, form, member); 529 this.fieldType = member.getFieldType(); 530 this.staticBase = staticBase; 531 this.staticOffset = staticOffset; 532 } 533 534 @Override Object checkCast(Object obj) { 535 return fieldType.cast(obj); 536 } 537 @Override 538 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 539 return new StaticAccessor(mt, lf, member, staticBase, staticOffset); 540 } 541 } 542 543 @ForceInline 544 /*non-public*/ static Object nullCheck(Object obj) { 545 return Objects.requireNonNull(obj); 546 } 547 548 @ForceInline 549 /*non-public*/ static Object staticBase(Object accessorObj) { 550 return ((StaticAccessor)accessorObj).staticBase; 551 } 552 553 @ForceInline 554 /*non-public*/ static long staticOffset(Object accessorObj) { 555 return ((StaticAccessor)accessorObj).staticOffset; 556 } 557 558 @ForceInline 559 /*non-public*/ static Object checkCast(Object mh, Object obj) { 560 return ((DirectMethodHandle) mh).checkCast(obj); 561 } 562 563 Object checkCast(Object obj) { 564 return member.getReturnType().cast(obj); 565 } 566 567 // Caching machinery for field accessors: 568 static final byte 569 AF_GETFIELD = 0, 570 AF_PUTFIELD = 1, 571 AF_GETSTATIC = 2, 572 AF_PUTSTATIC = 3, 573 AF_GETSTATIC_INIT = 4, 574 AF_PUTSTATIC_INIT = 5, 575 AF_LIMIT = 6; 576 // Enumerate the different field kinds using Wrapper, 577 // with an extra case added for checked references. 578 static final int 579 FT_LAST_WRAPPER = Wrapper.COUNT-1, 580 FT_UNCHECKED_REF = Wrapper.OBJECT.ordinal(), 581 FT_CHECKED_REF = FT_LAST_WRAPPER+1, 582 FT_LIMIT = FT_LAST_WRAPPER+2; 583 private static int afIndex(byte formOp, boolean isVolatile, int ftypeKind) { 584 return ((formOp * FT_LIMIT * 2) 585 + (isVolatile ? FT_LIMIT : 0) 586 + ftypeKind); 587 } 588 @Stable 589 private static final LambdaForm[] ACCESSOR_FORMS 590 = new LambdaForm[afIndex(AF_LIMIT, false, 0)]; 591 static int ftypeKind(Class<?> ftype) { 592 if (ftype.isPrimitive()) 593 return Wrapper.forPrimitiveType(ftype).ordinal(); 594 else if (VerifyType.isNullReferenceConversion(Object.class, ftype)) 595 return FT_UNCHECKED_REF; 596 else 597 return FT_CHECKED_REF; 598 } 599 600 /** 601 * Create a LF which can access the given field. 602 * Cache and share this structure among all fields with 603 * the same basicType and refKind. 604 */ 605 private static LambdaForm preparedFieldLambdaForm(MemberName m) { 606 Class<?> ftype = m.getFieldType(); 607 boolean isVolatile = m.isVolatile(); 608 byte formOp; 609 switch (m.getReferenceKind()) { 610 case REF_getField: formOp = AF_GETFIELD; break; 611 case REF_putField: formOp = AF_PUTFIELD; break; 612 case REF_getStatic: formOp = AF_GETSTATIC; break; 613 case REF_putStatic: formOp = AF_PUTSTATIC; break; 614 default: throw new InternalError(m.toString()); 615 } 616 if (shouldBeInitialized(m)) { 617 // precompute the barrier-free version: 618 preparedFieldLambdaForm(formOp, isVolatile, ftype); 619 assert((AF_GETSTATIC_INIT - AF_GETSTATIC) == 620 (AF_PUTSTATIC_INIT - AF_PUTSTATIC)); 621 formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC); 622 } 623 LambdaForm lform = preparedFieldLambdaForm(formOp, isVolatile, ftype); 624 maybeCompile(lform, m); 625 assert(lform.methodType().dropParameterTypes(0, 1) 626 .equals(m.getInvocationType().basicType())) 627 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); 628 return lform; 629 } 630 private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile, Class<?> ftype) { 631 int ftypeKind = ftypeKind(ftype); 632 int afIndex = afIndex(formOp, isVolatile, ftypeKind); 633 LambdaForm lform = ACCESSOR_FORMS[afIndex]; 634 if (lform != null) return lform; 635 lform = makePreparedFieldLambdaForm(formOp, isVolatile, ftypeKind); 636 ACCESSOR_FORMS[afIndex] = lform; // don't bother with a CAS 637 return lform; 638 } 639 640 private static final Wrapper[] ALL_WRAPPERS = Wrapper.values(); 641 642 private static Kind getFieldKind(boolean isGetter, boolean isVolatile, Wrapper wrapper) { 643 if (isGetter) { 644 if (isVolatile) { 645 switch (wrapper) { 646 case BOOLEAN: return GET_BOOLEAN_VOLATILE; 647 case BYTE: return GET_BYTE_VOLATILE; 648 case SHORT: return GET_SHORT_VOLATILE; 649 case CHAR: return GET_CHAR_VOLATILE; 650 case INT: return GET_INT_VOLATILE; 651 case LONG: return GET_LONG_VOLATILE; 652 case FLOAT: return GET_FLOAT_VOLATILE; 653 case DOUBLE: return GET_DOUBLE_VOLATILE; 654 case OBJECT: return GET_OBJECT_VOLATILE; 655 } 656 } else { 657 switch (wrapper) { 658 case BOOLEAN: return GET_BOOLEAN; 659 case BYTE: return GET_BYTE; 660 case SHORT: return GET_SHORT; 661 case CHAR: return GET_CHAR; 662 case INT: return GET_INT; 663 case LONG: return GET_LONG; 664 case FLOAT: return GET_FLOAT; 665 case DOUBLE: return GET_DOUBLE; 666 case OBJECT: return GET_OBJECT; 667 } 668 } 669 } else { 670 if (isVolatile) { 671 switch (wrapper) { 672 case BOOLEAN: return PUT_BOOLEAN_VOLATILE; 673 case BYTE: return PUT_BYTE_VOLATILE; 674 case SHORT: return PUT_SHORT_VOLATILE; 675 case CHAR: return PUT_CHAR_VOLATILE; 676 case INT: return PUT_INT_VOLATILE; 677 case LONG: return PUT_LONG_VOLATILE; 678 case FLOAT: return PUT_FLOAT_VOLATILE; 679 case DOUBLE: return PUT_DOUBLE_VOLATILE; 680 case OBJECT: return PUT_OBJECT_VOLATILE; 681 } 682 } else { 683 switch (wrapper) { 684 case BOOLEAN: return PUT_BOOLEAN; 685 case BYTE: return PUT_BYTE; 686 case SHORT: return PUT_SHORT; 687 case CHAR: return PUT_CHAR; 688 case INT: return PUT_INT; 689 case LONG: return PUT_LONG; 690 case FLOAT: return PUT_FLOAT; 691 case DOUBLE: return PUT_DOUBLE; 692 case OBJECT: return PUT_OBJECT; 693 } 694 } 695 } 696 throw new AssertionError("Invalid arguments"); 697 } 698 699 static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) { 700 boolean isGetter = (formOp & 1) == (AF_GETFIELD & 1); 701 boolean isStatic = (formOp >= AF_GETSTATIC); 702 boolean needsInit = (formOp >= AF_GETSTATIC_INIT); 703 boolean needsCast = (ftypeKind == FT_CHECKED_REF); 704 Wrapper fw = (needsCast ? Wrapper.OBJECT : ALL_WRAPPERS[ftypeKind]); 705 Class<?> ft = fw.primitiveType(); 706 assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind); 707 708 // getObject, putIntVolatile, etc. 709 Kind kind = getFieldKind(isGetter, isVolatile, fw); 710 711 MethodType linkerType; 712 if (isGetter) 713 linkerType = MethodType.methodType(ft, Object.class, long.class); 714 else 715 linkerType = MethodType.methodType(void.class, Object.class, long.class, ft); 716 MemberName linker = new MemberName(Unsafe.class, kind.methodName, linkerType, REF_invokeVirtual); 717 try { 718 linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class); 719 } catch (ReflectiveOperationException ex) { 720 throw newInternalError(ex); 721 } 722 723 // What is the external type of the lambda form? 724 MethodType mtype; 725 if (isGetter) 726 mtype = MethodType.methodType(ft); 727 else 728 mtype = MethodType.methodType(void.class, ft); 729 mtype = mtype.basicType(); // erase short to int, etc. 730 if (!isStatic) 731 mtype = mtype.insertParameterTypes(0, Object.class); 732 final int DMH_THIS = 0; 733 final int ARG_BASE = 1; 734 final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); 735 // if this is for non-static access, the base pointer is stored at this index: 736 final int OBJ_BASE = isStatic ? -1 : ARG_BASE; 737 // if this is for write access, the value to be written is stored at this index: 738 final int SET_VALUE = isGetter ? -1 : ARG_LIMIT - 1; 739 int nameCursor = ARG_LIMIT; 740 final int F_HOLDER = (isStatic ? nameCursor++ : -1); // static base if any 741 final int F_OFFSET = nameCursor++; // Either static offset or field offset. 742 final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1); 743 final int U_HOLDER = nameCursor++; // UNSAFE holder 744 final int INIT_BAR = (needsInit ? nameCursor++ : -1); 745 final int PRE_CAST = (needsCast && !isGetter ? nameCursor++ : -1); 746 final int LINKER_CALL = nameCursor++; 747 final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1); 748 final int RESULT = nameCursor-1; // either the call or the cast 749 Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType()); 750 if (needsInit) 751 names[INIT_BAR] = new Name(getFunction(NF_ensureInitialized), names[DMH_THIS]); 752 if (needsCast && !isGetter) 753 names[PRE_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[SET_VALUE]); 754 Object[] outArgs = new Object[1 + linkerType.parameterCount()]; 755 assert(outArgs.length == (isGetter ? 3 : 4)); 756 outArgs[0] = names[U_HOLDER] = new Name(getFunction(NF_UNSAFE)); 757 if (isStatic) { 758 outArgs[1] = names[F_HOLDER] = new Name(getFunction(NF_staticBase), names[DMH_THIS]); 759 outArgs[2] = names[F_OFFSET] = new Name(getFunction(NF_staticOffset), names[DMH_THIS]); 760 } else { 761 outArgs[1] = names[OBJ_CHECK] = new Name(getFunction(NF_checkBase), names[OBJ_BASE]); 762 outArgs[2] = names[F_OFFSET] = new Name(getFunction(NF_fieldOffset), names[DMH_THIS]); 763 } 764 if (!isGetter) { 765 outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]); 766 } 767 for (Object a : outArgs) assert(a != null); 768 names[LINKER_CALL] = new Name(linker, outArgs); 769 if (needsCast && isGetter) 770 names[POST_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[LINKER_CALL]); 771 for (Name n : names) assert(n != null); 772 773 LambdaForm form; 774 if (needsCast || needsInit) { 775 // can't use the pre-generated form when casting and/or initializing 776 form = new LambdaForm(ARG_LIMIT, names, RESULT); 777 } else { 778 form = new LambdaForm(ARG_LIMIT, names, RESULT, kind); 779 } 780 781 if (LambdaForm.debugNames()) { 782 // add some detail to the lambdaForm debugname, 783 // significant only for debugging 784 StringBuilder nameBuilder = new StringBuilder(kind.methodName); 785 if (isStatic) { 786 nameBuilder.append("Static"); 787 } else { 788 nameBuilder.append("Field"); 789 } 790 if (needsCast) { 791 nameBuilder.append("Cast"); 792 } 793 if (needsInit) { 794 nameBuilder.append("Init"); 795 } 796 LambdaForm.associateWithDebugName(form, nameBuilder.toString()); 797 } 798 return form; 799 } 800 801 /** 802 * Pre-initialized NamedFunctions for bootstrapping purposes. 803 */ 804 static final byte NF_internalMemberName = 0, 805 NF_internalMemberNameEnsureInit = 1, 806 NF_ensureInitialized = 2, 807 NF_fieldOffset = 3, 808 NF_checkBase = 4, 809 NF_staticBase = 5, 810 NF_staticOffset = 6, 811 NF_checkCast = 7, 812 NF_allocateInstance = 8, 813 NF_constructorMethod = 9, 814 NF_UNSAFE = 10, 815 NF_checkReceiver = 11, 816 NF_LIMIT = 12; 817 818 private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT]; 819 820 private static NamedFunction getFunction(byte func) { 821 NamedFunction nf = NFS[func]; 822 if (nf != null) { 823 return nf; 824 } 825 // Each nf must be statically invocable or we get tied up in our bootstraps. 826 nf = NFS[func] = createFunction(func); 827 assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf)); 828 return nf; 829 } 830 831 private static final MethodType OBJ_OBJ_TYPE = MethodType.methodType(Object.class, Object.class); 832 833 private static final MethodType LONG_OBJ_TYPE = MethodType.methodType(long.class, Object.class); 834 835 private static NamedFunction createFunction(byte func) { 836 try { 837 switch (func) { 838 case NF_internalMemberName: 839 return getNamedFunction("internalMemberName", OBJ_OBJ_TYPE); 840 case NF_internalMemberNameEnsureInit: 841 return getNamedFunction("internalMemberNameEnsureInit", OBJ_OBJ_TYPE); 842 case NF_ensureInitialized: 843 return getNamedFunction("ensureInitialized", MethodType.methodType(void.class, Object.class)); 844 case NF_fieldOffset: 845 return getNamedFunction("fieldOffset", LONG_OBJ_TYPE); 846 case NF_checkBase: 847 return getNamedFunction("checkBase", OBJ_OBJ_TYPE); 848 case NF_staticBase: 849 return getNamedFunction("staticBase", OBJ_OBJ_TYPE); 850 case NF_staticOffset: 851 return getNamedFunction("staticOffset", LONG_OBJ_TYPE); 852 case NF_checkCast: 853 return getNamedFunction("checkCast", MethodType.methodType(Object.class, Object.class, Object.class)); 854 case NF_allocateInstance: 855 return getNamedFunction("allocateInstance", OBJ_OBJ_TYPE); 856 case NF_constructorMethod: 857 return getNamedFunction("constructorMethod", OBJ_OBJ_TYPE); 858 case NF_UNSAFE: 859 MemberName member = new MemberName(MethodHandleStatics.class, "UNSAFE", Unsafe.class, REF_getField); 860 return new NamedFunction( 861 MemberName.getFactory() 862 .resolveOrFail(REF_getField, member, DirectMethodHandle.class, NoSuchMethodException.class)); 863 case NF_checkReceiver: 864 member = new MemberName(DirectMethodHandle.class, "checkReceiver", OBJ_OBJ_TYPE, REF_invokeVirtual); 865 return new NamedFunction( 866 MemberName.getFactory() 867 .resolveOrFail(REF_invokeVirtual, member, DirectMethodHandle.class, NoSuchMethodException.class)); 868 default: 869 throw newInternalError("Unknown function: " + func); 870 } 871 } catch (ReflectiveOperationException ex) { 872 throw newInternalError(ex); 873 } 874 } 875 876 private static NamedFunction getNamedFunction(String name, MethodType type) 877 throws ReflectiveOperationException 878 { 879 MemberName member = new MemberName(DirectMethodHandle.class, name, type, REF_invokeStatic); 880 return new NamedFunction( 881 MemberName.getFactory() 882 .resolveOrFail(REF_invokeStatic, member, DirectMethodHandle.class, NoSuchMethodException.class)); 883 } 884 885 static { 886 // The Holder class will contain pre-generated DirectMethodHandles resolved 887 // speculatively using MemberName.getFactory().resolveOrNull. However, that 888 // doesn't initialize the class, which subtly breaks inlining etc. By forcing 889 // initialization of the Holder class we avoid these issues. 890 UNSAFE.ensureClassInitialized(Holder.class); 891 } 892 893 /* Placeholder class for DirectMethodHandles generated ahead of time */ 894 final class Holder {} 895 }