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" |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 17 | #include <cassert> |
Jeff Cohen | 7cd57f4 | 2005-09-02 02:51:42 +0000 | [diff] [blame] | 18 | #include <cctype> |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 19 | #include <iostream> |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | // Static Helper Functions |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | /// hasFlag - Determine if a feature has a flag; '+' or '-' |
| 27 | /// |
| 28 | static inline bool hasFlag(const std::string &Feature) { |
| 29 | assert(!Feature.empty() && "Empty string"); |
| 30 | // Get first character |
| 31 | char Ch = Feature[0]; |
| 32 | // Check if first character is '+' or '-' flag |
| 33 | return Ch == '+' || Ch =='-'; |
| 34 | } |
| 35 | |
| 36 | /// StripFlag - Return string stripped of flag. |
| 37 | /// |
| 38 | static inline std::string StripFlag(const std::string &Feature) { |
| 39 | return hasFlag(Feature) ? Feature.substr(1) : Feature; |
| 40 | } |
| 41 | |
| 42 | /// isEnabled - Return true if enable flag; '+'. |
| 43 | /// |
| 44 | static inline bool isEnabled(const std::string &Feature) { |
| 45 | assert(!Feature.empty() && "Empty string"); |
| 46 | // Get first character |
| 47 | char Ch = Feature[0]; |
| 48 | // Check if first character is '+' for enabled |
| 49 | return Ch == '+'; |
| 50 | } |
| 51 | |
| 52 | /// PrependFlag - Return a string with a prepended flag; '+' or '-'. |
| 53 | /// |
| 54 | static inline std::string PrependFlag(const std::string &Feature, |
| 55 | bool IsEnabled) { |
| 56 | assert(!Feature.empty() && "Empty string"); |
| 57 | if (hasFlag(Feature)) return Feature; |
| 58 | return std::string(IsEnabled ? "+" : "-") + Feature; |
| 59 | } |
| 60 | |
| 61 | /// Split - Splits a string of comma separated items in to a vector of strings. |
| 62 | /// |
| 63 | static void Split(std::vector<std::string> &V, const std::string &S) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 64 | // Start at beginning of string. |
| 65 | size_t Pos = 0; |
| 66 | while (true) { |
| 67 | // Find the next comma |
| 68 | size_t Comma = S.find(',', Pos); |
| 69 | // If no comma found then the the rest of the string is used |
| 70 | if (Comma == std::string::npos) { |
| 71 | // Add string to vector |
| 72 | V.push_back(S.substr(Pos)); |
| 73 | break; |
| 74 | } |
| 75 | // Otherwise add substring to vector |
| 76 | V.push_back(S.substr(Pos, Comma - Pos)); |
| 77 | // Advance to next item |
| 78 | Pos = Comma + 1; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Join a vector of strings to a string with a comma separating each element. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 83 | /// |
| 84 | static std::string Join(const std::vector<std::string> &V) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 85 | // Start with empty string. |
| 86 | std::string Result; |
| 87 | // If the vector is not empty |
| 88 | if (!V.empty()) { |
| 89 | // Start with the CPU feature |
| 90 | Result = V[0]; |
| 91 | // For each successive feature |
| 92 | for (size_t i = 1; i < V.size(); i++) { |
| 93 | // Add a comma |
| 94 | Result += ","; |
| 95 | // Add the feature |
| 96 | Result += V[i]; |
| 97 | } |
| 98 | } |
| 99 | // Return the features string |
| 100 | return Result; |
| 101 | } |
| 102 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 103 | /// Adding features. |
| 104 | void SubtargetFeatures::AddFeature(const std::string &String, |
| 105 | bool IsEnabled) { |
| 106 | // Don't add empty features |
| 107 | if (!String.empty()) { |
| 108 | // Convert to lowercase, prepend flag and add to vector |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 109 | Features.push_back(PrependFlag(LowercaseString(String), IsEnabled)); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Find item in array using binary search. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 114 | static const SubtargetFeatureKV *Find(const std::string &S, |
| 115 | const SubtargetFeatureKV *A, size_t L) { |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 116 | // Determine the end of the array |
| 117 | const SubtargetFeatureKV *Hi = A + L; |
| 118 | // Binary search the array |
| 119 | const SubtargetFeatureKV *F = std::lower_bound(A, Hi, S); |
| 120 | // If not found then return NULL |
| 121 | if (F == Hi || std::string(F->Key) != S) return NULL; |
| 122 | // Return the found array item |
| 123 | return F; |
| 124 | } |
| 125 | |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 126 | /// Display help for feature choices. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 127 | /// |
| 128 | static void Help(const char *Heading, const SubtargetFeatureKV *Table, |
| 129 | size_t TableSize) { |
| 130 | // Determine the length of the longest key |
| 131 | size_t MaxLen = 0; |
| 132 | for (size_t i = 0; i < TableSize; i++) |
| 133 | MaxLen = std::max(MaxLen, std::strlen(Table[i].Key)); |
| 134 | // Print heading |
| 135 | std::cerr << "Help for " << Heading << " choices:\n\n"; |
| 136 | // For each feature |
| 137 | for (size_t i = 0; i < TableSize; i++) { |
| 138 | // Compute required padding |
| 139 | size_t Pad = MaxLen - std::strlen(Table[i].Key); |
| 140 | // Print details |
| 141 | std::cerr << Table[i].Key << std::string(Pad, ' ') << " - " |
| 142 | << Table[i].Desc << "\n"; |
| 143 | } |
| 144 | // Wrap it up |
| 145 | std::cerr << "\n\n"; |
| 146 | // Leave tool |
| 147 | exit(1); |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 150 | //===----------------------------------------------------------------------===// |
| 151 | // SubtargetFeatures Implementation |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | |
| 154 | SubtargetFeatures::SubtargetFeatures(const std::string &Initial) { |
| 155 | // Break up string into separate features |
| 156 | Split(Features, Initial); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | std::string SubtargetFeatures::getString() const { |
| 161 | return Join(Features); |
| 162 | } |
| 163 | void SubtargetFeatures::setString(const std::string &Initial) { |
| 164 | // Throw out old features |
| 165 | Features.clear(); |
| 166 | // Break up string into separate features |
| 167 | Split(Features, LowercaseString(Initial)); |
| 168 | } |
| 169 | |
| 170 | /// setCPU - Set the CPU string. Replaces previous setting. Setting to "" |
| 171 | /// clears CPU. |
| 172 | void SubtargetFeatures::setCPU(const std::string &String) { |
| 173 | Features[0] = LowercaseString(String); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 178 | /// Parse feature string for quick usage. |
Chris Lattner | 54195c1 | 2005-10-23 05:26:26 +0000 | [diff] [blame] | 179 | /// |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 180 | uint32_t SubtargetFeatures::Parse(const std::string &String, |
| 181 | const std::string &DefaultCPU, |
| 182 | const SubtargetFeatureKV *CPUTable, |
| 183 | size_t CPUTableSize, |
| 184 | const SubtargetFeatureKV *FeatureTable, |
| 185 | size_t FeatureTableSize) { |
| 186 | assert(CPUTable && "missing CPU table"); |
| 187 | assert(FeatureTable && "missing features table"); |
| 188 | #ifndef NDEBUG |
| 189 | for (size_t i = 1; i < CPUTableSize; i++) { |
| 190 | assert(strcmp(CPUTable[i - 1].Key, CPUTable[i].Key) < 0 && |
| 191 | "CPU table is not sorted"); |
| 192 | } |
| 193 | for (size_t i = 1; i < FeatureTableSize; i++) { |
| 194 | assert(strcmp(FeatureTable[i - 1].Key, FeatureTable[i].Key) < 0 && |
| 195 | "CPU features table is not sorted"); |
| 196 | } |
| 197 | #endif |
| 198 | std::vector<std::string> Features; // Subtarget features as a vector |
| 199 | uint32_t Bits = 0; // Resulting bits |
| 200 | // Split up features |
| 201 | Split(Features, String); |
| 202 | // Check if default is needed |
| 203 | if (Features[0].empty()) Features[0] = DefaultCPU; |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 204 | // Check for help |
| 205 | if (Features[0] == "help") Help("CPU", CPUTable, CPUTableSize); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 206 | // Find CPU entry |
| 207 | const SubtargetFeatureKV *CPUEntry = |
| 208 | Find(Features[0], CPUTable, CPUTableSize); |
| 209 | // If there is a match |
| 210 | if (CPUEntry) { |
| 211 | // Set base feature bits |
| 212 | Bits = CPUEntry->Value; |
| 213 | } else { |
Chris Lattner | 3eb3369 | 2005-09-07 05:44:14 +0000 | [diff] [blame] | 214 | std::cerr << "'" << Features[0] |
| 215 | << "' is not a recognized processor for this target" |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 216 | << " (ignoring processor)" |
| 217 | << "\n"; |
| 218 | } |
| 219 | // Iterate through each feature |
| 220 | for (size_t i = 1; i < Features.size(); i++) { |
| 221 | // Get next feature |
| 222 | const std::string &Feature = Features[i]; |
Jim Laskey | 839615a | 2005-09-02 19:27:43 +0000 | [diff] [blame] | 223 | // Check for help |
| 224 | if (Feature == "+help") Help("feature", FeatureTable, FeatureTableSize); |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 225 | // Find feature in table. |
| 226 | const SubtargetFeatureKV *FeatureEntry = |
| 227 | Find(StripFlag(Feature), FeatureTable, FeatureTableSize); |
| 228 | // If there is a match |
| 229 | if (FeatureEntry) { |
| 230 | // Enable/disable feature in bits |
| 231 | if (isEnabled(Feature)) Bits |= FeatureEntry->Value; |
| 232 | else Bits &= ~FeatureEntry->Value; |
| 233 | } else { |
Chris Lattner | 3eb3369 | 2005-09-07 05:44:14 +0000 | [diff] [blame] | 234 | std::cerr << "'" << Feature |
| 235 | << "' is not a recognized feature for this target" |
Jim Laskey | b3302db | 2005-09-01 21:36:18 +0000 | [diff] [blame] | 236 | << " (ignoring feature)" |
| 237 | << "\n"; |
| 238 | } |
| 239 | } |
| 240 | return Bits; |
| 241 | } |
| 242 | |
| 243 | /// Print feature string. |
| 244 | void SubtargetFeatures::print(std::ostream &OS) const { |
| 245 | for (size_t i = 0; i < Features.size(); i++) { |
| 246 | OS << Features[i] << " "; |
| 247 | } |
| 248 | OS << "\n"; |
| 249 | } |
| 250 | |
| 251 | /// Dump feature info. |
| 252 | void SubtargetFeatures::dump() const { |
| 253 | print(std::cerr); |
| 254 | } |