2727import java .util .Map ;
2828import java .util .concurrent .ConcurrentHashMap ;
2929
30- import org .graalvm .collections .Pair ;
3130import org .graalvm .nativeimage .ImageSingletons ;
3231
3332import com .oracle .svm .util .LogUtils ;
3433import com .oracle .svm .hosted .dataflow .AbstractFrame ;
3534import com .oracle .svm .hosted .dataflow .DataFlowAnalysisException ;
3635
3736import jdk .graal .compiler .bytecode .Bytecode ;
38- import jdk .graal .compiler .bytecode .BytecodeProvider ;
39- import jdk .graal .compiler .bytecode .ResolvedJavaMethodBytecodeProvider ;
40- import jdk .graal .compiler .nodes .graphbuilderconf .IntrinsicContext ;
37+ import jdk .vm .ci .code .BytecodePosition ;
4138import jdk .vm .ci .meta .JavaKind ;
4239import jdk .vm .ci .meta .ResolvedJavaMethod ;
4340
4441/**
4542 * Holds information on constant expressions as inferred by {@link ConstantExpressionAnalyzer}.
4643 */
47- public class ConstantExpressionRegistry {
44+ public final class ConstantExpressionRegistry {
4845
4946 public static ConstantExpressionRegistry singleton () {
5047 return ImageSingletons .lookup (ConstantExpressionRegistry .class );
@@ -59,32 +56,29 @@ public static ConstantExpressionRegistry singleton() {
5956 * Maps method and BCI pairs into abstract frames which represent the execution frame right
6057 * before the corresponding bytecode instruction.
6158 */
62- private Map <Pair < ResolvedJavaMethod , Integer >, AbstractFrame <ConstantExpressionAnalyzer .Value >> registry ;
63- private boolean isSealed ;
59+ private Map <BytecodePosition , AbstractFrame <ConstantExpressionAnalyzer .Value >> registry = new ConcurrentHashMap <>() ;
60+ private boolean sealed = false ;
6461
65- public ConstantExpressionRegistry () {
66- registry = new ConcurrentHashMap <>();
67- isSealed = false ;
62+ private final ConstantExpressionAnalyzer analyzer ;
63+
64+ ConstantExpressionRegistry (ConstantExpressionAnalyzer analyzer ) {
65+ this .analyzer = analyzer ;
6866 }
6967
70- public void analyzeAndStore (ConstantExpressionAnalyzer analyzer , ResolvedJavaMethod method , IntrinsicContext intrinsicContext ) {
71- assert !isSealed () : "Cannot store in registry when it is already sealed" ;
72- Bytecode bytecode = getBytecode (method , intrinsicContext );
68+ /**
69+ * Analyze the provided {@code bytecode} for constant expressions and store the results in the
70+ * registry.
71+ */
72+ public void store (Bytecode bytecode ) {
73+ assert !sealed : "Cannot store in registry when it is already sealed" ;
7374 try {
7475 Map <Integer , AbstractFrame <ConstantExpressionAnalyzer .Value >> abstractFrames = analyzer .analyze (bytecode );
75- abstractFrames .forEach ((key , value ) -> registry .put (Pair . create ( method , key ), value ));
76+ abstractFrames .forEach ((key , value ) -> registry .put (new BytecodePosition ( null , bytecode . getMethod () , key ), value ));
7677 } catch (DataFlowAnalysisException e ) {
77- LogUtils .warning ("Constant expression analysis failed for " + method .format ("%H.%n(%p)" ) + ": " + e .getMessage ());
78+ LogUtils .warning ("Constant expression analysis failed for " + bytecode . getMethod () .format ("%H.%n(%p)" ) + ": " + e .getMessage ());
7879 }
7980 }
8081
81- private static Bytecode getBytecode (ResolvedJavaMethod method , IntrinsicContext intrinsicContext ) {
82- BytecodeProvider bytecodeProvider = intrinsicContext == null
83- ? ResolvedJavaMethodBytecodeProvider .INSTANCE
84- : intrinsicContext .getBytecodeProvider ();
85- return bytecodeProvider .getBytecode (method );
86- }
87-
8882 /**
8983 * Attempt to get the inferred receiver of a {@code targetMethod} invocation at the specified
9084 * code location.
@@ -96,9 +90,12 @@ private static Bytecode getBytecode(ResolvedJavaMethod method, IntrinsicContext
9690 * {@code null} value is represented by {@link ConstantExpressionRegistry#NULL_MARKER}.
9791 */
9892 public Object getReceiver (ResolvedJavaMethod callerMethod , int bci , ResolvedJavaMethod targetMethod ) {
99- assert !isSealed () : "Registry is already sealed" ;
93+ assert !sealed : "Registry is already sealed" ;
10094 assert targetMethod .hasReceiver () : "Method " + targetMethod + " does not have receiver" ;
101- AbstractFrame <ConstantExpressionAnalyzer .Value > frame = registry .get (Pair .create (callerMethod , bci ));
95+ if (callerMethod == null ) {
96+ return null ;
97+ }
98+ AbstractFrame <ConstantExpressionAnalyzer .Value > frame = registry .get (new BytecodePosition (null , callerMethod , bci ));
10299 if (frame == null ) {
103100 return null ;
104101 }
@@ -138,10 +135,13 @@ public <T> T getReceiver(ResolvedJavaMethod callerMethod, int bci, ResolvedJavaM
138135 * {@code null} value is represented by {@link ConstantExpressionRegistry#NULL_MARKER}.
139136 */
140137 public Object getArgument (ResolvedJavaMethod callerMethod , int bci , ResolvedJavaMethod targetMethod , int index ) {
141- assert !isSealed () : "Registry is already sealed" ;
138+ assert !sealed : "Registry is already sealed" ;
142139 int numOfParameters = targetMethod .getSignature ().getParameterCount (false );
143140 assert 0 <= index && index < numOfParameters : "Argument index " + index + " out of bounds for " + targetMethod ;
144- AbstractFrame <ConstantExpressionAnalyzer .Value > frame = registry .get (Pair .create (callerMethod , bci ));
141+ if (callerMethod == null ) {
142+ return null ;
143+ }
144+ AbstractFrame <ConstantExpressionAnalyzer .Value > frame = registry .get (new BytecodePosition (null , callerMethod , bci ));
145145 if (frame == null ) {
146146 return null ;
147147 }
@@ -193,12 +193,8 @@ private static <T> T tryToCast(Object value, Class<T> type) {
193193 return type .cast (value );
194194 }
195195
196- public boolean isSealed () {
197- return isSealed ;
198- }
199-
200- public void seal () {
201- isSealed = true ;
196+ void seal () {
197+ sealed = true ;
202198 registry = null ;
203199 }
204200
0 commit comments