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"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/ErrorHandling.h"
18 const char *Triple::getArchTypeName(ArchType Kind) {
20 case UnknownArch: return "unknown";
22 case aarch64: return "aarch64";
23 case arm: return "arm";
24 case hexagon: return "hexagon";
25 case mips: return "mips";
26 case mipsel: return "mipsel";
27 case mips64: return "mips64";
28 case mips64el:return "mips64el";
29 case msp430: return "msp430";
30 case ppc64: return "powerpc64";
31 case ppc64le: return "powerpc64le";
32 case ppc: return "powerpc";
33 case r600: return "r600";
34 case sparc: return "sparc";
35 case sparcv9: return "sparcv9";
36 case systemz: return "s390x";
37 case tce: return "tce";
38 case thumb: return "thumb";
39 case x86: return "i386";
40 case x86_64: return "x86_64";
41 case xcore: return "xcore";
42 case nvptx: return "nvptx";
43 case nvptx64: return "nvptx64";
44 case le32: return "le32";
45 case amdil: return "amdil";
46 case spir: return "spir";
47 case spir64: return "spir64";
50 llvm_unreachable("Invalid ArchType!");
53 const char *Triple::getArchTypePrefix(ArchType Kind) {
58 case aarch64: return "aarch64";
61 case thumb: return "arm";
65 case ppc: return "ppc";
70 case mips64el:return "mips";
72 case hexagon: return "hexagon";
74 case r600: return "r600";
77 case sparc: return "sparc";
79 case systemz: return "systemz";
82 case x86_64: return "x86";
84 case xcore: return "xcore";
86 case nvptx: return "nvptx";
87 case nvptx64: return "nvptx";
88 case le32: return "le32";
89 case amdil: return "amdil";
90 case spir: return "spir";
91 case spir64: return "spir";
95 const char *Triple::getVendorTypeName(VendorType Kind) {
97 case UnknownVendor: return "unknown";
99 case Apple: return "apple";
100 case PC: return "pc";
101 case SCEI: return "scei";
102 case BGP: return "bgp";
103 case BGQ: return "bgq";
104 case Freescale: return "fsl";
105 case IBM: return "ibm";
106 case NVIDIA: return "nvidia";
109 llvm_unreachable("Invalid VendorType!");
112 const char *Triple::getOSTypeName(OSType Kind) {
114 case UnknownOS: return "unknown";
116 case AuroraUX: return "auroraux";
117 case Cygwin: return "cygwin";
118 case Darwin: return "darwin";
119 case DragonFly: return "dragonfly";
120 case FreeBSD: return "freebsd";
121 case IOS: return "ios";
122 case KFreeBSD: return "kfreebsd";
123 case Linux: return "linux";
124 case Lv2: return "lv2";
125 case MacOSX: return "macosx";
126 case MinGW32: return "mingw32";
127 case NetBSD: return "netbsd";
128 case OpenBSD: return "openbsd";
129 case Solaris: return "solaris";
130 case Win32: return "win32";
131 case Haiku: return "haiku";
132 case Minix: return "minix";
133 case RTEMS: return "rtems";
134 case NaCl: return "nacl";
135 case CNK: return "cnk";
136 case Bitrig: return "bitrig";
137 case AIX: return "aix";
138 case CUDA: return "cuda";
139 case NVCL: return "nvcl";
142 llvm_unreachable("Invalid OSType");
145 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
147 case UnknownEnvironment: return "unknown";
148 case GNU: return "gnu";
149 case GNUEABIHF: return "gnueabihf";
150 case GNUEABI: return "gnueabi";
151 case GNUX32: return "gnux32";
152 case CODE16: return "code16";
153 case EABI: return "eabi";
154 case EABIHF: return "eabihf";
155 case MachO: return "macho";
156 case Android: return "android";
157 case ELF: return "elf";
160 llvm_unreachable("Invalid EnvironmentType!");
163 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
164 return StringSwitch<Triple::ArchType>(Name)
165 .Case("aarch64", aarch64)
168 .Case("mipsel", mipsel)
169 .Case("mips64", mips64)
170 .Case("mips64el", mips64el)
171 .Case("msp430", msp430)
172 .Case("ppc64", ppc64)
175 .Case("ppc64le", ppc64le)
177 .Case("hexagon", hexagon)
178 .Case("sparc", sparc)
179 .Case("sparcv9", sparcv9)
180 .Case("systemz", systemz)
182 .Case("thumb", thumb)
184 .Case("x86-64", x86_64)
185 .Case("xcore", xcore)
186 .Case("nvptx", nvptx)
187 .Case("nvptx64", nvptx64)
189 .Case("amdil", amdil)
191 .Case("spir64", spir64)
192 .Default(UnknownArch);
195 // Returns architecture name that is understood by the target assembler.
196 const char *Triple::getArchNameForAssembler() {
197 if (!isOSDarwin() && getVendor() != Triple::Apple)
200 return StringSwitch<const char*>(getArchName())
201 .Case("i386", "i386")
202 .Case("x86_64", "x86_64")
203 .Case("powerpc", "ppc")
204 .Case("powerpc64", "ppc64")
205 .Case("powerpc64le", "ppc64le")
207 .Cases("armv4t", "thumbv4t", "armv4t")
208 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
209 .Cases("armv6", "thumbv6", "armv6")
210 .Cases("armv7", "thumbv7", "armv7")
211 .Case("r600", "r600")
212 .Case("nvptx", "nvptx")
213 .Case("nvptx64", "nvptx64")
214 .Case("le32", "le32")
215 .Case("amdil", "amdil")
216 .Case("spir", "spir")
217 .Case("spir64", "spir64")
221 static Triple::ArchType parseArch(StringRef ArchName) {
222 return StringSwitch<Triple::ArchType>(ArchName)
223 .Cases("i386", "i486", "i586", "i686", Triple::x86)
224 // FIXME: Do we need to support these?
225 .Cases("i786", "i886", "i986", Triple::x86)
226 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
227 .Case("powerpc", Triple::ppc)
228 .Cases("powerpc64", "ppu", Triple::ppc64)
229 .Case("powerpc64le", Triple::ppc64le)
230 .Case("aarch64", Triple::aarch64)
231 .Cases("arm", "xscale", Triple::arm)
232 // FIXME: It would be good to replace these with explicit names for all the
233 // various suffixes supported.
234 .StartsWith("armv", Triple::arm)
235 .Case("thumb", Triple::thumb)
236 .StartsWith("thumbv", Triple::thumb)
237 .Case("msp430", Triple::msp430)
238 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
239 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
240 .Cases("mips64", "mips64eb", Triple::mips64)
241 .Case("mips64el", Triple::mips64el)
242 .Case("r600", Triple::r600)
243 .Case("hexagon", Triple::hexagon)
244 .Case("s390x", Triple::systemz)
245 .Case("sparc", Triple::sparc)
246 .Cases("sparcv9", "sparc64", Triple::sparcv9)
247 .Case("tce", Triple::tce)
248 .Case("xcore", Triple::xcore)
249 .Case("nvptx", Triple::nvptx)
250 .Case("nvptx64", Triple::nvptx64)
251 .Case("le32", Triple::le32)
252 .Case("amdil", Triple::amdil)
253 .Case("spir", Triple::spir)
254 .Case("spir64", Triple::spir64)
255 .Default(Triple::UnknownArch);
258 static Triple::VendorType parseVendor(StringRef VendorName) {
259 return StringSwitch<Triple::VendorType>(VendorName)
260 .Case("apple", Triple::Apple)
261 .Case("pc", Triple::PC)
262 .Case("scei", Triple::SCEI)
263 .Case("bgp", Triple::BGP)
264 .Case("bgq", Triple::BGQ)
265 .Case("fsl", Triple::Freescale)
266 .Case("ibm", Triple::IBM)
267 .Case("nvidia", Triple::NVIDIA)
268 .Default(Triple::UnknownVendor);
271 static Triple::OSType parseOS(StringRef OSName) {
272 return StringSwitch<Triple::OSType>(OSName)
273 .StartsWith("auroraux", Triple::AuroraUX)
274 .StartsWith("cygwin", Triple::Cygwin)
275 .StartsWith("darwin", Triple::Darwin)
276 .StartsWith("dragonfly", Triple::DragonFly)
277 .StartsWith("freebsd", Triple::FreeBSD)
278 .StartsWith("ios", Triple::IOS)
279 .StartsWith("kfreebsd", Triple::KFreeBSD)
280 .StartsWith("linux", Triple::Linux)
281 .StartsWith("lv2", Triple::Lv2)
282 .StartsWith("macosx", Triple::MacOSX)
283 .StartsWith("mingw32", Triple::MinGW32)
284 .StartsWith("netbsd", Triple::NetBSD)
285 .StartsWith("openbsd", Triple::OpenBSD)
286 .StartsWith("solaris", Triple::Solaris)
287 .StartsWith("win32", Triple::Win32)
288 .StartsWith("haiku", Triple::Haiku)
289 .StartsWith("minix", Triple::Minix)
290 .StartsWith("rtems", Triple::RTEMS)
291 .StartsWith("nacl", Triple::NaCl)
292 .StartsWith("cnk", Triple::CNK)
293 .StartsWith("bitrig", Triple::Bitrig)
294 .StartsWith("aix", Triple::AIX)
295 .StartsWith("cuda", Triple::CUDA)
296 .StartsWith("nvcl", Triple::NVCL)
297 .Default(Triple::UnknownOS);
300 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
301 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
302 .StartsWith("eabihf", Triple::EABIHF)
303 .StartsWith("eabi", Triple::EABI)
304 .StartsWith("gnueabihf", Triple::GNUEABIHF)
305 .StartsWith("gnueabi", Triple::GNUEABI)
306 .StartsWith("gnux32", Triple::GNUX32)
307 .StartsWith("code16", Triple::CODE16)
308 .StartsWith("gnu", Triple::GNU)
309 .StartsWith("macho", Triple::MachO)
310 .StartsWith("android", Triple::Android)
311 .StartsWith("elf", Triple::ELF)
312 .Default(Triple::UnknownEnvironment);
315 /// \brief Construct a triple from the string representation provided.
317 /// This stores the string representation and parses the various pieces into
319 Triple::Triple(const Twine &Str)
321 Arch(parseArch(getArchName())),
322 Vendor(parseVendor(getVendorName())),
323 OS(parseOS(getOSName())),
324 Environment(parseEnvironment(getEnvironmentName())) {
327 /// \brief Construct a triple from string representations of the architecture,
330 /// This joins each argument into a canonical string representation and parses
331 /// them into enum members. It leaves the environment unknown and omits it from
332 /// the string representation.
333 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
334 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
335 Arch(parseArch(ArchStr.str())),
336 Vendor(parseVendor(VendorStr.str())),
337 OS(parseOS(OSStr.str())),
341 /// \brief Construct a triple from string representations of the architecture,
342 /// vendor, OS, and environment.
344 /// This joins each argument into a canonical string representation and parses
345 /// them into enum members.
346 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
347 const Twine &EnvironmentStr)
348 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
349 EnvironmentStr).str()),
350 Arch(parseArch(ArchStr.str())),
351 Vendor(parseVendor(VendorStr.str())),
352 OS(parseOS(OSStr.str())),
353 Environment(parseEnvironment(EnvironmentStr.str())) {
356 std::string Triple::normalize(StringRef Str) {
357 // Parse into components.
358 SmallVector<StringRef, 4> Components;
359 Str.split(Components, "-");
361 // If the first component corresponds to a known architecture, preferentially
362 // use it for the architecture. If the second component corresponds to a
363 // known vendor, preferentially use it for the vendor, etc. This avoids silly
364 // component movement when a component parses as (eg) both a valid arch and a
366 ArchType Arch = UnknownArch;
367 if (Components.size() > 0)
368 Arch = parseArch(Components[0]);
369 VendorType Vendor = UnknownVendor;
370 if (Components.size() > 1)
371 Vendor = parseVendor(Components[1]);
372 OSType OS = UnknownOS;
373 if (Components.size() > 2)
374 OS = parseOS(Components[2]);
375 EnvironmentType Environment = UnknownEnvironment;
376 if (Components.size() > 3)
377 Environment = parseEnvironment(Components[3]);
379 // Note which components are already in their final position. These will not
382 Found[0] = Arch != UnknownArch;
383 Found[1] = Vendor != UnknownVendor;
384 Found[2] = OS != UnknownOS;
385 Found[3] = Environment != UnknownEnvironment;
387 // If they are not there already, permute the components into their canonical
388 // positions by seeing if they parse as a valid architecture, and if so moving
389 // the component to the architecture position etc.
390 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
392 continue; // Already in the canonical position.
394 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
395 // Do not reparse any components that already matched.
396 if (Idx < array_lengthof(Found) && Found[Idx])
399 // Does this component parse as valid for the target position?
401 StringRef Comp = Components[Idx];
403 default: llvm_unreachable("unexpected component type!");
405 Arch = parseArch(Comp);
406 Valid = Arch != UnknownArch;
409 Vendor = parseVendor(Comp);
410 Valid = Vendor != UnknownVendor;
414 Valid = OS != UnknownOS;
417 Environment = parseEnvironment(Comp);
418 Valid = Environment != UnknownEnvironment;
422 continue; // Nope, try the next component.
424 // Move the component to the target position, pushing any non-fixed
425 // components that are in the way to the right. This tends to give
426 // good results in the common cases of a forgotten vendor component
427 // or a wrongly positioned environment.
429 // Insert left, pushing the existing components to the right. For
430 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
431 StringRef CurrentComponent(""); // The empty component.
432 // Replace the component we are moving with an empty component.
433 std::swap(CurrentComponent, Components[Idx]);
434 // Insert the component being moved at Pos, displacing any existing
435 // components to the right.
436 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
437 // Skip over any fixed components.
438 while (i < array_lengthof(Found) && Found[i])
440 // Place the component at the new position, getting the component
441 // that was at this position - it will be moved right.
442 std::swap(CurrentComponent, Components[i]);
444 } else if (Pos > Idx) {
445 // Push right by inserting empty components until the component at Idx
446 // reaches the target position Pos. For example, pc-a -> -pc-a when
447 // moving pc to the second position.
449 // Insert one empty component at Idx.
450 StringRef CurrentComponent(""); // The empty component.
451 for (unsigned i = Idx; i < Components.size();) {
452 // Place the component at the new position, getting the component
453 // that was at this position - it will be moved right.
454 std::swap(CurrentComponent, Components[i]);
455 // If it was placed on top of an empty component then we are done.
456 if (CurrentComponent.empty())
458 // Advance to the next component, skipping any fixed components.
459 while (++i < array_lengthof(Found) && Found[i])
462 // The last component was pushed off the end - append it.
463 if (!CurrentComponent.empty())
464 Components.push_back(CurrentComponent);
466 // Advance Idx to the component's new position.
467 while (++Idx < array_lengthof(Found) && Found[Idx])
469 } while (Idx < Pos); // Add more until the final position is reached.
471 assert(Pos < Components.size() && Components[Pos] == Comp &&
472 "Component moved wrong!");
478 // Special case logic goes here. At this point Arch, Vendor and OS have the
479 // correct values for the computed components.
481 // Stick the corrected components back together to form the normalized string.
482 std::string Normalized;
483 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
484 if (i) Normalized += '-';
485 Normalized += Components[i];
490 StringRef Triple::getArchName() const {
491 return StringRef(Data).split('-').first; // Isolate first component
494 StringRef Triple::getVendorName() const {
495 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
496 return Tmp.split('-').first; // Isolate second component
499 StringRef Triple::getOSName() const {
500 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
501 Tmp = Tmp.split('-').second; // Strip second component
502 return Tmp.split('-').first; // Isolate third component
505 StringRef Triple::getEnvironmentName() const {
506 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
507 Tmp = Tmp.split('-').second; // Strip second component
508 return Tmp.split('-').second; // Strip third component
511 StringRef Triple::getOSAndEnvironmentName() const {
512 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
513 return Tmp.split('-').second; // Strip second component
516 static unsigned EatNumber(StringRef &Str) {
517 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
521 // Consume the leading digit.
522 Result = Result*10 + (Str[0] - '0');
526 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
531 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
532 unsigned &Micro) const {
533 StringRef OSName = getOSName();
535 // Assume that the OS portion of the triple starts with the canonical name.
536 StringRef OSTypeName = getOSTypeName(getOS());
537 if (OSName.startswith(OSTypeName))
538 OSName = OSName.substr(OSTypeName.size());
540 // Any unset version defaults to 0.
541 Major = Minor = Micro = 0;
543 // Parse up to three components.
544 unsigned *Components[3] = { &Major, &Minor, &Micro };
545 for (unsigned i = 0; i != 3; ++i) {
546 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
549 // Consume the leading number.
550 *Components[i] = EatNumber(OSName);
552 // Consume the separator, if present.
553 if (OSName.startswith("."))
554 OSName = OSName.substr(1);
558 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
559 unsigned &Micro) const {
560 getOSVersion(Major, Minor, Micro);
563 default: llvm_unreachable("unexpected OS for Darwin triple");
565 // Default to darwin8, i.e., MacOSX 10.4.
568 // Darwin version numbers are skewed from OS X versions.
585 // Ignore the version from the triple. This is only handled because the
586 // the clang driver combines OS X and IOS support into a common Darwin
587 // toolchain that wants to know the OS X version number even when targeting
597 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
598 unsigned &Micro) const {
600 default: llvm_unreachable("unexpected OS for Darwin triple");
603 // Ignore the version from the triple. This is only handled because the
604 // the clang driver combines OS X and IOS support into a common Darwin
605 // toolchain that wants to know the iOS version number even when targeting
612 getOSVersion(Major, Minor, Micro);
620 void Triple::setTriple(const Twine &Str) {
624 void Triple::setArch(ArchType Kind) {
625 setArchName(getArchTypeName(Kind));
628 void Triple::setVendor(VendorType Kind) {
629 setVendorName(getVendorTypeName(Kind));
632 void Triple::setOS(OSType Kind) {
633 setOSName(getOSTypeName(Kind));
636 void Triple::setEnvironment(EnvironmentType Kind) {
637 setEnvironmentName(getEnvironmentTypeName(Kind));
640 void Triple::setArchName(StringRef Str) {
641 // Work around a miscompilation bug for Twines in gcc 4.0.3.
642 SmallString<64> Triple;
645 Triple += getVendorName();
647 Triple += getOSAndEnvironmentName();
648 setTriple(Triple.str());
651 void Triple::setVendorName(StringRef Str) {
652 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
655 void Triple::setOSName(StringRef Str) {
656 if (hasEnvironment())
657 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
658 "-" + getEnvironmentName());
660 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
663 void Triple::setEnvironmentName(StringRef Str) {
664 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
668 void Triple::setOSAndEnvironmentName(StringRef Str) {
669 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
672 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
674 case llvm::Triple::UnknownArch:
677 case llvm::Triple::msp430:
680 case llvm::Triple::amdil:
681 case llvm::Triple::arm:
682 case llvm::Triple::hexagon:
683 case llvm::Triple::le32:
684 case llvm::Triple::mips:
685 case llvm::Triple::mipsel:
686 case llvm::Triple::nvptx:
687 case llvm::Triple::ppc:
688 case llvm::Triple::r600:
689 case llvm::Triple::sparc:
690 case llvm::Triple::tce:
691 case llvm::Triple::thumb:
692 case llvm::Triple::x86:
693 case llvm::Triple::xcore:
694 case llvm::Triple::spir:
697 case llvm::Triple::aarch64:
698 case llvm::Triple::mips64:
699 case llvm::Triple::mips64el:
700 case llvm::Triple::nvptx64:
701 case llvm::Triple::ppc64:
702 case llvm::Triple::ppc64le:
703 case llvm::Triple::sparcv9:
704 case llvm::Triple::systemz:
705 case llvm::Triple::x86_64:
706 case llvm::Triple::spir64:
709 llvm_unreachable("Invalid architecture value");
712 bool Triple::isArch64Bit() const {
713 return getArchPointerBitWidth(getArch()) == 64;
716 bool Triple::isArch32Bit() const {
717 return getArchPointerBitWidth(getArch()) == 32;
720 bool Triple::isArch16Bit() const {
721 return getArchPointerBitWidth(getArch()) == 16;
724 Triple Triple::get32BitArchVariant() const {
727 case Triple::UnknownArch:
728 case Triple::aarch64:
730 case Triple::systemz:
731 case Triple::ppc64le:
732 T.setArch(UnknownArch);
738 case Triple::hexagon:
753 case Triple::mips64: T.setArch(Triple::mips); break;
754 case Triple::mips64el: T.setArch(Triple::mipsel); break;
755 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
756 case Triple::ppc64: T.setArch(Triple::ppc); break;
757 case Triple::sparcv9: T.setArch(Triple::sparc); break;
758 case Triple::x86_64: T.setArch(Triple::x86); break;
759 case Triple::spir64: T.setArch(Triple::spir); break;
764 Triple Triple::get64BitArchVariant() const {
767 case Triple::UnknownArch:
770 case Triple::hexagon:
777 T.setArch(UnknownArch);
780 case Triple::aarch64:
783 case Triple::mips64el:
784 case Triple::nvptx64:
786 case Triple::ppc64le:
787 case Triple::sparcv9:
788 case Triple::systemz:
793 case Triple::mips: T.setArch(Triple::mips64); break;
794 case Triple::mipsel: T.setArch(Triple::mips64el); break;
795 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
796 case Triple::ppc: T.setArch(Triple::ppc64); break;
797 case Triple::sparc: T.setArch(Triple::sparcv9); break;
798 case Triple::x86: T.setArch(Triple::x86_64); break;
799 case Triple::spir: T.setArch(Triple::spir64); break;