src/share/classes/com/sun/tools/javac/jvm/Pool.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.jvm;
aoqi@0 27
aoqi@0 28 import com.sun.tools.javac.code.Kinds;
aoqi@0 29 import com.sun.tools.javac.code.Symbol;
aoqi@0 30 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 31 import com.sun.tools.javac.code.Type;
aoqi@0 32 import com.sun.tools.javac.code.Types;
aoqi@0 33 import com.sun.tools.javac.code.Types.UniqueType;
aoqi@0 34
aoqi@0 35 import com.sun.tools.javac.util.ArrayUtils;
aoqi@0 36 import com.sun.tools.javac.util.Assert;
aoqi@0 37 import com.sun.tools.javac.util.Filter;
aoqi@0 38 import com.sun.tools.javac.util.Name;
aoqi@0 39
aoqi@0 40 import java.util.*;
aoqi@0 41
aoqi@0 42 /** An internal structure that corresponds to the constant pool of a classfile.
aoqi@0 43 *
aoqi@0 44 * <p><b>This is NOT part of any supported API.
aoqi@0 45 * If you write code that depends on this, you do so at your own risk.
aoqi@0 46 * This code and its internal interfaces are subject to change or
aoqi@0 47 * deletion without notice.</b>
aoqi@0 48 */
aoqi@0 49 public class Pool {
aoqi@0 50
aoqi@0 51 public static final int MAX_ENTRIES = 0xFFFF;
aoqi@0 52 public static final int MAX_STRING_LENGTH = 0xFFFF;
aoqi@0 53
aoqi@0 54 /** Index of next constant to be entered.
aoqi@0 55 */
aoqi@0 56 int pp;
aoqi@0 57
aoqi@0 58 /** The initial pool buffer.
aoqi@0 59 */
aoqi@0 60 Object[] pool;
aoqi@0 61
aoqi@0 62 /** A hashtable containing all constants in the pool.
aoqi@0 63 */
aoqi@0 64 Map<Object,Integer> indices;
aoqi@0 65
aoqi@0 66 Types types;
aoqi@0 67
aoqi@0 68 /** Construct a pool with given number of elements and element array.
aoqi@0 69 */
aoqi@0 70 public Pool(int pp, Object[] pool, Types types) {
aoqi@0 71 this.pp = pp;
aoqi@0 72 this.pool = pool;
aoqi@0 73 this.types = types;
aoqi@0 74 this.indices = new HashMap<Object,Integer>(pool.length);
aoqi@0 75 for (int i = 1; i < pp; i++) {
aoqi@0 76 if (pool[i] != null) indices.put(pool[i], i);
aoqi@0 77 }
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 /** Construct an empty pool.
aoqi@0 81 */
aoqi@0 82 public Pool(Types types) {
aoqi@0 83 this(1, new Object[64], types);
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 /** Return the number of entries in the constant pool.
aoqi@0 87 */
aoqi@0 88 public int numEntries() {
aoqi@0 89 return pp;
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 /** Remove everything from this pool.
aoqi@0 93 */
aoqi@0 94 public void reset() {
aoqi@0 95 pp = 1;
aoqi@0 96 indices.clear();
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 /** Place an object in the pool, unless it is already there.
aoqi@0 100 * If object is a symbol also enter its owner unless the owner is a
aoqi@0 101 * package. Return the object's index in the pool.
aoqi@0 102 */
aoqi@0 103 public int put(Object value) {
aoqi@0 104 value = makePoolValue(value);
aoqi@0 105 // assert !(value instanceof Type.TypeVar);
aoqi@0 106 Integer index = indices.get(value);
aoqi@0 107 if (index == null) {
aoqi@0 108 // System.err.println("put " + value + " " + value.getClass());//DEBUG
aoqi@0 109 index = pp;
aoqi@0 110 indices.put(value, index);
aoqi@0 111 pool = ArrayUtils.ensureCapacity(pool, pp);
aoqi@0 112 pool[pp++] = value;
aoqi@0 113 if (value instanceof Long || value instanceof Double) {
aoqi@0 114 pool = ArrayUtils.ensureCapacity(pool, pp);
aoqi@0 115 pool[pp++] = null;
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118 return index.intValue();
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 Object makePoolValue(Object o) {
aoqi@0 122 if (o instanceof DynamicMethodSymbol) {
aoqi@0 123 return new DynamicMethod((DynamicMethodSymbol)o, types);
aoqi@0 124 } else if (o instanceof MethodSymbol) {
aoqi@0 125 return new Method((MethodSymbol)o, types);
aoqi@0 126 } else if (o instanceof VarSymbol) {
aoqi@0 127 return new Variable((VarSymbol)o, types);
aoqi@0 128 } else if (o instanceof Type) {
aoqi@0 129 return new UniqueType((Type)o, types);
aoqi@0 130 } else {
aoqi@0 131 return o;
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 /** Return the given object's index in the pool,
aoqi@0 136 * or -1 if object is not in there.
aoqi@0 137 */
aoqi@0 138 public int get(Object o) {
aoqi@0 139 Integer n = indices.get(o);
aoqi@0 140 return n == null ? -1 : n.intValue();
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 static class Method extends DelegatedSymbol<MethodSymbol> {
aoqi@0 144 UniqueType uniqueType;
aoqi@0 145 Method(MethodSymbol m, Types types) {
aoqi@0 146 super(m);
aoqi@0 147 this.uniqueType = new UniqueType(m.type, types);
aoqi@0 148 }
aoqi@0 149 public boolean equals(Object any) {
aoqi@0 150 if (!(any instanceof Method)) return false;
aoqi@0 151 MethodSymbol o = ((Method)any).other;
aoqi@0 152 MethodSymbol m = this.other;
aoqi@0 153 return
aoqi@0 154 o.name == m.name &&
aoqi@0 155 o.owner == m.owner &&
aoqi@0 156 ((Method)any).uniqueType.equals(uniqueType);
aoqi@0 157 }
aoqi@0 158 public int hashCode() {
aoqi@0 159 MethodSymbol m = this.other;
aoqi@0 160 return
aoqi@0 161 m.name.hashCode() * 33 +
aoqi@0 162 m.owner.hashCode() * 9 +
aoqi@0 163 uniqueType.hashCode();
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 static class DynamicMethod extends Method {
aoqi@0 168 public Object[] uniqueStaticArgs;
aoqi@0 169
aoqi@0 170 DynamicMethod(DynamicMethodSymbol m, Types types) {
aoqi@0 171 super(m, types);
aoqi@0 172 uniqueStaticArgs = getUniqueTypeArray(m.staticArgs, types);
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 @Override
aoqi@0 176 public boolean equals(Object any) {
aoqi@0 177 if (!super.equals(any)) return false;
aoqi@0 178 if (!(any instanceof DynamicMethod)) return false;
aoqi@0 179 DynamicMethodSymbol dm1 = (DynamicMethodSymbol)other;
aoqi@0 180 DynamicMethodSymbol dm2 = (DynamicMethodSymbol)((DynamicMethod)any).other;
aoqi@0 181 return dm1.bsm == dm2.bsm &&
aoqi@0 182 dm1.bsmKind == dm2.bsmKind &&
aoqi@0 183 Arrays.equals(uniqueStaticArgs,
aoqi@0 184 ((DynamicMethod)any).uniqueStaticArgs);
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 @Override
aoqi@0 188 public int hashCode() {
aoqi@0 189 int hash = super.hashCode();
aoqi@0 190 DynamicMethodSymbol dm = (DynamicMethodSymbol)other;
aoqi@0 191 hash += dm.bsmKind * 7 +
aoqi@0 192 dm.bsm.hashCode() * 11;
aoqi@0 193 for (int i = 0; i < dm.staticArgs.length; i++) {
aoqi@0 194 hash += (uniqueStaticArgs[i].hashCode() * 23);
aoqi@0 195 }
aoqi@0 196 return hash;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 private Object[] getUniqueTypeArray(Object[] objects, Types types) {
aoqi@0 200 Object[] result = new Object[objects.length];
aoqi@0 201 for (int i = 0; i < objects.length; i++) {
aoqi@0 202 if (objects[i] instanceof Type) {
aoqi@0 203 result[i] = new UniqueType((Type)objects[i], types);
aoqi@0 204 } else {
aoqi@0 205 result[i] = objects[i];
aoqi@0 206 }
aoqi@0 207 }
aoqi@0 208 return result;
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 static class Variable extends DelegatedSymbol<VarSymbol> {
aoqi@0 213 UniqueType uniqueType;
aoqi@0 214 Variable(VarSymbol v, Types types) {
aoqi@0 215 super(v);
aoqi@0 216 this.uniqueType = new UniqueType(v.type, types);
aoqi@0 217 }
aoqi@0 218 public boolean equals(Object any) {
aoqi@0 219 if (!(any instanceof Variable)) return false;
aoqi@0 220 VarSymbol o = ((Variable)any).other;
aoqi@0 221 VarSymbol v = other;
aoqi@0 222 return
aoqi@0 223 o.name == v.name &&
aoqi@0 224 o.owner == v.owner &&
aoqi@0 225 ((Variable)any).uniqueType.equals(uniqueType);
aoqi@0 226 }
aoqi@0 227 public int hashCode() {
aoqi@0 228 VarSymbol v = other;
aoqi@0 229 return
aoqi@0 230 v.name.hashCode() * 33 +
aoqi@0 231 v.owner.hashCode() * 9 +
aoqi@0 232 uniqueType.hashCode();
aoqi@0 233 }
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 public static class MethodHandle {
aoqi@0 237
aoqi@0 238 /** Reference kind - see ClassFile */
aoqi@0 239 int refKind;
aoqi@0 240
aoqi@0 241 /** Reference symbol */
aoqi@0 242 Symbol refSym;
aoqi@0 243
aoqi@0 244 UniqueType uniqueType;
aoqi@0 245
aoqi@0 246 public MethodHandle(int refKind, Symbol refSym, Types types) {
aoqi@0 247 this.refKind = refKind;
aoqi@0 248 this.refSym = refSym;
aoqi@0 249 this.uniqueType = new UniqueType(this.refSym.type, types);
aoqi@0 250 checkConsistent();
aoqi@0 251 }
aoqi@0 252 public boolean equals(Object other) {
aoqi@0 253 if (!(other instanceof MethodHandle)) return false;
aoqi@0 254 MethodHandle mr = (MethodHandle) other;
aoqi@0 255 if (mr.refKind != refKind) return false;
aoqi@0 256 Symbol o = mr.refSym;
aoqi@0 257 return
aoqi@0 258 o.name == refSym.name &&
aoqi@0 259 o.owner == refSym.owner &&
aoqi@0 260 ((MethodHandle)other).uniqueType.equals(uniqueType);
aoqi@0 261 }
aoqi@0 262 public int hashCode() {
aoqi@0 263 return
aoqi@0 264 refKind * 65 +
aoqi@0 265 refSym.name.hashCode() * 33 +
aoqi@0 266 refSym.owner.hashCode() * 9 +
aoqi@0 267 uniqueType.hashCode();
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 /**
aoqi@0 271 * Check consistency of reference kind and symbol (see JVMS 4.4.8)
aoqi@0 272 */
aoqi@0 273 @SuppressWarnings("fallthrough")
aoqi@0 274 private void checkConsistent() {
aoqi@0 275 boolean staticOk = false;
aoqi@0 276 int expectedKind = -1;
aoqi@0 277 Filter<Name> nameFilter = nonInitFilter;
aoqi@0 278 boolean interfaceOwner = false;
aoqi@0 279 switch (refKind) {
aoqi@0 280 case ClassFile.REF_getStatic:
aoqi@0 281 case ClassFile.REF_putStatic:
aoqi@0 282 staticOk = true;
aoqi@0 283 case ClassFile.REF_getField:
aoqi@0 284 case ClassFile.REF_putField:
aoqi@0 285 expectedKind = Kinds.VAR;
aoqi@0 286 break;
aoqi@0 287 case ClassFile.REF_newInvokeSpecial:
aoqi@0 288 nameFilter = initFilter;
aoqi@0 289 expectedKind = Kinds.MTH;
aoqi@0 290 break;
aoqi@0 291 case ClassFile.REF_invokeInterface:
aoqi@0 292 interfaceOwner = true;
aoqi@0 293 expectedKind = Kinds.MTH;
aoqi@0 294 break;
aoqi@0 295 case ClassFile.REF_invokeStatic:
aoqi@0 296 interfaceOwner = true;
aoqi@0 297 staticOk = true;
aoqi@0 298 case ClassFile.REF_invokeVirtual:
aoqi@0 299 expectedKind = Kinds.MTH;
aoqi@0 300 break;
aoqi@0 301 case ClassFile.REF_invokeSpecial:
aoqi@0 302 interfaceOwner = true;
aoqi@0 303 expectedKind = Kinds.MTH;
aoqi@0 304 break;
aoqi@0 305 }
aoqi@0 306 Assert.check(!refSym.isStatic() || staticOk);
aoqi@0 307 Assert.check(refSym.kind == expectedKind);
aoqi@0 308 Assert.check(nameFilter.accepts(refSym.name));
aoqi@0 309 Assert.check(!refSym.owner.isInterface() || interfaceOwner);
aoqi@0 310 }
aoqi@0 311 //where
aoqi@0 312 Filter<Name> nonInitFilter = new Filter<Name>() {
aoqi@0 313 public boolean accepts(Name n) {
aoqi@0 314 return n != n.table.names.init && n != n.table.names.clinit;
aoqi@0 315 }
aoqi@0 316 };
aoqi@0 317
aoqi@0 318 Filter<Name> initFilter = new Filter<Name>() {
aoqi@0 319 public boolean accepts(Name n) {
aoqi@0 320 return n == n.table.names.init;
aoqi@0 321 }
aoqi@0 322 };
aoqi@0 323 }
aoqi@0 324 }

mercurial