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 tce: return "tce";
37 case thumb: return "thumb";
38 case x86: return "i386";
39 case x86_64: return "x86_64";
40 case xcore: return "xcore";
46 const char *Triple::getVendorTypeName(VendorType Kind) {
48 case UnknownVendor: return "unknown";
50 case Apple: return "apple";
57 const char *Triple::getOSTypeName(OSType Kind) {
59 case UnknownOS: return "unknown";
61 case AuroraUX: return "auroraux";
62 case Cygwin: return "cygwin";
63 case Darwin: return "darwin";
64 case DragonFly: return "dragonfly";
65 case FreeBSD: return "freebsd";
66 case Linux: return "linux";
67 case MinGW32: return "mingw32";
68 case MinGW64: return "mingw64";
69 case NetBSD: return "netbsd";
70 case OpenBSD: return "openbsd";
71 case Solaris: return "solaris";
72 case Win32: return "win32";
78 Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) {
85 if (Name == "cellspu")
101 if (Name == "systemz")
109 if (Name == "x86-64")
119 void Triple::Parse() const {
120 assert(!isInitialized() && "Invalid parse call.");
122 StringRef ArchName = getArchName();
123 StringRef VendorName = getVendorName();
124 StringRef OSName = getOSName();
126 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
127 ArchName[2] == '8' && ArchName[3] == '6' &&
128 ArchName[1] - '3' < 6) // i[3-9]86
130 else if (ArchName == "amd64" || ArchName == "x86_64")
132 else if (ArchName == "bfin")
134 else if (ArchName == "pic16")
136 else if (ArchName == "powerpc")
138 else if (ArchName == "powerpc64")
140 else if (ArchName == "arm" ||
141 ArchName.startswith("armv") ||
142 ArchName == "xscale")
144 else if (ArchName == "thumb" ||
145 ArchName.startswith("thumbv"))
147 else if (ArchName.startswith("alpha"))
149 else if (ArchName == "spu" || ArchName == "cellspu")
151 else if (ArchName == "msp430")
153 else if (ArchName == "mips" || ArchName == "mipsallegrex")
155 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
158 else if (ArchName == "sparc")
160 else if (ArchName == "s390x")
162 else if (ArchName == "tce")
168 // Handle some exceptional cases where the OS / environment components are
169 // stuck into the vendor field.
170 if (StringRef(getTriple()).count('-') == 1) {
171 StringRef VendorName = getVendorName();
173 if (VendorName.startswith("mingw32")) { // 'i386-mingw32', etc.
179 // arm-elf is another example, but we don't currently parse anything about
183 if (VendorName == "apple")
185 else if (VendorName == "pc")
188 Vendor = UnknownVendor;
190 if (OSName.startswith("auroraux"))
192 else if (OSName.startswith("cygwin"))
194 else if (OSName.startswith("darwin"))
196 else if (OSName.startswith("dragonfly"))
198 else if (OSName.startswith("freebsd"))
200 else if (OSName.startswith("linux"))
202 else if (OSName.startswith("mingw32"))
204 else if (OSName.startswith("mingw64"))
206 else if (OSName.startswith("netbsd"))
208 else if (OSName.startswith("openbsd"))
210 else if (OSName.startswith("solaris"))
212 else if (OSName.startswith("win32"))
217 assert(isInitialized() && "Failed to initialize!");
220 StringRef Triple::getArchName() const {
221 return StringRef(Data).split('-').first; // Isolate first component
224 StringRef Triple::getVendorName() const {
225 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
226 return Tmp.split('-').first; // Isolate second component
229 StringRef Triple::getOSName() const {
230 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
231 Tmp = Tmp.split('-').second; // Strip second component
232 return Tmp.split('-').first; // Isolate third component
235 StringRef Triple::getEnvironmentName() const {
236 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
237 Tmp = Tmp.split('-').second; // Strip second component
238 return Tmp.split('-').second; // Strip third component
241 StringRef Triple::getOSAndEnvironmentName() const {
242 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
243 return Tmp.split('-').second; // Strip second component
246 static unsigned EatNumber(StringRef &Str) {
247 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
248 unsigned Result = Str[0]-'0';
253 // Handle "darwin11".
254 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
255 Result = Result*10 + (Str[0] - '0');
263 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
264 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
265 /// not defined, return 0's. This requires that the triple have an OSType of
266 /// darwin before it is called.
267 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
268 unsigned &Revision) const {
269 assert(getOS() == Darwin && "Not a darwin target triple!");
270 StringRef OSName = getOSName();
271 assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
273 // Strip off "darwin".
274 OSName = OSName.substr(6);
276 Maj = Min = Revision = 0;
278 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
281 // The major version is the first digit.
282 Maj = EatNumber(OSName);
283 if (OSName.empty()) return;
285 // Handle minor version: 10.4.9 -> darwin8.9.
286 if (OSName[0] != '.')
290 OSName = OSName.substr(1);
292 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
295 Min = EatNumber(OSName);
296 if (OSName.empty()) return;
298 // Handle revision darwin8.9.1
299 if (OSName[0] != '.')
303 OSName = OSName.substr(1);
305 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
308 Revision = EatNumber(OSName);
311 void Triple::setTriple(const Twine &Str) {
316 void Triple::setArch(ArchType Kind) {
317 setArchName(getArchTypeName(Kind));
320 void Triple::setVendor(VendorType Kind) {
321 setVendorName(getVendorTypeName(Kind));
324 void Triple::setOS(OSType Kind) {
325 setOSName(getOSTypeName(Kind));
328 void Triple::setArchName(const StringRef &Str) {
329 setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
332 void Triple::setVendorName(const StringRef &Str) {
333 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
336 void Triple::setOSName(const StringRef &Str) {
337 if (hasEnvironment())
338 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
339 "-" + getEnvironmentName());
341 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
344 void Triple::setEnvironmentName(const StringRef &Str) {
345 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
349 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
350 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);