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 tce: return "tce";
36 case thumb: return "thumb";
37 case x86: return "i386";
38 case x86_64: return "x86_64";
39 case xcore: return "xcore";
40 case mblaze: return "mblaze";
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";
65 case mblaze: return "mblaze";
70 case mips64el:return "mips";
72 case hexagon: return "hexagon";
74 case r600: return "r600";
77 case sparc: return "sparc";
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";
106 llvm_unreachable("Invalid VendorType!");
109 const char *Triple::getOSTypeName(OSType Kind) {
111 case UnknownOS: return "unknown";
113 case AuroraUX: return "auroraux";
114 case Cygwin: return "cygwin";
115 case Darwin: return "darwin";
116 case DragonFly: return "dragonfly";
117 case FreeBSD: return "freebsd";
118 case IOS: return "ios";
119 case KFreeBSD: return "kfreebsd";
120 case Linux: return "linux";
121 case Lv2: return "lv2";
122 case MacOSX: return "macosx";
123 case MinGW32: return "mingw32";
124 case NetBSD: return "netbsd";
125 case OpenBSD: return "openbsd";
126 case Solaris: return "solaris";
127 case Win32: return "win32";
128 case Haiku: return "haiku";
129 case Minix: return "minix";
130 case RTEMS: return "rtems";
131 case NaCl: return "nacl";
132 case CNK: return "cnk";
133 case Bitrig: return "bitrig";
134 case AIX: return "aix";
137 llvm_unreachable("Invalid OSType");
140 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
142 case UnknownEnvironment: return "unknown";
143 case GNU: return "gnu";
144 case GNUEABIHF: return "gnueabihf";
145 case GNUEABI: return "gnueabi";
146 case GNUX32: return "gnux32";
147 case EABI: return "eabi";
148 case MachO: return "macho";
149 case Android: return "android";
150 case ELF: return "elf";
153 llvm_unreachable("Invalid EnvironmentType!");
156 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
157 return StringSwitch<Triple::ArchType>(Name)
158 .Case("aarch64", aarch64)
161 .Case("mipsel", mipsel)
162 .Case("mips64", mips64)
163 .Case("mips64el", mips64el)
164 .Case("msp430", msp430)
165 .Case("ppc64", ppc64)
168 .Case("mblaze", mblaze)
170 .Case("hexagon", hexagon)
171 .Case("sparc", sparc)
172 .Case("sparcv9", sparcv9)
174 .Case("thumb", thumb)
176 .Case("x86-64", x86_64)
177 .Case("xcore", xcore)
178 .Case("nvptx", nvptx)
179 .Case("nvptx64", nvptx64)
181 .Case("amdil", amdil)
183 .Case("spir64", spir64)
184 .Default(UnknownArch);
187 // Returns architecture name that is understood by the target assembler.
188 const char *Triple::getArchNameForAssembler() {
189 if (!isOSDarwin() && getVendor() != Triple::Apple)
192 return StringSwitch<const char*>(getArchName())
193 .Case("i386", "i386")
194 .Case("x86_64", "x86_64")
195 .Case("powerpc", "ppc")
196 .Case("powerpc64", "ppc64")
197 .Cases("mblaze", "microblaze", "mblaze")
199 .Cases("armv4t", "thumbv4t", "armv4t")
200 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
201 .Cases("armv6", "thumbv6", "armv6")
202 .Cases("armv7", "thumbv7", "armv7")
203 .Case("r600", "r600")
204 .Case("nvptx", "nvptx")
205 .Case("nvptx64", "nvptx64")
206 .Case("le32", "le32")
207 .Case("amdil", "amdil")
208 .Case("spir", "spir")
209 .Case("spir64", "spir64")
213 static Triple::ArchType parseArch(StringRef ArchName) {
214 return StringSwitch<Triple::ArchType>(ArchName)
215 .Cases("i386", "i486", "i586", "i686", Triple::x86)
216 // FIXME: Do we need to support these?
217 .Cases("i786", "i886", "i986", Triple::x86)
218 .Cases("amd64", "x86_64", Triple::x86_64)
219 .Case("powerpc", Triple::ppc)
220 .Cases("powerpc64", "ppu", Triple::ppc64)
221 .Case("mblaze", Triple::mblaze)
222 .Case("aarch64", Triple::aarch64)
223 .Cases("arm", "xscale", Triple::arm)
224 // FIXME: It would be good to replace these with explicit names for all the
225 // various suffixes supported.
226 .StartsWith("armv", Triple::arm)
227 .Case("thumb", Triple::thumb)
228 .StartsWith("thumbv", Triple::thumb)
229 .Case("msp430", Triple::msp430)
230 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
231 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
232 .Cases("mips64", "mips64eb", Triple::mips64)
233 .Case("mips64el", Triple::mips64el)
234 .Case("r600", Triple::r600)
235 .Case("hexagon", Triple::hexagon)
236 .Case("sparc", Triple::sparc)
237 .Case("sparcv9", Triple::sparcv9)
238 .Case("tce", Triple::tce)
239 .Case("xcore", Triple::xcore)
240 .Case("nvptx", Triple::nvptx)
241 .Case("nvptx64", Triple::nvptx64)
242 .Case("le32", Triple::le32)
243 .Case("amdil", Triple::amdil)
244 .Case("spir", Triple::spir)
245 .Case("spir64", Triple::spir64)
246 .Default(Triple::UnknownArch);
249 static Triple::VendorType parseVendor(StringRef VendorName) {
250 return StringSwitch<Triple::VendorType>(VendorName)
251 .Case("apple", Triple::Apple)
252 .Case("pc", Triple::PC)
253 .Case("scei", Triple::SCEI)
254 .Case("bgp", Triple::BGP)
255 .Case("bgq", Triple::BGQ)
256 .Case("fsl", Triple::Freescale)
257 .Case("ibm", Triple::IBM)
258 .Default(Triple::UnknownVendor);
261 static Triple::OSType parseOS(StringRef OSName) {
262 return StringSwitch<Triple::OSType>(OSName)
263 .StartsWith("auroraux", Triple::AuroraUX)
264 .StartsWith("cygwin", Triple::Cygwin)
265 .StartsWith("darwin", Triple::Darwin)
266 .StartsWith("dragonfly", Triple::DragonFly)
267 .StartsWith("freebsd", Triple::FreeBSD)
268 .StartsWith("ios", Triple::IOS)
269 .StartsWith("kfreebsd", Triple::KFreeBSD)
270 .StartsWith("linux", Triple::Linux)
271 .StartsWith("lv2", Triple::Lv2)
272 .StartsWith("macosx", Triple::MacOSX)
273 .StartsWith("mingw32", Triple::MinGW32)
274 .StartsWith("netbsd", Triple::NetBSD)
275 .StartsWith("openbsd", Triple::OpenBSD)
276 .StartsWith("solaris", Triple::Solaris)
277 .StartsWith("win32", Triple::Win32)
278 .StartsWith("haiku", Triple::Haiku)
279 .StartsWith("minix", Triple::Minix)
280 .StartsWith("rtems", Triple::RTEMS)
281 .StartsWith("nacl", Triple::NaCl)
282 .StartsWith("cnk", Triple::CNK)
283 .StartsWith("bitrig", Triple::Bitrig)
284 .StartsWith("aix", Triple::AIX)
285 .Default(Triple::UnknownOS);
288 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
289 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
290 .StartsWith("eabi", Triple::EABI)
291 .StartsWith("gnueabihf", Triple::GNUEABIHF)
292 .StartsWith("gnueabi", Triple::GNUEABI)
293 .StartsWith("gnux32", Triple::GNUX32)
294 .StartsWith("gnu", Triple::GNU)
295 .StartsWith("macho", Triple::MachO)
296 .StartsWith("android", Triple::Android)
297 .StartsWith("elf", Triple::ELF)
298 .Default(Triple::UnknownEnvironment);
301 /// \brief Construct a triple from the string representation provided.
303 /// This stores the string representation and parses the various pieces into
305 Triple::Triple(const Twine &Str)
307 Arch(parseArch(getArchName())),
308 Vendor(parseVendor(getVendorName())),
309 OS(parseOS(getOSName())),
310 Environment(parseEnvironment(getEnvironmentName())) {
313 /// \brief Construct a triple from string representations of the architecture,
316 /// This joins each argument into a canonical string representation and parses
317 /// them into enum members. It leaves the environment unknown and omits it from
318 /// the string representation.
319 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
320 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
321 Arch(parseArch(ArchStr.str())),
322 Vendor(parseVendor(VendorStr.str())),
323 OS(parseOS(OSStr.str())),
327 /// \brief Construct a triple from string representations of the architecture,
328 /// vendor, OS, and environment.
330 /// This joins each argument into a canonical string representation and parses
331 /// them into enum members.
332 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
333 const Twine &EnvironmentStr)
334 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
335 EnvironmentStr).str()),
336 Arch(parseArch(ArchStr.str())),
337 Vendor(parseVendor(VendorStr.str())),
338 OS(parseOS(OSStr.str())),
339 Environment(parseEnvironment(EnvironmentStr.str())) {
342 std::string Triple::normalize(StringRef Str) {
343 // Parse into components.
344 SmallVector<StringRef, 4> Components;
345 Str.split(Components, "-");
347 // If the first component corresponds to a known architecture, preferentially
348 // use it for the architecture. If the second component corresponds to a
349 // known vendor, preferentially use it for the vendor, etc. This avoids silly
350 // component movement when a component parses as (eg) both a valid arch and a
352 ArchType Arch = UnknownArch;
353 if (Components.size() > 0)
354 Arch = parseArch(Components[0]);
355 VendorType Vendor = UnknownVendor;
356 if (Components.size() > 1)
357 Vendor = parseVendor(Components[1]);
358 OSType OS = UnknownOS;
359 if (Components.size() > 2)
360 OS = parseOS(Components[2]);
361 EnvironmentType Environment = UnknownEnvironment;
362 if (Components.size() > 3)
363 Environment = parseEnvironment(Components[3]);
365 // Note which components are already in their final position. These will not
368 Found[0] = Arch != UnknownArch;
369 Found[1] = Vendor != UnknownVendor;
370 Found[2] = OS != UnknownOS;
371 Found[3] = Environment != UnknownEnvironment;
373 // If they are not there already, permute the components into their canonical
374 // positions by seeing if they parse as a valid architecture, and if so moving
375 // the component to the architecture position etc.
376 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
378 continue; // Already in the canonical position.
380 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
381 // Do not reparse any components that already matched.
382 if (Idx < array_lengthof(Found) && Found[Idx])
385 // Does this component parse as valid for the target position?
387 StringRef Comp = Components[Idx];
389 default: llvm_unreachable("unexpected component type!");
391 Arch = parseArch(Comp);
392 Valid = Arch != UnknownArch;
395 Vendor = parseVendor(Comp);
396 Valid = Vendor != UnknownVendor;
400 Valid = OS != UnknownOS;
403 Environment = parseEnvironment(Comp);
404 Valid = Environment != UnknownEnvironment;
408 continue; // Nope, try the next component.
410 // Move the component to the target position, pushing any non-fixed
411 // components that are in the way to the right. This tends to give
412 // good results in the common cases of a forgotten vendor component
413 // or a wrongly positioned environment.
415 // Insert left, pushing the existing components to the right. For
416 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
417 StringRef CurrentComponent(""); // The empty component.
418 // Replace the component we are moving with an empty component.
419 std::swap(CurrentComponent, Components[Idx]);
420 // Insert the component being moved at Pos, displacing any existing
421 // components to the right.
422 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
423 // Skip over any fixed components.
424 while (i < array_lengthof(Found) && Found[i])
426 // Place the component at the new position, getting the component
427 // that was at this position - it will be moved right.
428 std::swap(CurrentComponent, Components[i]);
430 } else if (Pos > Idx) {
431 // Push right by inserting empty components until the component at Idx
432 // reaches the target position Pos. For example, pc-a -> -pc-a when
433 // moving pc to the second position.
435 // Insert one empty component at Idx.
436 StringRef CurrentComponent(""); // The empty component.
437 for (unsigned i = Idx; i < Components.size();) {
438 // Place the component at the new position, getting the component
439 // that was at this position - it will be moved right.
440 std::swap(CurrentComponent, Components[i]);
441 // If it was placed on top of an empty component then we are done.
442 if (CurrentComponent.empty())
444 // Advance to the next component, skipping any fixed components.
445 while (++i < array_lengthof(Found) && Found[i])
448 // The last component was pushed off the end - append it.
449 if (!CurrentComponent.empty())
450 Components.push_back(CurrentComponent);
452 // Advance Idx to the component's new position.
453 while (++Idx < array_lengthof(Found) && Found[Idx])
455 } while (Idx < Pos); // Add more until the final position is reached.
457 assert(Pos < Components.size() && Components[Pos] == Comp &&
458 "Component moved wrong!");
464 // Special case logic goes here. At this point Arch, Vendor and OS have the
465 // correct values for the computed components.
467 // Stick the corrected components back together to form the normalized string.
468 std::string Normalized;
469 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
470 if (i) Normalized += '-';
471 Normalized += Components[i];
476 StringRef Triple::getArchName() const {
477 return StringRef(Data).split('-').first; // Isolate first component
480 StringRef Triple::getVendorName() const {
481 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
482 return Tmp.split('-').first; // Isolate second component
485 StringRef Triple::getOSName() const {
486 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
487 Tmp = Tmp.split('-').second; // Strip second component
488 return Tmp.split('-').first; // Isolate third component
491 StringRef Triple::getEnvironmentName() const {
492 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
493 Tmp = Tmp.split('-').second; // Strip second component
494 return Tmp.split('-').second; // Strip third component
497 StringRef Triple::getOSAndEnvironmentName() const {
498 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
499 return Tmp.split('-').second; // Strip second component
502 static unsigned EatNumber(StringRef &Str) {
503 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
507 // Consume the leading digit.
508 Result = Result*10 + (Str[0] - '0');
512 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
517 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
518 unsigned &Micro) const {
519 StringRef OSName = getOSName();
521 // Assume that the OS portion of the triple starts with the canonical name.
522 StringRef OSTypeName = getOSTypeName(getOS());
523 if (OSName.startswith(OSTypeName))
524 OSName = OSName.substr(OSTypeName.size());
526 // Any unset version defaults to 0.
527 Major = Minor = Micro = 0;
529 // Parse up to three components.
530 unsigned *Components[3] = { &Major, &Minor, &Micro };
531 for (unsigned i = 0; i != 3; ++i) {
532 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
535 // Consume the leading number.
536 *Components[i] = EatNumber(OSName);
538 // Consume the separator, if present.
539 if (OSName.startswith("."))
540 OSName = OSName.substr(1);
544 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
545 unsigned &Micro) const {
546 getOSVersion(Major, Minor, Micro);
549 default: llvm_unreachable("unexpected OS for Darwin triple");
551 // Default to darwin8, i.e., MacOSX 10.4.
554 // Darwin version numbers are skewed from OS X versions.
571 // Ignore the version from the triple. This is only handled because the
572 // the clang driver combines OS X and IOS support into a common Darwin
573 // toolchain that wants to know the OS X version number even when targeting
583 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
584 unsigned &Micro) const {
586 default: llvm_unreachable("unexpected OS for Darwin triple");
589 // Ignore the version from the triple. This is only handled because the
590 // the clang driver combines OS X and IOS support into a common Darwin
591 // toolchain that wants to know the iOS version number even when targeting
598 getOSVersion(Major, Minor, Micro);
606 void Triple::setTriple(const Twine &Str) {
610 void Triple::setArch(ArchType Kind) {
611 setArchName(getArchTypeName(Kind));
614 void Triple::setVendor(VendorType Kind) {
615 setVendorName(getVendorTypeName(Kind));
618 void Triple::setOS(OSType Kind) {
619 setOSName(getOSTypeName(Kind));
622 void Triple::setEnvironment(EnvironmentType Kind) {
623 setEnvironmentName(getEnvironmentTypeName(Kind));
626 void Triple::setArchName(StringRef Str) {
627 // Work around a miscompilation bug for Twines in gcc 4.0.3.
628 SmallString<64> Triple;
631 Triple += getVendorName();
633 Triple += getOSAndEnvironmentName();
634 setTriple(Triple.str());
637 void Triple::setVendorName(StringRef Str) {
638 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
641 void Triple::setOSName(StringRef Str) {
642 if (hasEnvironment())
643 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
644 "-" + getEnvironmentName());
646 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
649 void Triple::setEnvironmentName(StringRef Str) {
650 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
654 void Triple::setOSAndEnvironmentName(StringRef Str) {
655 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
658 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
660 case llvm::Triple::UnknownArch:
663 case llvm::Triple::msp430:
666 case llvm::Triple::amdil:
667 case llvm::Triple::arm:
668 case llvm::Triple::hexagon:
669 case llvm::Triple::le32:
670 case llvm::Triple::mblaze:
671 case llvm::Triple::mips:
672 case llvm::Triple::mipsel:
673 case llvm::Triple::nvptx:
674 case llvm::Triple::ppc:
675 case llvm::Triple::r600:
676 case llvm::Triple::sparc:
677 case llvm::Triple::tce:
678 case llvm::Triple::thumb:
679 case llvm::Triple::x86:
680 case llvm::Triple::xcore:
681 case llvm::Triple::spir:
684 case llvm::Triple::aarch64:
685 case llvm::Triple::mips64:
686 case llvm::Triple::mips64el:
687 case llvm::Triple::nvptx64:
688 case llvm::Triple::ppc64:
689 case llvm::Triple::sparcv9:
690 case llvm::Triple::x86_64:
691 case llvm::Triple::spir64:
694 llvm_unreachable("Invalid architecture value");
697 bool Triple::isArch64Bit() const {
698 return getArchPointerBitWidth(getArch()) == 64;
701 bool Triple::isArch32Bit() const {
702 return getArchPointerBitWidth(getArch()) == 32;
705 bool Triple::isArch16Bit() const {
706 return getArchPointerBitWidth(getArch()) == 16;
709 Triple Triple::get32BitArchVariant() const {
712 case Triple::UnknownArch:
713 case Triple::aarch64:
715 T.setArch(UnknownArch);
721 case Triple::hexagon:
737 case Triple::mips64: T.setArch(Triple::mips); break;
738 case Triple::mips64el: T.setArch(Triple::mipsel); break;
739 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
740 case Triple::ppc64: T.setArch(Triple::ppc); break;
741 case Triple::sparcv9: T.setArch(Triple::sparc); break;
742 case Triple::x86_64: T.setArch(Triple::x86); break;
743 case Triple::spir64: T.setArch(Triple::spir); break;
748 Triple Triple::get64BitArchVariant() const {
751 case Triple::UnknownArch:
754 case Triple::hexagon:
762 T.setArch(UnknownArch);
765 case Triple::aarch64:
768 case Triple::mips64el:
769 case Triple::nvptx64:
771 case Triple::sparcv9:
776 case Triple::mips: T.setArch(Triple::mips64); break;
777 case Triple::mipsel: T.setArch(Triple::mips64el); break;
778 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
779 case Triple::ppc: T.setArch(Triple::ppc64); break;
780 case Triple::sparc: T.setArch(Triple::sparcv9); break;
781 case Triple::x86: T.setArch(Triple::x86_64); break;
782 case Triple::spir: T.setArch(Triple::spir64); break;