package jdk.vm.ci.hotspot;
import static jdk.vm.ci.hotspot.CompilerToVM.compilerToVM;
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
import static jdk.vm.ci.hotspot.HotSpotVMConfig.config;
import static jdk.vm.ci.hotspot.UnsafeAccess.UNSAFE;
import jdk.vm.ci.common.JVMCIError;
import jdk.vm.ci.common.NativeImageReinitialize;
import jdk.vm.ci.meta.ConstantPool;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaField;
import jdk.vm.ci.meta.JavaMethod;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.Signature;
import jdk.vm.ci.meta.UnresolvedJavaField;
import jdk.vm.ci.meta.UnresolvedJavaMethod;
import jdk.vm.ci.meta.UnresolvedJavaType;
public final class HotSpotConstantPool implements ConstantPool, MetaspaceHandleObject {
public static class Bytecodes {
public static final int LDC = 18;
public static final int LDC_W = 19;
public static final int LDC2_W = 20;
public static final int GETSTATIC = 178;
public static final int PUTSTATIC = 179;
public static final int GETFIELD = 180;
public static final int PUTFIELD = 181;
public static final int INVOKEVIRTUAL = 182;
public static final int INVOKESPECIAL = 183;
public static final int INVOKESTATIC = 184;
public static final int INVOKEINTERFACE = 185;
public static final int INVOKEDYNAMIC = 186;
public static final int NEW = 187;
public static final int NEWARRAY = 188;
public static final int ANEWARRAY = 189;
public static final int CHECKCAST = 192;
public static final int INSTANCEOF = 193;
public static final int MULTIANEWARRAY = 197;
static boolean isInvoke(int opcode) {
switch (opcode) {
case INVOKEVIRTUAL:
case INVOKESPECIAL:
case INVOKESTATIC:
case INVOKEINTERFACE:
case INVOKEDYNAMIC:
return true;
default:
return false;
}
}
static boolean isInvokeHandleAlias(int opcode) {
switch (opcode) {
case INVOKEVIRTUAL:
case INVOKESPECIAL:
return true;
default:
return false;
}
}
}
static final class JvmConstant {
private final int tag;
private final String name;
JvmConstant(int tag, String name) {
this.tag = tag;
this.name = name;
}
@Override
public String toString() {
return name;
}
}
static final class JvmConstants {
private final HotSpotVMConfig c = config();
private final int externalMax = c.jvmConstantExternalMax;
private final int internalMax = c.jvmConstantInternalMax;
private final int internalMin = c.jvmConstantInternalMin;
private final JvmConstant[] table = new JvmConstant[externalMax + 1 + (internalMax - internalMin) + 1];
final JvmConstant jvmUtf8 = add(new JvmConstant(c.jvmConstantUtf8, "Utf8"));
final JvmConstant jvmInteger = add(new JvmConstant(c.jvmConstantInteger, "Integer"));
final JvmConstant jvmLong = add(new JvmConstant(c.jvmConstantLong, "Long"));
final JvmConstant jvmFloat = add(new JvmConstant(c.jvmConstantFloat, "Float"));
final JvmConstant jvmDouble = add(new JvmConstant(c.jvmConstantDouble, "Double"));
final JvmConstant jvmClass = add(new JvmConstant(c.jvmConstantClass, "Class"));
final JvmConstant jvmUnresolvedClass = add(new JvmConstant(c.jvmConstantUnresolvedClass, "UnresolvedClass"));
final JvmConstant jvmUnresolvedClassInError = add(new JvmConstant(c.jvmConstantUnresolvedClassInError, "UnresolvedClassInError"));
final JvmConstant jvmString = add(new JvmConstant(c.jvmConstantString, "String"));
final JvmConstant jvmFieldref = add(new JvmConstant(c.jvmConstantFieldref, "Fieldref"));
final JvmConstant jvmMethodref = add(new JvmConstant(c.jvmConstantMethodref, "Methodref"));
final JvmConstant jvmInterfaceMethodref = add(new JvmConstant(c.jvmConstantInterfaceMethodref, "InterfaceMethodref"));
final JvmConstant jvmNameAndType = add(new JvmConstant(c.jvmConstantNameAndType, "NameAndType"));
final JvmConstant jvmMethodHandle = add(new JvmConstant(c.jvmConstantMethodHandle, "MethodHandle"));
final JvmConstant jvmMethodHandleInError = add(new JvmConstant(c.jvmConstantMethodHandleInError, "MethodHandleInError"));
final JvmConstant jvmMethodType = add(new JvmConstant(c.jvmConstantMethodType, "MethodType"));
final JvmConstant jvmMethodTypeInError = add(new JvmConstant(c.jvmConstantMethodTypeInError, "MethodTypeInError"));
final JvmConstant jvmInvokeDynamic = add(new JvmConstant(c.jvmConstantInvokeDynamic, "InvokeDynamic"));
private JvmConstant add(JvmConstant constant) {
table[indexOf(constant.tag)] = constant;
return constant;
}
private int indexOf(int tag) {
if (tag >= internalMin) {
return tag - internalMin + externalMax + 1;
} else {
assert tag <= externalMax;
}
return tag;
}
JvmConstant get(int tag) {
JvmConstant res = table[indexOf(tag)];
if (res != null) {
return res;
}
throw new JVMCIError("Unknown JvmConstant tag %s", tag);
}
@NativeImageReinitialize private static volatile JvmConstants instance;
static JvmConstants instance() {
JvmConstants result = instance;
if (result == null) {
synchronized (JvmConstants.class) {
result = instance;
if (result == null) {
instance = result = new JvmConstants();
}
}
}
return result;
}
}
private static class LookupTypeCacheElement {
int lastCpi = Integer.MIN_VALUE;
JavaType javaType;
LookupTypeCacheElement(int lastCpi, JavaType javaType) {
super();
this.lastCpi = lastCpi;
this.javaType = javaType;
}
}
private final long metadataHandle;
private volatile LookupTypeCacheElement lastLookupType;
private final JvmConstants constants;
@SuppressWarnings("unused")
@VMEntryPoint
private static HotSpotConstantPool fromMetaspace(long metaspaceConstantPool) {
return new HotSpotConstantPool(metaspaceConstantPool);
}
private HotSpotConstantPool(long metadataHandle) {
this.metadataHandle = metadataHandle;
this.constants = JvmConstants.instance();
HandleCleaner.create(this, metadataHandle);
}
private HotSpotResolvedObjectType getHolder() {
return compilerToVM().getResolvedJavaType(this, config().constantPoolHolderOffset, false);
}
private static int rawIndexToConstantPoolCacheIndex(int rawIndex, int opcode) {
int index;
if (opcode == Bytecodes.INVOKEDYNAMIC) {
index = rawIndex;
assert index < 0 : "not an invokedynamic constant pool index " + index;
} else {
assert opcode == Bytecodes.GETFIELD || opcode == Bytecodes.PUTFIELD || opcode == Bytecodes.GETSTATIC || opcode == Bytecodes.PUTSTATIC || opcode == Bytecodes.INVOKEINTERFACE ||
opcode == Bytecodes.INVOKEVIRTUAL || opcode == Bytecodes.INVOKESPECIAL || opcode == Bytecodes.INVOKESTATIC : "unexpected invoke opcode " + opcode;
index = rawIndex + config().constantPoolCpCacheIndexTag;
}
return index;
}
private static int decodeConstantPoolCacheIndex(int index) {
if (isInvokedynamicIndex(index)) {
return decodeInvokedynamicIndex(index);
} else {
return index - config().constantPoolCpCacheIndexTag;
}
}
private static boolean isInvokedynamicIndex(int index) {
return index < 0;
}
private static int decodeInvokedynamicIndex(int i) {
assert isInvokedynamicIndex(i) : i;
return ~i;
}
long getMetaspaceConstantPool() {
return getMetaspacePointer();
}
@Override
public long getMetadataHandle() {
return metadataHandle;
}
private JvmConstant getTagAt(int index) {
assert checkBounds(index);
HotSpotVMConfig config = config();
final long metaspaceConstantPoolTags = UNSAFE.getAddress(getMetaspaceConstantPool() + config.constantPoolTagsOffset);
final int tag = UNSAFE.getByteVolatile(null, metaspaceConstantPoolTags + config.arrayU1DataOffset + index);
if (tag == 0) {
return null;
}
return constants.get(tag);
}
long getEntryAt(int index) {
assert checkBounds(index);
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize;
return UNSAFE.getAddress(getMetaspaceConstantPool() + config().constantPoolSize + offset);
}
private int getIntAt(int index) {
assert checkTag(index, constants.jvmInteger);
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize;
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolSize + offset);
}
private long getLongAt(int index) {
assert checkTag(index, constants.jvmLong);
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize;
return UNSAFE.getLong(getMetaspaceConstantPool() + config().constantPoolSize + offset);
}
private float getFloatAt(int index) {
assert checkTag(index, constants.jvmFloat);
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize;
return UNSAFE.getFloat(getMetaspaceConstantPool() + config().constantPoolSize + offset);
}
private double getDoubleAt(int index) {
assert checkTag(index, constants.jvmDouble);
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize;
return UNSAFE.getDouble(getMetaspaceConstantPool() + config().constantPoolSize + offset);
}
private int getNameAndTypeAt(int index) {
assert checkTag(index, constants.jvmNameAndType);
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize;
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolSize + offset);
}
private int getNameAndTypeRefIndexAt(int index) {
return compilerToVM().lookupNameAndTypeRefIndexInPool(this, index);
}
private String getNameOf(int which) {
return compilerToVM().lookupNameInPool(this, which);
}
private int getNameRefIndexAt(int index) {
final int refIndex = getNameAndTypeAt(index);
return refIndex & 0xFFFF;
}
private String getSignatureOf(int which) {
return compilerToVM().lookupSignatureInPool(this, which);
}
private int getSignatureRefIndexAt(int index) {
final int refIndex = getNameAndTypeAt(index);
return refIndex >>> 16;
}
private int getKlassRefIndexAt(int index) {
return compilerToVM().lookupKlassRefIndexInPool(this, index);
}
private int getUncachedKlassRefIndexAt(int index) {
assert checkTagIsFieldOrMethod(index);
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize;
final int refIndex = UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolSize + offset);
return refIndex & 0xFFFF;
}
private boolean checkBounds(int index) {
assert 0 <= index && index < length() : "index " + index + " not between 0 and " + length();
return true;
}
private boolean checkTag(int index, JvmConstant tag) {
final JvmConstant tagAt = getTagAt(index);
assert tagAt == tag : "constant pool tag at index " + index + " is " + tagAt + " but expected " + tag;
return true;
}
private boolean checkTagIsFieldOrMethod(int index) {
final JvmConstant tagAt = getTagAt(index);
assert tagAt == constants.jvmFieldref || tagAt == constants.jvmMethodref || tagAt == constants.jvmInterfaceMethodref : tagAt;
return true;
}
@Override
public int length() {
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolLengthOffset);
}
public boolean hasDynamicConstant() {
return (flags() & config().constantPoolHasDynamicConstant) != 0;
}
private int flags() {
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolFlagsOffset);
}
@Override
public Object lookupConstant(int cpi) {
assert cpi != 0;
final JvmConstant tag = getTagAt(cpi);
switch (tag.name) {
case "Integer":
return JavaConstant.forInt(getIntAt(cpi));
case "Long":
return JavaConstant.forLong(getLongAt(cpi));
case "Float":
return JavaConstant.forFloat(getFloatAt(cpi));
case "Double":
return JavaConstant.forDouble(getDoubleAt(cpi));
case "Class":
case "UnresolvedClass":
case "UnresolvedClassInError":
final int opcode = -1;
return lookupType(cpi, opcode);
case "String":
return compilerToVM().resolvePossiblyCachedConstantInPool(this, cpi);
case "MethodHandle":
case "MethodHandleInError":
case "MethodType":
case "MethodTypeInError":
return compilerToVM().resolvePossiblyCachedConstantInPool(this, cpi);
default:
throw new JVMCIError("Unknown constant pool tag %s", tag);
}
}
@Override
public String lookupUtf8(int cpi) {
assert checkTag(cpi, constants.jvmUtf8);
return compilerToVM().getSymbol(getEntryAt(cpi));
}
@Override
public Signature lookupSignature(int cpi) {
return new HotSpotSignature(runtime(), lookupUtf8(cpi));
}
@Override
public JavaConstant lookupAppendix(int cpi, int opcode) {
assert Bytecodes.isInvoke(opcode);
final int index = rawIndexToConstantPoolCacheIndex(cpi, opcode);
return compilerToVM().lookupAppendixInPool(this, index);
}
private static JavaType getJavaType(final Object type) {
if (type instanceof String) {
String name = (String) type;
return UnresolvedJavaType.create("L" + name + ";");
} else {
return (JavaType) type;
}
}
@Override
public JavaMethod lookupMethod(int cpi, int opcode) {
final int index = rawIndexToConstantPoolCacheIndex(cpi, opcode);
final HotSpotResolvedJavaMethod method = compilerToVM().lookupMethodInPool(this, index, (byte) opcode);
if (method != null) {
return method;
} else {
String name = getNameOf(index);
HotSpotSignature signature = new HotSpotSignature(runtime(), getSignatureOf(index));
if (opcode == Bytecodes.INVOKEDYNAMIC) {
HotSpotResolvedObjectType holder = runtime().getMethodHandleClass();
return new UnresolvedJavaMethod(name, signature, holder);
} else {
final int klassIndex = getKlassRefIndexAt(index);
final Object type = compilerToVM().lookupKlassInPool(this, klassIndex);
JavaType holder = getJavaType(type);
return new UnresolvedJavaMethod(name, signature, holder);
}
}
}
@Override
public JavaType lookupType(int cpi, int opcode) {
final LookupTypeCacheElement elem = this.lastLookupType;
if (elem != null && elem.lastCpi == cpi) {
return elem.javaType;
} else {
final Object type = compilerToVM().lookupKlassInPool(this, cpi);
JavaType result = getJavaType(type);
if (result instanceof ResolvedJavaType) {
this.lastLookupType = new LookupTypeCacheElement(cpi, result);
}
return result;
}
}
@Override
public JavaType lookupReferencedType(int cpi, int opcode) {
int index;
switch (opcode) {
case Bytecodes.CHECKCAST:
case Bytecodes.INSTANCEOF:
case Bytecodes.NEW:
case Bytecodes.ANEWARRAY:
case Bytecodes.MULTIANEWARRAY:
case Bytecodes.LDC:
case Bytecodes.LDC_W:
case Bytecodes.LDC2_W:
index = cpi;
break;
case Bytecodes.GETSTATIC:
case Bytecodes.PUTSTATIC:
case Bytecodes.GETFIELD:
case Bytecodes.PUTFIELD:
case Bytecodes.INVOKEVIRTUAL:
case Bytecodes.INVOKESPECIAL:
case Bytecodes.INVOKESTATIC:
case Bytecodes.INVOKEINTERFACE: {
index = rawIndexToConstantPoolCacheIndex(cpi, opcode);
index = getKlassRefIndexAt(index);
break;
}
default:
throw JVMCIError.shouldNotReachHere("Unexpected opcode " + opcode);
}
final Object type = compilerToVM().lookupKlassInPool(this, index);
return getJavaType(type);
}
@Override
public JavaField lookupField(int cpi, ResolvedJavaMethod method, int opcode) {
final int index = rawIndexToConstantPoolCacheIndex(cpi, opcode);
final int nameAndTypeIndex = getNameAndTypeRefIndexAt(index);
final int typeIndex = getSignatureRefIndexAt(nameAndTypeIndex);
String typeName = lookupUtf8(typeIndex);
JavaType type = runtime().lookupType(typeName, getHolder(), false);
final int holderIndex = getKlassRefIndexAt(index);
JavaType holder = lookupType(holderIndex, opcode);
if (holder instanceof HotSpotResolvedObjectTypeImpl) {
int[] info = new int[3];
HotSpotResolvedObjectTypeImpl resolvedHolder;
try {
resolvedHolder = compilerToVM().resolveFieldInPool(this, index, (HotSpotResolvedJavaMethodImpl) method, (byte) opcode, info);
} catch (Throwable t) {
return new UnresolvedJavaField(holder, lookupUtf8(getNameRefIndexAt(nameAndTypeIndex)), type);
}
final int flags = info[0];
final int offset = info[1];
final int fieldIndex = info[2];
HotSpotResolvedJavaField result = resolvedHolder.createField(type, offset, flags, fieldIndex);
return result;
} else {
return new UnresolvedJavaField(holder, lookupUtf8(getNameRefIndexAt(nameAndTypeIndex)), type);
}
}
public int rawIndexToConstantPoolIndex(int rawIndex, int opcode) {
int index;
if (isInvokedynamicIndex(rawIndex)) {
assert opcode == Bytecodes.INVOKEDYNAMIC;
index = decodeInvokedynamicIndex(rawIndex) + config().constantPoolCpCacheIndexTag;
} else {
assert opcode != Bytecodes.INVOKEDYNAMIC;
index = rawIndexToConstantPoolCacheIndex(rawIndex, opcode);
}
return compilerToVM().constantPoolRemapInstructionOperandFromCache(this, index);
}
@Override
public void loadReferencedType(int cpi, int opcode) {
loadReferencedType(cpi, opcode, true );
}
@SuppressWarnings("fallthrough")
public void loadReferencedType(int cpi, int opcode, boolean initialize) {
int index;
switch (opcode) {
case Bytecodes.CHECKCAST:
case Bytecodes.INSTANCEOF:
case Bytecodes.NEW:
case Bytecodes.ANEWARRAY:
case Bytecodes.MULTIANEWARRAY:
case Bytecodes.LDC:
case Bytecodes.LDC_W:
case Bytecodes.LDC2_W:
index = cpi;
break;
case Bytecodes.INVOKEDYNAMIC: {
index = decodeConstantPoolCacheIndex(cpi) + config().constantPoolCpCacheIndexTag;
index = compilerToVM().constantPoolRemapInstructionOperandFromCache(this, index);
break;
}
case Bytecodes.GETSTATIC:
case Bytecodes.PUTSTATIC:
case Bytecodes.GETFIELD:
case Bytecodes.PUTFIELD:
case Bytecodes.INVOKEVIRTUAL:
case Bytecodes.INVOKESPECIAL:
case Bytecodes.INVOKESTATIC:
case Bytecodes.INVOKEINTERFACE: {
index = rawIndexToConstantPoolCacheIndex(cpi, opcode);
index = compilerToVM().constantPoolRemapInstructionOperandFromCache(this, index);
break;
}
default:
throw JVMCIError.shouldNotReachHere("Unexpected opcode " + opcode);
}
final JvmConstant tag = getTagAt(index);
if (tag == null) {
assert getTagAt(index - 1) == constants.jvmDouble || getTagAt(index - 1) == constants.jvmLong;
return;
}
switch (tag.name) {
case "Methodref":
case "Fieldref":
case "InterfaceMethodref":
index = getUncachedKlassRefIndexAt(index);
final JvmConstant klassTag = getTagAt(index);
assert klassTag == constants.jvmClass || klassTag == constants.jvmUnresolvedClass || klassTag == constants.jvmUnresolvedClassInError : klassTag;
case "Class":
case "UnresolvedClass":
case "UnresolvedClassInError":
final HotSpotResolvedObjectTypeImpl type = compilerToVM().resolveTypeInPool(this, index);
if (initialize && !type.isPrimitive() && !type.isArray()) {
type.ensureInitialized();
}
if (tag == constants.jvmMethodref) {
if (Bytecodes.isInvokeHandleAlias(opcode) && isSignaturePolymorphicHolder(type)) {
final int methodRefCacheIndex = rawIndexToConstantPoolCacheIndex(cpi, opcode);
assert checkTag(compilerToVM().constantPoolRemapInstructionOperandFromCache(this, methodRefCacheIndex), constants.jvmMethodref);
compilerToVM().resolveInvokeHandleInPool(this, methodRefCacheIndex);
}
}
break;
case "InvokeDynamic":
if (isInvokedynamicIndex(cpi)) {
compilerToVM().resolveInvokeDynamicInPool(this, cpi);
}
break;
default:
break;
}
}
private static String[] signaturePolymorphicHolders;
@SuppressFBWarnings(value = "LI_LAZY_INIT_STATIC", justification = "signaturePolymorphicHolders is a cache, not a singleton that must be constructed exactly once" +
"and compiler re-ordering is not an issue due to the VM call")
static boolean isSignaturePolymorphicHolder(final ResolvedJavaType type) {
String name = type.getName();
if (signaturePolymorphicHolders == null) {
signaturePolymorphicHolders = compilerToVM().getSignaturePolymorphicHolders();
}
for (String holder : signaturePolymorphicHolders) {
if (name.equals(holder)) {
return true;
}
}
return false;
}
public boolean isResolvedDynamicInvoke(int cpi, int opcode) {
if (Bytecodes.isInvokeHandleAlias(opcode)) {
final int methodRefCacheIndex = rawIndexToConstantPoolCacheIndex(cpi, opcode);
assert checkTag(compilerToVM().constantPoolRemapInstructionOperandFromCache(this, methodRefCacheIndex), constants.jvmMethodref);
int op = compilerToVM().isResolvedInvokeHandleInPool(this, methodRefCacheIndex);
return op == opcode;
}
return false;
}
public String getSourceFileName() {
final int sourceFileNameIndex = UNSAFE.getChar(getMetaspaceConstantPool() + config().constantPoolSourceFileNameIndexOffset);
if (sourceFileNameIndex == 0) {
return null;
}
return lookupUtf8(sourceFileNameIndex);
}
@Override
public String toString() {
HotSpotResolvedObjectType holder = getHolder();
return "HotSpotConstantPool<" + holder.toJavaName() + ">";
}
}