1 /* 2 * Copyright (c) 2008, 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 26 package java.lang.invoke; 27 28 import sun.invoke.util.Wrapper; 29 import java.lang.ref.SoftReference; 30 import static java.lang.invoke.MethodHandleStatics.*; 31 32 /** 33 * Shared information for a group of method types, which differ 34 * only by reference types, and therefore share a common erasure 35 * and wrapping. 36 * <p> 37 * For an empirical discussion of the structure of method types, 38 * see <a href="http://groups.google.com/group/jvm-languages/browse_thread/thread/ac9308ae74da9b7e/"> 39 * the thread "Avoiding Boxing" on jvm-languages</a>. 40 * There are approximately 2000 distinct erased method types in the JDK. 41 * There are a little over 10 times that number of unerased types. 42 * No more than half of these are likely to be loaded at once. 43 * @author John Rose 44 */ 45 final class MethodTypeForm { 46 final int[] argToSlotTable, slotToArgTable; 47 final long argCounts; // packed slot & value counts 48 final long primCounts; // packed prim & double counts 49 final MethodType erasedType; // the canonical erasure 50 final MethodType basicType; // the canonical erasure, with primitives simplified 51 52 // BEGIN Android-removed: Cached adaptors / lambda forms. 53 // The upstream caching mechanism will not work on Android because of fundamental differences 54 // in the Android runtime/implementation of MethodHandle. LambdaForm is not supported on 55 // Android for similar reasons. 56 /* 57 // Cached adapter information: 58 @Stable final SoftReference<MethodHandle>[] methodHandles; 59 // Indexes into methodHandles: 60 static final int 61 MH_BASIC_INV = 0, // cached instance of MH.invokeBasic 62 MH_NF_INV = 1, // cached helper for LF.NamedFunction 63 MH_UNINIT_CS = 2, // uninitialized call site 64 MH_LIMIT = 3; 65 66 // Cached lambda form information, for basic types only: 67 final @Stable SoftReference<LambdaForm>[] lambdaForms; 68 // Indexes into lambdaForms: 69 static final int 70 LF_INVVIRTUAL = 0, // DMH invokeVirtual 71 LF_INVSTATIC = 1, 72 LF_INVSPECIAL = 2, 73 LF_NEWINVSPECIAL = 3, 74 LF_INVINTERFACE = 4, 75 LF_INVSTATIC_INIT = 5, // DMH invokeStatic with <clinit> barrier 76 LF_INTERPRET = 6, // LF interpreter 77 LF_REBIND = 7, // BoundMethodHandle 78 LF_DELEGATE = 8, // DelegatingMethodHandle 79 LF_DELEGATE_BLOCK_INLINING = 9, // Counting DelegatingMethodHandle w/ @DontInline 80 LF_EX_LINKER = 10, // invokeExact_MT (for invokehandle) 81 LF_EX_INVOKER = 11, // MHs.invokeExact 82 LF_GEN_LINKER = 12, // generic invoke_MT (for invokehandle) 83 LF_GEN_INVOKER = 13, // generic MHs.invoke 84 LF_CS_LINKER = 14, // linkToCallSite_CS 85 LF_MH_LINKER = 15, // linkToCallSite_MH 86 LF_GWC = 16, // guardWithCatch (catchException) 87 LF_GWT = 17, // guardWithTest 88 LF_LIMIT = 18; 89 */ 90 // END Android-removed: Cached adaptors / lambda forms. 91 92 /** Return the type corresponding uniquely (1-1) to this MT-form. 93 * It might have any primitive returns or arguments, but will have no references except Object. 94 */ erasedType()95 public MethodType erasedType() { 96 return erasedType; 97 } 98 99 /** Return the basic type derived from the erased type of this MT-form. 100 * A basic type is erased (all references Object) and also has all primitive 101 * types (except int, long, float, double, void) normalized to int. 102 * Such basic types correspond to low-level JVM calling sequences. 103 */ basicType()104 public MethodType basicType() { 105 return basicType; 106 } 107 assertIsBasicType()108 private boolean assertIsBasicType() { 109 // primitives must be flattened also 110 assert(erasedType == basicType) 111 : "erasedType: " + erasedType + " != basicType: " + basicType; 112 return true; 113 } 114 115 // BEGIN Android-removed: Cached adaptors / lambda forms. 116 /* 117 public MethodHandle cachedMethodHandle(int which) { 118 assert(assertIsBasicType()); 119 SoftReference<MethodHandle> entry = methodHandles[which]; 120 return (entry != null) ? entry.get() : null; 121 } 122 123 synchronized public MethodHandle setCachedMethodHandle(int which, MethodHandle mh) { 124 // Simulate a CAS, to avoid racy duplication of results. 125 SoftReference<MethodHandle> entry = methodHandles[which]; 126 if (entry != null) { 127 MethodHandle prev = entry.get(); 128 if (prev != null) { 129 return prev; 130 } 131 } 132 methodHandles[which] = new SoftReference<>(mh); 133 return mh; 134 } 135 136 public LambdaForm cachedLambdaForm(int which) { 137 assert(assertIsBasicType()); 138 SoftReference<LambdaForm> entry = lambdaForms[which]; 139 return (entry != null) ? entry.get() : null; 140 } 141 142 synchronized public LambdaForm setCachedLambdaForm(int which, LambdaForm form) { 143 // Simulate a CAS, to avoid racy duplication of results. 144 SoftReference<LambdaForm> entry = lambdaForms[which]; 145 if (entry != null) { 146 LambdaForm prev = entry.get(); 147 if (prev != null) { 148 return prev; 149 } 150 } 151 lambdaForms[which] = new SoftReference<>(form); 152 return form; 153 } 154 */ 155 // END Android-removed: Cached adaptors / lambda forms. 156 157 /** 158 * Build an MTF for a given type, which must have all references erased to Object. 159 * This MTF will stand for that type and all un-erased variations. 160 * Eagerly compute some basic properties of the type, common to all variations. 161 */ 162 @SuppressWarnings({"rawtypes", "unchecked"}) MethodTypeForm(MethodType erasedType)163 protected MethodTypeForm(MethodType erasedType) { 164 this.erasedType = erasedType; 165 166 Class<?>[] ptypes = erasedType.ptypes(); 167 int ptypeCount = ptypes.length; 168 int pslotCount = ptypeCount; // temp. estimate 169 int rtypeCount = 1; // temp. estimate 170 int rslotCount = 1; // temp. estimate 171 172 int[] argToSlotTab = null, slotToArgTab = null; 173 174 // Walk the argument types, looking for primitives. 175 int pac = 0, lac = 0, prc = 0, lrc = 0; 176 Class<?>[] epts = ptypes; 177 Class<?>[] bpts = epts; 178 for (int i = 0; i < epts.length; i++) { 179 Class<?> pt = epts[i]; 180 if (pt != Object.class) { 181 ++pac; 182 Wrapper w = Wrapper.forPrimitiveType(pt); 183 if (w.isDoubleWord()) ++lac; 184 if (w.isSubwordOrInt() && pt != int.class) { 185 if (bpts == epts) 186 bpts = bpts.clone(); 187 bpts[i] = int.class; 188 } 189 } 190 } 191 pslotCount += lac; // #slots = #args + #longs 192 Class<?> rt = erasedType.returnType(); 193 Class<?> bt = rt; 194 if (rt != Object.class) { 195 ++prc; // even void.class counts as a prim here 196 Wrapper w = Wrapper.forPrimitiveType(rt); 197 if (w.isDoubleWord()) ++lrc; 198 if (w.isSubwordOrInt() && rt != int.class) 199 bt = int.class; 200 // adjust #slots, #args 201 if (rt == void.class) 202 rtypeCount = rslotCount = 0; 203 else 204 rslotCount += lrc; 205 } 206 if (epts == bpts && bt == rt) { 207 this.basicType = erasedType; 208 } else { 209 this.basicType = MethodType.makeImpl(bt, bpts, true); 210 // fill in rest of data from the basic type: 211 MethodTypeForm that = this.basicType.form(); 212 assert(this != that); 213 this.primCounts = that.primCounts; 214 this.argCounts = that.argCounts; 215 this.argToSlotTable = that.argToSlotTable; 216 this.slotToArgTable = that.slotToArgTable; 217 // Android-removed: Cached adaptors / lambda forms. 218 // this.methodHandles = null; 219 // this.lambdaForms = null; 220 return; 221 } 222 if (lac != 0) { 223 int slot = ptypeCount + lac; 224 slotToArgTab = new int[slot+1]; 225 argToSlotTab = new int[1+ptypeCount]; 226 argToSlotTab[0] = slot; // argument "-1" is past end of slots 227 for (int i = 0; i < epts.length; i++) { 228 Class<?> pt = epts[i]; 229 Wrapper w = Wrapper.forBasicType(pt); 230 if (w.isDoubleWord()) --slot; 231 --slot; 232 slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note 233 argToSlotTab[1+i] = slot; 234 } 235 assert(slot == 0); // filled the table 236 } else if (pac != 0) { 237 // have primitives but no long primitives; share slot counts with generic 238 assert(ptypeCount == pslotCount); 239 MethodTypeForm that = MethodType.genericMethodType(ptypeCount).form(); 240 assert(this != that); 241 slotToArgTab = that.slotToArgTable; 242 argToSlotTab = that.argToSlotTable; 243 } else { 244 int slot = ptypeCount; // first arg is deepest in stack 245 slotToArgTab = new int[slot+1]; 246 argToSlotTab = new int[1+ptypeCount]; 247 argToSlotTab[0] = slot; // argument "-1" is past end of slots 248 for (int i = 0; i < ptypeCount; i++) { 249 --slot; 250 slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note 251 argToSlotTab[1+i] = slot; 252 } 253 } 254 this.primCounts = pack(lrc, prc, lac, pac); 255 this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount); 256 this.argToSlotTable = argToSlotTab; 257 this.slotToArgTable = slotToArgTab; 258 259 if (pslotCount >= 256) throw newIllegalArgumentException("too many arguments"); 260 261 // Initialize caches, but only for basic types 262 assert(basicType == erasedType); 263 // Android-removed: Cached adaptors / lambda forms. 264 // this.lambdaForms = new SoftReference[LF_LIMIT]; 265 // this.methodHandles = new SoftReference[MH_LIMIT]; 266 } 267 pack(int a, int b, int c, int d)268 private static long pack(int a, int b, int c, int d) { 269 assert(((a|b|c|d) & ~0xFFFF) == 0); 270 long hw = ((a << 16) | b), lw = ((c << 16) | d); 271 return (hw << 32) | lw; 272 } unpack(long packed, int word)273 private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d 274 assert(word <= 3); 275 return (char)(packed >> ((3-word) * 16)); 276 } 277 parameterCount()278 public int parameterCount() { // # outgoing values 279 return unpack(argCounts, 3); 280 } parameterSlotCount()281 public int parameterSlotCount() { // # outgoing interpreter slots 282 return unpack(argCounts, 2); 283 } returnCount()284 public int returnCount() { // = 0 (V), or 1 285 return unpack(argCounts, 1); 286 } returnSlotCount()287 public int returnSlotCount() { // = 0 (V), 2 (J/D), or 1 288 return unpack(argCounts, 0); 289 } primitiveParameterCount()290 public int primitiveParameterCount() { 291 return unpack(primCounts, 3); 292 } longPrimitiveParameterCount()293 public int longPrimitiveParameterCount() { 294 return unpack(primCounts, 2); 295 } primitiveReturnCount()296 public int primitiveReturnCount() { // = 0 (obj), or 1 297 return unpack(primCounts, 1); 298 } longPrimitiveReturnCount()299 public int longPrimitiveReturnCount() { // = 1 (J/D), or 0 300 return unpack(primCounts, 0); 301 } hasPrimitives()302 public boolean hasPrimitives() { 303 return primCounts != 0; 304 } hasNonVoidPrimitives()305 public boolean hasNonVoidPrimitives() { 306 if (primCounts == 0) return false; 307 if (primitiveParameterCount() != 0) return true; 308 return (primitiveReturnCount() != 0 && returnCount() != 0); 309 } hasLongPrimitives()310 public boolean hasLongPrimitives() { 311 return (longPrimitiveParameterCount() | longPrimitiveReturnCount()) != 0; 312 } parameterToArgSlot(int i)313 public int parameterToArgSlot(int i) { 314 return argToSlotTable[1+i]; 315 } argSlotToParameter(int argSlot)316 public int argSlotToParameter(int argSlot) { 317 // Note: Empty slots are represented by zero in this table. 318 // Valid arguments slots contain incremented entries, so as to be non-zero. 319 // We return -1 the caller to mean an empty slot. 320 return slotToArgTable[argSlot] - 1; 321 } 322 findForm(MethodType mt)323 static MethodTypeForm findForm(MethodType mt) { 324 MethodType erased = canonicalize(mt, ERASE, ERASE); 325 if (erased == null) { 326 // It is already erased. Make a new MethodTypeForm. 327 return new MethodTypeForm(mt); 328 } else { 329 // Share the MethodTypeForm with the erased version. 330 return erased.form(); 331 } 332 } 333 334 /** Codes for {@link #canonicalize(java.lang.Class, int)}. 335 * ERASE means change every reference to {@code Object}. 336 * WRAP means convert primitives (including {@code void} to their 337 * corresponding wrapper types. UNWRAP means the reverse of WRAP. 338 * INTS means convert all non-void primitive types to int or long, 339 * according to size. LONGS means convert all non-void primitives 340 * to long, regardless of size. RAW_RETURN means convert a type 341 * (assumed to be a return type) to int if it is smaller than an int, 342 * or if it is void. 343 */ 344 public static final int NO_CHANGE = 0, ERASE = 1, WRAP = 2, UNWRAP = 3, INTS = 4, LONGS = 5, RAW_RETURN = 6; 345 346 /** Canonicalize the types in the given method type. 347 * If any types change, intern the new type, and return it. 348 * Otherwise return null. 349 */ canonicalize(MethodType mt, int howRet, int howArgs)350 public static MethodType canonicalize(MethodType mt, int howRet, int howArgs) { 351 Class<?>[] ptypes = mt.ptypes(); 352 Class<?>[] ptc = MethodTypeForm.canonicalizeAll(ptypes, howArgs); 353 Class<?> rtype = mt.returnType(); 354 Class<?> rtc = MethodTypeForm.canonicalize(rtype, howRet); 355 if (ptc == null && rtc == null) { 356 // It is already canonical. 357 return null; 358 } 359 // Find the erased version of the method type: 360 if (rtc == null) rtc = rtype; 361 if (ptc == null) ptc = ptypes; 362 return MethodType.makeImpl(rtc, ptc, true); 363 } 364 365 /** Canonicalize the given return or param type. 366 * Return null if the type is already canonicalized. 367 */ canonicalize(Class<?> t, int how)368 static Class<?> canonicalize(Class<?> t, int how) { 369 Class<?> ct; 370 if (t == Object.class) { 371 // no change, ever 372 } else if (!t.isPrimitive()) { 373 switch (how) { 374 case UNWRAP: 375 ct = Wrapper.asPrimitiveType(t); 376 if (ct != t) return ct; 377 break; 378 case RAW_RETURN: 379 case ERASE: 380 return Object.class; 381 } 382 } else if (t == void.class) { 383 // no change, usually 384 switch (how) { 385 case RAW_RETURN: 386 return int.class; 387 case WRAP: 388 return Void.class; 389 } 390 } else { 391 // non-void primitive 392 switch (how) { 393 case WRAP: 394 return Wrapper.asWrapperType(t); 395 case INTS: 396 if (t == int.class || t == long.class) 397 return null; // no change 398 if (t == double.class) 399 return long.class; 400 return int.class; 401 case LONGS: 402 if (t == long.class) 403 return null; // no change 404 return long.class; 405 case RAW_RETURN: 406 if (t == int.class || t == long.class || 407 t == float.class || t == double.class) 408 return null; // no change 409 // everything else returns as an int 410 return int.class; 411 } 412 } 413 // no change; return null to signify 414 return null; 415 } 416 417 /** Canonicalize each param type in the given array. 418 * Return null if all types are already canonicalized. 419 */ canonicalizeAll(Class<?>[] ts, int how)420 static Class<?>[] canonicalizeAll(Class<?>[] ts, int how) { 421 Class<?>[] cs = null; 422 for (int imax = ts.length, i = 0; i < imax; i++) { 423 Class<?> c = canonicalize(ts[i], how); 424 if (c == void.class) 425 c = null; // a Void parameter was unwrapped to void; ignore 426 if (c != null) { 427 if (cs == null) 428 cs = ts.clone(); 429 cs[i] = c; 430 } 431 } 432 return cs; 433 } 434 435 @Override toString()436 public String toString() { 437 return "Form"+erasedType; 438 } 439 } 440