Skip to content

[GR-59419] Implement compact constant pools. #11128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public final class ExceptionHandler {

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

private final int startBCI;
private final int endBCI;
private final int handlerBCI;
private final int catchTypeCPI;
private final char startBCI;
private final char endBCI;
private final char handlerBCI;
private final char catchTypeCPI;
private final Symbol<Type> catchType;

/**
Expand All @@ -50,10 +50,10 @@ public final class ExceptionHandler {
* @param catchType the type caught by this exception handler
*/
public ExceptionHandler(int startBCI, int endBCI, int catchBCI, int catchTypeCPI, Symbol<Type> catchType) {
this.startBCI = startBCI;
this.endBCI = endBCI;
this.handlerBCI = catchBCI;
this.catchTypeCPI = catchTypeCPI;
this.startBCI = (char) startBCI;
this.endBCI = (char) endBCI;
this.handlerBCI = (char) catchBCI;
this.catchTypeCPI = (char) catchTypeCPI;
this.catchType = catchType;
}

Expand Down Expand Up @@ -124,7 +124,8 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "ExceptionHandler<startBCI=" + startBCI + ", endBCI=" + endBCI + ", handlerBCI=" + handlerBCI + ", catchTypeCPI=" + catchTypeCPI + ", catchType=" + catchType + ">";
return "ExceptionHandler<startBCI=" + getStartBCI() + ", endBCI=" + getEndBCI() + ", handlerBCI=" + getHandlerBCI() + ", catchTypeCPI=" + getCatchType() + ", catchType=" + getCatchType() +
">";
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.espresso.classfile;

import java.util.Arrays;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;

public final class ParserConstantPool extends ConstantPool {

public ParserConstantPool(byte[] tags, int[] entries, Symbol<?>[] symbols, int majorVersion, int minorVersion) {
super(tags, entries, symbols, majorVersion, minorVersion);
}

@Override
@TruffleBoundary
public RuntimeException classFormatError(String message) {
throw new ParserException.ClassFormatError(message);
}

@Override
public ParserConstantPool getParserConstantPool() {
return this;
}

public ParserConstantPool patchForHiddenClass(int thisKlassIndex, Symbol<?> newName) {
int newNameIndex = entries.length;
int newSymbolIndex = symbols.length;

byte[] newTags = Arrays.copyOf(tags, tags.length + 1);
int[] newEntries = Arrays.copyOf(entries, entries.length + 1);
Symbol<?>[] newSymbols = Arrays.copyOf(symbols, symbols.length + 1);

// Append a new UTF8 constant.
newSymbols[newSymbolIndex] = newName;
newTags[newNameIndex] = CONSTANT_Utf8;
newEntries[newNameIndex] = newSymbolIndex;

// Patch hidden class name index.
newEntries[thisKlassIndex] = newNameIndex;

// This will get resolved in the ObjectKlass constructor
// See initSelfReferenceInPool
return new ParserConstantPool(newTags, newEntries, newSymbols, majorVersion, minorVersion);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public final class ParserKlass {
/**
* Unresolved constant pool, only trivial entries (with no resolution involved) are computed.
*/
private final ImmutableConstantPool pool;
private final ParserConstantPool pool;

private final int thisKlassIndex;
private final long hiddenKlassId;

public ParserKlass(ImmutableConstantPool pool,
public ParserKlass(ParserConstantPool pool,
int flags,
Symbol<Name> name,
Symbol<Type> type,
Expand Down Expand Up @@ -112,7 +112,7 @@ public Symbol<Type>[] getSuperInterfaces() {
return superInterfaces;
}

public ImmutableConstantPool getConstantPool() {
public ParserConstantPool getConstantPool() {
return pool;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import java.util.function.Supplier;

import com.oracle.truffle.espresso.classfile.constantpool.Utf8Constant;
import com.oracle.truffle.espresso.classfile.descriptors.ByteSequence;
import com.oracle.truffle.espresso.classfile.descriptors.ModifiedUTF8;
import com.oracle.truffle.espresso.classfile.descriptors.Name;
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;
import com.oracle.truffle.espresso.classfile.descriptors.Type;
Expand All @@ -47,7 +47,7 @@ public interface ParsingContext {

Symbol<Type> getOrCreateTypeFromName(ByteSequence byteSequence);

Utf8Constant getOrCreateUtf8Constant(ByteSequence byteSequence);
Symbol<? extends ModifiedUTF8> getOrCreateUtf8(ByteSequence byteSequence);

interface Logger {
void log(String message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,26 @@
import java.util.Arrays;
import java.util.Objects;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.espresso.classfile.ConstantPool;
import com.oracle.truffle.espresso.classfile.descriptors.Name;
import com.oracle.truffle.espresso.classfile.descriptors.ParserSymbols.ParserNames;
import com.oracle.truffle.espresso.classfile.descriptors.Symbol;

public class Attribute {
public abstract class Attribute {

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

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

@CompilationFinal(dimensions = 1) //
private final byte[] data;

public final Symbol<Name> getName() {
return name;
}
public abstract Symbol<Name> getName();

/**
* Attribute raw data. Known attributes that parse the raw data, can drop the raw data (return
* null).
*/
public final byte[] getData() {
return data;
}

public Attribute(Symbol<Name> name, final byte[] data) {
this.name = name;
this.data = data;
public byte[] getData() {
return null;
}

/**
Expand All @@ -70,6 +61,20 @@ public boolean isSame(Attribute other, ConstantPool thisPool, ConstantPool other
if (other == null || getClass() != other.getClass()) {
return false;
}
return Objects.equals(name, other.name) && Arrays.equals(data, other.data);
return Objects.equals(getName(), other.getName()) && Arrays.equals(getData(), other.getData());
}

public static Attribute createRaw(Symbol<Name> name, byte[] data) {
return new Attribute() {
@Override
public Symbol<Name> getName() {
return name;
}

@Override
public byte[] getData() {
return data;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ public char getBootstrapMethodRef() {
private final Entry[] entries;

public BootstrapMethodsAttribute(Symbol<Name> name, Entry[] entries) {
super(name, null);
assert name == NAME;
this.entries = entries;
}

public Entry at(int index) {
return entries[index];
}

@Override
public Symbol<Name> getName() {
return NAME;
}
}
Loading
Loading