1 //===--- Triple.cpp - Target triple helper class --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Triple.h"
12 #include "llvm/ADT/Twine.h"
19 const char *Triple::getArchTypeName(ArchType Kind) {
21 case InvalidArch: return "<invalid>";
22 case UnknownArch: return "unknown";
24 case alpha: return "alpha";
25 case arm: return "arm";
26 case bfin: return "bfin";
27 case cellspu: return "cellspu";
28 case mips: return "mips";
29 case mipsel: return "mipsel";
30 case msp430: return "msp430";
31 case pic16: return "pic16";
32 case ppc64: return "powerpc64";
33 case ppc: return "powerpc";
34 case sparc: return "sparc";
35 case systemz: return "s390x";
36 case thumb: return "thumb";
37 case x86: return "i386";
38 case x86_64: return "x86_64";
39 case xcore: return "xcore";
45 const char *Triple::getVendorTypeName(VendorType Kind) {
47 case UnknownVendor: return "unknown";
49 case Apple: return "apple";
56 const char *Triple::getOSTypeName(OSType Kind) {
58 case UnknownOS: return "unknown";
60 case AuroraUX: return "auroraux";
61 case Cygwin: return "cygwin";
62 case Darwin: return "darwin";
63 case DragonFly: return "dragonfly";
64 case FreeBSD: return "freebsd";
65 case Linux: return "linux";
66 case MinGW32: return "mingw32";
67 case MinGW64: return "mingw64";
68 case NetBSD: return "netbsd";
69 case OpenBSD: return "openbsd";
70 case Solaris: return "solaris";
71 case Win32: return "win32";
77 Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) {
84 if (Name == "cellspu")
100 if (Name == "systemz")
106 if (Name == "x86-64")
116 void Triple::Parse() const {
117 assert(!isInitialized() && "Invalid parse call.");
119 StringRef ArchName = getArchName();
120 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
121 ArchName[2] == '8' && ArchName[3] == '6' &&
122 ArchName[1] - '3' < 6) // i[3-9]86
124 else if (ArchName == "amd64" || ArchName == "x86_64")
126 else if (ArchName == "pic16")
128 else if (ArchName == "powerpc")
130 else if (ArchName == "powerpc64")
132 else if (ArchName == "arm" ||
133 ArchName.startswith("armv"))
135 else if (ArchName == "thumb" ||
136 ArchName.startswith("thumbv"))
138 else if (ArchName.startswith("alpha"))
140 else if (ArchName == "spu" || ArchName == "cellspu")
142 else if (ArchName == "msp430")
144 else if (ArchName == "mips" || ArchName == "mipsallegrex")
146 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
149 else if (ArchName == "sparc")
151 else if (ArchName == "s390x")
156 StringRef VendorName = getVendorName();
157 if (VendorName == "apple")
159 else if (VendorName == "pc")
162 Vendor = UnknownVendor;
164 StringRef OSName = getOSName();
165 if (OSName.startswith("auroraux"))
167 else if (OSName.startswith("cygwin"))
169 else if (OSName.startswith("darwin"))
171 else if (OSName.startswith("dragonfly"))
173 else if (OSName.startswith("freebsd"))
175 else if (OSName.startswith("linux"))
177 else if (OSName.startswith("mingw32"))
179 else if (OSName.startswith("mingw64"))
181 else if (OSName.startswith("netbsd"))
183 else if (OSName.startswith("openbsd"))
185 else if (OSName.startswith("solaris"))
187 else if (OSName.startswith("win32"))
192 assert(isInitialized() && "Failed to initialize!");
195 StringRef Triple::getArchName() const {
196 return StringRef(Data).split('-').first; // Isolate first component
199 StringRef Triple::getVendorName() const {
200 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
201 return Tmp.split('-').first; // Isolate second component
204 StringRef Triple::getOSName() const {
205 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
206 Tmp = Tmp.split('-').second; // Strip second component
207 return Tmp.split('-').first; // Isolate third component
210 StringRef Triple::getEnvironmentName() const {
211 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
212 Tmp = Tmp.split('-').second; // Strip second component
213 return Tmp.split('-').second; // Strip third component
216 StringRef Triple::getOSAndEnvironmentName() const {
217 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
218 return Tmp.split('-').second; // Strip second component
221 static unsigned EatNumber(StringRef &Str) {
222 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
223 unsigned Result = Str[0]-'0';
228 // Handle "darwin11".
229 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
230 Result = Result*10 + (Str[0] - '0');
238 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
239 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
240 /// not defined, return 0's. This requires that the triple have an OSType of
241 /// darwin before it is called.
242 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
243 unsigned &Revision) const {
244 assert(getOS() == Darwin && "Not a darwin target triple!");
245 StringRef OSName = getOSName();
246 assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
248 // Strip off "darwin".
249 OSName = OSName.substr(6);
251 Maj = Min = Revision = 0;
253 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
256 // The major version is the first digit.
257 Maj = EatNumber(OSName);
258 if (OSName.empty()) return;
260 // Handle minor version: 10.4.9 -> darwin8.9.
261 if (OSName[0] != '.')
265 OSName = OSName.substr(1);
267 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
270 Min = EatNumber(OSName);
271 if (OSName.empty()) return;
273 // Handle revision darwin8.9.1
274 if (OSName[0] != '.')
278 OSName = OSName.substr(1);
280 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
283 Revision = EatNumber(OSName);
286 void Triple::setTriple(const Twine &Str) {
291 void Triple::setArch(ArchType Kind) {
292 setArchName(getArchTypeName(Kind));
295 void Triple::setVendor(VendorType Kind) {
296 setVendorName(getVendorTypeName(Kind));
299 void Triple::setOS(OSType Kind) {
300 setOSName(getOSTypeName(Kind));
303 void Triple::setArchName(const StringRef &Str) {
304 setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
307 void Triple::setVendorName(const StringRef &Str) {
308 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
311 void Triple::setOSName(const StringRef &Str) {
312 if (hasEnvironment())
313 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
314 "-" + getEnvironmentName());
316 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
319 void Triple::setEnvironmentName(const StringRef &Str) {
320 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
324 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
325 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);