langtools/src/share/classes/com/sun/tools/javac/jvm/Pool.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 14046 8ef5d5b19998
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.jvm;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/** An internal structure that corresponds to the constant pool of a classfile.
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    34
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    35
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
public class Pool {
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    public static final int MAX_ENTRIES = 0xFFFF;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    public static final int MAX_STRING_LENGTH = 0xFFFF;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    /** Index of next constant to be entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    int pp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    /** The initial pool buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    Object[] pool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /** A hashtable containing all constants in the pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    Map<Object,Integer> indices;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    /** Construct a pool with given number of elements and element array.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public Pool(int pp, Object[] pool) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        this.pp = pp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        this.pool = pool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        this.indices = new HashMap<Object,Integer>(pool.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        for (int i = 1; i < pp; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
            if (pool[i] != null) indices.put(pool[i], i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /** Construct an empty pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    public Pool() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        this(1, new Object[64]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /** Return the number of entries in the constant pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public int numEntries() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        return pp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /** Remove everything from this pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public void reset() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        pp = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        indices.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /** Double pool buffer in size.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private void doublePool() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        Object[] newpool = new Object[pool.length * 2];
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        System.arraycopy(pool, 0, newpool, 0, pool.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        pool = newpool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    /** Place an object in the pool, unless it is already there.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     *  If object is a symbol also enter its owner unless the owner is a
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     *  package.  Return the object's index in the pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    public int put(Object value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        if (value instanceof MethodSymbol)
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            value = new Method((MethodSymbol)value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        else if (value instanceof VarSymbol)
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            value = new Variable((VarSymbol)value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
//      assert !(value instanceof Type.TypeVar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        Integer index = indices.get(value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        if (index == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
//          System.err.println("put " + value + " " + value.getClass());//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            index = pp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            indices.put(value, index);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            if (pp == pool.length) doublePool();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            pool[pp++] = value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            if (value instanceof Long || value instanceof Double) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                if (pp == pool.length) doublePool();
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                pool[pp++] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        return index.intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    /** Return the given object's index in the pool,
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     *  or -1 if object is not in there.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public int get(Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        Integer n = indices.get(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        return n == null ? -1 : n.intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    static class Method extends DelegatedSymbol {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        MethodSymbol m;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        Method(MethodSymbol m) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            super(m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            this.m = m;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            if (!(other instanceof Method)) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            MethodSymbol o = ((Method)other).m;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                o.name == m.name &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                o.owner == m.owner &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                o.type.equals(m.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                m.name.hashCode() * 33 +
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                m.owner.hashCode() * 9 +
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                m.type.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    static class Variable extends DelegatedSymbol {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        VarSymbol v;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        Variable(VarSymbol v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            super(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            this.v = v;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            if (!(other instanceof Variable)) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            VarSymbol o = ((Variable)other).v;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                o.name == v.name &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                o.owner == v.owner &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                o.type.equals(v.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                v.name.hashCode() * 33 +
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                v.owner.hashCode() * 9 +
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                v.type.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
}