duke@1: /*
vromero@1541: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.jvm;
duke@1:
mcimadamore@1336: import com.sun.tools.javac.code.Kinds;
mcimadamore@1336: import com.sun.tools.javac.code.Symbol;
duke@1: import com.sun.tools.javac.code.Symbol.*;
vromero@1452: import com.sun.tools.javac.code.Type;
vromero@1452: import com.sun.tools.javac.code.Types;
vromero@1452: import com.sun.tools.javac.code.Types.UniqueType;
mcimadamore@1342:
mcimadamore@1343: import com.sun.tools.javac.util.ArrayUtils;
mcimadamore@1336: import com.sun.tools.javac.util.Assert;
mcimadamore@1336: import com.sun.tools.javac.util.Filter;
mcimadamore@1336: import com.sun.tools.javac.util.Name;
duke@1:
mcimadamore@1342: import java.util.*;
mcimadamore@1342:
duke@1: /** An internal structure that corresponds to the constant pool of a classfile.
duke@1: *
jjg@581: *
This is NOT part of any supported API.
jjg@581: * If you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class Pool {
duke@1:
duke@1: public static final int MAX_ENTRIES = 0xFFFF;
duke@1: public static final int MAX_STRING_LENGTH = 0xFFFF;
duke@1:
duke@1: /** Index of next constant to be entered.
duke@1: */
duke@1: int pp;
duke@1:
duke@1: /** The initial pool buffer.
duke@1: */
duke@1: Object[] pool;
duke@1:
duke@1: /** A hashtable containing all constants in the pool.
duke@1: */
duke@1: Map indices;
duke@1:
vromero@1452: Types types;
vromero@1452:
duke@1: /** Construct a pool with given number of elements and element array.
duke@1: */
vromero@1452: public Pool(int pp, Object[] pool, Types types) {
duke@1: this.pp = pp;
duke@1: this.pool = pool;
vromero@1452: this.types = types;
duke@1: this.indices = new HashMap(pool.length);
duke@1: for (int i = 1; i < pp; i++) {
duke@1: if (pool[i] != null) indices.put(pool[i], i);
duke@1: }
duke@1: }
duke@1:
duke@1: /** Construct an empty pool.
duke@1: */
vromero@1452: public Pool(Types types) {
vromero@1452: this(1, new Object[64], types);
duke@1: }
duke@1:
duke@1: /** Return the number of entries in the constant pool.
duke@1: */
duke@1: public int numEntries() {
duke@1: return pp;
duke@1: }
duke@1:
duke@1: /** Remove everything from this pool.
duke@1: */
duke@1: public void reset() {
duke@1: pp = 1;
duke@1: indices.clear();
duke@1: }
duke@1:
duke@1: /** Place an object in the pool, unless it is already there.
duke@1: * If object is a symbol also enter its owner unless the owner is a
duke@1: * package. Return the object's index in the pool.
duke@1: */
duke@1: public int put(Object value) {
mcimadamore@1415: value = makePoolValue(value);
duke@1: // assert !(value instanceof Type.TypeVar);
duke@1: Integer index = indices.get(value);
duke@1: if (index == null) {
duke@1: // System.err.println("put " + value + " " + value.getClass());//DEBUG
duke@1: index = pp;
duke@1: indices.put(value, index);
jjg@1339: pool = ArrayUtils.ensureCapacity(pool, pp);
duke@1: pool[pp++] = value;
duke@1: if (value instanceof Long || value instanceof Double) {
jjg@1339: pool = ArrayUtils.ensureCapacity(pool, pp);
duke@1: pool[pp++] = null;
duke@1: }
duke@1: }
duke@1: return index.intValue();
duke@1: }
duke@1:
mcimadamore@1415: Object makePoolValue(Object o) {
mcimadamore@1415: if (o instanceof DynamicMethodSymbol) {
vromero@1452: return new DynamicMethod((DynamicMethodSymbol)o, types);
mcimadamore@1415: } else if (o instanceof MethodSymbol) {
vromero@1452: return new Method((MethodSymbol)o, types);
mcimadamore@1415: } else if (o instanceof VarSymbol) {
vromero@1452: return new Variable((VarSymbol)o, types);
vromero@1452: } else if (o instanceof Type) {
vromero@1452: return new UniqueType((Type)o, types);
mcimadamore@1415: } else {
mcimadamore@1415: return o;
mcimadamore@1415: }
mcimadamore@1415: }
mcimadamore@1415:
duke@1: /** Return the given object's index in the pool,
duke@1: * or -1 if object is not in there.
duke@1: */
duke@1: public int get(Object o) {
duke@1: Integer n = indices.get(o);
duke@1: return n == null ? -1 : n.intValue();
duke@1: }
duke@1:
vromero@1541: static class Method extends DelegatedSymbol {
vromero@1452: UniqueType uniqueType;
vromero@1452: Method(MethodSymbol m, Types types) {
duke@1: super(m);
vromero@1452: this.uniqueType = new UniqueType(m.type, types);
duke@1: }
vromero@1541: public boolean equals(Object any) {
vromero@1541: if (!(any instanceof Method)) return false;
vromero@1541: MethodSymbol o = ((Method)any).other;
vromero@1541: MethodSymbol m = this.other;
duke@1: return
duke@1: o.name == m.name &&
duke@1: o.owner == m.owner &&
vromero@1541: ((Method)any).uniqueType.equals(uniqueType);
duke@1: }
duke@1: public int hashCode() {
vromero@1541: MethodSymbol m = this.other;
duke@1: return
duke@1: m.name.hashCode() * 33 +
duke@1: m.owner.hashCode() * 9 +
vromero@1452: uniqueType.hashCode();
duke@1: }
duke@1: }
duke@1:
mcimadamore@1415: static class DynamicMethod extends Method {
vromero@1452: public Object[] uniqueStaticArgs;
mcimadamore@1415:
vromero@1452: DynamicMethod(DynamicMethodSymbol m, Types types) {
vromero@1452: super(m, types);
vromero@1452: uniqueStaticArgs = getUniqueTypeArray(m.staticArgs, types);
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
vromero@1541: public boolean equals(Object any) {
vromero@1541: if (!super.equals(any)) return false;
vromero@1541: if (!(any instanceof DynamicMethod)) return false;
vromero@1541: DynamicMethodSymbol dm1 = (DynamicMethodSymbol)other;
vromero@1541: DynamicMethodSymbol dm2 = (DynamicMethodSymbol)((DynamicMethod)any).other;
mcimadamore@1415: return dm1.bsm == dm2.bsm &&
mcimadamore@1415: dm1.bsmKind == dm2.bsmKind &&
vromero@1452: Arrays.equals(uniqueStaticArgs,
vromero@1541: ((DynamicMethod)any).uniqueStaticArgs);
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
mcimadamore@1415: public int hashCode() {
mcimadamore@1415: int hash = super.hashCode();
vromero@1541: DynamicMethodSymbol dm = (DynamicMethodSymbol)other;
mcimadamore@1415: hash += dm.bsmKind * 7 +
mcimadamore@1415: dm.bsm.hashCode() * 11;
mcimadamore@1415: for (int i = 0; i < dm.staticArgs.length; i++) {
vromero@1452: hash += (uniqueStaticArgs[i].hashCode() * 23);
mcimadamore@1415: }
mcimadamore@1415: return hash;
mcimadamore@1415: }
vromero@1452:
vromero@1452: private Object[] getUniqueTypeArray(Object[] objects, Types types) {
vromero@1452: Object[] result = new Object[objects.length];
vromero@1452: for (int i = 0; i < objects.length; i++) {
vromero@1452: if (objects[i] instanceof Type) {
vromero@1452: result[i] = new UniqueType((Type)objects[i], types);
vromero@1452: } else {
vromero@1452: result[i] = objects[i];
vromero@1452: }
vromero@1452: }
vromero@1452: return result;
vromero@1452: }
mcimadamore@1415: }
mcimadamore@1415:
vromero@1541: static class Variable extends DelegatedSymbol {
vromero@1452: UniqueType uniqueType;
vromero@1452: Variable(VarSymbol v, Types types) {
duke@1: super(v);
vromero@1452: this.uniqueType = new UniqueType(v.type, types);
duke@1: }
vromero@1541: public boolean equals(Object any) {
vromero@1541: if (!(any instanceof Variable)) return false;
vromero@1541: VarSymbol o = ((Variable)any).other;
vromero@1541: VarSymbol v = other;
duke@1: return
duke@1: o.name == v.name &&
duke@1: o.owner == v.owner &&
vromero@1541: ((Variable)any).uniqueType.equals(uniqueType);
duke@1: }
duke@1: public int hashCode() {
vromero@1541: VarSymbol v = other;
duke@1: return
duke@1: v.name.hashCode() * 33 +
duke@1: v.owner.hashCode() * 9 +
vromero@1452: uniqueType.hashCode();
duke@1: }
duke@1: }
mcimadamore@1336:
mcimadamore@1336: public static class MethodHandle {
mcimadamore@1336:
mcimadamore@1336: /** Reference kind - see ClassFile */
mcimadamore@1336: int refKind;
mcimadamore@1336:
mcimadamore@1336: /** Reference symbol */
mcimadamore@1336: Symbol refSym;
mcimadamore@1336:
vromero@1452: UniqueType uniqueType;
vromero@1452:
vromero@1452: public MethodHandle(int refKind, Symbol refSym, Types types) {
mcimadamore@1336: this.refKind = refKind;
mcimadamore@1336: this.refSym = refSym;
vromero@1452: this.uniqueType = new UniqueType(this.refSym.type, types);
mcimadamore@1336: checkConsistent();
mcimadamore@1336: }
mcimadamore@1336: public boolean equals(Object other) {
mcimadamore@1336: if (!(other instanceof MethodHandle)) return false;
mcimadamore@1336: MethodHandle mr = (MethodHandle) other;
mcimadamore@1336: if (mr.refKind != refKind) return false;
mcimadamore@1336: Symbol o = mr.refSym;
mcimadamore@1336: return
mcimadamore@1336: o.name == refSym.name &&
mcimadamore@1336: o.owner == refSym.owner &&
vromero@1452: ((MethodHandle)other).uniqueType.equals(uniqueType);
mcimadamore@1336: }
mcimadamore@1336: public int hashCode() {
mcimadamore@1336: return
mcimadamore@1336: refKind * 65 +
mcimadamore@1336: refSym.name.hashCode() * 33 +
mcimadamore@1336: refSym.owner.hashCode() * 9 +
vromero@1452: uniqueType.hashCode();
mcimadamore@1336: }
mcimadamore@1336:
mcimadamore@1336: /**
mcimadamore@1336: * Check consistency of reference kind and symbol (see JVMS 4.4.8)
mcimadamore@1336: */
mcimadamore@1336: @SuppressWarnings("fallthrough")
mcimadamore@1336: private void checkConsistent() {
mcimadamore@1336: boolean staticOk = false;
mcimadamore@1336: int expectedKind = -1;
mcimadamore@1336: Filter nameFilter = nonInitFilter;
mcimadamore@1336: boolean interfaceOwner = false;
mcimadamore@1336: switch (refKind) {
mcimadamore@1336: case ClassFile.REF_getStatic:
mcimadamore@1336: case ClassFile.REF_putStatic:
mcimadamore@1336: staticOk = true;
mcimadamore@1336: case ClassFile.REF_getField:
mcimadamore@1336: case ClassFile.REF_putField:
mcimadamore@1336: expectedKind = Kinds.VAR;
mcimadamore@1336: break;
mcimadamore@1336: case ClassFile.REF_newInvokeSpecial:
mcimadamore@1336: nameFilter = initFilter;
mcimadamore@1336: expectedKind = Kinds.MTH;
mcimadamore@1336: break;
mcimadamore@1336: case ClassFile.REF_invokeInterface:
mcimadamore@1336: interfaceOwner = true;
mcimadamore@1336: expectedKind = Kinds.MTH;
mcimadamore@1336: break;
mcimadamore@1336: case ClassFile.REF_invokeStatic:
mcimadamore@1617: interfaceOwner = true;
mcimadamore@1336: staticOk = true;
mcimadamore@1336: case ClassFile.REF_invokeVirtual:
mcimadamore@1336: case ClassFile.REF_invokeSpecial:
mcimadamore@1336: expectedKind = Kinds.MTH;
mcimadamore@1336: break;
mcimadamore@1336: }
mcimadamore@1336: Assert.check(!refSym.isStatic() || staticOk);
mcimadamore@1336: Assert.check(refSym.kind == expectedKind);
mcimadamore@1336: Assert.check(nameFilter.accepts(refSym.name));
mcimadamore@1336: Assert.check(!refSym.owner.isInterface() || interfaceOwner);
mcimadamore@1336: }
mcimadamore@1336: //where
mcimadamore@1336: Filter nonInitFilter = new Filter() {
mcimadamore@1336: public boolean accepts(Name n) {
mcimadamore@1342: return n != n.table.names.init && n != n.table.names.clinit;
mcimadamore@1336: }
mcimadamore@1336: };
mcimadamore@1336:
mcimadamore@1336: Filter initFilter = new Filter() {
mcimadamore@1336: public boolean accepts(Name n) {
mcimadamore@1342: return n == n.table.names.init;
mcimadamore@1336: }
mcimadamore@1336: };
mcimadamore@1336: }
duke@1: }