File: ConstantPool.java

package info (click to toggle)
turbine-java 0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,560 kB
  • sloc: java: 37,940; xml: 354; makefile: 7
file content (221 lines) | stat: -rw-r--r-- 6,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.bytecode;

import com.google.common.collect.ImmutableList;
import com.google.turbine.model.Const;
import com.google.turbine.model.Const.IntValue;
import com.google.turbine.model.Const.StringValue;
import com.google.turbine.model.Const.Value;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** A constant pool builder, used when writing class files. */
public class ConstantPool {

  /** The next available constant pool entry. */
  int nextEntry = 1;

  private final Map<String, Integer> utf8Pool = new HashMap<>();
  private final Map<Integer, Integer> classInfoPool = new HashMap<>();
  private final Map<Integer, Integer> stringPool = new HashMap<>();
  private final Map<Integer, Integer> integerPool = new HashMap<>();
  private final Map<Double, Integer> doublePool = new HashMap<>();
  private final Map<Float, Integer> floatPool = new HashMap<>();
  private final Map<Long, Integer> longPool = new HashMap<>();
  private final Map<Integer, Integer> modulePool = new HashMap<>();
  private final Map<Integer, Integer> packagePool = new HashMap<>();

  private final List<Entry> constants = new ArrayList<>();

  /** The ordered list of constant pool entries. */
  public ImmutableList<Entry> constants() {
    return ImmutableList.copyOf(constants);
  }

  /** The number of constant pool entries the given kind takes up. */
  private static short width(Kind kind) {
    switch (kind) {
      case CLASS_INFO:
      case STRING:
      case INTEGER:
      case UTF8:
      case FLOAT:
      case MODULE:
      case PACKAGE:
        return 1;
      case LONG:
      case DOUBLE:
        // "In retrospect, making 8-byte constants take two constant pool entries
        // was a poor choice." -- JVMS 4.4.5
        return 2;
    }
    throw new AssertionError(kind);
  }

  /** A constant pool entry. */
  static class Entry {
    private final Kind kind;
    private final Value value;

    Entry(Kind kind, Value value) {
      this.kind = kind;
      this.value = value;
    }

    /** The entry kind. */
    public Kind kind() {
      return kind;
    }

    /** The entry's value. */
    public Value value() {
      return value;
    }
  }

  /** Adds a CONSTANT_Class_info entry to the pool. */
  int classInfo(String value) {
    Objects.requireNonNull(value);
    int utf8 = utf8(value);
    if (classInfoPool.containsKey(utf8)) {
      return classInfoPool.get(utf8);
    }
    int index = insert(new Entry(Kind.CLASS_INFO, new IntValue(utf8)));
    classInfoPool.put(utf8, index);
    return index;
  }

  /** Adds a CONSTANT_Utf8_info entry to the pool. */
  int utf8(String value) {
    Objects.requireNonNull(value);
    if (utf8Pool.containsKey(value)) {
      return utf8Pool.get(value);
    }
    int index = insert(new Entry(Kind.UTF8, new StringValue(value)));
    utf8Pool.put(value, index);
    return index;
  }

  int integer(int value) {
    if (integerPool.containsKey(value)) {
      return integerPool.get(value);
    }
    int index = insert(new Entry(Kind.INTEGER, new Const.IntValue(value)));
    integerPool.put(value, index);
    return index;
  }

  int longInfo(long value) {
    if (longPool.containsKey(value)) {
      return longPool.get(value);
    }
    int index = insert(new Entry(Kind.LONG, new Const.LongValue(value)));
    longPool.put(value, index);
    return index;
  }

  int doubleInfo(double value) {
    if (doublePool.containsKey(value)) {
      return doublePool.get(value);
    }
    int index = insert(new Entry(Kind.DOUBLE, new Const.DoubleValue(value)));
    doublePool.put(value, index);
    return index;
  }

  int floatInfo(float value) {
    if (floatPool.containsKey(value)) {
      return floatPool.get(value);
    }
    int index = insert(new Entry(Kind.FLOAT, new Const.FloatValue(value)));
    floatPool.put(value, index);
    return index;
  }

  int string(String value) {
    Objects.requireNonNull(value);
    int utf8 = utf8(value);
    if (stringPool.containsKey(utf8)) {
      return stringPool.get(utf8);
    }
    int index = insert(new Entry(Kind.STRING, new IntValue(utf8)));
    stringPool.put(utf8, index);
    return index;
  }

  /** Adds a CONSTANT_Module_info entry to the pool. */
  int moduleInfo(String value) {
    Objects.requireNonNull(value);
    int utf8 = utf8(value);
    if (modulePool.containsKey(utf8)) {
      return modulePool.get(utf8);
    }
    int index = insert(new Entry(Kind.MODULE, new IntValue(utf8)));
    modulePool.put(utf8, index);
    return index;
  }

  /** Adds a CONSTANT_Package_info entry to the pool. */
  int packageInfo(String value) {
    Objects.requireNonNull(value);
    int utf8 = utf8(value);
    if (packagePool.containsKey(utf8)) {
      return packagePool.get(utf8);
    }
    int index = insert(new Entry(Kind.PACKAGE, new IntValue(utf8)));
    packagePool.put(utf8, index);
    return index;
  }

  private int insert(Entry key) {
    int entry = nextEntry;
    constants.add(key);
    nextEntry += width(key.kind());
    if ((nextEntry & 0xffff) != nextEntry) {
      throw new AssertionError("constant pool has more than 2^16 entries");
    }
    return entry;
  }

  /** Constant pool entry kinds. */
  enum Kind {
    CLASS_INFO(7),
    STRING(8),
    INTEGER(3),
    DOUBLE(6),
    FLOAT(4),
    LONG(5),
    UTF8(1),
    MODULE(19),
    PACKAGE(20);

    private final short tag;

    Kind(int tag) {
      this.tag = (short) tag;
    }

    /** The JVMS Table 4.4-A tag. */
    public short tag() {
      return tag;
    }
  }
}