5050import static com .oracle .truffle .espresso .substitutions .jvmci .Target_com_oracle_truffle_espresso_jvmci_meta_EspressoResolvedInstanceType .toJVMCIField ;
5151import static com .oracle .truffle .espresso .substitutions .jvmci .Target_com_oracle_truffle_espresso_jvmci_meta_EspressoResolvedInstanceType .toJVMCIMethod ;
5252
53- import java .util .logging .Level ;
54-
5553import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
5654import com .oracle .truffle .espresso .classfile .ConstantPool ;
5755import com .oracle .truffle .espresso .classfile .JavaKind ;
@@ -212,19 +210,22 @@ private static Field tryResolveField(int fieldIndex, Klass symbolicHolder, Runti
212210 RuntimeConstantPool constantPool = getRuntimeConstantPool (self , meta );
213211 if (safeTagAt (constantPool , cpi , meta ) == ConstantPool .Tag .CLASS ) {
214212 ResolvedConstant resolvedConstant = constantPool .peekResolvedOrNull (cpi , meta );
215- if (resolvedConstant == null || !resolvedConstant .isSuccess ()) {
216- return toJVMCIUnresolvedType (TypeSymbols .nameToType (constantPool .className (cpi )), meta );
217- }
218- Klass klass = (Klass ) resolvedConstant .value ();
219- LOGGER .finer (() -> "ECP.lookupType found " + klass );
220- return toJVMCIObjectType (klass , meta );
213+ return resolvedConstantToJVMCIObjectType (resolvedConstant , constantPool , cpi , meta );
221214 }
222215 if (safeTagAt (constantPool , cpi , meta ) == ConstantPool .Tag .UTF8 ) {
223216 return toJVMCIUnresolvedType (TypeSymbols .nameToType (constantPool .utf8At (cpi )), meta );
224217 }
225218 throw meta .throwIllegalArgumentExceptionBoundary ();
226219 }
227220
221+ private static StaticObject resolvedConstantToJVMCIObjectType (ResolvedConstant resolvedConstant , RuntimeConstantPool constantPool , int cpi , Meta meta ) {
222+ if (resolvedConstant == null || !resolvedConstant .isSuccess ()) {
223+ return toJVMCIUnresolvedType (TypeSymbols .nameToType (constantPool .className (cpi )), meta );
224+ }
225+ Klass klass = (Klass ) resolvedConstant .value ();
226+ return toJVMCIObjectType (klass , meta );
227+ }
228+
228229 @ Substitution (hasReceiver = true )
229230 @ TruffleBoundary
230231 public static @ JavaType (internalName = "Lcom/oracle/truffle/espresso/jvmci/meta/EspressoResolvedJavaMethod;" ) StaticObject lookupResolvedMethod (StaticObject self , int cpi , int opcode ,
@@ -358,6 +359,7 @@ private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, Ru
358359 StaticObject cpHolder = meta .jvmci .EspressoConstantPool_holder .getObject (self );
359360 ObjectKlass cpHolderKlass = (ObjectKlass ) meta .jvmci .HIDDEN_OBJECTKLASS_MIRROR .getHiddenObject (cpHolder );
360361 RuntimeConstantPool constantPool = cpHolderKlass .getConstantPool ();
362+ int classCpi ;
361363 switch (opcode ) {
362364 case CHECKCAST :
363365 case INSTANCEOF :
@@ -367,15 +369,10 @@ private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, Ru
367369 case LDC :
368370 case LDC_W :
369371 case LDC2_W :
370- if (safeTagAt (constantPool , cpi , meta ) == ConstantPool .Tag .CLASS ) {
371- ResolvedConstant resolvedConstant = constantPool .peekResolvedOrNull (cpi , meta );
372- if (resolvedConstant == null || !resolvedConstant .isSuccess ()) {
373- return toJVMCIUnresolvedType (TypeSymbols .nameToType (constantPool .className (cpi )), meta );
374- }
375- Klass klass = (Klass ) resolvedConstant .value ();
376- LOGGER .finer (() -> "ECP.lookupReferencedType found " + klass );
377- return toJVMCIObjectType (klass , meta );
372+ if (safeTagAt (constantPool , cpi , meta ) != ConstantPool .Tag .CLASS ) {
373+ throw meta .throwIllegalArgumentExceptionBoundary ("Opcode and constant pool entry types mismatch" );
378374 }
375+ classCpi = cpi ;
379376 break ;
380377 case GETSTATIC :
381378 case PUTSTATIC :
@@ -385,27 +382,28 @@ private static Method tryResolveMethod(int methodIndex, Klass symbolicHolder, Ru
385382 case INVOKESPECIAL :
386383 case INVOKESTATIC :
387384 case INVOKEINTERFACE :
388- if (safeTagAt (constantPool , cpi , meta ).isMember ()) {
389- int holderClassIndex = constantPool .memberClassIndex (cpi );
390- Klass holderKlass ;
391- try {
392- holderKlass = findObjectType (holderClassIndex , constantPool , false , meta );
393- } catch (EspressoException e ) {
394- LOGGER .log (Level .FINE , "ECP.lookupReferencedType exception during lookup" , e );
395- holderKlass = null ;
396- }
397- if (holderKlass == null ) {
398- Symbol <Name > holderName = constantPool .memberClassName (cpi );
399- return toJVMCIUnresolvedType (TypeSymbols .nameToType (holderName ), meta );
400- }
401- Klass finalHolderKlass = holderKlass ;
402- LOGGER .finer (() -> "ECP.lookupReferencedType found " + finalHolderKlass );
403- return toJVMCIObjectType (holderKlass , meta );
385+ if (!safeTagAt (constantPool , cpi , meta ).isMember ()) {
386+ throw meta .throwIllegalArgumentExceptionBoundary ("Opcode and constant pool entry types mismatch" );
404387 }
388+ classCpi = constantPool .memberClassIndex (cpi );
405389 break ;
406- }
407- LOGGER .warning (() -> "Unsupported CP entry type for lookupReferencedType: " + safeTagAt (constantPool , cpi , meta ) + " " + constantPool .toString (cpi ) + " for " + Bytecodes .nameOf (opcode ));
408- throw meta .throwIllegalArgumentExceptionBoundary ();
390+ default :
391+ LOGGER .warning (() -> "Unsupported CP entry type for lookupReferencedType: " + safeTagAt (constantPool , cpi , meta ) + " " + constantPool .toString (cpi ) + " for " +
392+ Bytecodes .nameOf (opcode ));
393+ throw meta .throwIllegalArgumentExceptionBoundary ("Unsupported CP entry type" );
394+ }
395+ Klass klass ;
396+ try {
397+ klass = findObjectType (classCpi , constantPool , false , meta );
398+ } catch (EspressoException e ) {
399+ throw EspressoError .shouldNotReachHere ("findObjectType with resolve=false should never throw" , e );
400+ }
401+ if (klass == null ) {
402+ Symbol <Name > className = constantPool .className (classCpi );
403+ return toJVMCIUnresolvedType (TypeSymbols .nameToType (className ), meta );
404+ }
405+ LOGGER .finer (() -> "ECP.lookupReferencedType found " + klass );
406+ return toJVMCIObjectType (klass , meta );
409407 }
410408
411409 @ Substitution (hasReceiver = true )
@@ -506,27 +504,13 @@ public static boolean loadReferencedType0(StaticObject self, int cpi, int opcode
506504 }
507505 switch (tag ) {
508506 case CLASS -> {
509- if (resolvedConstantOrNull == null || !resolvedConstantOrNull .isSuccess ()) {
510- EspressoError .guarantee (!resolve || resolvedConstantOrNull != null , "Should have been resolved" );
511- return toJVMCIUnresolvedType (TypeSymbols .nameToType (constantPool .className (cpi )), meta );
512- }
513- Klass klass = (Klass ) resolvedConstantOrNull .value ();
514- LOGGER .finer (() -> "ECP.lookupConstant found " + klass );
515- return toJVMCIObjectType (klass , meta );
507+ EspressoError .guarantee (!resolve || resolvedConstantOrNull != null , "Should have been resolved" );
508+ return resolvedConstantToJVMCIObjectType (resolvedConstantOrNull , constantPool , cpi , meta );
516509 }
517510 case STRING -> {
518511 return wrapEspressoObjectConstant (constantPool .resolvedStringAt (cpi ), meta );
519512 }
520- case METHODHANDLE -> {
521- if (resolvedConstantOrNull != null ) {
522- return wrapEspressoObjectConstant ((StaticObject ) resolvedConstantOrNull .value (), meta );
523- } else if (resolve ) {
524- throw EspressoError .shouldNotReachHere ();
525- } else {
526- return StaticObject .NULL ;
527- }
528- }
529- case METHODTYPE -> {
513+ case METHODHANDLE , METHODTYPE -> {
530514 if (resolvedConstantOrNull != null ) {
531515 return wrapEspressoObjectConstant ((StaticObject ) resolvedConstantOrNull .value (), meta );
532516 } else if (resolve ) {
0 commit comments