Skip to content

Commit

Permalink
[GR-61575] Avoid usage of truffle methods in classfile project.
Browse files Browse the repository at this point in the history
PullRequest: graal/19881
  • Loading branch information
gilles-duboscq committed Jan 24, 2025
2 parents a86c2be + ee57881 commit bb418a9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,16 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.truffle.espresso.classfile.attributes;

import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.INVOKEDYNAMIC;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.JSR;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.JSR_W;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.MONITORENTER;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.MONITOREXIT;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.RET;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.espresso.classfile.ClassfileParser;
import com.oracle.truffle.espresso.classfile.ExceptionHandler;
import com.oracle.truffle.espresso.classfile.bytecode.BytecodeStream;
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 final class CodeAttribute extends Attribute {

public static final Symbol<Name> NAME = ParserNames.Code;

private final int majorVersion;
Expand All @@ -57,12 +46,6 @@ public final class CodeAttribute extends Attribute {
@CompilationFinal(dimensions = 1) //
private final Attribute[] attributes;

private static final int FLAGS_READY = 0x1;
private static final int FLAGS_HAS_JSR = 0x2;
private static final int FLAGS_USES_MONITORS = 0x4;
private static final int FLAGS_HAS_INDY = 0x8;
@CompilationFinal byte flags;

public CodeAttribute(Symbol<Name> name, int maxStack, int maxLocals, byte[] code, ExceptionHandler[] exceptionHandlerEntries, Attribute[] attributes, int majorVersion) {
super(name, null);
this.maxStack = maxStack;
Expand Down Expand Up @@ -107,50 +90,6 @@ public StackMapTableAttribute getStackMapFrame() {
return null;
}

/**
* Returns true if this method uses the JSR/RET bytecodes.
*/
public boolean hasJsr() {
return (getFlags() & FLAGS_HAS_JSR) != 0;
}

public boolean usesMonitors() {
return (getFlags() & FLAGS_USES_MONITORS) != 0;
}

public boolean usesIndy() {
return (getFlags() & FLAGS_HAS_INDY) != 0;
}

private byte getFlags() {
byte localFlags = flags;
if (localFlags == 0) {
CompilerDirectives.transferToInterpreterAndInvalidate();
flags = localFlags = computeFlags(originalCode);
assert localFlags != 0;
}
return localFlags;
}

private static byte computeFlags(byte[] code) {
BytecodeStream bs = new BytecodeStream(code);
int bci = 0;
int flags = FLAGS_READY;
while (bci < bs.endBCI()) {
int opcode = bs.opcode(bci);
switch (opcode) {
case JSR, JSR_W, RET ->
flags |= FLAGS_HAS_JSR;
case MONITORENTER, MONITOREXIT ->
flags |= FLAGS_USES_MONITORS;
case INVOKEDYNAMIC ->
flags |= FLAGS_HAS_INDY;
}
bci = bs.nextBCI(bci);
}
return (byte) flags;
}

public LineNumberTableAttribute getLineNumberTableAttribute() {
for (Attribute attr : attributes) {
if (attr.getName() == ParserNames.LineNumberTable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.nio.ByteBuffer;
import java.util.Objects;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

Expand Down Expand Up @@ -66,7 +65,6 @@ public static ByteSequence wrap(final byte[] underlyingBytes) {

public static ByteSequence wrap(final byte[] underlyingBytes, int offset, int length) {
if ((length > 0 && offset >= underlyingBytes.length) || offset + (long) length > underlyingBytes.length || length < 0 || offset < 0) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new IndexOutOfBoundsException("ByteSequence illegal bounds: offset: " + offset + " length: " + length + " bytes length: " + underlyingBytes.length);
}
return new ByteSequence(underlyingBytes, hashOfRange(underlyingBytes, offset, length)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.ALOAD_0;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.GETFIELD;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.GETSTATIC;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.INVOKEDYNAMIC;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.JSR;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.JSR_W;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.MONITORENTER;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.MONITOREXIT;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.PUTFIELD;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.PUTSTATIC;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.RET;
import static com.oracle.truffle.espresso.classfile.bytecode.Bytecodes.RETURN;

import java.io.PrintStream;
Expand Down Expand Up @@ -1407,6 +1413,11 @@ public CallTarget getContinuableCallTarget(MethodVersion mv, int bci) {
* new MethodVersion object.
*/
public final class MethodVersion implements MethodRef, ModifiersProvider {
private static final int CODE_FLAGS_READY = 0x1;
private static final int CODE_FLAGS_HAS_JSR = 0x2;
private static final int CODE_FLAGS_USES_MONITORS = 0x4;
private static final int CODE_FLAGS_HAS_INDY = 0x8;

private final ObjectKlass.KlassVersion klassVersion;
private final RuntimeConstantPool pool;
private final LinkedMethod linkedMethod;
Expand All @@ -1421,6 +1432,7 @@ public final class MethodVersion implements MethodRef, ModifiersProvider {
@CompilationFinal private int itableIndex = -1;

@CompilationFinal private byte refKind;
@CompilationFinal private byte codeFlags;

@CompilationFinal(dimensions = 1) //
private ObjectKlass[] checkedExceptions;
Expand Down Expand Up @@ -1953,15 +1965,52 @@ public String toString() {

public boolean usesMonitors() {
// Whether we need to use an additional frame slot for monitor unlock on kill.
return isSynchronized() || codeAttribute != null && codeAttribute.usesMonitors();
return isSynchronized() || (getCodeFlags() & CODE_FLAGS_USES_MONITORS) != 0;
}

/**
* Returns true if this method uses the JSR/RET bytecodes.
*/
public boolean hasJsr() {
return codeAttribute != null && codeAttribute.hasJsr();
return (getCodeFlags() & CODE_FLAGS_HAS_JSR) != 0;
}

public boolean usesIndy() {
return codeAttribute != null && codeAttribute.usesIndy();
return (getCodeFlags() & CODE_FLAGS_HAS_INDY) != 0;
}

private byte getCodeFlags() {
byte localFlags = codeFlags;
if (localFlags == 0) {
CompilerDirectives.transferToInterpreterAndInvalidate();
if (codeAttribute == null) {
localFlags = CODE_FLAGS_READY;
} else {
localFlags = computeFlags(codeAttribute.getOriginalCode());
}
assert localFlags != 0;
codeFlags = localFlags;
}
return localFlags;
}

private static byte computeFlags(byte[] code) {
BytecodeStream bs = new BytecodeStream(code);
int bci = 0;
int flags = CODE_FLAGS_READY;
while (bci < bs.endBCI()) {
int opcode = bs.opcode(bci);
switch (opcode) {
case JSR, JSR_W, RET ->
flags |= CODE_FLAGS_HAS_JSR;
case MONITORENTER, MONITOREXIT ->
flags |= CODE_FLAGS_USES_MONITORS;
case INVOKEDYNAMIC ->
flags |= CODE_FLAGS_HAS_INDY;
}
bci = bs.nextBCI(bci);
}
return (byte) flags;
}

public int getRefKind() {
Expand Down

0 comments on commit bb418a9

Please sign in to comment.