blob: b6c5c71818cbe118883167fed3a32ada0f68b05e [file] [log] [blame]
Ian Rogersd582fa42014-11-05 23:46:43 -08001/*
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#ifndef ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_
18#define ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_
19
Andreas Gampe0415b4e2015-01-06 15:17:07 -080020#include <memory>
Ian Rogersd582fa42014-11-05 23:46:43 -080021#include <ostream>
22#include <vector>
23
24#include "base/macros.h"
25#include "instruction_set.h"
26
27namespace art {
28
29class ArmInstructionSetFeatures;
30class Arm64InstructionSetFeatures;
31class MipsInstructionSetFeatures;
Andreas Gampe57b34292015-01-14 15:45:59 -080032class Mips64InstructionSetFeatures;
Ian Rogersd582fa42014-11-05 23:46:43 -080033class X86InstructionSetFeatures;
34class X86_64InstructionSetFeatures;
35
36// Abstraction used to describe features of a different instruction sets.
37class InstructionSetFeatures {
38 public:
39 // Process a CPU variant string for the given ISA and create an InstructionSetFeatures.
Andreas Gampe0415b4e2015-01-06 15:17:07 -080040 static std::unique_ptr<const InstructionSetFeatures> FromVariant(InstructionSet isa,
41 const std::string& variant,
42 std::string* error_msg);
Ian Rogersd582fa42014-11-05 23:46:43 -080043
44 // Parse a bitmap for the given isa and create an InstructionSetFeatures.
Andreas Gampe0415b4e2015-01-06 15:17:07 -080045 static std::unique_ptr<const InstructionSetFeatures> FromBitmap(InstructionSet isa,
46 uint32_t bitmap);
Ian Rogersd582fa42014-11-05 23:46:43 -080047
48 // Turn C pre-processor #defines into the equivalent instruction set features for kRuntimeISA.
Andreas Gampe0415b4e2015-01-06 15:17:07 -080049 static std::unique_ptr<const InstructionSetFeatures> FromCppDefines();
Ian Rogersd582fa42014-11-05 23:46:43 -080050
51 // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
Andreas Gampe0415b4e2015-01-06 15:17:07 -080052 static std::unique_ptr<const InstructionSetFeatures> FromCpuInfo();
Ian Rogersd582fa42014-11-05 23:46:43 -080053
54 // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
55 // InstructionSetFeatures.
Andreas Gampe0415b4e2015-01-06 15:17:07 -080056 static std::unique_ptr<const InstructionSetFeatures> FromHwcap();
Ian Rogersd582fa42014-11-05 23:46:43 -080057
58 // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
59 // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
Andreas Gampe0415b4e2015-01-06 15:17:07 -080060 static std::unique_ptr<const InstructionSetFeatures> FromAssembly();
Ian Rogersd582fa42014-11-05 23:46:43 -080061
62 // Parse a string of the form "div,-atomic_ldrd_strd" adding and removing these features to
63 // create a new InstructionSetFeatures.
Andreas Gampe0415b4e2015-01-06 15:17:07 -080064 std::unique_ptr<const InstructionSetFeatures> AddFeaturesFromString(
65 const std::string& feature_list, std::string* error_msg) const WARN_UNUSED;
Ian Rogersd582fa42014-11-05 23:46:43 -080066
67 // Are these features the same as the other given features?
68 virtual bool Equals(const InstructionSetFeatures* other) const = 0;
69
70 // Return the ISA these features relate to.
71 virtual InstructionSet GetInstructionSet() const = 0;
72
73 // Return a bitmap that represents the features. ISA specific.
74 virtual uint32_t AsBitmap() const = 0;
75
76 // Return a string of the form "div,lpae" or "none".
77 virtual std::string GetFeatureString() const = 0;
78
Ian Rogersd582fa42014-11-05 23:46:43 -080079 // Down cast this ArmInstructionFeatures.
80 const ArmInstructionSetFeatures* AsArmInstructionSetFeatures() const;
81
82 // Down cast this Arm64InstructionFeatures.
83 const Arm64InstructionSetFeatures* AsArm64InstructionSetFeatures() const;
84
85 // Down cast this MipsInstructionFeatures.
86 const MipsInstructionSetFeatures* AsMipsInstructionSetFeatures() const;
87
Andreas Gampe57b34292015-01-14 15:45:59 -080088 // Down cast this Mips64InstructionFeatures.
89 const Mips64InstructionSetFeatures* AsMips64InstructionSetFeatures() const;
90
Ian Rogersd582fa42014-11-05 23:46:43 -080091 // Down cast this X86InstructionFeatures.
92 const X86InstructionSetFeatures* AsX86InstructionSetFeatures() const;
93
94 // Down cast this X86_64InstructionFeatures.
95 const X86_64InstructionSetFeatures* AsX86_64InstructionSetFeatures() const;
96
97 virtual ~InstructionSetFeatures() {}
98
99 protected:
Serban Constantinescub595b402016-09-23 11:06:03 +0100100 InstructionSetFeatures() {}
Ian Rogersd582fa42014-11-05 23:46:43 -0800101
102 // Returns true if variant appears in the array variants.
Andreas Gampe24a05f42015-04-03 13:46:54 -0700103 static bool FindVariantInArray(const char* const variants[], size_t num_variants,
Ian Rogersd582fa42014-11-05 23:46:43 -0800104 const std::string& variant);
105
106 // Add architecture specific features in sub-classes.
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800107 virtual std::unique_ptr<const InstructionSetFeatures>
Serban Constantinescub595b402016-09-23 11:06:03 +0100108 AddFeaturesFromSplitString(const std::vector<std::string>& features,
Ian Rogersd582fa42014-11-05 23:46:43 -0800109 std::string* error_msg) const = 0;
110
111 private:
Ian Rogersd582fa42014-11-05 23:46:43 -0800112 DISALLOW_COPY_AND_ASSIGN(InstructionSetFeatures);
113};
114std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs);
115
116} // namespace art
117
118#endif // ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_