Skip to content

Commit b871606

Browse files
committed
[GR-59419] Implement compact constant pools.
PullRequest: graal/20339
2 parents 1768167 + fd67e62 commit b871606

File tree

103 files changed

+2793
-4014
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+2793
-4014
lines changed

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/ClassfileParser.java

Lines changed: 154 additions & 128 deletions
Large diffs are not rendered by default.

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/ConstantPool.java

Lines changed: 770 additions & 147 deletions
Large diffs are not rendered by default.

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/ExceptionHandler.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public final class ExceptionHandler {
3434

3535
public static final ExceptionHandler[] EMPTY_ARRAY = new ExceptionHandler[0];
3636

37-
private final int startBCI;
38-
private final int endBCI;
39-
private final int handlerBCI;
40-
private final int catchTypeCPI;
37+
private final char startBCI;
38+
private final char endBCI;
39+
private final char handlerBCI;
40+
private final char catchTypeCPI;
4141
private final Symbol<Type> catchType;
4242

4343
/**
@@ -50,10 +50,10 @@ public final class ExceptionHandler {
5050
* @param catchType the type caught by this exception handler
5151
*/
5252
public ExceptionHandler(int startBCI, int endBCI, int catchBCI, int catchTypeCPI, Symbol<Type> catchType) {
53-
this.startBCI = startBCI;
54-
this.endBCI = endBCI;
55-
this.handlerBCI = catchBCI;
56-
this.catchTypeCPI = catchTypeCPI;
53+
this.startBCI = (char) startBCI;
54+
this.endBCI = (char) endBCI;
55+
this.handlerBCI = (char) catchBCI;
56+
this.catchTypeCPI = (char) catchTypeCPI;
5757
this.catchType = catchType;
5858
}
5959

@@ -124,7 +124,8 @@ public boolean equals(Object obj) {
124124

125125
@Override
126126
public String toString() {
127-
return "ExceptionHandler<startBCI=" + startBCI + ", endBCI=" + endBCI + ", handlerBCI=" + handlerBCI + ", catchTypeCPI=" + catchTypeCPI + ", catchType=" + catchType + ">";
127+
return "ExceptionHandler<startBCI=" + getStartBCI() + ", endBCI=" + getEndBCI() + ", handlerBCI=" + getHandlerBCI() + ", catchTypeCPI=" + getCatchType() + ", catchType=" + getCatchType() +
128+
">";
128129
}
129130

130131
@Override

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/ImmutableConstantPool.java

Lines changed: 0 additions & 115 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package com.oracle.truffle.espresso.classfile;
24+
25+
import java.util.Arrays;
26+
27+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
28+
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;
29+
30+
public final class ParserConstantPool extends ConstantPool {
31+
32+
public ParserConstantPool(byte[] tags, int[] entries, Symbol<?>[] symbols, int majorVersion, int minorVersion) {
33+
super(tags, entries, symbols, majorVersion, minorVersion);
34+
}
35+
36+
@Override
37+
@TruffleBoundary
38+
public RuntimeException classFormatError(String message) {
39+
throw new ParserException.ClassFormatError(message);
40+
}
41+
42+
@Override
43+
public ParserConstantPool getParserConstantPool() {
44+
return this;
45+
}
46+
47+
public ParserConstantPool patchForHiddenClass(int thisKlassIndex, Symbol<?> newName) {
48+
int newNameIndex = entries.length;
49+
int newSymbolIndex = symbols.length;
50+
51+
byte[] newTags = Arrays.copyOf(tags, tags.length + 1);
52+
int[] newEntries = Arrays.copyOf(entries, entries.length + 1);
53+
Symbol<?>[] newSymbols = Arrays.copyOf(symbols, symbols.length + 1);
54+
55+
// Append a new UTF8 constant.
56+
newSymbols[newSymbolIndex] = newName;
57+
newTags[newNameIndex] = CONSTANT_Utf8;
58+
newEntries[newNameIndex] = newSymbolIndex;
59+
60+
// Patch hidden class name index.
61+
newEntries[thisKlassIndex] = newNameIndex;
62+
63+
// This will get resolved in the ObjectKlass constructor
64+
// See initSelfReferenceInPool
65+
return new ParserConstantPool(newTags, newEntries, newSymbols, majorVersion, minorVersion);
66+
}
67+
68+
}

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/ParserKlass.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public final class ParserKlass {
5555
/**
5656
* Unresolved constant pool, only trivial entries (with no resolution involved) are computed.
5757
*/
58-
private final ImmutableConstantPool pool;
58+
private final ParserConstantPool pool;
5959

6060
private final int thisKlassIndex;
6161
private final long hiddenKlassId;
6262

63-
public ParserKlass(ImmutableConstantPool pool,
63+
public ParserKlass(ParserConstantPool pool,
6464
int flags,
6565
Symbol<Name> name,
6666
Symbol<Type> type,
@@ -112,7 +112,7 @@ public Symbol<Type>[] getSuperInterfaces() {
112112
return superInterfaces;
113113
}
114114

115-
public ImmutableConstantPool getConstantPool() {
115+
public ParserConstantPool getConstantPool() {
116116
return pool;
117117
}
118118

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/ParsingContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import java.util.function.Supplier;
2626

27-
import com.oracle.truffle.espresso.classfile.constantpool.Utf8Constant;
2827
import com.oracle.truffle.espresso.classfile.descriptors.ByteSequence;
28+
import com.oracle.truffle.espresso.classfile.descriptors.ModifiedUTF8;
2929
import com.oracle.truffle.espresso.classfile.descriptors.Name;
3030
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;
3131
import com.oracle.truffle.espresso.classfile.descriptors.Type;
@@ -47,7 +47,7 @@ public interface ParsingContext {
4747

4848
Symbol<Type> getOrCreateTypeFromName(ByteSequence byteSequence);
4949

50-
Utf8Constant getOrCreateUtf8Constant(ByteSequence byteSequence);
50+
Symbol<? extends ModifiedUTF8> getOrCreateUtf8(ByteSequence byteSequence);
5151

5252
interface Logger {
5353
void log(String message);

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/attributes/Attribute.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,26 @@
2525
import java.util.Arrays;
2626
import java.util.Objects;
2727

28-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
2928
import com.oracle.truffle.espresso.classfile.ConstantPool;
3029
import com.oracle.truffle.espresso.classfile.descriptors.Name;
30+
import com.oracle.truffle.espresso.classfile.descriptors.ParserSymbols.ParserNames;
3131
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;
3232

33-
public class Attribute {
33+
public abstract class Attribute {
3434

3535
public static final Attribute[] EMPTY_ARRAY = new Attribute[0];
3636

37-
private final Symbol<Name> name;
37+
// Singleton instance for the Synthetic attribute.
38+
public static final Attribute SYNTHETIC = createRaw(ParserNames.Synthetic, null);
3839

39-
@CompilationFinal(dimensions = 1) //
40-
private final byte[] data;
41-
42-
public final Symbol<Name> getName() {
43-
return name;
44-
}
40+
public abstract Symbol<Name> getName();
4541

4642
/**
4743
* Attribute raw data. Known attributes that parse the raw data, can drop the raw data (return
4844
* null).
4945
*/
50-
public final byte[] getData() {
51-
return data;
52-
}
53-
54-
public Attribute(Symbol<Name> name, final byte[] data) {
55-
this.name = name;
56-
this.data = data;
46+
public byte[] getData() {
47+
return null;
5748
}
5849

5950
/**
@@ -70,6 +61,20 @@ public boolean isSame(Attribute other, ConstantPool thisPool, ConstantPool other
7061
if (other == null || getClass() != other.getClass()) {
7162
return false;
7263
}
73-
return Objects.equals(name, other.name) && Arrays.equals(data, other.data);
64+
return Objects.equals(getName(), other.getName()) && Arrays.equals(getData(), other.getData());
65+
}
66+
67+
public static Attribute createRaw(Symbol<Name> name, byte[] data) {
68+
return new Attribute() {
69+
@Override
70+
public Symbol<Name> getName() {
71+
return name;
72+
}
73+
74+
@Override
75+
public byte[] getData() {
76+
return data;
77+
}
78+
};
7479
}
7580
}

espresso/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/attributes/BootstrapMethodsAttribute.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ public char getBootstrapMethodRef() {
6363
private final Entry[] entries;
6464

6565
public BootstrapMethodsAttribute(Symbol<Name> name, Entry[] entries) {
66-
super(name, null);
66+
assert name == NAME;
6767
this.entries = entries;
6868
}
6969

7070
public Entry at(int index) {
7171
return entries[index];
7272
}
73+
74+
@Override
75+
public Symbol<Name> getName() {
76+
return NAME;
77+
}
7378
}

0 commit comments

Comments
 (0)