27
27
import java .util .List ;
28
28
import java .util .Map ;
29
29
import java .util .Map .Entry ;
30
+ import java .util .function .LongFunction ;
30
31
31
32
import jdk .vm .ci .common .JVMCIError ;
32
33
import jdk .vm .ci .runtime .JVMCIBackend ;
33
- import static jdk .vm .ci .hotspot .UnsafeAccess .UNSAFE ;
34
34
35
35
public interface HotSpotJVMCIBackendFactory {
36
36
@@ -48,7 +48,8 @@ public interface HotSpotJVMCIBackendFactory {
48
48
* @param enumType the class of {@code CPUFeatureType}
49
49
* @param constants VM constants. Each entry whose key starts with {@code "VM_Version::CPU_"}
50
50
* specifies a CPU feature and its value is a mask for a bit in {@code features}
51
- * @param features bits specifying CPU features
51
+ * @param bitMaskSupplier supplier to get the bit mask for the corresponding VM constant
52
+ * @param featuresSupplier supplier to get the bits specifying CPU features
52
53
* @param renaming maps from VM feature names to enum constant names where the two differ
53
54
* @throws IllegalArgumentException if any VM CPU feature constant cannot be converted to an
54
55
* enum value
@@ -57,18 +58,19 @@ public interface HotSpotJVMCIBackendFactory {
57
58
static <CPUFeatureType extends Enum <CPUFeatureType >> EnumSet <CPUFeatureType > convertFeatures (
58
59
Class <CPUFeatureType > enumType ,
59
60
Map <String , Long > constants ,
60
- long features ,
61
+ LongFunction <Long > bitMaskSupplier ,
62
+ LongFunction <Long > featuresSupplier ,
61
63
Map <String , String > renaming ) {
62
64
EnumSet <CPUFeatureType > outFeatures = EnumSet .noneOf (enumType );
63
65
List <String > missing = new ArrayList <>();
64
66
for (Entry <String , Long > e : constants .entrySet ()) {
65
- long bitMask = e .getValue ();
67
+ long bitMask = bitMaskSupplier . apply ( e .getValue () );
66
68
String key = e .getKey ();
67
69
if (key .startsWith ("VM_Version::CPU_" )) {
68
70
String name = key .substring ("VM_Version::CPU_" .length ());
69
71
try {
70
72
CPUFeatureType feature = Enum .valueOf (enumType , renaming .getOrDefault (name , name ));
71
- if ((features & bitMask ) != 0 ) {
73
+ if ((featuresSupplier . apply ( e . getValue ()) & bitMask ) != 0 ) {
72
74
outFeatures .add (feature );
73
75
}
74
76
} catch (IllegalArgumentException iae ) {
@@ -82,57 +84,4 @@ static <CPUFeatureType extends Enum<CPUFeatureType>> EnumSet<CPUFeatureType> con
82
84
return outFeatures ;
83
85
}
84
86
85
- /**
86
- * Converts CPU features bit map into enum constants.
87
- *
88
- * @param <CPUFeatureType> CPU feature enum type
89
- * @param enumType the class of {@code CPUFeatureType}
90
- * @param constants VM constants. Each entry whose key starts with {@code "VM_Version::CPU_"}
91
- * specifies a CPU feature and its value is a mask for a bit in {@code features}
92
- * @param featuresBitMapAddress pointer to {@code VM_Features::_features_bitmap} field of {@code VM_Version::_features}
93
- * @param featuresBitMapSize size of feature bit map in bytes
94
- * @param renaming maps from VM feature names to enum constant names where the two differ
95
- * @throws IllegalArgumentException if any VM CPU feature constant cannot be converted to an
96
- * enum value
97
- * @return the set of converted values
98
- */
99
- static <CPUFeatureType extends Enum <CPUFeatureType >> EnumSet <CPUFeatureType > convertFeatures (
100
- Class <CPUFeatureType > enumType ,
101
- Map <String , Long > constants ,
102
- long featuresBitMapAddress ,
103
- long featuresBitMapSize ,
104
- Map <String , String > renaming ) {
105
- EnumSet <CPUFeatureType > outFeatures = EnumSet .noneOf (enumType );
106
- List <String > missing = new ArrayList <>();
107
-
108
- for (Entry <String , Long > e : constants .entrySet ()) {
109
- String key = e .getKey ();
110
- long bitIndex = e .getValue ();
111
- if (key .startsWith ("VM_Version::CPU_" )) {
112
- String name = key .substring ("VM_Version::CPU_" .length ());
113
- try {
114
- final long featuresElementShiftCount = 6 ; // log (# of bits per long)
115
- final long featuresElementMask = (1L << featuresElementShiftCount ) - 1 ;
116
-
117
- CPUFeatureType feature = Enum .valueOf (enumType , renaming .getOrDefault (name , name ));
118
-
119
- long featureIndex = bitIndex >>> featuresElementShiftCount ;
120
- long featureBitMask = 1L << (bitIndex & featuresElementMask );
121
- assert featureIndex < featuresBitMapSize ;
122
-
123
- long featuresElement = UNSAFE .getLong (featuresBitMapAddress + featureIndex * Long .BYTES );
124
-
125
- if ((featuresElement & featureBitMask ) != 0 ) {
126
- outFeatures .add (feature );
127
- }
128
- } catch (IllegalArgumentException iae ) {
129
- missing .add (name );
130
- }
131
- }
132
- }
133
- if (!missing .isEmpty ()) {
134
- throw new JVMCIError ("Missing CPU feature constants: %s" , missing );
135
- }
136
- return outFeatures ;
137
- }
138
87
}
0 commit comments