blob: a9ed84f78984715ddc9e21309b719cd044a62de6 [file] [log] [blame]
Jim Laskeyb3302db2005-09-01 21:36:18 +00001//===- 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 Lattner54195c12005-10-23 05:26:26 +000015#include "llvm/ADT/StringExtras.h"
Jim Laskeyb3302db2005-09-01 21:36:18 +000016#include <algorithm>
Jim Laskeyb3302db2005-09-01 21:36:18 +000017#include <cassert>
Jeff Cohen7cd57f42005-09-02 02:51:42 +000018#include <cctype>
Chris Lattner54195c12005-10-23 05:26:26 +000019#include <iostream>
Jim Laskeyb3302db2005-09-01 21:36:18 +000020using namespace llvm;
21
Chris Lattner54195c12005-10-23 05:26:26 +000022//===----------------------------------------------------------------------===//
23// Static Helper Functions
24//===----------------------------------------------------------------------===//
25
26/// hasFlag - Determine if a feature has a flag; '+' or '-'
27///
28static 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///
38static 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///
44static 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///
54static 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///
63static void Split(std::vector<std::string> &V, const std::string &S) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000064 // 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 Lattner54195c12005-10-23 05:26:26 +000083///
84static std::string Join(const std::vector<std::string> &V) {
Jim Laskeyb3302db2005-09-01 21:36:18 +000085 // 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 Laskeyb3302db2005-09-01 21:36:18 +0000103/// Adding features.
104void 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 Lattner54195c12005-10-23 05:26:26 +0000109 Features.push_back(PrependFlag(LowercaseString(String), IsEnabled));
Jim Laskeyb3302db2005-09-01 21:36:18 +0000110 }
111}
112
113/// Find item in array using binary search.
Chris Lattner54195c12005-10-23 05:26:26 +0000114static const SubtargetFeatureKV *Find(const std::string &S,
115 const SubtargetFeatureKV *A, size_t L) {
Jim Laskeyb3302db2005-09-01 21:36:18 +0000116 // 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 Laskey839615a2005-09-02 19:27:43 +0000126/// Display help for feature choices.
Chris Lattner54195c12005-10-23 05:26:26 +0000127///
128static 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 Laskey839615a2005-09-02 19:27:43 +0000148}
149
Chris Lattner54195c12005-10-23 05:26:26 +0000150//===----------------------------------------------------------------------===//
151// SubtargetFeatures Implementation
152//===----------------------------------------------------------------------===//
153
154SubtargetFeatures::SubtargetFeatures(const std::string &Initial) {
155 // Break up string into separate features
156 Split(Features, Initial);
157}
158
159
160std::string SubtargetFeatures::getString() const {
161 return Join(Features);
162}
163void 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.
172void SubtargetFeatures::setCPU(const std::string &String) {
173 Features[0] = LowercaseString(String);
174}
175
176
177
Jim Laskeyb3302db2005-09-01 21:36:18 +0000178/// Parse feature string for quick usage.
Chris Lattner54195c12005-10-23 05:26:26 +0000179///
Jim Laskeyb3302db2005-09-01 21:36:18 +0000180uint32_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 Laskey839615a2005-09-02 19:27:43 +0000204 // Check for help
205 if (Features[0] == "help") Help("CPU", CPUTable, CPUTableSize);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000206 // 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 Lattner3eb33692005-09-07 05:44:14 +0000214 std::cerr << "'" << Features[0]
215 << "' is not a recognized processor for this target"
Jim Laskeyb3302db2005-09-01 21:36:18 +0000216 << " (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 Laskey839615a2005-09-02 19:27:43 +0000223 // Check for help
224 if (Feature == "+help") Help("feature", FeatureTable, FeatureTableSize);
Jim Laskeyb3302db2005-09-01 21:36:18 +0000225 // 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 Lattner3eb33692005-09-07 05:44:14 +0000234 std::cerr << "'" << Feature
235 << "' is not a recognized feature for this target"
Jim Laskeyb3302db2005-09-01 21:36:18 +0000236 << " (ignoring feature)"
237 << "\n";
238 }
239 }
240 return Bits;
241}
242
243/// Print feature string.
244void 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.
252void SubtargetFeatures::dump() const {
253 print(std::cerr);
254}