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 arm: return "arm";
23 case hexagon: return "hexagon";
24 case mips: return "mips";
25 case mipsel: return "mipsel";
26 case mips64: return "mips64";
27 case mips64el:return "mips64el";
28 case msp430: return "msp430";
29 case ppc64: return "powerpc64";
30 case ppc: return "powerpc";
31 case r600: return "r600";
32 case sparc: return "sparc";
33 case sparcv9: return "sparcv9";
34 case tce: return "tce";
35 case thumb: return "thumb";
36 case x86: return "i386";
37 case x86_64: return "x86_64";
38 case xcore: return "xcore";
39 case mblaze: return "mblaze";
40 case nvptx: return "nvptx";
41 case nvptx64: return "nvptx64";
42 case le32: return "le32";
43 case amdil: return "amdil";
44 case spir: return "spir";
45 case spir64: return "spir64";
48 llvm_unreachable("Invalid ArchType!");
51 const char *Triple::getArchTypePrefix(ArchType Kind) {
57 case thumb: return "arm";
60 case ppc: return "ppc";
62 case mblaze: return "mblaze";
67 case mips64el:return "mips";
69 case hexagon: return "hexagon";
71 case r600: return "r600";
74 case sparc: return "sparc";
77 case x86_64: return "x86";
79 case xcore: return "xcore";
81 case nvptx: return "nvptx";
82 case nvptx64: return "nvptx";
83 case le32: return "le32";
84 case amdil: return "amdil";
85 case spir: return "spir";
86 case spir64: return "spir";
90 const char *Triple::getVendorTypeName(VendorType Kind) {
92 case UnknownVendor: return "unknown";
94 case Apple: return "apple";
96 case SCEI: return "scei";
97 case BGP: return "bgp";
98 case BGQ: return "bgq";
99 case Freescale: return "fsl";
100 case IBM: return "ibm";
103 llvm_unreachable("Invalid VendorType!");
106 const char *Triple::getOSTypeName(OSType Kind) {
108 case UnknownOS: return "unknown";
110 case AuroraUX: return "auroraux";
111 case Cygwin: return "cygwin";
112 case Darwin: return "darwin";
113 case DragonFly: return "dragonfly";
114 case FreeBSD: return "freebsd";
115 case IOS: return "ios";
116 case KFreeBSD: return "kfreebsd";
117 case Linux: return "linux";
118 case Lv2: return "lv2";
119 case MacOSX: return "macosx";
120 case MinGW32: return "mingw32";
121 case NetBSD: return "netbsd";
122 case OpenBSD: return "openbsd";
123 case Solaris: return "solaris";
124 case Win32: return "win32";
125 case Haiku: return "haiku";
126 case Minix: return "minix";
127 case RTEMS: return "rtems";
128 case NaCl: return "nacl";
129 case CNK: return "cnk";
130 case Bitrig: return "bitrig";
131 case AIX: return "aix";
134 llvm_unreachable("Invalid OSType");
137 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
139 case UnknownEnvironment: return "unknown";
140 case GNU: return "gnu";
141 case GNUEABIHF: return "gnueabihf";
142 case GNUEABI: return "gnueabi";
143 case GNUX32: return "gnux32";
144 case EABI: return "eabi";
145 case MachO: return "macho";
146 case Android: return "android";
147 case ELF: return "elf";
150 llvm_unreachable("Invalid EnvironmentType!");
153 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
154 return StringSwitch<Triple::ArchType>(Name)
157 .Case("mipsel", mipsel)
158 .Case("mips64", mips64)
159 .Case("mips64el", mips64el)
160 .Case("msp430", msp430)
161 .Case("ppc64", ppc64)
164 .Case("mblaze", mblaze)
166 .Case("hexagon", hexagon)
167 .Case("sparc", sparc)
168 .Case("sparcv9", sparcv9)
170 .Case("thumb", thumb)
172 .Case("x86-64", x86_64)
173 .Case("xcore", xcore)
174 .Case("nvptx", nvptx)
175 .Case("nvptx64", nvptx64)
177 .Case("amdil", amdil)
179 .Case("spir64", spir64)
180 .Default(UnknownArch);
183 // Returns architecture name that is understood by the target assembler.
184 const char *Triple::getArchNameForAssembler() {
185 if (!isOSDarwin() && getVendor() != Triple::Apple)
188 return StringSwitch<const char*>(getArchName())
189 .Case("i386", "i386")
190 .Case("x86_64", "x86_64")
191 .Case("powerpc", "ppc")
192 .Case("powerpc64", "ppc64")
193 .Cases("mblaze", "microblaze", "mblaze")
195 .Cases("armv4t", "thumbv4t", "armv4t")
196 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
197 .Cases("armv6", "thumbv6", "armv6")
198 .Cases("armv7", "thumbv7", "armv7")
199 .Case("r600", "r600")
200 .Case("nvptx", "nvptx")
201 .Case("nvptx64", "nvptx64")
202 .Case("le32", "le32")
203 .Case("amdil", "amdil")
204 .Case("spir", "spir")
205 .Case("spir64", "spir64")
209 static Triple::ArchType parseArch(StringRef ArchName) {
210 return StringSwitch<Triple::ArchType>(ArchName)
211 .Cases("i386", "i486", "i586", "i686", Triple::x86)
212 // FIXME: Do we need to support these?
213 .Cases("i786", "i886", "i986", Triple::x86)
214 .Cases("amd64", "x86_64", Triple::x86_64)
215 .Case("powerpc", Triple::ppc)
216 .Cases("powerpc64", "ppu", Triple::ppc64)
217 .Case("mblaze", Triple::mblaze)
218 .Cases("arm", "xscale", Triple::arm)
219 // FIXME: It would be good to replace these with explicit names for all the
220 // various suffixes supported.
221 .StartsWith("armv", Triple::arm)
222 .Case("thumb", Triple::thumb)
223 .StartsWith("thumbv", Triple::thumb)
224 .Case("msp430", Triple::msp430)
225 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
226 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
227 .Cases("mips64", "mips64eb", Triple::mips64)
228 .Case("mips64el", Triple::mips64el)
229 .Case("r600", Triple::r600)
230 .Case("hexagon", Triple::hexagon)
231 .Case("sparc", Triple::sparc)
232 .Case("sparcv9", Triple::sparcv9)
233 .Case("tce", Triple::tce)
234 .Case("xcore", Triple::xcore)
235 .Case("nvptx", Triple::nvptx)
236 .Case("nvptx64", Triple::nvptx64)
237 .Case("le32", Triple::le32)
238 .Case("amdil", Triple::amdil)
239 .Case("spir", Triple::spir)
240 .Case("spir64", Triple::spir64)
241 .Default(Triple::UnknownArch);
244 static Triple::VendorType parseVendor(StringRef VendorName) {
245 return StringSwitch<Triple::VendorType>(VendorName)
246 .Case("apple", Triple::Apple)
247 .Case("pc", Triple::PC)
248 .Case("scei", Triple::SCEI)
249 .Case("bgp", Triple::BGP)
250 .Case("bgq", Triple::BGQ)
251 .Case("fsl", Triple::Freescale)
252 .Case("ibm", Triple::IBM)
253 .Default(Triple::UnknownVendor);
256 static Triple::OSType parseOS(StringRef OSName) {
257 return StringSwitch<Triple::OSType>(OSName)
258 .StartsWith("auroraux", Triple::AuroraUX)
259 .StartsWith("cygwin", Triple::Cygwin)
260 .StartsWith("darwin", Triple::Darwin)
261 .StartsWith("dragonfly", Triple::DragonFly)
262 .StartsWith("freebsd", Triple::FreeBSD)
263 .StartsWith("ios", Triple::IOS)
264 .StartsWith("kfreebsd", Triple::KFreeBSD)
265 .StartsWith("linux", Triple::Linux)
266 .StartsWith("lv2", Triple::Lv2)
267 .StartsWith("macosx", Triple::MacOSX)
268 .StartsWith("mingw32", Triple::MinGW32)
269 .StartsWith("netbsd", Triple::NetBSD)
270 .StartsWith("openbsd", Triple::OpenBSD)
271 .StartsWith("solaris", Triple::Solaris)
272 .StartsWith("win32", Triple::Win32)
273 .StartsWith("haiku", Triple::Haiku)
274 .StartsWith("minix", Triple::Minix)
275 .StartsWith("rtems", Triple::RTEMS)
276 .StartsWith("nacl", Triple::NaCl)
277 .StartsWith("cnk", Triple::CNK)
278 .StartsWith("bitrig", Triple::Bitrig)
279 .StartsWith("aix", Triple::AIX)
280 .Default(Triple::UnknownOS);
283 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
284 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
285 .StartsWith("eabi", Triple::EABI)
286 .StartsWith("gnueabihf", Triple::GNUEABIHF)
287 .StartsWith("gnueabi", Triple::GNUEABI)
288 .StartsWith("gnux32", Triple::GNUX32)
289 .StartsWith("gnu", Triple::GNU)
290 .StartsWith("macho", Triple::MachO)
291 .StartsWith("android", Triple::Android)
292 .StartsWith("elf", Triple::ELF)
293 .Default(Triple::UnknownEnvironment);
296 /// \brief Construct a triple from the string representation provided.
298 /// This stores the string representation and parses the various pieces into
300 Triple::Triple(const Twine &Str)
302 Arch(parseArch(getArchName())),
303 Vendor(parseVendor(getVendorName())),
304 OS(parseOS(getOSName())),
305 Environment(parseEnvironment(getEnvironmentName())) {
308 /// \brief Construct a triple from string representations of the architecture,
311 /// This joins each argument into a canonical string representation and parses
312 /// them into enum members. It leaves the environment unknown and omits it from
313 /// the string representation.
314 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
315 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
316 Arch(parseArch(ArchStr.str())),
317 Vendor(parseVendor(VendorStr.str())),
318 OS(parseOS(OSStr.str())),
322 /// \brief Construct a triple from string representations of the architecture,
323 /// vendor, OS, and environment.
325 /// This joins each argument into a canonical string representation and parses
326 /// them into enum members.
327 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
328 const Twine &EnvironmentStr)
329 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
330 EnvironmentStr).str()),
331 Arch(parseArch(ArchStr.str())),
332 Vendor(parseVendor(VendorStr.str())),
333 OS(parseOS(OSStr.str())),
334 Environment(parseEnvironment(EnvironmentStr.str())) {
337 std::string Triple::normalize(StringRef Str) {
338 // Parse into components.
339 SmallVector<StringRef, 4> Components;
340 Str.split(Components, "-");
342 // If the first component corresponds to a known architecture, preferentially
343 // use it for the architecture. If the second component corresponds to a
344 // known vendor, preferentially use it for the vendor, etc. This avoids silly
345 // component movement when a component parses as (eg) both a valid arch and a
347 ArchType Arch = UnknownArch;
348 if (Components.size() > 0)
349 Arch = parseArch(Components[0]);
350 VendorType Vendor = UnknownVendor;
351 if (Components.size() > 1)
352 Vendor = parseVendor(Components[1]);
353 OSType OS = UnknownOS;
354 if (Components.size() > 2)
355 OS = parseOS(Components[2]);
356 EnvironmentType Environment = UnknownEnvironment;
357 if (Components.size() > 3)
358 Environment = parseEnvironment(Components[3]);
360 // Note which components are already in their final position. These will not
363 Found[0] = Arch != UnknownArch;
364 Found[1] = Vendor != UnknownVendor;
365 Found[2] = OS != UnknownOS;
366 Found[3] = Environment != UnknownEnvironment;
368 // If they are not there already, permute the components into their canonical
369 // positions by seeing if they parse as a valid architecture, and if so moving
370 // the component to the architecture position etc.
371 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
373 continue; // Already in the canonical position.
375 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
376 // Do not reparse any components that already matched.
377 if (Idx < array_lengthof(Found) && Found[Idx])
380 // Does this component parse as valid for the target position?
382 StringRef Comp = Components[Idx];
384 default: llvm_unreachable("unexpected component type!");
386 Arch = parseArch(Comp);
387 Valid = Arch != UnknownArch;
390 Vendor = parseVendor(Comp);
391 Valid = Vendor != UnknownVendor;
395 Valid = OS != UnknownOS;
398 Environment = parseEnvironment(Comp);
399 Valid = Environment != UnknownEnvironment;
403 continue; // Nope, try the next component.
405 // Move the component to the target position, pushing any non-fixed
406 // components that are in the way to the right. This tends to give
407 // good results in the common cases of a forgotten vendor component
408 // or a wrongly positioned environment.
410 // Insert left, pushing the existing components to the right. For
411 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
412 StringRef CurrentComponent(""); // The empty component.
413 // Replace the component we are moving with an empty component.
414 std::swap(CurrentComponent, Components[Idx]);
415 // Insert the component being moved at Pos, displacing any existing
416 // components to the right.
417 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
418 // Skip over any fixed components.
419 while (i < array_lengthof(Found) && Found[i])
421 // Place the component at the new position, getting the component
422 // that was at this position - it will be moved right.
423 std::swap(CurrentComponent, Components[i]);
425 } else if (Pos > Idx) {
426 // Push right by inserting empty components until the component at Idx
427 // reaches the target position Pos. For example, pc-a -> -pc-a when
428 // moving pc to the second position.
430 // Insert one empty component at Idx.
431 StringRef CurrentComponent(""); // The empty component.
432 for (unsigned i = Idx; i < Components.size();) {
433 // Place the component at the new position, getting the component
434 // that was at this position - it will be moved right.
435 std::swap(CurrentComponent, Components[i]);
436 // If it was placed on top of an empty component then we are done.
437 if (CurrentComponent.empty())
439 // Advance to the next component, skipping any fixed components.
440 while (++i < array_lengthof(Found) && Found[i])
443 // The last component was pushed off the end - append it.
444 if (!CurrentComponent.empty())
445 Components.push_back(CurrentComponent);
447 // Advance Idx to the component's new position.
448 while (++Idx < array_lengthof(Found) && Found[Idx])
450 } while (Idx < Pos); // Add more until the final position is reached.
452 assert(Pos < Components.size() && Components[Pos] == Comp &&
453 "Component moved wrong!");
459 // Special case logic goes here. At this point Arch, Vendor and OS have the
460 // correct values for the computed components.
462 // Stick the corrected components back together to form the normalized string.
463 std::string Normalized;
464 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
465 if (i) Normalized += '-';
466 Normalized += Components[i];
471 StringRef Triple::getArchName() const {
472 return StringRef(Data).split('-').first; // Isolate first component
475 StringRef Triple::getVendorName() const {
476 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
477 return Tmp.split('-').first; // Isolate second component
480 StringRef Triple::getOSName() const {
481 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
482 Tmp = Tmp.split('-').second; // Strip second component
483 return Tmp.split('-').first; // Isolate third component
486 StringRef Triple::getEnvironmentName() const {
487 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
488 Tmp = Tmp.split('-').second; // Strip second component
489 return Tmp.split('-').second; // Strip third component
492 StringRef Triple::getOSAndEnvironmentName() const {
493 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
494 return Tmp.split('-').second; // Strip second component
497 static unsigned EatNumber(StringRef &Str) {
498 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
502 // Consume the leading digit.
503 Result = Result*10 + (Str[0] - '0');
507 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
512 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
513 unsigned &Micro) const {
514 StringRef OSName = getOSName();
516 // Assume that the OS portion of the triple starts with the canonical name.
517 StringRef OSTypeName = getOSTypeName(getOS());
518 if (OSName.startswith(OSTypeName))
519 OSName = OSName.substr(OSTypeName.size());
521 // Any unset version defaults to 0.
522 Major = Minor = Micro = 0;
524 // Parse up to three components.
525 unsigned *Components[3] = { &Major, &Minor, &Micro };
526 for (unsigned i = 0; i != 3; ++i) {
527 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
530 // Consume the leading number.
531 *Components[i] = EatNumber(OSName);
533 // Consume the separator, if present.
534 if (OSName.startswith("."))
535 OSName = OSName.substr(1);
539 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
540 unsigned &Micro) const {
541 getOSVersion(Major, Minor, Micro);
544 default: llvm_unreachable("unexpected OS for Darwin triple");
546 // Default to darwin8, i.e., MacOSX 10.4.
549 // Darwin version numbers are skewed from OS X versions.
566 // Ignore the version from the triple. This is only handled because the
567 // the clang driver combines OS X and IOS support into a common Darwin
568 // toolchain that wants to know the OS X version number even when targeting
578 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
579 unsigned &Micro) const {
581 default: llvm_unreachable("unexpected OS for Darwin triple");
584 // Ignore the version from the triple. This is only handled because the
585 // the clang driver combines OS X and IOS support into a common Darwin
586 // toolchain that wants to know the iOS version number even when targeting
593 getOSVersion(Major, Minor, Micro);
601 void Triple::setTriple(const Twine &Str) {
605 void Triple::setArch(ArchType Kind) {
606 setArchName(getArchTypeName(Kind));
609 void Triple::setVendor(VendorType Kind) {
610 setVendorName(getVendorTypeName(Kind));
613 void Triple::setOS(OSType Kind) {
614 setOSName(getOSTypeName(Kind));
617 void Triple::setEnvironment(EnvironmentType Kind) {
618 setEnvironmentName(getEnvironmentTypeName(Kind));
621 void Triple::setArchName(StringRef Str) {
622 // Work around a miscompilation bug for Twines in gcc 4.0.3.
623 SmallString<64> Triple;
626 Triple += getVendorName();
628 Triple += getOSAndEnvironmentName();
629 setTriple(Triple.str());
632 void Triple::setVendorName(StringRef Str) {
633 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
636 void Triple::setOSName(StringRef Str) {
637 if (hasEnvironment())
638 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
639 "-" + getEnvironmentName());
641 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
644 void Triple::setEnvironmentName(StringRef Str) {
645 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
649 void Triple::setOSAndEnvironmentName(StringRef Str) {
650 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
653 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
655 case llvm::Triple::UnknownArch:
658 case llvm::Triple::msp430:
661 case llvm::Triple::amdil:
662 case llvm::Triple::arm:
663 case llvm::Triple::hexagon:
664 case llvm::Triple::le32:
665 case llvm::Triple::mblaze:
666 case llvm::Triple::mips:
667 case llvm::Triple::mipsel:
668 case llvm::Triple::nvptx:
669 case llvm::Triple::ppc:
670 case llvm::Triple::r600:
671 case llvm::Triple::sparc:
672 case llvm::Triple::tce:
673 case llvm::Triple::thumb:
674 case llvm::Triple::x86:
675 case llvm::Triple::xcore:
676 case llvm::Triple::spir:
679 case llvm::Triple::mips64:
680 case llvm::Triple::mips64el:
681 case llvm::Triple::nvptx64:
682 case llvm::Triple::ppc64:
683 case llvm::Triple::sparcv9:
684 case llvm::Triple::x86_64:
685 case llvm::Triple::spir64:
688 llvm_unreachable("Invalid architecture value");
691 bool Triple::isArch64Bit() const {
692 return getArchPointerBitWidth(getArch()) == 64;
695 bool Triple::isArch32Bit() const {
696 return getArchPointerBitWidth(getArch()) == 32;
699 bool Triple::isArch16Bit() const {
700 return getArchPointerBitWidth(getArch()) == 16;
703 Triple Triple::get32BitArchVariant() const {
706 case Triple::UnknownArch:
708 T.setArch(UnknownArch);
714 case Triple::hexagon:
730 case Triple::mips64: T.setArch(Triple::mips); break;
731 case Triple::mips64el: T.setArch(Triple::mipsel); break;
732 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
733 case Triple::ppc64: T.setArch(Triple::ppc); break;
734 case Triple::sparcv9: T.setArch(Triple::sparc); break;
735 case Triple::x86_64: T.setArch(Triple::x86); break;
736 case Triple::spir64: T.setArch(Triple::spir); break;
741 Triple Triple::get64BitArchVariant() const {
744 case Triple::UnknownArch:
747 case Triple::hexagon:
755 T.setArch(UnknownArch);
760 case Triple::mips64el:
761 case Triple::nvptx64:
763 case Triple::sparcv9:
768 case Triple::mips: T.setArch(Triple::mips64); break;
769 case Triple::mipsel: T.setArch(Triple::mips64el); break;
770 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
771 case Triple::ppc: T.setArch(Triple::ppc64); break;
772 case Triple::sparc: T.setArch(Triple::sparcv9); break;
773 case Triple::x86: T.setArch(Triple::x86_64); break;
774 case Triple::spir: T.setArch(Triple::spir64); break;