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 int vmslots; // total number of parameter slots 51 final MethodType erasedType; // the canonical erasure 52 final MethodType basicType; // the canonical erasure, with primitives simplified 53 54 // Cached adapter information: 55 @Stable String typeString; // argument type signature characters 56 @Stable MethodHandle genericInvoker; // JVM hook for inexact invoke 57 @Stable MethodHandle basicInvoker; // cached instance of MH.invokeBasic 58 @Stable MethodHandle namedFunctionInvoker; // cached helper for LF.NamedFunction 59 60 // Cached lambda form information, for basic types only: 61 final @Stable LambdaForm[] lambdaForms; 62 // Indexes into lambdaForms: 63 static final int 64 LF_INVVIRTUAL = 0, // DMH invokeVirtual 65 LF_INVSTATIC = 1, 66 LF_INVSPECIAL = 2, 67 LF_NEWINVSPECIAL = 3, 68 LF_INVINTERFACE = 4, 69 LF_INVSTATIC_INIT = 5, // DMH invokeStatic with <clinit> barrier 70 LF_INTERPRET = 6, // LF interpreter 71 LF_COUNTER = 7, // CMH wrapper 72 LF_REINVOKE = 8, // other wrapper 73 LF_EX_LINKER = 9, // invokeExact_MT 74 LF_EX_INVOKER = 10, // invokeExact MH 75 LF_GEN_LINKER = 11, 76 LF_GEN_INVOKER = 12, 77 LF_CS_LINKER = 13, // linkToCallSite_CS 78 LF_MH_LINKER = 14, // linkToCallSite_MH 79 LF_GWC = 15, 80 LF_LIMIT = 16; 81 82 public MethodType erasedType() { 83 return erasedType; 84 } 85 86 public MethodType basicType() { 87 return basicType; 88 } 89 90 public LambdaForm cachedLambdaForm(int which) { 91 return lambdaForms[which]; 92 } 93 94 public LambdaForm setCachedLambdaForm(int which, LambdaForm form) { 95 // Should we perform some sort of CAS, to avoid racy duplication? 96 return lambdaForms[which] = form; 97 } 98 99 public MethodHandle basicInvoker() { 100 assert(erasedType == basicType) : "erasedType: " + erasedType + " != basicType: " + basicType; // primitives must be flattened also 101 MethodHandle invoker = basicInvoker; 102 if (invoker != null) return invoker; 103 invoker = DirectMethodHandle.make(invokeBasicMethod(basicType)); 104 basicInvoker = invoker; 105 return invoker; 106 } 107 108 // This next one is called from LambdaForm.NamedFunction.<init>. 109 /*non-public*/ static MemberName invokeBasicMethod(MethodType basicType) { 110 assert(basicType == basicType.basicType()); 111 try { 112 // Do approximately the same as this public API call: 113 // Lookup.findVirtual(MethodHandle.class, name, type); 114 // But bypass access and corner case checks, since we know exactly what we need. 115 return IMPL_LOOKUP.resolveOrFail(REF_invokeVirtual, MethodHandle.class, "invokeBasic", basicType); 116 } catch (ReflectiveOperationException ex) { 117 throw newInternalError("JVM cannot find invoker for "+basicType, ex); 118 } 119 } 120 121 /** 122 * Build an MTF for a given type, which must have all references erased to Object. 123 * This MTF will stand for that type and all un-erased variations. 124 * Eagerly compute some basic properties of the type, common to all variations. 125 */ 126 protected MethodTypeForm(MethodType erasedType) { 127 this.erasedType = erasedType; 128 129 Class<?>[] ptypes = erasedType.ptypes(); 130 int ptypeCount = ptypes.length; 131 int pslotCount = ptypeCount; // temp. estimate 132 int rtypeCount = 1; // temp. estimate 133 int rslotCount = 1; // temp. estimate 134 135 int[] argToSlotTab = null, slotToArgTab = null; 136 137 // Walk the argument types, looking for primitives. 138 int pac = 0, lac = 0, prc = 0, lrc = 0; 139 Class<?>[] epts = ptypes; 140 Class<?>[] bpts = epts; 141 for (int i = 0; i < epts.length; i++) { 142 Class<?> pt = epts[i]; 143 if (pt != Object.class) { 144 ++pac; 145 Wrapper w = Wrapper.forPrimitiveType(pt); 146 if (w.isDoubleWord()) ++lac; 147 if (w.isSubwordOrInt() && pt != int.class) { 148 if (bpts == epts) 149 bpts = bpts.clone(); 150 bpts[i] = int.class; 151 } 152 } 153 } 154 pslotCount += lac; // #slots = #args + #longs 155 Class<?> rt = erasedType.returnType(); 156 Class<?> bt = rt; 157 if (rt != Object.class) { 158 ++prc; // even void.class counts as a prim here 159 Wrapper w = Wrapper.forPrimitiveType(rt); 160 if (w.isDoubleWord()) ++lrc; 161 if (w.isSubwordOrInt() && rt != int.class) 162 bt = int.class; 163 // adjust #slots, #args 164 if (rt == void.class) 165 rtypeCount = rslotCount = 0; 166 else 167 rslotCount += lrc; 168 } 169 if (epts == bpts && bt == rt) { 170 this.basicType = erasedType; 171 } else { 172 this.basicType = MethodType.makeImpl(bt, bpts, true); 173 } 174 if (lac != 0) { 175 int slot = ptypeCount + lac; 176 slotToArgTab = new int[slot+1]; 177 argToSlotTab = new int[1+ptypeCount]; 178 argToSlotTab[0] = slot; // argument "-1" is past end of slots 179 for (int i = 0; i < epts.length; i++) { 180 Class<?> pt = epts[i]; 181 Wrapper w = Wrapper.forBasicType(pt); 182 if (w.isDoubleWord()) --slot; 183 --slot; 184 slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note 185 argToSlotTab[1+i] = slot; 186 } 187 assert(slot == 0); // filled the table 188 } 189 this.primCounts = pack(lrc, prc, lac, pac); 190 this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount); 191 if (slotToArgTab == null) { 192 int slot = ptypeCount; // first arg is deepest in stack 193 slotToArgTab = new int[slot+1]; 194 argToSlotTab = new int[1+ptypeCount]; 195 argToSlotTab[0] = slot; // argument "-1" is past end of slots 196 for (int i = 0; i < ptypeCount; i++) { 197 --slot; 198 slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note 199 argToSlotTab[1+i] = slot; 200 } 201 } 202 this.argToSlotTable = argToSlotTab; 203 this.slotToArgTable = slotToArgTab; 204 205 if (pslotCount >= 256) throw newIllegalArgumentException("too many arguments"); 206 207 // send a few bits down to the JVM: 208 this.vmslots = parameterSlotCount(); 209 210 if (basicType == erasedType) { 211 lambdaForms = new LambdaForm[LF_LIMIT]; 212 } else { 213 lambdaForms = null; // could be basicType.form().lambdaForms; 214 } 215 } 216 217 private static long pack(int a, int b, int c, int d) { 218 assert(((a|b|c|d) & ~0xFFFF) == 0); 219 long hw = ((a << 16) | b), lw = ((c << 16) | d); 220 return (hw << 32) | lw; 221 } 222 private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d 223 assert(word <= 3); 224 return (char)(packed >> ((3-word) * 16)); 225 } 226 227 public int parameterCount() { // # outgoing values 228 return unpack(argCounts, 3); 229 } 230 public int parameterSlotCount() { // # outgoing interpreter slots 231 return unpack(argCounts, 2); 232 } 233 public int returnCount() { // = 0 (V), or 1 234 return unpack(argCounts, 1); 235 } 236 public int returnSlotCount() { // = 0 (V), 2 (J/D), or 1 237 return unpack(argCounts, 0); 238 } 239 public int primitiveParameterCount() { 240 return unpack(primCounts, 3); 241 } 242 public int longPrimitiveParameterCount() { 243 return unpack(primCounts, 2); 244 } 245 public int primitiveReturnCount() { // = 0 (obj), or 1 246 return unpack(primCounts, 1); 247 } 248 public int longPrimitiveReturnCount() { // = 1 (J/D), or 0 249 return unpack(primCounts, 0); 250 } 251 public boolean hasPrimitives() { 252 return primCounts != 0; 253 } 254 public boolean hasNonVoidPrimitives() { 255 if (primCounts == 0) return false; 256 if (primitiveParameterCount() != 0) return true; 257 return (primitiveReturnCount() != 0 && returnCount() != 0); 258 } 259 public boolean hasLongPrimitives() { 260 return (longPrimitiveParameterCount() | longPrimitiveReturnCount()) != 0; 261 } 262 public int parameterToArgSlot(int i) { 263 return argToSlotTable[1+i]; 264 } 265 public int argSlotToParameter(int argSlot) { 266 // Note: Empty slots are represented by zero in this table. 267 // Valid arguments slots contain incremented entries, so as to be non-zero. 268 // We return -1 the caller to mean an empty slot. 269 return slotToArgTable[argSlot] - 1; 270 } 271 272 static MethodTypeForm findForm(MethodType mt) { 273 MethodType erased = canonicalize(mt, ERASE, ERASE); 274 if (erased == null) { 275 // It is already erased. Make a new MethodTypeForm. 276 return new MethodTypeForm(mt); 277 } else { 278 // Share the MethodTypeForm with the erased version. 279 return erased.form(); 280 } 281 } 282 283 /** Codes for {@link #canonicalize(java.lang.Class, int)}. 284 * ERASE means change every reference to {@code Object}. 285 * WRAP means convert primitives (including {@code void} to their 286 * corresponding wrapper types. UNWRAP means the reverse of WRAP. 287 * INTS means convert all non-void primitive types to int or long, 288 * according to size. LONGS means convert all non-void primitives 289 * to long, regardless of size. RAW_RETURN means convert a type 290 * (assumed to be a return type) to int if it is smaller than an int, 291 * or if it is void. 292 */ 293 public static final int NO_CHANGE = 0, ERASE = 1, WRAP = 2, UNWRAP = 3, INTS = 4, LONGS = 5, RAW_RETURN = 6; 294 295 /** Canonicalize the types in the given method type. 296 * If any types change, intern the new type, and return it. 297 * Otherwise return null. 298 */ 299 public static MethodType canonicalize(MethodType mt, int howRet, int howArgs) { 300 Class<?>[] ptypes = mt.ptypes(); 301 Class<?>[] ptc = MethodTypeForm.canonicalizes(ptypes, howArgs); 302 Class<?> rtype = mt.returnType(); 303 Class<?> rtc = MethodTypeForm.canonicalize(rtype, howRet); 304 if (ptc == null && rtc == null) { 305 // It is already canonical. 306 return null; 307 } 308 // Find the erased version of the method type: 309 if (rtc == null) rtc = rtype; 310 if (ptc == null) ptc = ptypes; 311 return MethodType.makeImpl(rtc, ptc, true); 312 } 313 314 /** Canonicalize the given return or param type. 315 * Return null if the type is already canonicalized. 316 */ 317 static Class<?> canonicalize(Class<?> t, int how) { 318 Class<?> ct; 319 if (t == Object.class) { 320 // no change, ever 321 } else if (!t.isPrimitive()) { 322 switch (how) { 323 case UNWRAP: 324 ct = Wrapper.asPrimitiveType(t); 325 if (ct != t) return ct; 326 break; 327 case RAW_RETURN: 328 case ERASE: 329 return Object.class; 330 } 331 } else if (t == void.class) { 332 // no change, usually 333 switch (how) { 334 case RAW_RETURN: 335 return int.class; 336 case WRAP: 337 return Void.class; 338 } 339 } else { 340 // non-void primitive 341 switch (how) { 342 case WRAP: 343 return Wrapper.asWrapperType(t); 344 case INTS: 345 if (t == int.class || t == long.class) 346 return null; // no change 347 if (t == double.class) 348 return long.class; 349 return int.class; 350 case LONGS: 351 if (t == long.class) 352 return null; // no change 353 return long.class; 354 case RAW_RETURN: 355 if (t == int.class || t == long.class || 356 t == float.class || t == double.class) 357 return null; // no change 358 // everything else returns as an int 359 return int.class; 360 } 361 } 362 // no change; return null to signify 363 return null; 364 } 365 366 /** Canonicalize each param type in the given array. 367 * Return null if all types are already canonicalized. 368 */ 369 static Class<?>[] canonicalizes(Class<?>[] ts, int how) { 370 Class<?>[] cs = null; 371 for (int imax = ts.length, i = 0; i < imax; i++) { 372 Class<?> c = canonicalize(ts[i], how); 373 if (c == void.class) 374 c = null; // a Void parameter was unwrapped to void; ignore 375 if (c != null) { 376 if (cs == null) 377 cs = ts.clone(); 378 cs[i] = c; 379 } 380 } 381 return cs; 382 } 383 384 @Override 385 public String toString() { 386 return "Form"+erasedType; 387 } 388 389 }