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 ppc: return "powerpc";
32 case r600: return "r600";
33 case sparc: return "sparc";
34 case sparcv9: return "sparcv9";
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";
41 case mblaze: return "mblaze";
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";
64 case ppc: return "ppc";
66 case mblaze: return "mblaze";
71 case mips64el:return "mips";
73 case hexagon: return "hexagon";
75 case r600: return "r600";
78 case sparc: return "sparc";
80 case systemz: return "systemz";
83 case x86_64: return "x86";
85 case xcore: return "xcore";
87 case nvptx: return "nvptx";
88 case nvptx64: return "nvptx";
89 case le32: return "le32";
90 case amdil: return "amdil";
91 case spir: return "spir";
92 case spir64: return "spir";
96 const char *Triple::getVendorTypeName(VendorType Kind) {
98 case UnknownVendor: return "unknown";
100 case Apple: return "apple";
101 case PC: return "pc";
102 case SCEI: return "scei";
103 case BGP: return "bgp";
104 case BGQ: return "bgq";
105 case Freescale: return "fsl";
106 case IBM: return "ibm";
107 case NVIDIA: return "nvidia";
110 llvm_unreachable("Invalid VendorType!");
113 const char *Triple::getOSTypeName(OSType Kind) {
115 case UnknownOS: return "unknown";
117 case AuroraUX: return "auroraux";
118 case Cygwin: return "cygwin";
119 case Darwin: return "darwin";
120 case DragonFly: return "dragonfly";
121 case FreeBSD: return "freebsd";
122 case IOS: return "ios";
123 case KFreeBSD: return "kfreebsd";
124 case Linux: return "linux";
125 case Lv2: return "lv2";
126 case MacOSX: return "macosx";
127 case MinGW32: return "mingw32";
128 case NetBSD: return "netbsd";
129 case OpenBSD: return "openbsd";
130 case Solaris: return "solaris";
131 case Win32: return "win32";
132 case Haiku: return "haiku";
133 case Minix: return "minix";
134 case RTEMS: return "rtems";
135 case NaCl: return "nacl";
136 case CNK: return "cnk";
137 case Bitrig: return "bitrig";
138 case AIX: return "aix";
139 case CUDA: return "cuda";
140 case NVCL: return "nvcl";
143 llvm_unreachable("Invalid OSType");
146 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
148 case UnknownEnvironment: return "unknown";
149 case GNU: return "gnu";
150 case GNUEABIHF: return "gnueabihf";
151 case GNUEABI: return "gnueabi";
152 case GNUX32: return "gnux32";
153 case EABI: return "eabi";
154 case MachO: return "macho";
155 case Android: return "android";
156 case ELF: return "elf";
159 llvm_unreachable("Invalid EnvironmentType!");
162 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
163 return StringSwitch<Triple::ArchType>(Name)
164 .Case("aarch64", aarch64)
167 .Case("mipsel", mipsel)
168 .Case("mips64", mips64)
169 .Case("mips64el", mips64el)
170 .Case("msp430", msp430)
171 .Case("ppc64", ppc64)
174 .Case("mblaze", mblaze)
176 .Case("hexagon", hexagon)
177 .Case("sparc", sparc)
178 .Case("sparcv9", sparcv9)
179 .Case("systemz", systemz)
181 .Case("thumb", thumb)
183 .Case("x86-64", x86_64)
184 .Case("xcore", xcore)
185 .Case("nvptx", nvptx)
186 .Case("nvptx64", nvptx64)
188 .Case("amdil", amdil)
190 .Case("spir64", spir64)
191 .Default(UnknownArch);
194 // Returns architecture name that is understood by the target assembler.
195 const char *Triple::getArchNameForAssembler() {
196 if (!isOSDarwin() && getVendor() != Triple::Apple)
199 return StringSwitch<const char*>(getArchName())
200 .Case("i386", "i386")
201 .Case("x86_64", "x86_64")
202 .Case("powerpc", "ppc")
203 .Case("powerpc64", "ppc64")
204 .Cases("mblaze", "microblaze", "mblaze")
206 .Cases("armv4t", "thumbv4t", "armv4t")
207 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
208 .Cases("armv6", "thumbv6", "armv6")
209 .Cases("armv7", "thumbv7", "armv7")
210 .Case("r600", "r600")
211 .Case("nvptx", "nvptx")
212 .Case("nvptx64", "nvptx64")
213 .Case("le32", "le32")
214 .Case("amdil", "amdil")
215 .Case("spir", "spir")
216 .Case("spir64", "spir64")
220 static Triple::ArchType parseArch(StringRef ArchName) {
221 return StringSwitch<Triple::ArchType>(ArchName)
222 .Cases("i386", "i486", "i586", "i686", Triple::x86)
223 // FIXME: Do we need to support these?
224 .Cases("i786", "i886", "i986", Triple::x86)
225 .Cases("amd64", "x86_64", Triple::x86_64)
226 .Case("powerpc", Triple::ppc)
227 .Cases("powerpc64", "ppu", Triple::ppc64)
228 .Case("mblaze", Triple::mblaze)
229 .Case("aarch64", Triple::aarch64)
230 .Cases("arm", "xscale", Triple::arm)
231 // FIXME: It would be good to replace these with explicit names for all the
232 // various suffixes supported.
233 .StartsWith("armv", Triple::arm)
234 .Case("thumb", Triple::thumb)
235 .StartsWith("thumbv", Triple::thumb)
236 .Case("msp430", Triple::msp430)
237 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
238 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
239 .Cases("mips64", "mips64eb", Triple::mips64)
240 .Case("mips64el", Triple::mips64el)
241 .Case("r600", Triple::r600)
242 .Case("hexagon", Triple::hexagon)
243 .Case("s390x", Triple::systemz)
244 .Case("sparc", Triple::sparc)
245 .Cases("sparcv9", "sparc64", Triple::sparcv9)
246 .Case("tce", Triple::tce)
247 .Case("xcore", Triple::xcore)
248 .Case("nvptx", Triple::nvptx)
249 .Case("nvptx64", Triple::nvptx64)
250 .Case("le32", Triple::le32)
251 .Case("amdil", Triple::amdil)
252 .Case("spir", Triple::spir)
253 .Case("spir64", Triple::spir64)
254 .Default(Triple::UnknownArch);
257 static Triple::VendorType parseVendor(StringRef VendorName) {
258 return StringSwitch<Triple::VendorType>(VendorName)
259 .Case("apple", Triple::Apple)
260 .Case("pc", Triple::PC)
261 .Case("scei", Triple::SCEI)
262 .Case("bgp", Triple::BGP)
263 .Case("bgq", Triple::BGQ)
264 .Case("fsl", Triple::Freescale)
265 .Case("ibm", Triple::IBM)
266 .Case("nvidia", Triple::NVIDIA)
267 .Default(Triple::UnknownVendor);
270 static Triple::OSType parseOS(StringRef OSName) {
271 return StringSwitch<Triple::OSType>(OSName)
272 .StartsWith("auroraux", Triple::AuroraUX)
273 .StartsWith("cygwin", Triple::Cygwin)
274 .StartsWith("darwin", Triple::Darwin)
275 .StartsWith("dragonfly", Triple::DragonFly)
276 .StartsWith("freebsd", Triple::FreeBSD)
277 .StartsWith("ios", Triple::IOS)
278 .StartsWith("kfreebsd", Triple::KFreeBSD)
279 .StartsWith("linux", Triple::Linux)
280 .StartsWith("lv2", Triple::Lv2)
281 .StartsWith("macosx", Triple::MacOSX)
282 .StartsWith("mingw32", Triple::MinGW32)
283 .StartsWith("netbsd", Triple::NetBSD)
284 .StartsWith("openbsd", Triple::OpenBSD)
285 .StartsWith("solaris", Triple::Solaris)
286 .StartsWith("win32", Triple::Win32)
287 .StartsWith("haiku", Triple::Haiku)
288 .StartsWith("minix", Triple::Minix)
289 .StartsWith("rtems", Triple::RTEMS)
290 .StartsWith("nacl", Triple::NaCl)
291 .StartsWith("cnk", Triple::CNK)
292 .StartsWith("bitrig", Triple::Bitrig)
293 .StartsWith("aix", Triple::AIX)
294 .StartsWith("cuda", Triple::CUDA)
295 .StartsWith("nvcl", Triple::NVCL)
296 .Default(Triple::UnknownOS);
299 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
300 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
301 .StartsWith("eabi", Triple::EABI)
302 .StartsWith("gnueabihf", Triple::GNUEABIHF)
303 .StartsWith("gnueabi", Triple::GNUEABI)
304 .StartsWith("gnux32", Triple::GNUX32)
305 .StartsWith("gnu", Triple::GNU)
306 .StartsWith("macho", Triple::MachO)
307 .StartsWith("android", Triple::Android)
308 .StartsWith("elf", Triple::ELF)
309 .Default(Triple::UnknownEnvironment);
312 /// \brief Construct a triple from the string representation provided.
314 /// This stores the string representation and parses the various pieces into
316 Triple::Triple(const Twine &Str)
318 Arch(parseArch(getArchName())),
319 Vendor(parseVendor(getVendorName())),
320 OS(parseOS(getOSName())),
321 Environment(parseEnvironment(getEnvironmentName())) {
324 /// \brief Construct a triple from string representations of the architecture,
327 /// This joins each argument into a canonical string representation and parses
328 /// them into enum members. It leaves the environment unknown and omits it from
329 /// the string representation.
330 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
331 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
332 Arch(parseArch(ArchStr.str())),
333 Vendor(parseVendor(VendorStr.str())),
334 OS(parseOS(OSStr.str())),
338 /// \brief Construct a triple from string representations of the architecture,
339 /// vendor, OS, and environment.
341 /// This joins each argument into a canonical string representation and parses
342 /// them into enum members.
343 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
344 const Twine &EnvironmentStr)
345 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
346 EnvironmentStr).str()),
347 Arch(parseArch(ArchStr.str())),
348 Vendor(parseVendor(VendorStr.str())),
349 OS(parseOS(OSStr.str())),
350 Environment(parseEnvironment(EnvironmentStr.str())) {
353 std::string Triple::normalize(StringRef Str) {
354 // Parse into components.
355 SmallVector<StringRef, 4> Components;
356 Str.split(Components, "-");
358 // If the first component corresponds to a known architecture, preferentially
359 // use it for the architecture. If the second component corresponds to a
360 // known vendor, preferentially use it for the vendor, etc. This avoids silly
361 // component movement when a component parses as (eg) both a valid arch and a
363 ArchType Arch = UnknownArch;
364 if (Components.size() > 0)
365 Arch = parseArch(Components[0]);
366 VendorType Vendor = UnknownVendor;
367 if (Components.size() > 1)
368 Vendor = parseVendor(Components[1]);
369 OSType OS = UnknownOS;
370 if (Components.size() > 2)
371 OS = parseOS(Components[2]);
372 EnvironmentType Environment = UnknownEnvironment;
373 if (Components.size() > 3)
374 Environment = parseEnvironment(Components[3]);
376 // Note which components are already in their final position. These will not
379 Found[0] = Arch != UnknownArch;
380 Found[1] = Vendor != UnknownVendor;
381 Found[2] = OS != UnknownOS;
382 Found[3] = Environment != UnknownEnvironment;
384 // If they are not there already, permute the components into their canonical
385 // positions by seeing if they parse as a valid architecture, and if so moving
386 // the component to the architecture position etc.
387 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
389 continue; // Already in the canonical position.
391 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
392 // Do not reparse any components that already matched.
393 if (Idx < array_lengthof(Found) && Found[Idx])
396 // Does this component parse as valid for the target position?
398 StringRef Comp = Components[Idx];
400 default: llvm_unreachable("unexpected component type!");
402 Arch = parseArch(Comp);
403 Valid = Arch != UnknownArch;
406 Vendor = parseVendor(Comp);
407 Valid = Vendor != UnknownVendor;
411 Valid = OS != UnknownOS;
414 Environment = parseEnvironment(Comp);
415 Valid = Environment != UnknownEnvironment;
419 continue; // Nope, try the next component.
421 // Move the component to the target position, pushing any non-fixed
422 // components that are in the way to the right. This tends to give
423 // good results in the common cases of a forgotten vendor component
424 // or a wrongly positioned environment.
426 // Insert left, pushing the existing components to the right. For
427 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
428 StringRef CurrentComponent(""); // The empty component.
429 // Replace the component we are moving with an empty component.
430 std::swap(CurrentComponent, Components[Idx]);
431 // Insert the component being moved at Pos, displacing any existing
432 // components to the right.
433 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
434 // Skip over any fixed components.
435 while (i < array_lengthof(Found) && Found[i])
437 // Place the component at the new position, getting the component
438 // that was at this position - it will be moved right.
439 std::swap(CurrentComponent, Components[i]);
441 } else if (Pos > Idx) {
442 // Push right by inserting empty components until the component at Idx
443 // reaches the target position Pos. For example, pc-a -> -pc-a when
444 // moving pc to the second position.
446 // Insert one empty component at Idx.
447 StringRef CurrentComponent(""); // The empty component.
448 for (unsigned i = Idx; i < Components.size();) {
449 // Place the component at the new position, getting the component
450 // that was at this position - it will be moved right.
451 std::swap(CurrentComponent, Components[i]);
452 // If it was placed on top of an empty component then we are done.
453 if (CurrentComponent.empty())
455 // Advance to the next component, skipping any fixed components.
456 while (++i < array_lengthof(Found) && Found[i])
459 // The last component was pushed off the end - append it.
460 if (!CurrentComponent.empty())
461 Components.push_back(CurrentComponent);
463 // Advance Idx to the component's new position.
464 while (++Idx < array_lengthof(Found) && Found[Idx])
466 } while (Idx < Pos); // Add more until the final position is reached.
468 assert(Pos < Components.size() && Components[Pos] == Comp &&
469 "Component moved wrong!");
475 // Special case logic goes here. At this point Arch, Vendor and OS have the
476 // correct values for the computed components.
478 // Stick the corrected components back together to form the normalized string.
479 std::string Normalized;
480 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
481 if (i) Normalized += '-';
482 Normalized += Components[i];
487 StringRef Triple::getArchName() const {
488 return StringRef(Data).split('-').first; // Isolate first component
491 StringRef Triple::getVendorName() const {
492 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
493 return Tmp.split('-').first; // Isolate second component
496 StringRef Triple::getOSName() const {
497 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
498 Tmp = Tmp.split('-').second; // Strip second component
499 return Tmp.split('-').first; // Isolate third component
502 StringRef Triple::getEnvironmentName() const {
503 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
504 Tmp = Tmp.split('-').second; // Strip second component
505 return Tmp.split('-').second; // Strip third component
508 StringRef Triple::getOSAndEnvironmentName() const {
509 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
510 return Tmp.split('-').second; // Strip second component
513 static unsigned EatNumber(StringRef &Str) {
514 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
518 // Consume the leading digit.
519 Result = Result*10 + (Str[0] - '0');
523 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
528 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
529 unsigned &Micro) const {
530 StringRef OSName = getOSName();
532 // Assume that the OS portion of the triple starts with the canonical name.
533 StringRef OSTypeName = getOSTypeName(getOS());
534 if (OSName.startswith(OSTypeName))
535 OSName = OSName.substr(OSTypeName.size());
537 // Any unset version defaults to 0.
538 Major = Minor = Micro = 0;
540 // Parse up to three components.
541 unsigned *Components[3] = { &Major, &Minor, &Micro };
542 for (unsigned i = 0; i != 3; ++i) {
543 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
546 // Consume the leading number.
547 *Components[i] = EatNumber(OSName);
549 // Consume the separator, if present.
550 if (OSName.startswith("."))
551 OSName = OSName.substr(1);
555 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
556 unsigned &Micro) const {
557 getOSVersion(Major, Minor, Micro);
560 default: llvm_unreachable("unexpected OS for Darwin triple");
562 // Default to darwin8, i.e., MacOSX 10.4.
565 // Darwin version numbers are skewed from OS X versions.
582 // Ignore the version from the triple. This is only handled because the
583 // the clang driver combines OS X and IOS support into a common Darwin
584 // toolchain that wants to know the OS X version number even when targeting
594 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
595 unsigned &Micro) const {
597 default: llvm_unreachable("unexpected OS for Darwin triple");
600 // Ignore the version from the triple. This is only handled because the
601 // the clang driver combines OS X and IOS support into a common Darwin
602 // toolchain that wants to know the iOS version number even when targeting
609 getOSVersion(Major, Minor, Micro);
617 void Triple::setTriple(const Twine &Str) {
621 void Triple::setArch(ArchType Kind) {
622 setArchName(getArchTypeName(Kind));
625 void Triple::setVendor(VendorType Kind) {
626 setVendorName(getVendorTypeName(Kind));
629 void Triple::setOS(OSType Kind) {
630 setOSName(getOSTypeName(Kind));
633 void Triple::setEnvironment(EnvironmentType Kind) {
634 setEnvironmentName(getEnvironmentTypeName(Kind));
637 void Triple::setArchName(StringRef Str) {
638 // Work around a miscompilation bug for Twines in gcc 4.0.3.
639 SmallString<64> Triple;
642 Triple += getVendorName();
644 Triple += getOSAndEnvironmentName();
645 setTriple(Triple.str());
648 void Triple::setVendorName(StringRef Str) {
649 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
652 void Triple::setOSName(StringRef Str) {
653 if (hasEnvironment())
654 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
655 "-" + getEnvironmentName());
657 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
660 void Triple::setEnvironmentName(StringRef Str) {
661 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
665 void Triple::setOSAndEnvironmentName(StringRef Str) {
666 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
669 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
671 case llvm::Triple::UnknownArch:
674 case llvm::Triple::msp430:
677 case llvm::Triple::amdil:
678 case llvm::Triple::arm:
679 case llvm::Triple::hexagon:
680 case llvm::Triple::le32:
681 case llvm::Triple::mblaze:
682 case llvm::Triple::mips:
683 case llvm::Triple::mipsel:
684 case llvm::Triple::nvptx:
685 case llvm::Triple::ppc:
686 case llvm::Triple::r600:
687 case llvm::Triple::sparc:
688 case llvm::Triple::tce:
689 case llvm::Triple::thumb:
690 case llvm::Triple::x86:
691 case llvm::Triple::xcore:
692 case llvm::Triple::spir:
695 case llvm::Triple::aarch64:
696 case llvm::Triple::mips64:
697 case llvm::Triple::mips64el:
698 case llvm::Triple::nvptx64:
699 case llvm::Triple::ppc64:
700 case llvm::Triple::sparcv9:
701 case llvm::Triple::systemz:
702 case llvm::Triple::x86_64:
703 case llvm::Triple::spir64:
706 llvm_unreachable("Invalid architecture value");
709 bool Triple::isArch64Bit() const {
710 return getArchPointerBitWidth(getArch()) == 64;
713 bool Triple::isArch32Bit() const {
714 return getArchPointerBitWidth(getArch()) == 32;
717 bool Triple::isArch16Bit() const {
718 return getArchPointerBitWidth(getArch()) == 16;
721 Triple Triple::get32BitArchVariant() const {
724 case Triple::UnknownArch:
725 case Triple::aarch64:
727 case Triple::systemz:
728 T.setArch(UnknownArch);
734 case Triple::hexagon:
750 case Triple::mips64: T.setArch(Triple::mips); break;
751 case Triple::mips64el: T.setArch(Triple::mipsel); break;
752 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
753 case Triple::ppc64: T.setArch(Triple::ppc); break;
754 case Triple::sparcv9: T.setArch(Triple::sparc); break;
755 case Triple::x86_64: T.setArch(Triple::x86); break;
756 case Triple::spir64: T.setArch(Triple::spir); break;
761 Triple Triple::get64BitArchVariant() const {
764 case Triple::UnknownArch:
767 case Triple::hexagon:
775 T.setArch(UnknownArch);
778 case Triple::aarch64:
781 case Triple::mips64el:
782 case Triple::nvptx64:
784 case Triple::sparcv9:
785 case Triple::systemz:
790 case Triple::mips: T.setArch(Triple::mips64); break;
791 case Triple::mipsel: T.setArch(Triple::mips64el); break;
792 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
793 case Triple::ppc: T.setArch(Triple::ppc64); break;
794 case Triple::sparc: T.setArch(Triple::sparcv9); break;
795 case Triple::x86: T.setArch(Triple::x86_64); break;
796 case Triple::spir: T.setArch(Triple::spir64); break;