Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 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 "instruction_set_features.h" |
| 18 | |
| 19 | #include "base/casts.h" |
| 20 | #include "utils.h" |
| 21 | |
| 22 | |
| 23 | #include "arm/instruction_set_features_arm.h" |
| 24 | #include "arm64/instruction_set_features_arm64.h" |
| 25 | #include "mips/instruction_set_features_mips.h" |
| 26 | #include "x86/instruction_set_features_x86.h" |
| 27 | #include "x86_64/instruction_set_features_x86_64.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | const InstructionSetFeatures* InstructionSetFeatures::FromVariant(InstructionSet isa, |
| 32 | const std::string& variant, |
| 33 | std::string* error_msg) { |
| 34 | const InstructionSetFeatures* result; |
| 35 | switch (isa) { |
| 36 | case kArm: |
| 37 | case kThumb2: |
| 38 | result = ArmInstructionSetFeatures::FromVariant(variant, error_msg); |
| 39 | break; |
| 40 | case kArm64: |
| 41 | result = Arm64InstructionSetFeatures::FromVariant(variant, error_msg); |
| 42 | break; |
| 43 | case kMips: |
| 44 | result = MipsInstructionSetFeatures::FromVariant(variant, error_msg); |
| 45 | break; |
| 46 | case kX86: |
| 47 | result = X86InstructionSetFeatures::FromVariant(variant, error_msg); |
| 48 | break; |
| 49 | case kX86_64: |
| 50 | result = X86_64InstructionSetFeatures::FromVariant(variant, error_msg); |
| 51 | break; |
| 52 | default: |
| 53 | UNIMPLEMENTED(FATAL) << isa; |
| 54 | UNREACHABLE(); |
| 55 | } |
| 56 | CHECK_EQ(result == nullptr, error_msg->size() != 0); |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | const InstructionSetFeatures* InstructionSetFeatures::FromBitmap(InstructionSet isa, |
| 61 | uint32_t bitmap) { |
| 62 | const InstructionSetFeatures* result; |
| 63 | switch (isa) { |
| 64 | case kArm: |
| 65 | case kThumb2: |
| 66 | result = ArmInstructionSetFeatures::FromBitmap(bitmap); |
| 67 | break; |
| 68 | case kArm64: |
| 69 | result = Arm64InstructionSetFeatures::FromBitmap(bitmap); |
| 70 | break; |
| 71 | case kMips: |
| 72 | result = MipsInstructionSetFeatures::FromBitmap(bitmap); |
| 73 | break; |
| 74 | case kX86: |
| 75 | result = X86InstructionSetFeatures::FromBitmap(bitmap); |
| 76 | break; |
| 77 | case kX86_64: |
| 78 | result = X86_64InstructionSetFeatures::FromBitmap(bitmap); |
| 79 | break; |
| 80 | default: |
| 81 | UNIMPLEMENTED(FATAL) << isa; |
| 82 | UNREACHABLE(); |
| 83 | } |
| 84 | CHECK_EQ(bitmap, result->AsBitmap()); |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | const InstructionSetFeatures* InstructionSetFeatures::FromCppDefines() { |
| 89 | const InstructionSetFeatures* result; |
| 90 | switch (kRuntimeISA) { |
| 91 | case kArm: |
| 92 | case kThumb2: |
| 93 | result = ArmInstructionSetFeatures::FromCppDefines(); |
| 94 | break; |
| 95 | case kArm64: |
| 96 | result = Arm64InstructionSetFeatures::FromCppDefines(); |
| 97 | break; |
| 98 | case kMips: |
| 99 | result = MipsInstructionSetFeatures::FromCppDefines(); |
| 100 | break; |
| 101 | case kX86: |
| 102 | result = X86InstructionSetFeatures::FromCppDefines(); |
| 103 | break; |
| 104 | case kX86_64: |
| 105 | result = X86_64InstructionSetFeatures::FromCppDefines(); |
| 106 | break; |
| 107 | default: |
| 108 | UNIMPLEMENTED(FATAL) << kRuntimeISA; |
| 109 | UNREACHABLE(); |
| 110 | } |
| 111 | return result; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | const InstructionSetFeatures* InstructionSetFeatures::FromCpuInfo() { |
| 116 | const InstructionSetFeatures* result; |
| 117 | switch (kRuntimeISA) { |
| 118 | case kArm: |
| 119 | case kThumb2: |
| 120 | result = ArmInstructionSetFeatures::FromCpuInfo(); |
| 121 | break; |
| 122 | case kArm64: |
| 123 | result = Arm64InstructionSetFeatures::FromCpuInfo(); |
| 124 | break; |
| 125 | case kMips: |
| 126 | result = MipsInstructionSetFeatures::FromCpuInfo(); |
| 127 | break; |
| 128 | case kX86: |
| 129 | result = X86InstructionSetFeatures::FromCpuInfo(); |
| 130 | break; |
| 131 | case kX86_64: |
| 132 | result = X86_64InstructionSetFeatures::FromCpuInfo(); |
| 133 | break; |
| 134 | default: |
| 135 | UNIMPLEMENTED(FATAL) << kRuntimeISA; |
| 136 | UNREACHABLE(); |
| 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | const InstructionSetFeatures* InstructionSetFeatures::FromHwcap() { |
| 142 | const InstructionSetFeatures* result; |
| 143 | switch (kRuntimeISA) { |
| 144 | case kArm: |
| 145 | case kThumb2: |
| 146 | result = ArmInstructionSetFeatures::FromHwcap(); |
| 147 | break; |
| 148 | case kArm64: |
| 149 | result = Arm64InstructionSetFeatures::FromHwcap(); |
| 150 | break; |
| 151 | case kMips: |
| 152 | result = MipsInstructionSetFeatures::FromHwcap(); |
| 153 | break; |
| 154 | case kX86: |
| 155 | result = X86InstructionSetFeatures::FromHwcap(); |
| 156 | break; |
| 157 | case kX86_64: |
| 158 | result = X86_64InstructionSetFeatures::FromHwcap(); |
| 159 | break; |
| 160 | default: |
| 161 | UNIMPLEMENTED(FATAL) << kRuntimeISA; |
| 162 | UNREACHABLE(); |
| 163 | } |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | const InstructionSetFeatures* InstructionSetFeatures::FromAssembly() { |
| 168 | const InstructionSetFeatures* result; |
| 169 | switch (kRuntimeISA) { |
| 170 | case kArm: |
| 171 | case kThumb2: |
| 172 | result = ArmInstructionSetFeatures::FromAssembly(); |
| 173 | break; |
| 174 | case kArm64: |
| 175 | result = Arm64InstructionSetFeatures::FromAssembly(); |
| 176 | break; |
| 177 | case kMips: |
| 178 | result = MipsInstructionSetFeatures::FromAssembly(); |
| 179 | break; |
| 180 | case kX86: |
| 181 | result = X86InstructionSetFeatures::FromAssembly(); |
| 182 | break; |
| 183 | case kX86_64: |
| 184 | result = X86_64InstructionSetFeatures::FromAssembly(); |
| 185 | break; |
| 186 | default: |
| 187 | UNIMPLEMENTED(FATAL) << kRuntimeISA; |
| 188 | UNREACHABLE(); |
| 189 | } |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | const InstructionSetFeatures* InstructionSetFeatures::AddFeaturesFromString( |
| 194 | const std::string& feature_list, std::string* error_msg) const { |
| 195 | if (feature_list.empty()) { |
| 196 | *error_msg = "No instruction set features specified"; |
| 197 | return nullptr; |
| 198 | } |
| 199 | std::vector<std::string> features; |
| 200 | Split(feature_list, ',', &features); |
| 201 | bool smp = smp_; |
| 202 | bool use_default = false; // Have we seen the 'default' feature? |
| 203 | bool first = false; // Is this first feature? |
| 204 | for (auto it = features.begin(); it != features.end();) { |
| 205 | if (use_default) { |
| 206 | *error_msg = "Unexpected instruction set features after 'default'"; |
| 207 | return nullptr; |
| 208 | } |
| 209 | std::string feature = Trim(*it); |
| 210 | bool erase = false; |
| 211 | if (feature == "default") { |
| 212 | if (!first) { |
| 213 | use_default = true; |
| 214 | erase = true; |
| 215 | } else { |
| 216 | *error_msg = "Unexpected instruction set features before 'default'"; |
| 217 | return nullptr; |
| 218 | } |
| 219 | } else if (feature == "smp") { |
| 220 | smp = true; |
| 221 | erase = true; |
| 222 | } else if (feature == "-smp") { |
| 223 | smp = false; |
| 224 | erase = true; |
| 225 | } |
| 226 | // Erase the smp feature once processed. |
| 227 | if (!erase) { |
| 228 | ++it; |
| 229 | } else { |
| 230 | it = features.erase(it); |
| 231 | } |
| 232 | first = true; |
| 233 | } |
| 234 | DCHECK_EQ(use_default, features.empty()); |
| 235 | return AddFeaturesFromSplitString(smp, features, error_msg); |
| 236 | } |
| 237 | |
| 238 | const ArmInstructionSetFeatures* InstructionSetFeatures::AsArmInstructionSetFeatures() const { |
| 239 | DCHECK_EQ(kArm, GetInstructionSet()); |
| 240 | return down_cast<const ArmInstructionSetFeatures*>(this); |
| 241 | } |
| 242 | |
| 243 | const Arm64InstructionSetFeatures* InstructionSetFeatures::AsArm64InstructionSetFeatures() const { |
| 244 | DCHECK_EQ(kArm64, GetInstructionSet()); |
| 245 | return down_cast<const Arm64InstructionSetFeatures*>(this); |
| 246 | } |
| 247 | |
| 248 | const MipsInstructionSetFeatures* InstructionSetFeatures::AsMipsInstructionSetFeatures() const { |
| 249 | DCHECK_EQ(kMips, GetInstructionSet()); |
| 250 | return down_cast<const MipsInstructionSetFeatures*>(this); |
| 251 | } |
| 252 | |
| 253 | const X86InstructionSetFeatures* InstructionSetFeatures::AsX86InstructionSetFeatures() const { |
| 254 | DCHECK(kX86 == GetInstructionSet() || kX86_64 == GetInstructionSet()); |
| 255 | return down_cast<const X86InstructionSetFeatures*>(this); |
| 256 | } |
| 257 | |
| 258 | const X86_64InstructionSetFeatures* InstructionSetFeatures::AsX86_64InstructionSetFeatures() const { |
| 259 | DCHECK_EQ(kX86_64, GetInstructionSet()); |
| 260 | return down_cast<const X86_64InstructionSetFeatures*>(this); |
| 261 | } |
| 262 | |
| 263 | bool InstructionSetFeatures::FindVariantInArray(const char* variants[], size_t num_variants, |
| 264 | const std::string& variant) { |
| 265 | const char** begin = variants; |
| 266 | const char** end = begin + num_variants; |
| 267 | return std::find(begin, end, variant) != end; |
| 268 | } |
| 269 | |
| 270 | std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs) { |
| 271 | os << "ISA: " << rhs.GetInstructionSet() << " Feature string: " << rhs.GetFeatureString(); |
| 272 | return os; |
| 273 | } |
| 274 | |
| 275 | } // namespace art |