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/SmallString.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/STLExtras.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 cellspu: return "cellspu";
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";
47 llvm_unreachable("Invalid ArchType!");
50 const char *Triple::getArchTypePrefix(ArchType Kind) {
56 case thumb: return "arm";
58 case cellspu: return "spu";
61 case ppc: return "ppc";
63 case mblaze: return "mblaze";
68 case mips64el:return "mips";
70 case hexagon: return "hexagon";
72 case r600: return "r600";
75 case sparc: return "sparc";
78 case x86_64: return "x86";
80 case xcore: return "xcore";
82 case nvptx: return "nvptx";
83 case nvptx64: return "nvptx";
84 case le32: return "le32";
85 case amdil: return "amdil";
89 const char *Triple::getVendorTypeName(VendorType Kind) {
91 case UnknownVendor: return "unknown";
93 case Apple: return "apple";
95 case SCEI: return "scei";
96 case BGP: return "bgp";
97 case BGQ: return "bgq";
98 case Freescale: return "fsl";
101 llvm_unreachable("Invalid VendorType!");
104 const char *Triple::getOSTypeName(OSType Kind) {
106 case UnknownOS: return "unknown";
108 case AuroraUX: return "auroraux";
109 case Cygwin: return "cygwin";
110 case Darwin: return "darwin";
111 case DragonFly: return "dragonfly";
112 case FreeBSD: return "freebsd";
113 case IOS: return "ios";
114 case KFreeBSD: return "kfreebsd";
115 case Linux: return "linux";
116 case Lv2: return "lv2";
117 case MacOSX: return "macosx";
118 case MinGW32: return "mingw32";
119 case NetBSD: return "netbsd";
120 case OpenBSD: return "openbsd";
121 case Solaris: return "solaris";
122 case Win32: return "win32";
123 case Haiku: return "haiku";
124 case Minix: return "minix";
125 case RTEMS: return "rtems";
126 case NativeClient: return "nacl";
127 case CNK: return "cnk";
128 case Bitrig: return "bitrig";
131 llvm_unreachable("Invalid OSType");
134 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
136 case UnknownEnvironment: return "unknown";
137 case GNU: return "gnu";
138 case GNUEABIHF: return "gnueabihf";
139 case GNUEABI: return "gnueabi";
140 case EABI: return "eabi";
141 case MachO: return "macho";
142 case Android: return "android";
145 llvm_unreachable("Invalid EnvironmentType!");
148 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
149 return StringSwitch<Triple::ArchType>(Name)
151 .Case("cellspu", cellspu)
153 .Case("mipsel", mipsel)
154 .Case("mips64", mips64)
155 .Case("mips64el", mips64el)
156 .Case("msp430", msp430)
157 .Case("ppc64", ppc64)
160 .Case("mblaze", mblaze)
162 .Case("hexagon", hexagon)
163 .Case("sparc", sparc)
164 .Case("sparcv9", sparcv9)
166 .Case("thumb", thumb)
168 .Case("x86-64", x86_64)
169 .Case("xcore", xcore)
170 .Case("nvptx", nvptx)
171 .Case("nvptx64", nvptx64)
173 .Case("amdil", amdil)
174 .Default(UnknownArch);
177 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
178 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
179 // archs which Darwin doesn't use.
181 // The matching this routine does is fairly pointless, since it is neither the
182 // complete architecture list, nor a reasonable subset. The problem is that
183 // historically the driver driver accepts this and also ties its -march=
184 // handling to the architecture name, so we need to be careful before removing
187 // This code must be kept in sync with Clang's Darwin specific argument
190 return StringSwitch<ArchType>(Str)
191 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
192 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
193 .Case("ppc64", Triple::ppc64)
194 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
195 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
197 .Case("x86_64", Triple::x86_64)
198 // This is derived from the driver driver.
199 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
200 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
201 .Case("r600", Triple::r600)
202 .Case("nvptx", Triple::nvptx)
203 .Case("nvptx64", Triple::nvptx64)
204 .Case("amdil", Triple::amdil)
205 .Default(Triple::UnknownArch);
208 // Returns architecture name that is understood by the target assembler.
209 const char *Triple::getArchNameForAssembler() {
210 if (!isOSDarwin() && getVendor() != Triple::Apple)
213 return StringSwitch<const char*>(getArchName())
214 .Case("i386", "i386")
215 .Case("x86_64", "x86_64")
216 .Case("powerpc", "ppc")
217 .Case("powerpc64", "ppc64")
218 .Cases("mblaze", "microblaze", "mblaze")
220 .Cases("armv4t", "thumbv4t", "armv4t")
221 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
222 .Cases("armv6", "thumbv6", "armv6")
223 .Cases("armv7", "thumbv7", "armv7")
224 .Case("r600", "r600")
225 .Case("nvptx", "nvptx")
226 .Case("nvptx64", "nvptx64")
227 .Case("le32", "le32")
228 .Case("amdil", "amdil")
232 static Triple::ArchType parseArch(StringRef ArchName) {
233 return StringSwitch<Triple::ArchType>(ArchName)
234 .Cases("i386", "i486", "i586", "i686", Triple::x86)
235 // FIXME: Do we need to support these?
236 .Cases("i786", "i886", "i986", Triple::x86)
237 .Cases("amd64", "x86_64", Triple::x86_64)
238 .Case("powerpc", Triple::ppc)
239 .Cases("powerpc64", "ppu", Triple::ppc64)
240 .Case("mblaze", Triple::mblaze)
241 .Cases("arm", "xscale", Triple::arm)
242 // FIXME: It would be good to replace these with explicit names for all the
243 // various suffixes supported.
244 .StartsWith("armv", Triple::arm)
245 .Case("thumb", Triple::thumb)
246 .StartsWith("thumbv", Triple::thumb)
247 .Cases("spu", "cellspu", Triple::cellspu)
248 .Case("msp430", Triple::msp430)
249 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
250 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
251 .Cases("mips64", "mips64eb", Triple::mips64)
252 .Case("mips64el", Triple::mips64el)
253 .Case("r600", Triple::r600)
254 .Case("hexagon", Triple::hexagon)
255 .Case("sparc", Triple::sparc)
256 .Case("sparcv9", Triple::sparcv9)
257 .Case("tce", Triple::tce)
258 .Case("xcore", Triple::xcore)
259 .Case("nvptx", Triple::nvptx)
260 .Case("nvptx64", Triple::nvptx64)
261 .Case("le32", Triple::le32)
262 .Case("amdil", Triple::amdil)
263 .Default(Triple::UnknownArch);
266 static Triple::VendorType parseVendor(StringRef VendorName) {
267 return StringSwitch<Triple::VendorType>(VendorName)
268 .Case("apple", Triple::Apple)
269 .Case("pc", Triple::PC)
270 .Case("scei", Triple::SCEI)
271 .Case("bgp", Triple::BGP)
272 .Case("bgq", Triple::BGQ)
273 .Case("fsl", Triple::Freescale)
274 .Default(Triple::UnknownVendor);
277 static Triple::OSType parseOS(StringRef OSName) {
278 return StringSwitch<Triple::OSType>(OSName)
279 .StartsWith("auroraux", Triple::AuroraUX)
280 .StartsWith("cygwin", Triple::Cygwin)
281 .StartsWith("darwin", Triple::Darwin)
282 .StartsWith("dragonfly", Triple::DragonFly)
283 .StartsWith("freebsd", Triple::FreeBSD)
284 .StartsWith("ios", Triple::IOS)
285 .StartsWith("kfreebsd", Triple::KFreeBSD)
286 .StartsWith("linux", Triple::Linux)
287 .StartsWith("lv2", Triple::Lv2)
288 .StartsWith("macosx", Triple::MacOSX)
289 .StartsWith("mingw32", Triple::MinGW32)
290 .StartsWith("netbsd", Triple::NetBSD)
291 .StartsWith("openbsd", Triple::OpenBSD)
292 .StartsWith("solaris", Triple::Solaris)
293 .StartsWith("win32", Triple::Win32)
294 .StartsWith("haiku", Triple::Haiku)
295 .StartsWith("minix", Triple::Minix)
296 .StartsWith("rtems", Triple::RTEMS)
297 .StartsWith("nacl", Triple::NativeClient)
298 .StartsWith("cnk", Triple::CNK)
299 .StartsWith("bitrig", Triple::Bitrig)
300 .Default(Triple::UnknownOS);
303 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
304 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
305 .StartsWith("eabi", Triple::EABI)
306 .StartsWith("gnueabihf", Triple::GNUEABIHF)
307 .StartsWith("gnueabi", Triple::GNUEABI)
308 .StartsWith("gnu", Triple::GNU)
309 .StartsWith("macho", Triple::MachO)
310 .StartsWith("android", Triple::Android)
311 .Default(Triple::UnknownEnvironment);
314 /// \brief Construct a triple from the string representation provided.
316 /// This stores the string representation and parses the various pieces into
318 Triple::Triple(const Twine &Str)
320 Arch(parseArch(getArchName())),
321 Vendor(parseVendor(getVendorName())),
322 OS(parseOS(getOSName())),
323 Environment(parseEnvironment(getEnvironmentName())) {
326 /// \brief Construct a triple from string representations of the architecture,
329 /// This joins each argument into a canonical string representation and parses
330 /// them into enum members. It leaves the environment unknown and omits it from
331 /// the string representation.
332 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
333 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
334 Arch(parseArch(ArchStr.str())),
335 Vendor(parseVendor(VendorStr.str())),
336 OS(parseOS(OSStr.str())),
340 /// \brief Construct a triple from string representations of the architecture,
341 /// vendor, OS, and environment.
343 /// This joins each argument into a canonical string representation and parses
344 /// them into enum members.
345 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
346 const Twine &EnvironmentStr)
347 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
348 EnvironmentStr).str()),
349 Arch(parseArch(ArchStr.str())),
350 Vendor(parseVendor(VendorStr.str())),
351 OS(parseOS(OSStr.str())),
352 Environment(parseEnvironment(EnvironmentStr.str())) {
355 std::string Triple::normalize(StringRef Str) {
356 // Parse into components.
357 SmallVector<StringRef, 4> Components;
358 Str.split(Components, "-");
360 // If the first component corresponds to a known architecture, preferentially
361 // use it for the architecture. If the second component corresponds to a
362 // known vendor, preferentially use it for the vendor, etc. This avoids silly
363 // component movement when a component parses as (eg) both a valid arch and a
365 ArchType Arch = UnknownArch;
366 if (Components.size() > 0)
367 Arch = parseArch(Components[0]);
368 VendorType Vendor = UnknownVendor;
369 if (Components.size() > 1)
370 Vendor = parseVendor(Components[1]);
371 OSType OS = UnknownOS;
372 if (Components.size() > 2)
373 OS = parseOS(Components[2]);
374 EnvironmentType Environment = UnknownEnvironment;
375 if (Components.size() > 3)
376 Environment = parseEnvironment(Components[3]);
378 // Note which components are already in their final position. These will not
381 Found[0] = Arch != UnknownArch;
382 Found[1] = Vendor != UnknownVendor;
383 Found[2] = OS != UnknownOS;
384 Found[3] = Environment != UnknownEnvironment;
386 // If they are not there already, permute the components into their canonical
387 // positions by seeing if they parse as a valid architecture, and if so moving
388 // the component to the architecture position etc.
389 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
391 continue; // Already in the canonical position.
393 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
394 // Do not reparse any components that already matched.
395 if (Idx < array_lengthof(Found) && Found[Idx])
398 // Does this component parse as valid for the target position?
400 StringRef Comp = Components[Idx];
402 default: llvm_unreachable("unexpected component type!");
404 Arch = parseArch(Comp);
405 Valid = Arch != UnknownArch;
408 Vendor = parseVendor(Comp);
409 Valid = Vendor != UnknownVendor;
413 Valid = OS != UnknownOS;
416 Environment = parseEnvironment(Comp);
417 Valid = Environment != UnknownEnvironment;
421 continue; // Nope, try the next component.
423 // Move the component to the target position, pushing any non-fixed
424 // components that are in the way to the right. This tends to give
425 // good results in the common cases of a forgotten vendor component
426 // or a wrongly positioned environment.
428 // Insert left, pushing the existing components to the right. For
429 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
430 StringRef CurrentComponent(""); // The empty component.
431 // Replace the component we are moving with an empty component.
432 std::swap(CurrentComponent, Components[Idx]);
433 // Insert the component being moved at Pos, displacing any existing
434 // components to the right.
435 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
436 // Skip over any fixed components.
437 while (i < array_lengthof(Found) && Found[i])
439 // Place the component at the new position, getting the component
440 // that was at this position - it will be moved right.
441 std::swap(CurrentComponent, Components[i]);
443 } else if (Pos > Idx) {
444 // Push right by inserting empty components until the component at Idx
445 // reaches the target position Pos. For example, pc-a -> -pc-a when
446 // moving pc to the second position.
448 // Insert one empty component at Idx.
449 StringRef CurrentComponent(""); // The empty component.
450 for (unsigned i = Idx; i < Components.size();) {
451 // Place the component at the new position, getting the component
452 // that was at this position - it will be moved right.
453 std::swap(CurrentComponent, Components[i]);
454 // If it was placed on top of an empty component then we are done.
455 if (CurrentComponent.empty())
457 // Advance to the next component, skipping any fixed components.
458 while (++i < array_lengthof(Found) && Found[i])
461 // The last component was pushed off the end - append it.
462 if (!CurrentComponent.empty())
463 Components.push_back(CurrentComponent);
465 // Advance Idx to the component's new position.
466 while (++Idx < array_lengthof(Found) && Found[Idx])
468 } while (Idx < Pos); // Add more until the final position is reached.
470 assert(Pos < Components.size() && Components[Pos] == Comp &&
471 "Component moved wrong!");
477 // Special case logic goes here. At this point Arch, Vendor and OS have the
478 // correct values for the computed components.
480 // Stick the corrected components back together to form the normalized string.
481 std::string Normalized;
482 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
483 if (i) Normalized += '-';
484 Normalized += Components[i];
489 StringRef Triple::getArchName() const {
490 return StringRef(Data).split('-').first; // Isolate first component
493 StringRef Triple::getVendorName() const {
494 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
495 return Tmp.split('-').first; // Isolate second component
498 StringRef Triple::getOSName() const {
499 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
500 Tmp = Tmp.split('-').second; // Strip second component
501 return Tmp.split('-').first; // Isolate third component
504 StringRef Triple::getEnvironmentName() const {
505 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
506 Tmp = Tmp.split('-').second; // Strip second component
507 return Tmp.split('-').second; // Strip third component
510 StringRef Triple::getOSAndEnvironmentName() const {
511 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
512 return Tmp.split('-').second; // Strip second component
515 static unsigned EatNumber(StringRef &Str) {
516 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
520 // Consume the leading digit.
521 Result = Result*10 + (Str[0] - '0');
525 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
530 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
531 unsigned &Micro) const {
532 StringRef OSName = getOSName();
534 // Assume that the OS portion of the triple starts with the canonical name.
535 StringRef OSTypeName = getOSTypeName(getOS());
536 if (OSName.startswith(OSTypeName))
537 OSName = OSName.substr(OSTypeName.size());
539 // Any unset version defaults to 0.
540 Major = Minor = Micro = 0;
542 // Parse up to three components.
543 unsigned *Components[3] = { &Major, &Minor, &Micro };
544 for (unsigned i = 0; i != 3; ++i) {
545 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
548 // Consume the leading number.
549 *Components[i] = EatNumber(OSName);
551 // Consume the separator, if present.
552 if (OSName.startswith("."))
553 OSName = OSName.substr(1);
557 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
558 unsigned &Micro) const {
559 getOSVersion(Major, Minor, Micro);
562 default: llvm_unreachable("unexpected OS for Darwin triple");
564 // Default to darwin8, i.e., MacOSX 10.4.
567 // Darwin version numbers are skewed from OS X versions.
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 OS X version number even when targeting
596 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
597 unsigned &Micro) const {
599 default: llvm_unreachable("unexpected OS for Darwin triple");
602 // Ignore the version from the triple. This is only handled because the
603 // the clang driver combines OS X and IOS support into a common Darwin
604 // toolchain that wants to know the iOS version number even when targeting
611 getOSVersion(Major, Minor, Micro);
619 void Triple::setTriple(const Twine &Str) {
623 void Triple::setArch(ArchType Kind) {
624 setArchName(getArchTypeName(Kind));
627 void Triple::setVendor(VendorType Kind) {
628 setVendorName(getVendorTypeName(Kind));
631 void Triple::setOS(OSType Kind) {
632 setOSName(getOSTypeName(Kind));
635 void Triple::setEnvironment(EnvironmentType Kind) {
636 setEnvironmentName(getEnvironmentTypeName(Kind));
639 void Triple::setArchName(StringRef Str) {
640 // Work around a miscompilation bug for Twines in gcc 4.0.3.
641 SmallString<64> Triple;
644 Triple += getVendorName();
646 Triple += getOSAndEnvironmentName();
647 setTriple(Triple.str());
650 void Triple::setVendorName(StringRef Str) {
651 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
654 void Triple::setOSName(StringRef Str) {
655 if (hasEnvironment())
656 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
657 "-" + getEnvironmentName());
659 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
662 void Triple::setEnvironmentName(StringRef Str) {
663 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
667 void Triple::setOSAndEnvironmentName(StringRef Str) {
668 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
671 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
673 case llvm::Triple::UnknownArch:
676 case llvm::Triple::msp430:
679 case llvm::Triple::amdil:
680 case llvm::Triple::arm:
681 case llvm::Triple::cellspu:
682 case llvm::Triple::hexagon:
683 case llvm::Triple::le32:
684 case llvm::Triple::mblaze:
685 case llvm::Triple::mips:
686 case llvm::Triple::mipsel:
687 case llvm::Triple::nvptx:
688 case llvm::Triple::ppc:
689 case llvm::Triple::r600:
690 case llvm::Triple::sparc:
691 case llvm::Triple::tce:
692 case llvm::Triple::thumb:
693 case llvm::Triple::x86:
694 case llvm::Triple::xcore:
697 case llvm::Triple::mips64:
698 case llvm::Triple::mips64el:
699 case llvm::Triple::nvptx64:
700 case llvm::Triple::ppc64:
701 case llvm::Triple::sparcv9:
702 case llvm::Triple::x86_64:
705 llvm_unreachable("Invalid architecture value");
708 bool Triple::isArch64Bit() const {
709 return getArchPointerBitWidth(getArch()) == 64;
712 bool Triple::isArch32Bit() const {
713 return getArchPointerBitWidth(getArch()) == 32;
716 bool Triple::isArch16Bit() const {
717 return getArchPointerBitWidth(getArch()) == 16;
720 Triple Triple::get32BitArchVariant() const {
723 case Triple::UnknownArch:
725 T.setArch(UnknownArch);
730 case Triple::cellspu:
731 case Triple::hexagon:
747 case Triple::mips64: T.setArch(Triple::mips); break;
748 case Triple::mips64el: T.setArch(Triple::mipsel); break;
749 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
750 case Triple::ppc64: T.setArch(Triple::ppc); break;
751 case Triple::sparcv9: T.setArch(Triple::sparc); break;
752 case Triple::x86_64: T.setArch(Triple::x86); break;
757 Triple Triple::get64BitArchVariant() const {
760 case Triple::UnknownArch:
763 case Triple::cellspu:
764 case Triple::hexagon:
772 T.setArch(UnknownArch);
776 case Triple::mips64el:
777 case Triple::nvptx64:
779 case Triple::sparcv9:
784 case Triple::mips: T.setArch(Triple::mips64); break;
785 case Triple::mipsel: T.setArch(Triple::mips64el); break;
786 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
787 case Triple::ppc: T.setArch(Triple::ppc64); break;
788 case Triple::sparc: T.setArch(Triple::sparcv9); break;
789 case Triple::x86: T.setArch(Triple::x86_64); break;