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