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

Sat, 15 Dec 2012 13:54:51 +0000

author
vromero
date
Sat, 15 Dec 2012 13:54:51 +0000
changeset 1452
de1ec6fc93fe
parent 1415
01c9d4161882
child 1541
4cc73ec94686
permissions
-rw-r--r--

8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
Reviewed-by: jjg, mcimadamore

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

mercurial