1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <algorithm>
18 
19 #include "instruction_set_features.h"
20 
21 #include <algorithm>
22 #include <ostream>
23 
24 #include "android-base/strings.h"
25 
26 #include "base/casts.h"
27 #include "base/utils.h"
28 
29 #include "arm/instruction_set_features_arm.h"
30 #include "arm64/instruction_set_features_arm64.h"
31 #include "x86/instruction_set_features_x86.h"
32 #include "x86_64/instruction_set_features_x86_64.h"
33 
34 namespace art {
35 
FromVariant(InstructionSet isa,const std::string & variant,std::string * error_msg)36 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromVariant(
37     InstructionSet isa, const std::string& variant, std::string* error_msg) {
38   switch (isa) {
39     case InstructionSet::kArm:
40     case InstructionSet::kThumb2:
41       return ArmInstructionSetFeatures::FromVariant(variant, error_msg);
42     case InstructionSet::kArm64:
43       return Arm64InstructionSetFeatures::FromVariant(variant, error_msg);
44     case InstructionSet::kX86:
45       return X86InstructionSetFeatures::FromVariant(variant, error_msg);
46     case InstructionSet::kX86_64:
47       return X86_64InstructionSetFeatures::FromVariant(variant, error_msg);
48 
49     default:
50       break;
51   }
52   UNIMPLEMENTED(FATAL) << isa;
53   UNREACHABLE();
54 }
55 
FromBitmap(InstructionSet isa,uint32_t bitmap)56 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromBitmap(InstructionSet isa,
57                                                                                  uint32_t bitmap) {
58   std::unique_ptr<const InstructionSetFeatures> result;
59   switch (isa) {
60     case InstructionSet::kArm:
61     case InstructionSet::kThumb2:
62       result = ArmInstructionSetFeatures::FromBitmap(bitmap);
63       break;
64     case InstructionSet::kArm64:
65       result = Arm64InstructionSetFeatures::FromBitmap(bitmap);
66       break;
67     case InstructionSet::kX86:
68       result = X86InstructionSetFeatures::FromBitmap(bitmap);
69       break;
70     case InstructionSet::kX86_64:
71       result = X86_64InstructionSetFeatures::FromBitmap(bitmap);
72       break;
73 
74     default:
75       UNIMPLEMENTED(FATAL) << isa;
76       UNREACHABLE();
77   }
78   CHECK_EQ(bitmap, result->AsBitmap());
79   return result;
80 }
81 
FromCppDefines()82 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCppDefines() {
83   switch (kRuntimeISA) {
84     case InstructionSet::kArm:
85     case InstructionSet::kThumb2:
86       return ArmInstructionSetFeatures::FromCppDefines();
87     case InstructionSet::kArm64:
88       return Arm64InstructionSetFeatures::FromCppDefines();
89     case InstructionSet::kX86:
90       return X86InstructionSetFeatures::FromCppDefines();
91     case InstructionSet::kX86_64:
92       return X86_64InstructionSetFeatures::FromCppDefines();
93 
94     default:
95       break;
96   }
97   UNIMPLEMENTED(FATAL) << kRuntimeISA;
98   UNREACHABLE();
99 }
100 
FromRuntimeDetection()101 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromRuntimeDetection() {
102   switch (kRuntimeISA) {
103 #ifdef ART_TARGET_ANDROID
104     case InstructionSet::kArm64:
105       return Arm64InstructionSetFeatures::FromHwcap();
106 #endif
107     default:
108       return nullptr;
109   }
110 }
111 
FromCpuInfo()112 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCpuInfo() {
113   switch (kRuntimeISA) {
114     case InstructionSet::kArm:
115     case InstructionSet::kThumb2:
116       return ArmInstructionSetFeatures::FromCpuInfo();
117     case InstructionSet::kArm64:
118       return Arm64InstructionSetFeatures::FromCpuInfo();
119     case InstructionSet::kX86:
120       return X86InstructionSetFeatures::FromCpuInfo();
121     case InstructionSet::kX86_64:
122       return X86_64InstructionSetFeatures::FromCpuInfo();
123 
124     default:
125       break;
126   }
127   UNIMPLEMENTED(FATAL) << kRuntimeISA;
128   UNREACHABLE();
129 }
130 
FromHwcap()131 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromHwcap() {
132   switch (kRuntimeISA) {
133     case InstructionSet::kArm:
134     case InstructionSet::kThumb2:
135       return ArmInstructionSetFeatures::FromHwcap();
136     case InstructionSet::kArm64:
137       return Arm64InstructionSetFeatures::FromHwcap();
138     case InstructionSet::kX86:
139       return X86InstructionSetFeatures::FromHwcap();
140     case InstructionSet::kX86_64:
141       return X86_64InstructionSetFeatures::FromHwcap();
142 
143     default:
144       break;
145   }
146   UNIMPLEMENTED(FATAL) << kRuntimeISA;
147   UNREACHABLE();
148 }
149 
FromAssembly()150 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromAssembly() {
151   switch (kRuntimeISA) {
152     case InstructionSet::kArm:
153     case InstructionSet::kThumb2:
154       return ArmInstructionSetFeatures::FromAssembly();
155     case InstructionSet::kArm64:
156       return Arm64InstructionSetFeatures::FromAssembly();
157     case InstructionSet::kX86:
158       return X86InstructionSetFeatures::FromAssembly();
159     case InstructionSet::kX86_64:
160       return X86_64InstructionSetFeatures::FromAssembly();
161 
162     default:
163       break;
164   }
165   UNIMPLEMENTED(FATAL) << kRuntimeISA;
166   UNREACHABLE();
167 }
168 
FromCpuFeatures()169 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCpuFeatures() {
170   switch (kRuntimeISA) {
171     case InstructionSet::kArm:
172     case InstructionSet::kThumb2:
173       return ArmInstructionSetFeatures::FromCpuFeatures();
174     case InstructionSet::kArm64:
175       return Arm64InstructionSetFeatures::FromCpuFeatures();
176     case InstructionSet::kX86:
177       return X86InstructionSetFeatures::FromCpuFeatures();
178     case InstructionSet::kX86_64:
179       return X86_64InstructionSetFeatures::FromCpuFeatures();
180 
181     default:
182       break;
183   }
184   UNIMPLEMENTED(FATAL) << kRuntimeISA;
185   UNREACHABLE();
186 }
187 
AddFeaturesFromString(const std::string & feature_list,std::string * error_msg) const188 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::AddFeaturesFromString(
189     const std::string& feature_list, /* out */ std::string* error_msg) const {
190   std::vector<std::string> features;
191   Split(feature_list, ',', &features);
192   std::transform(std::begin(features), std::end(features), std::begin(features),
193                  [](const std::string &s) { return android::base::Trim(s); });
194   auto empty_strings_begin = std::copy_if(std::begin(features), std::end(features),
195                                           std::begin(features),
196                                           [](const std::string& s) { return !s.empty(); });
197   features.erase(empty_strings_begin, std::end(features));
198   if (features.empty()) {
199     *error_msg = "No instruction set features specified";
200     return nullptr;
201   }
202 
203   bool use_default = false;
204   bool use_runtime_detection = false;
205   for (const std::string& feature : features) {
206     if (feature == "default") {
207       if (features.size() > 1) {
208         *error_msg = "Specific instruction set feature(s) cannot be used when 'default' is used.";
209         return nullptr;
210       }
211       use_default = true;
212       features.pop_back();
213       break;
214     } else if (feature == "runtime") {
215       if (features.size() > 1) {
216         *error_msg = "Specific instruction set feature(s) cannot be used when 'runtime' is used.";
217         return nullptr;
218       }
219       use_runtime_detection = true;
220       features.pop_back();
221       break;
222     }
223   }
224   // Expectation: "default" and "runtime" are standalone, no other feature names.
225   // But an empty features vector after processing can also come along if the
226   // handled feature names  are the only ones in the list. So
227   // logically, we check "default or runtime => features.empty."
228   DCHECK((!use_default && !use_runtime_detection) || features.empty());
229 
230   std::unique_ptr<const InstructionSetFeatures> runtime_detected_features;
231   if (use_runtime_detection) {
232     runtime_detected_features = FromRuntimeDetection();
233   }
234 
235   if (runtime_detected_features != nullptr) {
236     return AddRuntimeDetectedFeatures(runtime_detected_features.get());
237   } else {
238     return AddFeaturesFromSplitString(features, error_msg);
239   }
240 }
241 
AsArmInstructionSetFeatures() const242 const ArmInstructionSetFeatures* InstructionSetFeatures::AsArmInstructionSetFeatures() const {
243   DCHECK_EQ(InstructionSet::kArm, GetInstructionSet());
244   return down_cast<const ArmInstructionSetFeatures*>(this);
245 }
246 
AsArm64InstructionSetFeatures() const247 const Arm64InstructionSetFeatures* InstructionSetFeatures::AsArm64InstructionSetFeatures() const {
248   DCHECK_EQ(InstructionSet::kArm64, GetInstructionSet());
249   return down_cast<const Arm64InstructionSetFeatures*>(this);
250 }
251 
AsX86InstructionSetFeatures() const252 const X86InstructionSetFeatures* InstructionSetFeatures::AsX86InstructionSetFeatures() const {
253   DCHECK(InstructionSet::kX86 == GetInstructionSet() ||
254          InstructionSet::kX86_64 == GetInstructionSet());
255   return down_cast<const X86InstructionSetFeatures*>(this);
256 }
257 
AsX86_64InstructionSetFeatures() const258 const X86_64InstructionSetFeatures* InstructionSetFeatures::AsX86_64InstructionSetFeatures() const {
259   DCHECK_EQ(InstructionSet::kX86_64, GetInstructionSet());
260   return down_cast<const X86_64InstructionSetFeatures*>(this);
261 }
262 
FindVariantInArray(const char * const variants[],size_t num_variants,const std::string & variant)263 bool InstructionSetFeatures::FindVariantInArray(const char* const variants[], size_t num_variants,
264                                                 const std::string& variant) {
265   const char* const * begin = variants;
266   const char* const * end = begin + num_variants;
267   return std::find(begin, end, variant) != end;
268 }
269 
AddRuntimeDetectedFeatures(const InstructionSetFeatures * features ATTRIBUTE_UNUSED) const270 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::AddRuntimeDetectedFeatures(
271     const InstructionSetFeatures *features ATTRIBUTE_UNUSED) const {
272   UNIMPLEMENTED(FATAL) << kRuntimeISA;
273   UNREACHABLE();
274 }
275 
operator <<(std::ostream & os,const InstructionSetFeatures & rhs)276 std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs) {
277   os << "ISA: " << rhs.GetInstructionSet() << " Feature string: " << rhs.GetFeatureString();
278   return os;
279 }
280 
281 }  // namespace art
282