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 InvalidArch: return "<invalid>";
21 case UnknownArch: return "unknown";
23 case arm: return "arm";
24 case cellspu: return "cellspu";
25 case hexagon: return "hexagon";
26 case mips: return "mips";
27 case mipsel: return "mipsel";
28 case mips64: return "mips64";
29 case mips64el:return "mips64el";
30 case msp430: return "msp430";
31 case ppc64: return "powerpc64";
32 case ppc: return "powerpc";
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 ptx32: return "ptx32";
42 case ptx64: return "ptx64";
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";
65 case hexagon: return "hexagon";
68 case sparc: return "sparc";
71 case x86_64: return "x86";
73 case xcore: return "xcore";
75 case ptx32: return "ptx";
76 case ptx64: return "ptx";
77 case le32: return "le32";
78 case amdil: return "amdil";
82 const char *Triple::getVendorTypeName(VendorType Kind) {
84 case UnknownVendor: return "unknown";
86 case Apple: return "apple";
88 case SCEI: return "scei";
91 llvm_unreachable("Invalid VendorType!");
94 const char *Triple::getOSTypeName(OSType Kind) {
96 case UnknownOS: return "unknown";
98 case AuroraUX: return "auroraux";
99 case Cygwin: return "cygwin";
100 case Darwin: return "darwin";
101 case DragonFly: return "dragonfly";
102 case FreeBSD: return "freebsd";
103 case IOS: return "ios";
104 case KFreeBSD: return "kfreebsd";
105 case Linux: return "linux";
106 case Lv2: return "lv2";
107 case MacOSX: return "macosx";
108 case MinGW32: return "mingw32";
109 case NetBSD: return "netbsd";
110 case OpenBSD: return "openbsd";
111 case Psp: return "psp";
112 case Solaris: return "solaris";
113 case Win32: return "win32";
114 case Haiku: return "haiku";
115 case Minix: return "minix";
116 case RTEMS: return "rtems";
117 case NativeClient: return "nacl";
120 llvm_unreachable("Invalid OSType");
123 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
125 case UnknownEnvironment: return "unknown";
126 case GNU: return "gnu";
127 case GNUEABIHF: return "gnueabihf";
128 case GNUEABI: return "gnueabi";
129 case EABI: return "eabi";
130 case MachO: return "macho";
131 case ANDROIDEABI: return "androideabi";
134 llvm_unreachable("Invalid EnvironmentType!");
137 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
138 return StringSwitch<Triple::ArchType>(Name)
140 .Case("cellspu", cellspu)
142 .Case("mipsel", mipsel)
143 .Case("mips64", mips64)
144 .Case("mips64el", mips64el)
145 .Case("msp430", msp430)
146 .Case("ppc64", ppc64)
149 .Case("mblaze", mblaze)
150 .Case("hexagon", hexagon)
151 .Case("sparc", sparc)
152 .Case("sparcv9", sparcv9)
154 .Case("thumb", thumb)
156 .Case("x86-64", x86_64)
157 .Case("xcore", xcore)
158 .Case("ptx32", ptx32)
159 .Case("ptx64", ptx64)
161 .Case("amdil", amdil)
162 .Default(UnknownArch);
165 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
166 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
167 // archs which Darwin doesn't use.
169 // The matching this routine does is fairly pointless, since it is neither the
170 // complete architecture list, nor a reasonable subset. The problem is that
171 // historically the driver driver accepts this and also ties its -march=
172 // handling to the architecture name, so we need to be careful before removing
175 // This code must be kept in sync with Clang's Darwin specific argument
178 return StringSwitch<ArchType>(Str)
179 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
180 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
181 .Case("ppc64", Triple::ppc64)
182 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
183 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
185 .Case("x86_64", Triple::x86_64)
186 // This is derived from the driver driver.
187 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
188 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
189 .Case("ptx32", Triple::ptx32)
190 .Case("ptx64", Triple::ptx64)
191 .Case("amdil", Triple::amdil)
192 .Default(Triple::UnknownArch);
195 // Returns architecture name that is understood by the target assembler.
196 const char *Triple::getArchNameForAssembler() {
197 if (!isOSDarwin() && getVendor() != Triple::Apple)
200 return StringSwitch<const char*>(getArchName())
201 .Case("i386", "i386")
202 .Case("x86_64", "x86_64")
203 .Case("powerpc", "ppc")
204 .Case("powerpc64", "ppc64")
205 .Cases("mblaze", "microblaze", "mblaze")
207 .Cases("armv4t", "thumbv4t", "armv4t")
208 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
209 .Cases("armv6", "thumbv6", "armv6")
210 .Cases("armv7", "thumbv7", "armv7")
211 .Case("ptx32", "ptx32")
212 .Case("ptx64", "ptx64")
213 .Case("le32", "le32")
214 .Case("amdil", "amdil")
220 Triple::ArchType Triple::ParseArch(StringRef ArchName) {
221 // Handle some unusual patterns.
222 // FIXME: It would be good to replace these with explicit names for all the
223 // various suffixes supported.
224 if (ArchName.startswith("armv")) return arm;
225 if (ArchName.startswith("thumbv")) return thumb;
227 return StringSwitch<ArchType>(ArchName)
228 .Cases("i386", "i486", "i586", "i686", x86)
229 .Cases("i786", "i886", "i986", x86) // FIXME: Do we need to support these?
230 .Cases("amd64", "x86_64", x86_64)
231 .Case("powerpc", ppc)
232 .Cases("powerpc64", "ppu", ppc64)
233 .Case("mblaze", mblaze)
234 .Cases("arm", "xscale", arm)
235 .Case("thumb", thumb)
236 .Cases("spu", "cellspu", cellspu)
237 .Case("msp430", msp430)
238 .Cases("mips", "mipseb", "mipsallegrex", mips)
239 .Cases("mipsel", "mipsallegrexel", "psp", mipsel)
240 .Cases("mips64", "mips64eb", mips64)
241 .Case("mips64el", mips64el)
242 .Case("hexagon", hexagon)
243 .Case("sparc", sparc)
244 .Case("sparcv9", sparcv9)
246 .Case("xcore", xcore)
247 .Case("ptx32", ptx32)
248 .Case("ptx64", ptx64)
250 .Case("amdil", amdil)
251 .Default(UnknownArch);
254 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
255 return StringSwitch<VendorType>(VendorName)
256 .Case("apple", Apple)
259 .Default(UnknownVendor);
262 Triple::OSType Triple::ParseOS(StringRef OSName) {
263 if (OSName.startswith("auroraux"))
265 else if (OSName.startswith("cygwin"))
267 else if (OSName.startswith("darwin"))
269 else if (OSName.startswith("dragonfly"))
271 else if (OSName.startswith("freebsd"))
273 else if (OSName.startswith("ios"))
275 else if (OSName.startswith("kfreebsd"))
277 else if (OSName.startswith("linux"))
279 else if (OSName.startswith("lv2"))
281 else if (OSName.startswith("macosx"))
283 else if (OSName.startswith("mingw32"))
285 else if (OSName.startswith("netbsd"))
287 else if (OSName.startswith("openbsd"))
289 else if (OSName.startswith("psp"))
291 else if (OSName.startswith("solaris"))
293 else if (OSName.startswith("win32"))
295 else if (OSName.startswith("haiku"))
297 else if (OSName.startswith("minix"))
299 else if (OSName.startswith("rtems"))
301 else if (OSName.startswith("nacl"))
307 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
308 if (EnvironmentName.startswith("eabi"))
310 else if (EnvironmentName.startswith("gnueabihf"))
312 else if (EnvironmentName.startswith("gnueabi"))
314 else if (EnvironmentName.startswith("gnu"))
316 else if (EnvironmentName.startswith("macho"))
318 else if (EnvironmentName.startswith("androideabi"))
321 return UnknownEnvironment;
324 void Triple::Parse() const {
325 assert(!isInitialized() && "Invalid parse call.");
327 Arch = ParseArch(getArchName());
328 Vendor = ParseVendor(getVendorName());
329 OS = ParseOS(getOSName());
330 Environment = ParseEnvironment(getEnvironmentName());
332 assert(isInitialized() && "Failed to initialize!");
335 std::string Triple::normalize(StringRef Str) {
336 // Parse into components.
337 SmallVector<StringRef, 4> Components;
338 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
339 Last = Str.find('-', First);
340 Components.push_back(Str.slice(First, Last));
343 // If the first component corresponds to a known architecture, preferentially
344 // use it for the architecture. If the second component corresponds to a
345 // known vendor, preferentially use it for the vendor, etc. This avoids silly
346 // component movement when a component parses as (eg) both a valid arch and a
348 ArchType Arch = UnknownArch;
349 if (Components.size() > 0)
350 Arch = ParseArch(Components[0]);
351 VendorType Vendor = UnknownVendor;
352 if (Components.size() > 1)
353 Vendor = ParseVendor(Components[1]);
354 OSType OS = UnknownOS;
355 if (Components.size() > 2)
356 OS = ParseOS(Components[2]);
357 EnvironmentType Environment = UnknownEnvironment;
358 if (Components.size() > 3)
359 Environment = ParseEnvironment(Components[3]);
361 // Note which components are already in their final position. These will not
364 Found[0] = Arch != UnknownArch;
365 Found[1] = Vendor != UnknownVendor;
366 Found[2] = OS != UnknownOS;
367 Found[3] = Environment != UnknownEnvironment;
369 // If they are not there already, permute the components into their canonical
370 // positions by seeing if they parse as a valid architecture, and if so moving
371 // the component to the architecture position etc.
372 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
374 continue; // Already in the canonical position.
376 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
377 // Do not reparse any components that already matched.
378 if (Idx < array_lengthof(Found) && Found[Idx])
381 // Does this component parse as valid for the target position?
383 StringRef Comp = Components[Idx];
385 default: llvm_unreachable("unexpected component type!");
387 Arch = ParseArch(Comp);
388 Valid = Arch != UnknownArch;
391 Vendor = ParseVendor(Comp);
392 Valid = Vendor != UnknownVendor;
396 Valid = OS != UnknownOS;
399 Environment = ParseEnvironment(Comp);
400 Valid = Environment != UnknownEnvironment;
404 continue; // Nope, try the next component.
406 // Move the component to the target position, pushing any non-fixed
407 // components that are in the way to the right. This tends to give
408 // good results in the common cases of a forgotten vendor component
409 // or a wrongly positioned environment.
411 // Insert left, pushing the existing components to the right. For
412 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
413 StringRef CurrentComponent(""); // The empty component.
414 // Replace the component we are moving with an empty component.
415 std::swap(CurrentComponent, Components[Idx]);
416 // Insert the component being moved at Pos, displacing any existing
417 // components to the right.
418 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
419 // Skip over any fixed components.
420 while (i < array_lengthof(Found) && Found[i]) ++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]) {}
449 } while (Idx < Pos); // Add more until the final position is reached.
451 assert(Pos < Components.size() && Components[Pos] == Comp &&
452 "Component moved wrong!");
458 // Special case logic goes here. At this point Arch, Vendor and OS have the
459 // correct values for the computed components.
461 // Stick the corrected components back together to form the normalized string.
462 std::string Normalized;
463 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
464 if (i) Normalized += '-';
465 Normalized += Components[i];
470 StringRef Triple::getArchName() const {
471 return StringRef(Data).split('-').first; // Isolate first component
474 StringRef Triple::getVendorName() const {
475 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
476 return Tmp.split('-').first; // Isolate second component
479 StringRef Triple::getOSName() const {
480 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
481 Tmp = Tmp.split('-').second; // Strip second component
482 return Tmp.split('-').first; // Isolate third component
485 StringRef Triple::getEnvironmentName() const {
486 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
487 Tmp = Tmp.split('-').second; // Strip second component
488 return Tmp.split('-').second; // Strip third component
491 StringRef Triple::getOSAndEnvironmentName() const {
492 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
493 return Tmp.split('-').second; // Strip second component
496 static unsigned EatNumber(StringRef &Str) {
497 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
501 // Consume the leading digit.
502 Result = Result*10 + (Str[0] - '0');
506 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
511 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
512 unsigned &Micro) const {
513 StringRef OSName = getOSName();
515 // Assume that the OS portion of the triple starts with the canonical name.
516 StringRef OSTypeName = getOSTypeName(getOS());
517 if (OSName.startswith(OSTypeName))
518 OSName = OSName.substr(OSTypeName.size());
520 // Any unset version defaults to 0.
521 Major = Minor = Micro = 0;
523 // Parse up to three components.
524 unsigned *Components[3] = { &Major, &Minor, &Micro };
525 for (unsigned i = 0; i != 3; ++i) {
526 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
529 // Consume the leading number.
530 *Components[i] = EatNumber(OSName);
532 // Consume the separator, if present.
533 if (OSName.startswith("."))
534 OSName = OSName.substr(1);
538 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
539 unsigned &Micro) const {
540 getOSVersion(Major, Minor, Micro);
543 default: llvm_unreachable("unexpected OS for Darwin triple");
545 // Default to darwin8, i.e., MacOSX 10.4.
548 // Darwin version numbers are skewed from OS X versions.
565 // Ignore the version from the triple. This is only handled because the
566 // the clang driver combines OS X and IOS support into a common Darwin
567 // toolchain that wants to know the OS X version number even when targeting
577 void Triple::setTriple(const Twine &Str) {
582 void Triple::setArch(ArchType Kind) {
583 setArchName(getArchTypeName(Kind));
586 void Triple::setVendor(VendorType Kind) {
587 setVendorName(getVendorTypeName(Kind));
590 void Triple::setOS(OSType Kind) {
591 setOSName(getOSTypeName(Kind));
594 void Triple::setEnvironment(EnvironmentType Kind) {
595 setEnvironmentName(getEnvironmentTypeName(Kind));
598 void Triple::setArchName(StringRef Str) {
599 // Work around a miscompilation bug for Twines in gcc 4.0.3.
600 SmallString<64> Triple;
603 Triple += getVendorName();
605 Triple += getOSAndEnvironmentName();
606 setTriple(Triple.str());
609 void Triple::setVendorName(StringRef Str) {
610 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
613 void Triple::setOSName(StringRef Str) {
614 if (hasEnvironment())
615 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
616 "-" + getEnvironmentName());
618 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
621 void Triple::setEnvironmentName(StringRef Str) {
622 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
626 void Triple::setOSAndEnvironmentName(StringRef Str) {
627 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
630 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
632 case llvm::Triple::UnknownArch:
633 case llvm::Triple::InvalidArch:
636 case llvm::Triple::msp430:
639 case llvm::Triple::amdil:
640 case llvm::Triple::arm:
641 case llvm::Triple::cellspu:
642 case llvm::Triple::hexagon:
643 case llvm::Triple::le32:
644 case llvm::Triple::mblaze:
645 case llvm::Triple::mips:
646 case llvm::Triple::mipsel:
647 case llvm::Triple::ppc:
648 case llvm::Triple::ptx32:
649 case llvm::Triple::sparc:
650 case llvm::Triple::tce:
651 case llvm::Triple::thumb:
652 case llvm::Triple::x86:
653 case llvm::Triple::xcore:
656 case llvm::Triple::mips64:
657 case llvm::Triple::mips64el:
658 case llvm::Triple::ppc64:
659 case llvm::Triple::ptx64:
660 case llvm::Triple::sparcv9:
661 case llvm::Triple::x86_64:
664 llvm_unreachable("Invalid architecture value");
667 bool Triple::isArch64Bit() const {
668 return getArchPointerBitWidth(getArch()) == 64;
671 bool Triple::isArch32Bit() const {
672 return getArchPointerBitWidth(getArch()) == 32;
675 bool Triple::isArch16Bit() const {
676 return getArchPointerBitWidth(getArch()) == 16;
679 Triple Triple::get32BitArchVariant() const {
682 case Triple::UnknownArch:
683 case Triple::InvalidArch:
685 T.setArch(UnknownArch);
690 case Triple::cellspu:
691 case Triple::hexagon:
706 case Triple::mips64: T.setArch(Triple::mips); break;
707 case Triple::mips64el: T.setArch(Triple::mipsel); break;
708 case Triple::ppc64: T.setArch(Triple::ppc); break;
709 case Triple::ptx64: T.setArch(Triple::ptx32); break;
710 case Triple::sparcv9: T.setArch(Triple::sparc); break;
711 case Triple::x86_64: T.setArch(Triple::x86); break;
716 Triple Triple::get64BitArchVariant() const {
719 case Triple::InvalidArch:
720 case Triple::UnknownArch:
723 case Triple::cellspu:
724 case Triple::hexagon:
731 T.setArch(UnknownArch);
735 case Triple::mips64el:
738 case Triple::sparcv9:
743 case Triple::mips: T.setArch(Triple::mips64); break;
744 case Triple::mipsel: T.setArch(Triple::mips64el); break;
745 case Triple::ppc: T.setArch(Triple::ppc64); break;
746 case Triple::ptx32: T.setArch(Triple::ptx64); break;
747 case Triple::sparc: T.setArch(Triple::sparcv9); break;
748 case Triple::x86: T.setArch(Triple::x86_64); break;