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 NetBSD: return "netbsd";
67 case OpenBSD: return "openbsd";
68 case Win32: return "win32";
74 Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) {
81 if (Name == "cellspu")
95 if (Name == "systemz")
101 if (Name == "x86_64")
111 void Triple::Parse() const {
112 assert(!isInitialized() && "Invalid parse call.");
114 StringRef ArchName = getArchName();
115 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
116 ArchName[2] == '8' && ArchName[3] == '6' &&
117 ArchName[1] - '3' < 6) // i[3-9]86
119 else if (ArchName == "amd64" || ArchName == "x86_64")
121 else if (ArchName == "powerpc")
123 else if (ArchName == "powerpc64")
125 else if (ArchName == "arm" ||
126 ArchName.startswith("armv"))
128 else if (ArchName == "thumb" ||
129 ArchName.startswith("thumbv"))
131 else if (ArchName.startswith("alpha"))
133 else if (ArchName == "spu" || ArchName == "cellspu")
135 else if (ArchName == "msp430")
137 else if (ArchName == "mips" || ArchName == "mipsallegrex")
139 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
142 else if (ArchName == "sparc")
144 else if (ArchName == "s390x")
146 else if (ArchName == "bfin")
151 StringRef VendorName = getVendorName();
152 if (VendorName == "apple")
154 else if (VendorName == "pc")
157 Vendor = UnknownVendor;
159 StringRef OSName = getOSName();
160 if (OSName.startswith("auroraux"))
162 else if (OSName.startswith("cygwin"))
164 else if (OSName.startswith("darwin"))
166 else if (OSName.startswith("dragonfly"))
168 else if (OSName.startswith("freebsd"))
170 else if (OSName.startswith("linux"))
172 else if (OSName.startswith("mingw32"))
174 else if (OSName.startswith("netbsd"))
176 else if (OSName.startswith("openbsd"))
178 else if (OSName.startswith("win32"))
183 assert(isInitialized() && "Failed to initialize!");
186 StringRef Triple::getArchName() const {
187 return StringRef(Data).split('-').first; // Isolate first component
190 StringRef Triple::getVendorName() const {
191 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
192 return Tmp.split('-').first; // Isolate second component
195 StringRef Triple::getOSName() const {
196 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
197 Tmp = Tmp.split('-').second; // Strip second component
198 return Tmp.split('-').first; // Isolate third component
201 StringRef Triple::getEnvironmentName() const {
202 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
203 Tmp = Tmp.split('-').second; // Strip second component
204 return Tmp.split('-').second; // Strip third component
207 StringRef Triple::getOSAndEnvironmentName() const {
208 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
209 return Tmp.split('-').second; // Strip second component
212 static unsigned EatNumber(StringRef &Str) {
213 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
214 unsigned Result = Str[0]-'0';
219 // Handle "darwin11".
220 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
221 Result = Result*10 + (Str[0] - '0');
229 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
230 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
231 /// not defined, return 0's. This requires that the triple have an OSType of
232 /// darwin before it is called.
233 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
234 unsigned &Revision) const {
235 assert(getOS() == Darwin && "Not a darwin target triple!");
236 StringRef OSName = getOSName();
237 assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
239 // Strip off "darwin".
240 OSName = OSName.substr(6);
242 Maj = Min = Revision = 0;
244 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
247 // The major version is the first digit.
248 Maj = EatNumber(OSName);
249 if (OSName.empty()) return;
251 // Handle minor version: 10.4.9 -> darwin8.9.
252 if (OSName[0] != '.')
256 OSName = OSName.substr(1);
258 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
261 Min = EatNumber(OSName);
262 if (OSName.empty()) return;
264 // Handle revision darwin8.9.1
265 if (OSName[0] != '.')
269 OSName = OSName.substr(1);
271 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
274 Revision = EatNumber(OSName);
277 void Triple::setTriple(const Twine &Str) {
282 void Triple::setArch(ArchType Kind) {
283 setArchName(getArchTypeName(Kind));
286 void Triple::setVendor(VendorType Kind) {
287 setVendorName(getVendorTypeName(Kind));
290 void Triple::setOS(OSType Kind) {
291 setOSName(getOSTypeName(Kind));
294 void Triple::setArchName(const StringRef &Str) {
295 setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
298 void Triple::setVendorName(const StringRef &Str) {
299 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
302 void Triple::setOSName(const StringRef &Str) {
303 if (hasEnvironment())
304 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
305 "-" + getEnvironmentName());
307 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
310 void Triple::setEnvironmentName(const StringRef &Str) {
311 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
315 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
316 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);