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 nvptx: return "nvptx";
42 case nvptx64: return "nvptx64";
43 case le32: return "le32";
44 case amdil: return "amdil";
45 case spir: return "spir";
46 case spir64: return "spir64";
49 llvm_unreachable("Invalid ArchType!");
52 const char *Triple::getArchTypePrefix(ArchType Kind) {
57 case aarch64: return "aarch64";
60 case thumb: return "arm";
63 case ppc: return "ppc";
68 case mips64el:return "mips";
70 case hexagon: return "hexagon";
72 case r600: return "r600";
75 case sparc: return "sparc";
77 case systemz: return "systemz";
80 case x86_64: return "x86";
82 case xcore: return "xcore";
84 case nvptx: return "nvptx";
85 case nvptx64: return "nvptx";
86 case le32: return "le32";
87 case amdil: return "amdil";
88 case spir: return "spir";
89 case spir64: return "spir";
93 const char *Triple::getVendorTypeName(VendorType Kind) {
95 case UnknownVendor: return "unknown";
97 case Apple: return "apple";
99 case SCEI: return "scei";
100 case BGP: return "bgp";
101 case BGQ: return "bgq";
102 case Freescale: return "fsl";
103 case IBM: return "ibm";
104 case NVIDIA: return "nvidia";
107 llvm_unreachable("Invalid VendorType!");
110 const char *Triple::getOSTypeName(OSType Kind) {
112 case UnknownOS: return "unknown";
114 case AuroraUX: return "auroraux";
115 case Cygwin: return "cygwin";
116 case Darwin: return "darwin";
117 case DragonFly: return "dragonfly";
118 case FreeBSD: return "freebsd";
119 case IOS: return "ios";
120 case KFreeBSD: return "kfreebsd";
121 case Linux: return "linux";
122 case Lv2: return "lv2";
123 case MacOSX: return "macosx";
124 case MinGW32: return "mingw32";
125 case NetBSD: return "netbsd";
126 case OpenBSD: return "openbsd";
127 case Solaris: return "solaris";
128 case Win32: return "win32";
129 case Haiku: return "haiku";
130 case Minix: return "minix";
131 case RTEMS: return "rtems";
132 case NaCl: return "nacl";
133 case CNK: return "cnk";
134 case Bitrig: return "bitrig";
135 case AIX: return "aix";
136 case CUDA: return "cuda";
137 case NVCL: return "nvcl";
140 llvm_unreachable("Invalid OSType");
143 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
145 case UnknownEnvironment: return "unknown";
146 case GNU: return "gnu";
147 case GNUEABIHF: return "gnueabihf";
148 case GNUEABI: return "gnueabi";
149 case GNUX32: return "gnux32";
150 case EABI: return "eabi";
151 case MachO: return "macho";
152 case Android: return "android";
153 case ELF: return "elf";
156 llvm_unreachable("Invalid EnvironmentType!");
159 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
160 return StringSwitch<Triple::ArchType>(Name)
161 .Case("aarch64", aarch64)
164 .Case("mipsel", mipsel)
165 .Case("mips64", mips64)
166 .Case("mips64el", mips64el)
167 .Case("msp430", msp430)
168 .Case("ppc64", ppc64)
172 .Case("hexagon", hexagon)
173 .Case("sparc", sparc)
174 .Case("sparcv9", sparcv9)
175 .Case("systemz", systemz)
177 .Case("thumb", thumb)
179 .Case("x86-64", x86_64)
180 .Case("xcore", xcore)
181 .Case("nvptx", nvptx)
182 .Case("nvptx64", nvptx64)
184 .Case("amdil", amdil)
186 .Case("spir64", spir64)
187 .Default(UnknownArch);
190 // Returns architecture name that is understood by the target assembler.
191 const char *Triple::getArchNameForAssembler() {
192 if (!isOSDarwin() && getVendor() != Triple::Apple)
195 return StringSwitch<const char*>(getArchName())
196 .Case("i386", "i386")
197 .Case("x86_64", "x86_64")
198 .Case("powerpc", "ppc")
199 .Case("powerpc64", "ppc64")
201 .Cases("armv4t", "thumbv4t", "armv4t")
202 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
203 .Cases("armv6", "thumbv6", "armv6")
204 .Cases("armv7", "thumbv7", "armv7")
205 .Case("r600", "r600")
206 .Case("nvptx", "nvptx")
207 .Case("nvptx64", "nvptx64")
208 .Case("le32", "le32")
209 .Case("amdil", "amdil")
210 .Case("spir", "spir")
211 .Case("spir64", "spir64")
215 static Triple::ArchType parseArch(StringRef ArchName) {
216 return StringSwitch<Triple::ArchType>(ArchName)
217 .Cases("i386", "i486", "i586", "i686", Triple::x86)
218 // FIXME: Do we need to support these?
219 .Cases("i786", "i886", "i986", Triple::x86)
220 .Cases("amd64", "x86_64", Triple::x86_64)
221 .Case("powerpc", Triple::ppc)
222 .Cases("powerpc64", "ppu", Triple::ppc64)
223 .Case("aarch64", Triple::aarch64)
224 .Cases("arm", "xscale", Triple::arm)
225 // FIXME: It would be good to replace these with explicit names for all the
226 // various suffixes supported.
227 .StartsWith("armv", Triple::arm)
228 .Case("thumb", Triple::thumb)
229 .StartsWith("thumbv", Triple::thumb)
230 .Case("msp430", Triple::msp430)
231 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
232 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
233 .Cases("mips64", "mips64eb", Triple::mips64)
234 .Case("mips64el", Triple::mips64el)
235 .Case("r600", Triple::r600)
236 .Case("hexagon", Triple::hexagon)
237 .Case("s390x", Triple::systemz)
238 .Case("sparc", Triple::sparc)
239 .Cases("sparcv9", "sparc64", Triple::sparcv9)
240 .Case("tce", Triple::tce)
241 .Case("xcore", Triple::xcore)
242 .Case("nvptx", Triple::nvptx)
243 .Case("nvptx64", Triple::nvptx64)
244 .Case("le32", Triple::le32)
245 .Case("amdil", Triple::amdil)
246 .Case("spir", Triple::spir)
247 .Case("spir64", Triple::spir64)
248 .Default(Triple::UnknownArch);
251 static Triple::VendorType parseVendor(StringRef VendorName) {
252 return StringSwitch<Triple::VendorType>(VendorName)
253 .Case("apple", Triple::Apple)
254 .Case("pc", Triple::PC)
255 .Case("scei", Triple::SCEI)
256 .Case("bgp", Triple::BGP)
257 .Case("bgq", Triple::BGQ)
258 .Case("fsl", Triple::Freescale)
259 .Case("ibm", Triple::IBM)
260 .Case("nvidia", Triple::NVIDIA)
261 .Default(Triple::UnknownVendor);
264 static Triple::OSType parseOS(StringRef OSName) {
265 return StringSwitch<Triple::OSType>(OSName)
266 .StartsWith("auroraux", Triple::AuroraUX)
267 .StartsWith("cygwin", Triple::Cygwin)
268 .StartsWith("darwin", Triple::Darwin)
269 .StartsWith("dragonfly", Triple::DragonFly)
270 .StartsWith("freebsd", Triple::FreeBSD)
271 .StartsWith("ios", Triple::IOS)
272 .StartsWith("kfreebsd", Triple::KFreeBSD)
273 .StartsWith("linux", Triple::Linux)
274 .StartsWith("lv2", Triple::Lv2)
275 .StartsWith("macosx", Triple::MacOSX)
276 .StartsWith("mingw32", Triple::MinGW32)
277 .StartsWith("netbsd", Triple::NetBSD)
278 .StartsWith("openbsd", Triple::OpenBSD)
279 .StartsWith("solaris", Triple::Solaris)
280 .StartsWith("win32", Triple::Win32)
281 .StartsWith("haiku", Triple::Haiku)
282 .StartsWith("minix", Triple::Minix)
283 .StartsWith("rtems", Triple::RTEMS)
284 .StartsWith("nacl", Triple::NaCl)
285 .StartsWith("cnk", Triple::CNK)
286 .StartsWith("bitrig", Triple::Bitrig)
287 .StartsWith("aix", Triple::AIX)
288 .StartsWith("cuda", Triple::CUDA)
289 .StartsWith("nvcl", Triple::NVCL)
290 .Default(Triple::UnknownOS);
293 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
294 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
295 .StartsWith("eabi", Triple::EABI)
296 .StartsWith("gnueabihf", Triple::GNUEABIHF)
297 .StartsWith("gnueabi", Triple::GNUEABI)
298 .StartsWith("gnux32", Triple::GNUX32)
299 .StartsWith("gnu", Triple::GNU)
300 .StartsWith("macho", Triple::MachO)
301 .StartsWith("android", Triple::Android)
302 .StartsWith("elf", Triple::ELF)
303 .Default(Triple::UnknownEnvironment);
306 /// \brief Construct a triple from the string representation provided.
308 /// This stores the string representation and parses the various pieces into
310 Triple::Triple(const Twine &Str)
312 Arch(parseArch(getArchName())),
313 Vendor(parseVendor(getVendorName())),
314 OS(parseOS(getOSName())),
315 Environment(parseEnvironment(getEnvironmentName())) {
318 /// \brief Construct a triple from string representations of the architecture,
321 /// This joins each argument into a canonical string representation and parses
322 /// them into enum members. It leaves the environment unknown and omits it from
323 /// the string representation.
324 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
325 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
326 Arch(parseArch(ArchStr.str())),
327 Vendor(parseVendor(VendorStr.str())),
328 OS(parseOS(OSStr.str())),
332 /// \brief Construct a triple from string representations of the architecture,
333 /// vendor, OS, and environment.
335 /// This joins each argument into a canonical string representation and parses
336 /// them into enum members.
337 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
338 const Twine &EnvironmentStr)
339 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
340 EnvironmentStr).str()),
341 Arch(parseArch(ArchStr.str())),
342 Vendor(parseVendor(VendorStr.str())),
343 OS(parseOS(OSStr.str())),
344 Environment(parseEnvironment(EnvironmentStr.str())) {
347 std::string Triple::normalize(StringRef Str) {
348 // Parse into components.
349 SmallVector<StringRef, 4> Components;
350 Str.split(Components, "-");
352 // If the first component corresponds to a known architecture, preferentially
353 // use it for the architecture. If the second component corresponds to a
354 // known vendor, preferentially use it for the vendor, etc. This avoids silly
355 // component movement when a component parses as (eg) both a valid arch and a
357 ArchType Arch = UnknownArch;
358 if (Components.size() > 0)
359 Arch = parseArch(Components[0]);
360 VendorType Vendor = UnknownVendor;
361 if (Components.size() > 1)
362 Vendor = parseVendor(Components[1]);
363 OSType OS = UnknownOS;
364 if (Components.size() > 2)
365 OS = parseOS(Components[2]);
366 EnvironmentType Environment = UnknownEnvironment;
367 if (Components.size() > 3)
368 Environment = parseEnvironment(Components[3]);
370 // Note which components are already in their final position. These will not
373 Found[0] = Arch != UnknownArch;
374 Found[1] = Vendor != UnknownVendor;
375 Found[2] = OS != UnknownOS;
376 Found[3] = Environment != UnknownEnvironment;
378 // If they are not there already, permute the components into their canonical
379 // positions by seeing if they parse as a valid architecture, and if so moving
380 // the component to the architecture position etc.
381 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
383 continue; // Already in the canonical position.
385 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
386 // Do not reparse any components that already matched.
387 if (Idx < array_lengthof(Found) && Found[Idx])
390 // Does this component parse as valid for the target position?
392 StringRef Comp = Components[Idx];
394 default: llvm_unreachable("unexpected component type!");
396 Arch = parseArch(Comp);
397 Valid = Arch != UnknownArch;
400 Vendor = parseVendor(Comp);
401 Valid = Vendor != UnknownVendor;
405 Valid = OS != UnknownOS;
408 Environment = parseEnvironment(Comp);
409 Valid = Environment != UnknownEnvironment;
413 continue; // Nope, try the next component.
415 // Move the component to the target position, pushing any non-fixed
416 // components that are in the way to the right. This tends to give
417 // good results in the common cases of a forgotten vendor component
418 // or a wrongly positioned environment.
420 // Insert left, pushing the existing components to the right. For
421 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
422 StringRef CurrentComponent(""); // The empty component.
423 // Replace the component we are moving with an empty component.
424 std::swap(CurrentComponent, Components[Idx]);
425 // Insert the component being moved at Pos, displacing any existing
426 // components to the right.
427 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
428 // Skip over any fixed components.
429 while (i < array_lengthof(Found) && Found[i])
431 // Place the component at the new position, getting the component
432 // that was at this position - it will be moved right.
433 std::swap(CurrentComponent, Components[i]);
435 } else if (Pos > Idx) {
436 // Push right by inserting empty components until the component at Idx
437 // reaches the target position Pos. For example, pc-a -> -pc-a when
438 // moving pc to the second position.
440 // Insert one empty component at Idx.
441 StringRef CurrentComponent(""); // The empty component.
442 for (unsigned i = Idx; i < Components.size();) {
443 // Place the component at the new position, getting the component
444 // that was at this position - it will be moved right.
445 std::swap(CurrentComponent, Components[i]);
446 // If it was placed on top of an empty component then we are done.
447 if (CurrentComponent.empty())
449 // Advance to the next component, skipping any fixed components.
450 while (++i < array_lengthof(Found) && Found[i])
453 // The last component was pushed off the end - append it.
454 if (!CurrentComponent.empty())
455 Components.push_back(CurrentComponent);
457 // Advance Idx to the component's new position.
458 while (++Idx < array_lengthof(Found) && Found[Idx])
460 } while (Idx < Pos); // Add more until the final position is reached.
462 assert(Pos < Components.size() && Components[Pos] == Comp &&
463 "Component moved wrong!");
469 // Special case logic goes here. At this point Arch, Vendor and OS have the
470 // correct values for the computed components.
472 // Stick the corrected components back together to form the normalized string.
473 std::string Normalized;
474 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
475 if (i) Normalized += '-';
476 Normalized += Components[i];
481 StringRef Triple::getArchName() const {
482 return StringRef(Data).split('-').first; // Isolate first component
485 StringRef Triple::getVendorName() const {
486 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
487 return Tmp.split('-').first; // Isolate second component
490 StringRef Triple::getOSName() const {
491 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
492 Tmp = Tmp.split('-').second; // Strip second component
493 return Tmp.split('-').first; // Isolate third component
496 StringRef Triple::getEnvironmentName() const {
497 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
498 Tmp = Tmp.split('-').second; // Strip second component
499 return Tmp.split('-').second; // Strip third component
502 StringRef Triple::getOSAndEnvironmentName() const {
503 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
504 return Tmp.split('-').second; // Strip second component
507 static unsigned EatNumber(StringRef &Str) {
508 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
512 // Consume the leading digit.
513 Result = Result*10 + (Str[0] - '0');
517 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
522 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
523 unsigned &Micro) const {
524 StringRef OSName = getOSName();
526 // Assume that the OS portion of the triple starts with the canonical name.
527 StringRef OSTypeName = getOSTypeName(getOS());
528 if (OSName.startswith(OSTypeName))
529 OSName = OSName.substr(OSTypeName.size());
531 // Any unset version defaults to 0.
532 Major = Minor = Micro = 0;
534 // Parse up to three components.
535 unsigned *Components[3] = { &Major, &Minor, &Micro };
536 for (unsigned i = 0; i != 3; ++i) {
537 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
540 // Consume the leading number.
541 *Components[i] = EatNumber(OSName);
543 // Consume the separator, if present.
544 if (OSName.startswith("."))
545 OSName = OSName.substr(1);
549 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
550 unsigned &Micro) const {
551 getOSVersion(Major, Minor, Micro);
554 default: llvm_unreachable("unexpected OS for Darwin triple");
556 // Default to darwin8, i.e., MacOSX 10.4.
559 // Darwin version numbers are skewed from OS X versions.
576 // Ignore the version from the triple. This is only handled because the
577 // the clang driver combines OS X and IOS support into a common Darwin
578 // toolchain that wants to know the OS X version number even when targeting
588 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
589 unsigned &Micro) const {
591 default: llvm_unreachable("unexpected OS for Darwin triple");
594 // Ignore the version from the triple. This is only handled because the
595 // the clang driver combines OS X and IOS support into a common Darwin
596 // toolchain that wants to know the iOS version number even when targeting
603 getOSVersion(Major, Minor, Micro);
611 void Triple::setTriple(const Twine &Str) {
615 void Triple::setArch(ArchType Kind) {
616 setArchName(getArchTypeName(Kind));
619 void Triple::setVendor(VendorType Kind) {
620 setVendorName(getVendorTypeName(Kind));
623 void Triple::setOS(OSType Kind) {
624 setOSName(getOSTypeName(Kind));
627 void Triple::setEnvironment(EnvironmentType Kind) {
628 setEnvironmentName(getEnvironmentTypeName(Kind));
631 void Triple::setArchName(StringRef Str) {
632 // Work around a miscompilation bug for Twines in gcc 4.0.3.
633 SmallString<64> Triple;
636 Triple += getVendorName();
638 Triple += getOSAndEnvironmentName();
639 setTriple(Triple.str());
642 void Triple::setVendorName(StringRef Str) {
643 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
646 void Triple::setOSName(StringRef Str) {
647 if (hasEnvironment())
648 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
649 "-" + getEnvironmentName());
651 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
654 void Triple::setEnvironmentName(StringRef Str) {
655 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
659 void Triple::setOSAndEnvironmentName(StringRef Str) {
660 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
663 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
665 case llvm::Triple::UnknownArch:
668 case llvm::Triple::msp430:
671 case llvm::Triple::amdil:
672 case llvm::Triple::arm:
673 case llvm::Triple::hexagon:
674 case llvm::Triple::le32:
675 case llvm::Triple::mips:
676 case llvm::Triple::mipsel:
677 case llvm::Triple::nvptx:
678 case llvm::Triple::ppc:
679 case llvm::Triple::r600:
680 case llvm::Triple::sparc:
681 case llvm::Triple::tce:
682 case llvm::Triple::thumb:
683 case llvm::Triple::x86:
684 case llvm::Triple::xcore:
685 case llvm::Triple::spir:
688 case llvm::Triple::aarch64:
689 case llvm::Triple::mips64:
690 case llvm::Triple::mips64el:
691 case llvm::Triple::nvptx64:
692 case llvm::Triple::ppc64:
693 case llvm::Triple::sparcv9:
694 case llvm::Triple::systemz:
695 case llvm::Triple::x86_64:
696 case llvm::Triple::spir64:
699 llvm_unreachable("Invalid architecture value");
702 bool Triple::isArch64Bit() const {
703 return getArchPointerBitWidth(getArch()) == 64;
706 bool Triple::isArch32Bit() const {
707 return getArchPointerBitWidth(getArch()) == 32;
710 bool Triple::isArch16Bit() const {
711 return getArchPointerBitWidth(getArch()) == 16;
714 Triple Triple::get32BitArchVariant() const {
717 case Triple::UnknownArch:
718 case Triple::aarch64:
720 case Triple::systemz:
721 T.setArch(UnknownArch);
727 case Triple::hexagon:
742 case Triple::mips64: T.setArch(Triple::mips); break;
743 case Triple::mips64el: T.setArch(Triple::mipsel); break;
744 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
745 case Triple::ppc64: T.setArch(Triple::ppc); break;
746 case Triple::sparcv9: T.setArch(Triple::sparc); break;
747 case Triple::x86_64: T.setArch(Triple::x86); break;
748 case Triple::spir64: T.setArch(Triple::spir); break;
753 Triple Triple::get64BitArchVariant() const {
756 case Triple::UnknownArch:
759 case Triple::hexagon:
766 T.setArch(UnknownArch);
769 case Triple::aarch64:
772 case Triple::mips64el:
773 case Triple::nvptx64:
775 case Triple::sparcv9:
776 case Triple::systemz:
781 case Triple::mips: T.setArch(Triple::mips64); break;
782 case Triple::mipsel: T.setArch(Triple::mips64el); break;
783 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
784 case Triple::ppc: T.setArch(Triple::ppc64); break;
785 case Triple::sparc: T.setArch(Triple::sparcv9); break;
786 case Triple::x86: T.setArch(Triple::x86_64); break;
787 case Triple::spir: T.setArch(Triple::spir64); break;