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";
65 case hexagon: return "hexagon";
67 case r600: return "r600";
70 case sparc: return "sparc";
73 case x86_64: return "x86";
75 case xcore: return "xcore";
77 case nvptx: return "nvptx";
78 case nvptx64: return "nvptx";
79 case le32: return "le32";
80 case amdil: return "amdil";
84 const char *Triple::getVendorTypeName(VendorType Kind) {
86 case UnknownVendor: return "unknown";
88 case Apple: return "apple";
90 case SCEI: return "scei";
91 case BGP: return "bgp";
92 case BGQ: return "bgq";
95 llvm_unreachable("Invalid VendorType!");
98 const char *Triple::getOSTypeName(OSType Kind) {
100 case UnknownOS: return "unknown";
102 case AuroraUX: return "auroraux";
103 case Cygwin: return "cygwin";
104 case Darwin: return "darwin";
105 case DragonFly: return "dragonfly";
106 case FreeBSD: return "freebsd";
107 case IOS: return "ios";
108 case KFreeBSD: return "kfreebsd";
109 case Linux: return "linux";
110 case Lv2: return "lv2";
111 case MacOSX: return "macosx";
112 case MinGW32: return "mingw32";
113 case NetBSD: return "netbsd";
114 case OpenBSD: return "openbsd";
115 case Solaris: return "solaris";
116 case Win32: return "win32";
117 case Haiku: return "haiku";
118 case Minix: return "minix";
119 case RTEMS: return "rtems";
120 case NativeClient: return "nacl";
121 case CNK: return "cnk";
124 llvm_unreachable("Invalid OSType");
127 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
129 case UnknownEnvironment: return "unknown";
130 case GNU: return "gnu";
131 case GNUEABIHF: return "gnueabihf";
132 case GNUEABI: return "gnueabi";
133 case EABI: return "eabi";
134 case MachO: return "macho";
135 case ANDROIDEABI: return "androideabi";
138 llvm_unreachable("Invalid EnvironmentType!");
141 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
142 return StringSwitch<Triple::ArchType>(Name)
144 .Case("cellspu", cellspu)
146 .Case("mipsel", mipsel)
147 .Case("mips64", mips64)
148 .Case("mips64el", mips64el)
149 .Case("msp430", msp430)
150 .Case("ppc64", ppc64)
153 .Case("mblaze", mblaze)
155 .Case("hexagon", hexagon)
156 .Case("sparc", sparc)
157 .Case("sparcv9", sparcv9)
159 .Case("thumb", thumb)
161 .Case("x86-64", x86_64)
162 .Case("xcore", xcore)
163 .Case("nvptx", nvptx)
164 .Case("nvptx64", nvptx64)
166 .Case("amdil", amdil)
167 .Default(UnknownArch);
170 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
171 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
172 // archs which Darwin doesn't use.
174 // The matching this routine does is fairly pointless, since it is neither the
175 // complete architecture list, nor a reasonable subset. The problem is that
176 // historically the driver driver accepts this and also ties its -march=
177 // handling to the architecture name, so we need to be careful before removing
180 // This code must be kept in sync with Clang's Darwin specific argument
183 return StringSwitch<ArchType>(Str)
184 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
185 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
186 .Case("ppc64", Triple::ppc64)
187 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
188 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
190 .Case("x86_64", Triple::x86_64)
191 // This is derived from the driver driver.
192 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
193 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
194 .Case("r600", Triple::r600)
195 .Case("nvptx", Triple::nvptx)
196 .Case("nvptx64", Triple::nvptx64)
197 .Case("amdil", Triple::amdil)
198 .Default(Triple::UnknownArch);
201 // Returns architecture name that is understood by the target assembler.
202 const char *Triple::getArchNameForAssembler() {
203 if (!isOSDarwin() && getVendor() != Triple::Apple)
206 return StringSwitch<const char*>(getArchName())
207 .Case("i386", "i386")
208 .Case("x86_64", "x86_64")
209 .Case("powerpc", "ppc")
210 .Case("powerpc64", "ppc64")
211 .Cases("mblaze", "microblaze", "mblaze")
213 .Cases("armv4t", "thumbv4t", "armv4t")
214 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
215 .Cases("armv6", "thumbv6", "armv6")
216 .Cases("armv7", "thumbv7", "armv7")
217 .Case("r600", "r600")
218 .Case("nvptx", "nvptx")
219 .Case("nvptx64", "nvptx64")
220 .Case("le32", "le32")
221 .Case("amdil", "amdil")
225 static Triple::ArchType parseArch(StringRef ArchName) {
226 return StringSwitch<Triple::ArchType>(ArchName)
227 .Cases("i386", "i486", "i586", "i686", Triple::x86)
228 // FIXME: Do we need to support these?
229 .Cases("i786", "i886", "i986", Triple::x86)
230 .Cases("amd64", "x86_64", Triple::x86_64)
231 .Case("powerpc", Triple::ppc)
232 .Cases("powerpc64", "ppu", Triple::ppc64)
233 .Case("mblaze", Triple::mblaze)
234 .Cases("arm", "xscale", Triple::arm)
235 // FIXME: It would be good to replace these with explicit names for all the
236 // various suffixes supported.
237 .StartsWith("armv", Triple::arm)
238 .Case("thumb", Triple::thumb)
239 .StartsWith("thumbv", Triple::thumb)
240 .Cases("spu", "cellspu", Triple::cellspu)
241 .Case("msp430", Triple::msp430)
242 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
243 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
244 .Cases("mips64", "mips64eb", Triple::mips64)
245 .Case("mips64el", Triple::mips64el)
246 .Case("r600", Triple::r600)
247 .Case("hexagon", Triple::hexagon)
248 .Case("sparc", Triple::sparc)
249 .Case("sparcv9", Triple::sparcv9)
250 .Case("tce", Triple::tce)
251 .Case("xcore", Triple::xcore)
252 .Case("nvptx", Triple::nvptx)
253 .Case("nvptx64", Triple::nvptx64)
254 .Case("le32", Triple::le32)
255 .Case("amdil", Triple::amdil)
256 .Default(Triple::UnknownArch);
259 static Triple::VendorType parseVendor(StringRef VendorName) {
260 return StringSwitch<Triple::VendorType>(VendorName)
261 .Case("apple", Triple::Apple)
262 .Case("pc", Triple::PC)
263 .Case("scei", Triple::SCEI)
264 .Case("bgp", Triple::BGP)
265 .Case("bgq", Triple::BGQ)
266 .Default(Triple::UnknownVendor);
269 static Triple::OSType parseOS(StringRef OSName) {
270 return StringSwitch<Triple::OSType>(OSName)
271 .StartsWith("auroraux", Triple::AuroraUX)
272 .StartsWith("cygwin", Triple::Cygwin)
273 .StartsWith("darwin", Triple::Darwin)
274 .StartsWith("dragonfly", Triple::DragonFly)
275 .StartsWith("freebsd", Triple::FreeBSD)
276 .StartsWith("ios", Triple::IOS)
277 .StartsWith("kfreebsd", Triple::KFreeBSD)
278 .StartsWith("linux", Triple::Linux)
279 .StartsWith("lv2", Triple::Lv2)
280 .StartsWith("macosx", Triple::MacOSX)
281 .StartsWith("mingw32", Triple::MinGW32)
282 .StartsWith("netbsd", Triple::NetBSD)
283 .StartsWith("openbsd", Triple::OpenBSD)
284 .StartsWith("solaris", Triple::Solaris)
285 .StartsWith("win32", Triple::Win32)
286 .StartsWith("haiku", Triple::Haiku)
287 .StartsWith("minix", Triple::Minix)
288 .StartsWith("rtems", Triple::RTEMS)
289 .StartsWith("nacl", Triple::NativeClient)
290 .StartsWith("cnk", Triple::CNK)
291 .Default(Triple::UnknownOS);
294 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
295 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
296 .StartsWith("eabi", Triple::EABI)
297 .StartsWith("gnueabihf", Triple::GNUEABIHF)
298 .StartsWith("gnueabi", Triple::GNUEABI)
299 .StartsWith("gnu", Triple::GNU)
300 .StartsWith("macho", Triple::MachO)
301 .StartsWith("androideabi", Triple::ANDROIDEABI)
302 .Default(Triple::UnknownEnvironment);
305 /// \brief Construct a triple from the string representation provided.
307 /// This stores the string representation and parses the various pieces into
309 Triple::Triple(const Twine &Str)
311 Arch(parseArch(getArchName())),
312 Vendor(parseVendor(getVendorName())),
313 OS(parseOS(getOSName())),
314 Environment(parseEnvironment(getEnvironmentName())) {
317 /// \brief Construct a triple from string representations of the architecture,
320 /// This joins each argument into a canonical string representation and parses
321 /// them into enum members. It leaves the environment unknown and omits it from
322 /// the string representation.
323 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
324 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
325 Arch(parseArch(ArchStr.str())),
326 Vendor(parseVendor(VendorStr.str())),
327 OS(parseOS(OSStr.str())),
331 /// \brief Construct a triple from string representations of the architecture,
332 /// vendor, OS, and environment.
334 /// This joins each argument into a canonical string representation and parses
335 /// them into enum members.
336 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
337 const Twine &EnvironmentStr)
338 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
339 EnvironmentStr).str()),
340 Arch(parseArch(ArchStr.str())),
341 Vendor(parseVendor(VendorStr.str())),
342 OS(parseOS(OSStr.str())),
343 Environment(parseEnvironment(EnvironmentStr.str())) {
346 std::string Triple::normalize(StringRef Str) {
347 // Parse into components.
348 SmallVector<StringRef, 4> Components;
349 Str.split(Components, "-");
351 // If the first component corresponds to a known architecture, preferentially
352 // use it for the architecture. If the second component corresponds to a
353 // known vendor, preferentially use it for the vendor, etc. This avoids silly
354 // component movement when a component parses as (eg) both a valid arch and a
356 ArchType Arch = UnknownArch;
357 if (Components.size() > 0)
358 Arch = parseArch(Components[0]);
359 VendorType Vendor = UnknownVendor;
360 if (Components.size() > 1)
361 Vendor = parseVendor(Components[1]);
362 OSType OS = UnknownOS;
363 if (Components.size() > 2)
364 OS = parseOS(Components[2]);
365 EnvironmentType Environment = UnknownEnvironment;
366 if (Components.size() > 3)
367 Environment = parseEnvironment(Components[3]);
369 // Note which components are already in their final position. These will not
372 Found[0] = Arch != UnknownArch;
373 Found[1] = Vendor != UnknownVendor;
374 Found[2] = OS != UnknownOS;
375 Found[3] = Environment != UnknownEnvironment;
377 // If they are not there already, permute the components into their canonical
378 // positions by seeing if they parse as a valid architecture, and if so moving
379 // the component to the architecture position etc.
380 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
382 continue; // Already in the canonical position.
384 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
385 // Do not reparse any components that already matched.
386 if (Idx < array_lengthof(Found) && Found[Idx])
389 // Does this component parse as valid for the target position?
391 StringRef Comp = Components[Idx];
393 default: llvm_unreachable("unexpected component type!");
395 Arch = parseArch(Comp);
396 Valid = Arch != UnknownArch;
399 Vendor = parseVendor(Comp);
400 Valid = Vendor != UnknownVendor;
404 Valid = OS != UnknownOS;
407 Environment = parseEnvironment(Comp);
408 Valid = Environment != UnknownEnvironment;
412 continue; // Nope, try the next component.
414 // Move the component to the target position, pushing any non-fixed
415 // components that are in the way to the right. This tends to give
416 // good results in the common cases of a forgotten vendor component
417 // or a wrongly positioned environment.
419 // Insert left, pushing the existing components to the right. For
420 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
421 StringRef CurrentComponent(""); // The empty component.
422 // Replace the component we are moving with an empty component.
423 std::swap(CurrentComponent, Components[Idx]);
424 // Insert the component being moved at Pos, displacing any existing
425 // components to the right.
426 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
427 // Skip over any fixed components.
428 while (i < array_lengthof(Found) && Found[i])
430 // Place the component at the new position, getting the component
431 // that was at this position - it will be moved right.
432 std::swap(CurrentComponent, Components[i]);
434 } else if (Pos > Idx) {
435 // Push right by inserting empty components until the component at Idx
436 // reaches the target position Pos. For example, pc-a -> -pc-a when
437 // moving pc to the second position.
439 // Insert one empty component at Idx.
440 StringRef CurrentComponent(""); // The empty component.
441 for (unsigned i = Idx; i < Components.size();) {
442 // Place the component at the new position, getting the component
443 // that was at this position - it will be moved right.
444 std::swap(CurrentComponent, Components[i]);
445 // If it was placed on top of an empty component then we are done.
446 if (CurrentComponent.empty())
448 // Advance to the next component, skipping any fixed components.
449 while (++i < array_lengthof(Found) && Found[i])
452 // The last component was pushed off the end - append it.
453 if (!CurrentComponent.empty())
454 Components.push_back(CurrentComponent);
456 // Advance Idx to the component's new position.
457 while (++Idx < array_lengthof(Found) && Found[Idx])
459 } while (Idx < Pos); // Add more until the final position is reached.
461 assert(Pos < Components.size() && Components[Pos] == Comp &&
462 "Component moved wrong!");
468 // Special case logic goes here. At this point Arch, Vendor and OS have the
469 // correct values for the computed components.
471 // Stick the corrected components back together to form the normalized string.
472 std::string Normalized;
473 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
474 if (i) Normalized += '-';
475 Normalized += Components[i];
480 StringRef Triple::getArchName() const {
481 return StringRef(Data).split('-').first; // Isolate first component
484 StringRef Triple::getVendorName() const {
485 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
486 return Tmp.split('-').first; // Isolate second component
489 StringRef Triple::getOSName() const {
490 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
491 Tmp = Tmp.split('-').second; // Strip second component
492 return Tmp.split('-').first; // Isolate third component
495 StringRef Triple::getEnvironmentName() const {
496 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
497 Tmp = Tmp.split('-').second; // Strip second component
498 return Tmp.split('-').second; // Strip third component
501 StringRef Triple::getOSAndEnvironmentName() const {
502 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
503 return Tmp.split('-').second; // Strip second component
506 static unsigned EatNumber(StringRef &Str) {
507 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
511 // Consume the leading digit.
512 Result = Result*10 + (Str[0] - '0');
516 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
521 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
522 unsigned &Micro) const {
523 StringRef OSName = getOSName();
525 // Assume that the OS portion of the triple starts with the canonical name.
526 StringRef OSTypeName = getOSTypeName(getOS());
527 if (OSName.startswith(OSTypeName))
528 OSName = OSName.substr(OSTypeName.size());
530 // Any unset version defaults to 0.
531 Major = Minor = Micro = 0;
533 // Parse up to three components.
534 unsigned *Components[3] = { &Major, &Minor, &Micro };
535 for (unsigned i = 0; i != 3; ++i) {
536 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
539 // Consume the leading number.
540 *Components[i] = EatNumber(OSName);
542 // Consume the separator, if present.
543 if (OSName.startswith("."))
544 OSName = OSName.substr(1);
548 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
549 unsigned &Micro) const {
550 getOSVersion(Major, Minor, Micro);
553 default: llvm_unreachable("unexpected OS for Darwin triple");
555 // Default to darwin8, i.e., MacOSX 10.4.
558 // Darwin version numbers are skewed from OS X versions.
575 // Ignore the version from the triple. This is only handled because the
576 // the clang driver combines OS X and IOS support into a common Darwin
577 // toolchain that wants to know the OS X version number even when targeting
587 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
588 unsigned &Micro) const {
590 default: llvm_unreachable("unexpected OS for Darwin triple");
593 // Ignore the version from the triple. This is only handled because the
594 // the clang driver combines OS X and IOS support into a common Darwin
595 // toolchain that wants to know the iOS version number even when targeting
602 getOSVersion(Major, Minor, Micro);
610 void Triple::setTriple(const Twine &Str) {
614 void Triple::setArch(ArchType Kind) {
615 setArchName(getArchTypeName(Kind));
618 void Triple::setVendor(VendorType Kind) {
619 setVendorName(getVendorTypeName(Kind));
622 void Triple::setOS(OSType Kind) {
623 setOSName(getOSTypeName(Kind));
626 void Triple::setEnvironment(EnvironmentType Kind) {
627 setEnvironmentName(getEnvironmentTypeName(Kind));
630 void Triple::setArchName(StringRef Str) {
631 // Work around a miscompilation bug for Twines in gcc 4.0.3.
632 SmallString<64> Triple;
635 Triple += getVendorName();
637 Triple += getOSAndEnvironmentName();
638 setTriple(Triple.str());
641 void Triple::setVendorName(StringRef Str) {
642 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
645 void Triple::setOSName(StringRef Str) {
646 if (hasEnvironment())
647 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
648 "-" + getEnvironmentName());
650 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
653 void Triple::setEnvironmentName(StringRef Str) {
654 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
658 void Triple::setOSAndEnvironmentName(StringRef Str) {
659 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
662 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
664 case llvm::Triple::UnknownArch:
667 case llvm::Triple::msp430:
670 case llvm::Triple::amdil:
671 case llvm::Triple::arm:
672 case llvm::Triple::cellspu:
673 case llvm::Triple::hexagon:
674 case llvm::Triple::le32:
675 case llvm::Triple::mblaze:
676 case llvm::Triple::mips:
677 case llvm::Triple::mipsel:
678 case llvm::Triple::nvptx:
679 case llvm::Triple::ppc:
680 case llvm::Triple::r600:
681 case llvm::Triple::sparc:
682 case llvm::Triple::tce:
683 case llvm::Triple::thumb:
684 case llvm::Triple::x86:
685 case llvm::Triple::xcore:
688 case llvm::Triple::mips64:
689 case llvm::Triple::mips64el:
690 case llvm::Triple::nvptx64:
691 case llvm::Triple::ppc64:
692 case llvm::Triple::sparcv9:
693 case llvm::Triple::x86_64:
696 llvm_unreachable("Invalid architecture value");
699 bool Triple::isArch64Bit() const {
700 return getArchPointerBitWidth(getArch()) == 64;
703 bool Triple::isArch32Bit() const {
704 return getArchPointerBitWidth(getArch()) == 32;
707 bool Triple::isArch16Bit() const {
708 return getArchPointerBitWidth(getArch()) == 16;
711 Triple Triple::get32BitArchVariant() const {
714 case Triple::UnknownArch:
716 T.setArch(UnknownArch);
721 case Triple::cellspu:
722 case Triple::hexagon:
738 case Triple::mips64: T.setArch(Triple::mips); break;
739 case Triple::mips64el: T.setArch(Triple::mipsel); break;
740 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
741 case Triple::ppc64: T.setArch(Triple::ppc); break;
742 case Triple::sparcv9: T.setArch(Triple::sparc); break;
743 case Triple::x86_64: T.setArch(Triple::x86); break;
748 Triple Triple::get64BitArchVariant() const {
751 case Triple::UnknownArch:
754 case Triple::cellspu:
755 case Triple::hexagon:
763 T.setArch(UnknownArch);
767 case Triple::mips64el:
768 case Triple::nvptx64:
770 case Triple::sparcv9:
775 case Triple::mips: T.setArch(Triple::mips64); break;
776 case Triple::mipsel: T.setArch(Triple::mips64el); break;
777 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
778 case Triple::ppc: T.setArch(Triple::ppc64); break;
779 case Triple::sparc: T.setArch(Triple::sparcv9); break;
780 case Triple::x86: T.setArch(Triple::x86_64); break;