Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 1 | //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Jim Laskey and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SubtargetFeature interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Target/SubtargetFeature.h" |
| 15 | |
| 16 | #include <string> |
| 17 | #include <algorithm> |
| 18 | #include <vector> |
| 19 | #include <cassert> |
Jeff Cohen | 7cd57f4 | 2005-09-02 02:51:42 +0000 | [diff] [blame] | 20 | #include <cctype> |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | /// Splits a string of comma separated items in to a vector of strings. |
| 25 | void SubtargetFeatures::Split(std::vector<std::string> &V, |
| 26 | const std::string &S) { |
| 27 | // Start at beginning of string. |
| 28 | size_t Pos = 0; |
| 29 | while (true) { |
| 30 | // Find the next comma |
| 31 | size_t Comma = S.find(',', Pos); |
| 32 | // If no comma found then the the rest of the string is used |
| 33 | if (Comma == std::string::npos) { |
| 34 | // Add string to vector |
| 35 | V.push_back(S.substr(Pos)); |
| 36 | break; |
| 37 | } |
| 38 | // Otherwise add substring to vector |
| 39 | V.push_back(S.substr(Pos, Comma - Pos)); |
| 40 | // Advance to next item |
| 41 | Pos = Comma + 1; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Join a vector of strings to a string with a comma separating each element. |
| 46 | std::string SubtargetFeatures::Join(const std::vector<std::string> &V) { |
| 47 | // Start with empty string. |
| 48 | std::string Result; |
| 49 | // If the vector is not empty |
| 50 | if (!V.empty()) { |
| 51 | // Start with the CPU feature |
| 52 | Result = V[0]; |
| 53 | // For each successive feature |
| 54 | for (size_t i = 1; i < V.size(); i++) { |
| 55 | // Add a comma |
| 56 | Result += ","; |
| 57 | // Add the feature |
| 58 | Result += V[i]; |
| 59 | } |
| 60 | } |
| 61 | // Return the features string |
| 62 | return Result; |
| 63 | } |
| 64 | |
| 65 | /// Convert a string to lowercase. |
| 66 | std::string SubtargetFeatures::toLower(const std::string &S) { |
| 67 | // Copy the string |
| 68 | std::string Result = S; |
| 69 | // For each character in string |
| 70 | for (size_t i = 0; i < Result.size(); i++) { |
| 71 | // Convert character to lowercase |
| 72 | Result[i] = std::tolower(Result[i]); |
| 73 | } |
| 74 | // Return the lowercased string |
| 75 | return Result; |
| 76 | } |
| 77 | |
| 78 | /// Adding features. |
| 79 | void SubtargetFeatures::AddFeature(const std::string &String, |
| 80 | bool IsEnabled) { |
| 81 | // Don't add empty features |
| 82 | if (!String.empty()) { |
| 83 | // Convert to lowercase, prepend flag and add to vector |
| 84 | Features.push_back(PrependFlag(toLower(String), IsEnabled)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Find item in array using binary search. |
| 89 | const SubtargetFeatureKV * |
| 90 | SubtargetFeatures::Find(const std::string &S, |
| 91 | const SubtargetFeatureKV *A, size_t L) { |
| 92 | // Determine the end of the array |
| 93 | const SubtargetFeatureKV *Hi = A + L; |
| 94 | // Binary search the array |
| 95 | const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S); |
| 96 | // If not found then return NULL |
| 97 | if (F == Hi || std::string(F->Key) != S) return NULL; |
| 98 | // Return the found array item |
| 99 | return F; |
| 100 | } |
| 101 | |
| 102 | /// Parse feature string for quick usage. |
| 103 | uint32_t SubtargetFeatures::Parse(const std::string &String, |
| 104 | const std::string &DefaultCPU, |
| 105 | const SubtargetFeatureKV *CPUTable, |
| 106 | size_t CPUTableSize, |
| 107 | const SubtargetFeatureKV *FeatureTable, |
| 108 | size_t FeatureTableSize) { |
| 109 | assert(CPUTable && "missing CPU table"); |
| 110 | assert(FeatureTable && "missing features table"); |
| 111 | #ifndef NDEBUG |
| 112 | for (size_t i = 1; i < CPUTableSize; i++) { |
| 113 | assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 && |
| 114 | "CPU table is not sorted"); |
| 115 | } |
| 116 | for (size_t i = 1; i < FeatureTableSize; i++) { |
| 117 | assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 && |
| 118 | "CPU features table is not sorted"); |
| 119 | } |
| 120 | #endif |
| 121 | std::vector<std::string> Features; // Subtarget features as a vector |
| 122 | uint32_t Bits = 0; // Resulting bits |
| 123 | // Split up features |
| 124 | Split(Features, String); |
| 125 | // Check if default is needed |
| 126 | if (Features[0].empty()) Features[0] = DefaultCPU; |
| 127 | // Find CPU entry |
| 128 | const SubtargetFeatureKV *CPUEntry = |
| 129 | Find(Features[0], CPUTable, CPUTableSize); |
| 130 | // If there is a match |
| 131 | if (CPUEntry) { |
| 132 | // Set base feature bits |
| 133 | Bits = CPUEntry->Value; |
| 134 | } else { |
| 135 | std::cerr << Features[0] |
| 136 | << " is not a recognized processor for this target" |
| 137 | << " (ignoring processor)" |
| 138 | << "\n"; |
| 139 | } |
| 140 | // Iterate through each feature |
| 141 | for (size_t i = 1; i < Features.size(); i++) { |
| 142 | // Get next feature |
| 143 | const std::string &Feature = Features[i]; |
| 144 | // Find feature in table. |
| 145 | const SubtargetFeatureKV *FeatureEntry = |
| 146 | Find(StripFlag(Feature), FeatureTable, FeatureTableSize); |
| 147 | // If there is a match |
| 148 | if (FeatureEntry) { |
| 149 | // Enable/disable feature in bits |
| 150 | if (isEnabled(Feature)) Bits |= FeatureEntry->Value; |
| 151 | else Bits &= ~FeatureEntry->Value; |
| 152 | } else { |
| 153 | std::cerr << Feature |
| 154 | << " is not a recognized feature for this target" |
| 155 | << " (ignoring feature)" |
| 156 | << "\n"; |
| 157 | } |
| 158 | } |
| 159 | return Bits; |
| 160 | } |
| 161 | |
| 162 | /// Print feature string. |
| 163 | void SubtargetFeatures::print(std::ostream &OS) const { |
| 164 | for (size_t i = 0; i < Features.size(); i++) { |
| 165 | OS << Features[i] << " "; |
| 166 | } |
| 167 | OS << "\n"; |
| 168 | } |
| 169 | |
| 170 | /// Dump feature info. |
| 171 | void SubtargetFeatures::dump() const { |
| 172 | print(std::cerr); |
| 173 | } |