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 ppc64: return "powerpc64";
32 case ppc: return "powerpc";
33 case sparc: return "sparc";
34 case systemz: return "s390x";
35 case thumb: return "thumb";
36 case x86: return "i386";
37 case x86_64: return "x86_64";
38 case xcore: return "xcore";
44 const char *Triple::getVendorTypeName(VendorType Kind) {
46 case UnknownVendor: return "unknown";
48 case Apple: return "apple";
55 const char *Triple::getOSTypeName(OSType Kind) {
57 case UnknownOS: return "unknown";
59 case AuroraUX: return "auroraux";
60 case Cygwin: return "cygwin";
61 case Darwin: return "darwin";
62 case DragonFly: return "dragonfly";
63 case FreeBSD: return "freebsd";
64 case Linux: return "linux";
65 case MinGW32: return "mingw32";
66 case MinGW64: return "mingw64";
67 case NetBSD: return "netbsd";
68 case OpenBSD: return "openbsd";
69 case Win32: return "win32";
75 Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) {
82 if (Name == "cellspu")
96 if (Name == "systemz")
102 if (Name == "x86-64")
112 void Triple::Parse() const {
113 assert(!isInitialized() && "Invalid parse call.");
115 StringRef ArchName = getArchName();
116 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
117 ArchName[2] == '8' && ArchName[3] == '6' &&
118 ArchName[1] - '3' < 6) // i[3-9]86
120 else if (ArchName == "amd64" || ArchName == "x86_64")
122 else if (ArchName == "powerpc")
124 else if (ArchName == "powerpc64")
126 else if (ArchName == "arm" ||
127 ArchName.startswith("armv"))
129 else if (ArchName == "thumb" ||
130 ArchName.startswith("thumbv"))
132 else if (ArchName.startswith("alpha"))
134 else if (ArchName == "spu" || ArchName == "cellspu")
136 else if (ArchName == "msp430")
138 else if (ArchName == "mips" || ArchName == "mipsallegrex")
140 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
143 else if (ArchName == "sparc")
145 else if (ArchName == "s390x")
147 else if (ArchName == "bfin")
152 StringRef VendorName = getVendorName();
153 if (VendorName == "apple")
155 else if (VendorName == "pc")
158 Vendor = UnknownVendor;
160 StringRef OSName = getOSName();
161 if (OSName.startswith("auroraux"))
163 else if (OSName.startswith("cygwin"))
165 else if (OSName.startswith("darwin"))
167 else if (OSName.startswith("dragonfly"))
169 else if (OSName.startswith("freebsd"))
171 else if (OSName.startswith("linux"))
173 else if (OSName.startswith("mingw32"))
175 else if (OSName.startswith("mingw64"))
177 else if (OSName.startswith("netbsd"))
179 else if (OSName.startswith("openbsd"))
181 else if (OSName.startswith("win32"))
186 assert(isInitialized() && "Failed to initialize!");
189 StringRef Triple::getArchName() const {
190 return StringRef(Data).split('-').first; // Isolate first component
193 StringRef Triple::getVendorName() const {
194 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
195 return Tmp.split('-').first; // Isolate second component
198 StringRef Triple::getOSName() const {
199 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
200 Tmp = Tmp.split('-').second; // Strip second component
201 return Tmp.split('-').first; // Isolate third component
204 StringRef Triple::getEnvironmentName() const {
205 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
206 Tmp = Tmp.split('-').second; // Strip second component
207 return Tmp.split('-').second; // Strip third component
210 StringRef Triple::getOSAndEnvironmentName() const {
211 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
212 return Tmp.split('-').second; // Strip second component
215 static unsigned EatNumber(StringRef &Str) {
216 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
217 unsigned Result = Str[0]-'0';
222 // Handle "darwin11".
223 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
224 Result = Result*10 + (Str[0] - '0');
232 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
233 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
234 /// not defined, return 0's. This requires that the triple have an OSType of
235 /// darwin before it is called.
236 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
237 unsigned &Revision) const {
238 assert(getOS() == Darwin && "Not a darwin target triple!");
239 StringRef OSName = getOSName();
240 assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
242 // Strip off "darwin".
243 OSName = OSName.substr(6);
245 Maj = Min = Revision = 0;
247 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
250 // The major version is the first digit.
251 Maj = EatNumber(OSName);
252 if (OSName.empty()) return;
254 // Handle minor version: 10.4.9 -> darwin8.9.
255 if (OSName[0] != '.')
259 OSName = OSName.substr(1);
261 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
264 Min = EatNumber(OSName);
265 if (OSName.empty()) return;
267 // Handle revision darwin8.9.1
268 if (OSName[0] != '.')
272 OSName = OSName.substr(1);
274 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
277 Revision = EatNumber(OSName);
280 void Triple::setTriple(const Twine &Str) {
285 void Triple::setArch(ArchType Kind) {
286 setArchName(getArchTypeName(Kind));
289 void Triple::setVendor(VendorType Kind) {
290 setVendorName(getVendorTypeName(Kind));
293 void Triple::setOS(OSType Kind) {
294 setOSName(getOSTypeName(Kind));
297 void Triple::setArchName(const StringRef &Str) {
298 setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
301 void Triple::setVendorName(const StringRef &Str) {
302 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
305 void Triple::setOSName(const StringRef &Str) {
306 if (hasEnvironment())
307 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
308 "-" + getEnvironmentName());
310 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
313 void Triple::setEnvironmentName(const StringRef &Str) {
314 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
318 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
319 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);