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