Skip to content

Commit 34f2be1

Browse files
committed
[GR-68752] Crema constant pool improvements.
PullRequest: graal/21870
2 parents 24a1a36 + 41388d7 commit 34f2be1

File tree

12 files changed

+621
-394
lines changed

12 files changed

+621
-394
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ protected RuntimeException unexpectedEntry(int index, String description, Tag...
295295
throw unexpectedEntry(index, tagAt(index), description, expected);
296296
}
297297

298-
private void checkTag(int index, byte expectedTag) {
298+
protected void checkTag(int index, byte expectedTag) {
299299
byte tag = byteTagAt(index);
300300
if (tag != expectedTag) {
301301
throw unexpectedEntry(index, Tag.fromValue(tag), null, Tag.fromValue(expectedTag));

espresso-shared/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/ParserConstantPool.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.function.Function;
3030

3131
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
32+
import com.oracle.truffle.espresso.classfile.descriptors.ByteSequence;
3233
import com.oracle.truffle.espresso.classfile.descriptors.ModifiedUTF8;
3334
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;
3435

@@ -107,7 +108,10 @@ public byte[] toBytesForSerialization() {
107108
}
108109

109110
@FunctionalInterface
110-
public interface Symbolify<T extends ModifiedUTF8> extends Function<byte[], Symbol<T>> {
111+
public interface Symbolify<T extends ModifiedUTF8> extends Function<ByteSequence, Symbol<T>> {
112+
default Symbol<T> apply(String string) {
113+
return apply(ByteSequence.create(string));
114+
}
111115
}
112116

113117
/**
@@ -130,7 +134,7 @@ public static ParserConstantPool fromBytesForSerialization(byte[] bytes, Symboli
130134
int symbolLength = bb.getInt();
131135
byte[] symbolBytes = new byte[symbolLength];
132136
bb.get(symbolBytes);
133-
symbols[i] = symbolify.apply(symbolBytes);
137+
symbols[i] = symbolify.apply(ByteSequence.wrap(symbolBytes));
134138
}
135139
assert !bb.hasRemaining();
136140
return new ParserConstantPool(tags, entries, symbols, majorVersion, minorVersion);

espresso-shared/src/com.oracle.truffle.espresso.classfile/src/com/oracle/truffle/espresso/classfile/descriptors/TypeSymbols.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3333
import com.oracle.truffle.espresso.classfile.JavaKind;
34+
import com.oracle.truffle.espresso.classfile.ParserConstantPool;
3435
import com.oracle.truffle.espresso.classfile.ParserException;
3536

3637
/**
@@ -430,6 +431,29 @@ public static ByteSequence nameToType(ByteSequence name) {
430431
return wrap;
431432
}
432433

434+
/**
435+
* Reverse operation of {@link #fromClassNameEntry(Symbol)}. This conversion is <b>NOT</b> valid
436+
* for primitive types, to avoid ambiguity e.g. LI; vs I
437+
*/
438+
public static ByteSequence toClassNameEntry(Symbol<Type> type) {
439+
assert !isPrimitive(type);
440+
if (isArray(type)) {
441+
return type;
442+
}
443+
assert type.byteAt(0) == 'L';
444+
assert type.byteAt(type.length() - 1) == ';';
445+
return type.subSequence(1, type.length() - 1);
446+
}
447+
448+
/**
449+
* Reverse operation of {@link #fromClassNameEntry(Symbol)}. This conversion is <b>NOT</b> valid
450+
* for primitive types, to avoid ambiguity e.g. LI; vs I
451+
*/
452+
public static Symbol<Name> toClassNameEntry(Symbol<Type> type, ParserConstantPool.Symbolify<Name> symbolify) {
453+
ByteSequence className = toClassNameEntry(type);
454+
return symbolify.apply(className);
455+
}
456+
433457
public static ByteSequence getRuntimePackage(ByteSequence symbol) {
434458
if (symbol.byteAt(0) == '[') {
435459
int arrayDimensions = getArrayDimensions(symbol);

substratevm/src/com.oracle.svm.interpreter.metadata/src/com/oracle/svm/interpreter/metadata/InterpreterConstantPool.java

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import java.util.List;
3030

31+
import jdk.vm.ci.meta.JavaKind;
32+
import jdk.vm.ci.meta.PrimitiveConstant;
3133
import org.graalvm.nativeimage.Platform;
3234
import org.graalvm.nativeimage.Platforms;
3335

@@ -229,12 +231,6 @@ public Object resolvedAt(int cpi, InterpreterResolvedObjectType accessingClass)
229231
}
230232
}
231233

232-
assert !isUnresolved(entry);
233-
if (entry instanceof Throwable throwable) {
234-
// Cached exception.
235-
throw uncheckedThrow(throwable);
236-
}
237-
238234
return entry;
239235
}
240236

@@ -243,7 +239,7 @@ private static boolean isUnresolved(Object entry) {
243239
}
244240

245241
@SuppressWarnings("unchecked")
246-
private static <T extends Throwable> RuntimeException uncheckedThrow(Throwable t) throws T {
242+
protected static <T extends Throwable> RuntimeException uncheckedThrow(Throwable t) throws T {
247243
throw (T) t;
248244
}
249245

@@ -267,7 +263,58 @@ public InterpreterResolvedObjectType resolvedTypeAt(InterpreterResolvedObjectTyp
267263

268264
public String resolveStringAt(int cpi) {
269265
Object resolvedEntry = resolvedAt(cpi, null);
266+
if (resolvedEntry instanceof ReferenceConstant<?> referenceConstant) {
267+
resolvedEntry = referenceConstant.getReferent();
268+
}
270269
assert resolvedEntry != null;
271270
return (String) resolvedEntry;
272271
}
272+
273+
@Override
274+
public int intAt(int index) {
275+
checkTag(index, CONSTANT_Integer);
276+
Object entry = cachedEntries[index];
277+
assert entry == null || entry instanceof PrimitiveConstant;
278+
if (entry instanceof PrimitiveConstant primitiveConstant) {
279+
assert primitiveConstant.getJavaKind() == JavaKind.Int;
280+
return primitiveConstant.asInt();
281+
}
282+
return super.intAt(index);
283+
}
284+
285+
@Override
286+
public float floatAt(int index) {
287+
checkTag(index, CONSTANT_Float);
288+
Object entry = cachedEntries[index];
289+
assert entry == null || entry instanceof PrimitiveConstant;
290+
if (entry instanceof PrimitiveConstant primitiveConstant) {
291+
assert primitiveConstant.getJavaKind() == JavaKind.Float;
292+
return primitiveConstant.asFloat();
293+
}
294+
return super.floatAt(index);
295+
}
296+
297+
@Override
298+
public double doubleAt(int index) {
299+
checkTag(index, CONSTANT_Double);
300+
Object entry = cachedEntries[index];
301+
assert entry == null || entry instanceof PrimitiveConstant;
302+
if (entry instanceof PrimitiveConstant primitiveConstant) {
303+
assert primitiveConstant.getJavaKind() == JavaKind.Double;
304+
return primitiveConstant.asDouble();
305+
}
306+
return super.doubleAt(index);
307+
}
308+
309+
@Override
310+
public long longAt(int index) {
311+
checkTag(index, CONSTANT_Long);
312+
Object entry = cachedEntries[index];
313+
assert entry == null || entry instanceof PrimitiveConstant;
314+
if (entry instanceof PrimitiveConstant primitiveConstant) {
315+
assert primitiveConstant.getJavaKind() == JavaKind.Long;
316+
return primitiveConstant.asLong();
317+
}
318+
return super.longAt(index);
319+
}
273320
}

substratevm/src/com.oracle.svm.interpreter.metadata/src/com/oracle/svm/interpreter/metadata/InterpreterResolvedJavaType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public boolean isJavaLangObject() {
164164
@Override
165165
public Symbol<Name> getSymbolicName() {
166166
// This is assumed to be low-traffic
167-
return SymbolsSupport.getNames().getOrCreate(TypeSymbols.typeToName(type));
167+
return SymbolsSupport.getNames().getOrCreate(TypeSymbols.toClassNameEntry(type));
168168
}
169169

170170
@Override

substratevm/src/com.oracle.svm.interpreter.metadata/src/com/oracle/svm/interpreter/metadata/serialization/Serializers.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.oracle.svm.core.snippets.KnownIntrinsics;
4141
import com.oracle.svm.core.util.VMError;
4242
import com.oracle.svm.espresso.classfile.ParserConstantPool;
43-
import com.oracle.svm.espresso.classfile.descriptors.ByteSequence;
4443
import com.oracle.svm.espresso.classfile.descriptors.ModifiedUTF8;
4544
import com.oracle.svm.espresso.classfile.descriptors.Symbol;
4645
import com.oracle.svm.interpreter.metadata.InterpreterConstantPool;
@@ -532,8 +531,7 @@ public static ValueSerializer<ReferenceConstant<?>> newReferenceConstantSerializ
532531

533532
@SuppressWarnings("unchecked")
534533
ParserConstantPool parserConstantPool = ParserConstantPool.fromBytesForSerialization(parserConstantPoolBytes,
535-
bytes -> {
536-
ByteSequence byteSequence = ByteSequence.wrap(bytes);
534+
byteSequence -> {
537535
return (Symbol<ModifiedUTF8>) SymbolsSupport.getUtf8().getOrCreateValidUtf8(byteSequence);
538536
});
539537
Object[] cachedEntries = context.readerFor(Object[].class).read(context, in);

0 commit comments

Comments
 (0)