4747
4848/**
4949 * Implementation of {@link ConstantPool} for HotSpot.
50+ *
51+ * The following convention is used in the jdk.vm.ci.hotspot package when accessing the ConstantPool with an index:
52+ * <ul>
53+ * <li>rawIndex - Index in the bytecode stream after the opcode (could be rewritten for some opcodes)</li>
54+ * <li>cpi - The constant pool index (as specified in JVM Spec)</li>
55+ * <li>cpci - The constant pool cache index, used only by the four bytecodes INVOKE{VIRTUAL,SPECIAL,STATIC,INTERFACE}.
56+ * It's the same as {@code rawIndex + HotSpotVMConfig::constantPoolCpCacheIndexTag}. </li>
57+ * <li>which - May be either a {@code rawIndex} or a {@code cpci}.</li>
58+ * </ul>
59+ *
60+ * Note that {@code cpci} and {@code which} are used only in the HotSpot-specific implementation. They
61+ * are not used by the public interface in jdk.vm.ci.meta.*.
62+ * After JDK-8301993, all uses of {@code cpci} and {@code which} will be replaced with {@code rawIndex}.
5063 */
5164public final class HotSpotConstantPool implements ConstantPool , MetaspaceHandleObject {
5265
@@ -252,25 +265,15 @@ private HotSpotResolvedObjectType getHolder() {
252265 * @return constant pool cache index
253266 */
254267 private static int rawIndexToConstantPoolCacheIndex (int rawIndex , int opcode ) {
255- int index ;
256- if (opcode == Bytecodes .INVOKEDYNAMIC ) {
257- index = rawIndex ;
258- // See: ConstantPool::is_invokedynamic_index
259- if (index >= 0 ) {
260- throw new IllegalArgumentException ("not an invokedynamic constant pool index " + index );
261- }
268+ if (opcode == Bytecodes .INVOKEINTERFACE ||
269+ opcode == Bytecodes .INVOKEVIRTUAL ||
270+ opcode == Bytecodes .INVOKESPECIAL ||
271+ opcode == Bytecodes .INVOKESTATIC ) {
272+ return rawIndex + config ().constantPoolCpCacheIndexTag ;
262273 } else {
263- if (opcode == Bytecodes .INVOKEINTERFACE ||
264- opcode == Bytecodes .INVOKEVIRTUAL ||
265- opcode == Bytecodes .INVOKESPECIAL ||
266- opcode == Bytecodes .INVOKESTATIC ) {
267- index = rawIndex + config ().constantPoolCpCacheIndexTag ;
268- } else {
269- throw new IllegalArgumentException ("unexpected opcode " + opcode );
270-
271- }
274+ // Only the above 4 bytecodes use ConstantPoolCacheIndex
275+ throw new IllegalArgumentException ("unexpected opcode " + opcode );
272276 }
273- return index ;
274277 }
275278
276279 /**
@@ -581,8 +584,8 @@ private static String argumentAsString(JavaConstant arg) {
581584 }
582585
583586 @ Override
584- public BootstrapMethodInvocation lookupBootstrapMethodInvocation (int rawCpi , int opcode ) {
585- int cpi = opcode == -1 ? rawCpi : rawIndexToConstantPoolIndex ( rawCpi , opcode );
587+ public BootstrapMethodInvocation lookupBootstrapMethodInvocation (int index , int opcode ) {
588+ int cpi = opcode == -1 ? index : indyIndexConstantPoolIndex ( index , opcode );
586589 final JvmConstant tag = getTagAt (cpi );
587590 switch (tag .name ) {
588591 case "InvokeDynamic" :
@@ -685,13 +688,19 @@ public Signature lookupSignature(int cpi) {
685688 }
686689
687690 @ Override
688- public JavaConstant lookupAppendix (int cpi , int opcode ) {
691+ public JavaConstant lookupAppendix (int rawIndex , int opcode ) {
689692 if (!Bytecodes .isInvoke (opcode )) {
690- throw new IllegalArgumentException ("expected an invoke bytecode at " + cpi + ", got " + opcode );
693+ throw new IllegalArgumentException ("expected an invoke bytecode for " + rawIndex + ", got " + opcode );
691694 }
692695
693- final int index = rawIndexToConstantPoolCacheIndex (cpi , opcode );
694- return compilerToVM ().lookupAppendixInPool (this , index );
696+ if (opcode == Bytecodes .INVOKEDYNAMIC ) {
697+ if (!isInvokedynamicIndex (rawIndex )) {
698+ throw new IllegalArgumentException ("expected a raw index for INVOKEDYNAMIC but got " + rawIndex );
699+ }
700+ return compilerToVM ().lookupAppendixInPool (this , rawIndex );
701+ } else {
702+ return compilerToVM ().lookupAppendixInPool (this , rawIndexToConstantPoolCacheIndex (rawIndex , opcode ));
703+ }
695704 }
696705
697706 /**
@@ -709,19 +718,19 @@ private static JavaType getJavaType(final Object type) {
709718 }
710719
711720 @ Override
712- public JavaMethod lookupMethod (int cpi , int opcode , ResolvedJavaMethod caller ) {
713- final int index = rawIndexToConstantPoolCacheIndex (cpi , opcode );
714- final HotSpotResolvedJavaMethod method = compilerToVM ().lookupMethodInPool (this , index , (byte ) opcode , (HotSpotResolvedJavaMethodImpl ) caller );
721+ public JavaMethod lookupMethod (int rawIndex , int opcode , ResolvedJavaMethod caller ) {
722+ final int cpci = rawIndexToConstantPoolCacheIndex (rawIndex , opcode );
723+ final HotSpotResolvedJavaMethod method = compilerToVM ().lookupMethodInPool (this , cpci , (byte ) opcode , (HotSpotResolvedJavaMethodImpl ) caller );
715724 if (method != null ) {
716725 return method ;
717726 } else {
718727 // Get the method's name and signature.
719- String name = getNameOf (index , opcode );
720- HotSpotSignature signature = new HotSpotSignature (runtime (), getSignatureOf (index , opcode ));
728+ String name = getNameOf (cpci , opcode );
729+ HotSpotSignature signature = new HotSpotSignature (runtime (), getSignatureOf (cpci , opcode ));
721730 if (opcode == Bytecodes .INVOKEDYNAMIC ) {
722731 return new UnresolvedJavaMethod (name , signature , runtime ().getMethodHandleClass ());
723732 } else {
724- final int klassIndex = getKlassRefIndexAt (index , opcode );
733+ final int klassIndex = getKlassRefIndexAt (cpci , opcode );
725734 final Object type = compilerToVM ().lookupKlassInPool (this , klassIndex );
726735 return new UnresolvedJavaMethod (name , signature , getJavaType (type ));
727736 }
@@ -780,7 +789,6 @@ public JavaType lookupReferencedType(int rawIndex, int opcode) {
780789
781790 @ Override
782791 public JavaField lookupField (int rawIndex , ResolvedJavaMethod method , int opcode ) {
783- final int cpi = compilerToVM ().decodeFieldIndexToCPIndex (this , rawIndex );
784792 final int nameAndTypeIndex = getNameAndTypeRefIndexAt (rawIndex , opcode );
785793 final int typeIndex = getSignatureRefIndexAt (nameAndTypeIndex );
786794 String typeName = lookupUtf8 (typeIndex );
@@ -813,32 +821,23 @@ public JavaField lookupField(int rawIndex, ResolvedJavaMethod method, int opcode
813821 }
814822
815823 /**
816- * Converts a raw index from the bytecodes to a constant pool index (not a cache index) .
824+ * Converts a raw index for the INVOKEDYNAMIC bytecode to a constant pool index.
817825 *
818826 * @param rawIndex index from the bytecode
819827 *
820- * @param opcode bytecode to convert the index for
828+ * @param opcode bytecode to convert the index for. Must be INVOKEDYNAMIC.
821829 *
822830 * @return constant pool index
823831 */
824- public int rawIndexToConstantPoolIndex (int rawIndex , int opcode ) {
832+ private int indyIndexConstantPoolIndex (int rawIndex , int opcode ) {
825833 if (isInvokedynamicIndex (rawIndex )) {
826834 if (opcode != Bytecodes .INVOKEDYNAMIC ) {
827835 throw new IllegalArgumentException ("expected INVOKEDYNAMIC at " + rawIndex + ", got " + opcode );
828836 }
829837 return compilerToVM ().decodeIndyIndexToCPIndex (this , rawIndex , false );
838+ } else {
839+ throw new IllegalArgumentException ("expected a raw index for INVOKEDYNAMIC but got " + rawIndex );
830840 }
831- if (opcode == Bytecodes .INVOKEDYNAMIC ) {
832- throw new IllegalArgumentException ("unexpected INVOKEDYNAMIC at " + rawIndex );
833- }
834- if (opcode == Bytecodes .GETSTATIC ||
835- opcode == Bytecodes .PUTSTATIC ||
836- opcode == Bytecodes .GETFIELD ||
837- opcode == Bytecodes .PUTFIELD ) {
838- return compilerToVM ().decodeFieldIndexToCPIndex (this , rawIndex );
839- }
840- int index = rawIndexToConstantPoolCacheIndex (rawIndex , opcode );
841- return compilerToVM ().constantPoolRemapInstructionOperandFromCache (this , index );
842841 }
843842
844843 @ Override
0 commit comments