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";
45 case spir: return "spir";
48 llvm_unreachable("Invalid ArchType!");
51 const char *Triple::getArchTypePrefix(ArchType Kind) {
57 case thumb: return "arm";
59 case cellspu: return "spu";
62 case ppc: return "ppc";
64 case mblaze: return "mblaze";
69 case mips64el:return "mips";
71 case hexagon: return "hexagon";
73 case r600: return "r600";
76 case sparc: return "sparc";
79 case x86_64: return "x86";
81 case xcore: return "xcore";
83 case nvptx: return "nvptx";
84 case nvptx64: return "nvptx";
85 case le32: return "le32";
86 case amdil: return "amdil";
87 case spir: return "spir";
91 const char *Triple::getVendorTypeName(VendorType Kind) {
93 case UnknownVendor: return "unknown";
95 case Apple: return "apple";
97 case SCEI: return "scei";
98 case BGP: return "bgp";
99 case BGQ: return "bgq";
100 case Freescale: return "fsl";
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 NativeClient: return "nacl";
129 case CNK: return "cnk";
130 case Bitrig: return "bitrig";
133 llvm_unreachable("Invalid OSType");
136 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
138 case UnknownEnvironment: return "unknown";
139 case GNU: return "gnu";
140 case GNUEABIHF: return "gnueabihf";
141 case GNUEABI: return "gnueabi";
142 case EABI: return "eabi";
143 case MachO: return "macho";
144 case Android: return "android";
145 case ELF: return "elf";
148 llvm_unreachable("Invalid EnvironmentType!");
151 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
152 return StringSwitch<Triple::ArchType>(Name)
154 .Case("cellspu", cellspu)
156 .Case("mipsel", mipsel)
157 .Case("mips64", mips64)
158 .Case("mips64el", mips64el)
159 .Case("msp430", msp430)
160 .Case("ppc64", ppc64)
163 .Case("mblaze", mblaze)
165 .Case("hexagon", hexagon)
166 .Case("sparc", sparc)
167 .Case("sparcv9", sparcv9)
169 .Case("thumb", thumb)
171 .Case("x86-64", x86_64)
172 .Case("xcore", xcore)
173 .Case("nvptx", nvptx)
174 .Case("nvptx64", nvptx64)
176 .Case("amdil", amdil)
178 .Default(UnknownArch);
181 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
182 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
183 // archs which Darwin doesn't use.
185 // The matching this routine does is fairly pointless, since it is neither the
186 // complete architecture list, nor a reasonable subset. The problem is that
187 // historically the driver driver accepts this and also ties its -march=
188 // handling to the architecture name, so we need to be careful before removing
191 // This code must be kept in sync with Clang's Darwin specific argument
194 return StringSwitch<ArchType>(Str)
195 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
196 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
197 .Case("ppc64", Triple::ppc64)
198 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
199 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
201 .Case("x86_64", Triple::x86_64)
202 // This is derived from the driver driver.
203 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
204 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
205 .Case("r600", Triple::r600)
206 .Case("nvptx", Triple::nvptx)
207 .Case("nvptx64", Triple::nvptx64)
208 .Case("amdil", Triple::amdil)
209 .Case("spir", Triple::spir)
210 .Default(Triple::UnknownArch);
213 // Returns architecture name that is understood by the target assembler.
214 const char *Triple::getArchNameForAssembler() {
215 if (!isOSDarwin() && getVendor() != Triple::Apple)
218 return StringSwitch<const char*>(getArchName())
219 .Case("i386", "i386")
220 .Case("x86_64", "x86_64")
221 .Case("powerpc", "ppc")
222 .Case("powerpc64", "ppc64")
223 .Cases("mblaze", "microblaze", "mblaze")
225 .Cases("armv4t", "thumbv4t", "armv4t")
226 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
227 .Cases("armv6", "thumbv6", "armv6")
228 .Cases("armv7", "thumbv7", "armv7")
229 .Case("r600", "r600")
230 .Case("nvptx", "nvptx")
231 .Case("nvptx64", "nvptx64")
232 .Case("le32", "le32")
233 .Case("amdil", "amdil")
234 .Case("spir", "spir")
238 static Triple::ArchType parseArch(StringRef ArchName) {
239 return StringSwitch<Triple::ArchType>(ArchName)
240 .Cases("i386", "i486", "i586", "i686", Triple::x86)
241 // FIXME: Do we need to support these?
242 .Cases("i786", "i886", "i986", Triple::x86)
243 .Cases("amd64", "x86_64", Triple::x86_64)
244 .Case("powerpc", Triple::ppc)
245 .Cases("powerpc64", "ppu", Triple::ppc64)
246 .Case("mblaze", Triple::mblaze)
247 .Cases("arm", "xscale", Triple::arm)
248 // FIXME: It would be good to replace these with explicit names for all the
249 // various suffixes supported.
250 .StartsWith("armv", Triple::arm)
251 .Case("thumb", Triple::thumb)
252 .StartsWith("thumbv", Triple::thumb)
253 .Cases("spu", "cellspu", Triple::cellspu)
254 .Case("msp430", Triple::msp430)
255 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
256 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
257 .Cases("mips64", "mips64eb", Triple::mips64)
258 .Case("mips64el", Triple::mips64el)
259 .Case("r600", Triple::r600)
260 .Case("hexagon", Triple::hexagon)
261 .Case("sparc", Triple::sparc)
262 .Case("sparcv9", Triple::sparcv9)
263 .Case("tce", Triple::tce)
264 .Case("xcore", Triple::xcore)
265 .Case("nvptx", Triple::nvptx)
266 .Case("nvptx64", Triple::nvptx64)
267 .Case("le32", Triple::le32)
268 .Case("amdil", Triple::amdil)
269 .Case("spir", Triple::spir)
270 .Default(Triple::UnknownArch);
273 static Triple::VendorType parseVendor(StringRef VendorName) {
274 return StringSwitch<Triple::VendorType>(VendorName)
275 .Case("apple", Triple::Apple)
276 .Case("pc", Triple::PC)
277 .Case("scei", Triple::SCEI)
278 .Case("bgp", Triple::BGP)
279 .Case("bgq", Triple::BGQ)
280 .Case("fsl", Triple::Freescale)
281 .Default(Triple::UnknownVendor);
284 static Triple::OSType parseOS(StringRef OSName) {
285 return StringSwitch<Triple::OSType>(OSName)
286 .StartsWith("auroraux", Triple::AuroraUX)
287 .StartsWith("cygwin", Triple::Cygwin)
288 .StartsWith("darwin", Triple::Darwin)
289 .StartsWith("dragonfly", Triple::DragonFly)
290 .StartsWith("freebsd", Triple::FreeBSD)
291 .StartsWith("ios", Triple::IOS)
292 .StartsWith("kfreebsd", Triple::KFreeBSD)
293 .StartsWith("linux", Triple::Linux)
294 .StartsWith("lv2", Triple::Lv2)
295 .StartsWith("macosx", Triple::MacOSX)
296 .StartsWith("mingw32", Triple::MinGW32)
297 .StartsWith("netbsd", Triple::NetBSD)
298 .StartsWith("openbsd", Triple::OpenBSD)
299 .StartsWith("solaris", Triple::Solaris)
300 .StartsWith("win32", Triple::Win32)
301 .StartsWith("haiku", Triple::Haiku)
302 .StartsWith("minix", Triple::Minix)
303 .StartsWith("rtems", Triple::RTEMS)
304 .StartsWith("nacl", Triple::NativeClient)
305 .StartsWith("cnk", Triple::CNK)
306 .StartsWith("bitrig", Triple::Bitrig)
307 .Default(Triple::UnknownOS);
310 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
311 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
312 .StartsWith("eabi", Triple::EABI)
313 .StartsWith("gnueabihf", Triple::GNUEABIHF)
314 .StartsWith("gnueabi", Triple::GNUEABI)
315 .StartsWith("gnu", Triple::GNU)
316 .StartsWith("macho", Triple::MachO)
317 .StartsWith("android", Triple::Android)
318 .StartsWith("elf", Triple::ELF)
319 .Default(Triple::UnknownEnvironment);
322 /// \brief Construct a triple from the string representation provided.
324 /// This stores the string representation and parses the various pieces into
326 Triple::Triple(const Twine &Str)
328 Arch(parseArch(getArchName())),
329 Vendor(parseVendor(getVendorName())),
330 OS(parseOS(getOSName())),
331 Environment(parseEnvironment(getEnvironmentName())) {
334 /// \brief Construct a triple from string representations of the architecture,
337 /// This joins each argument into a canonical string representation and parses
338 /// them into enum members. It leaves the environment unknown and omits it from
339 /// the string representation.
340 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
341 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
342 Arch(parseArch(ArchStr.str())),
343 Vendor(parseVendor(VendorStr.str())),
344 OS(parseOS(OSStr.str())),
348 /// \brief Construct a triple from string representations of the architecture,
349 /// vendor, OS, and environment.
351 /// This joins each argument into a canonical string representation and parses
352 /// them into enum members.
353 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
354 const Twine &EnvironmentStr)
355 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
356 EnvironmentStr).str()),
357 Arch(parseArch(ArchStr.str())),
358 Vendor(parseVendor(VendorStr.str())),
359 OS(parseOS(OSStr.str())),
360 Environment(parseEnvironment(EnvironmentStr.str())) {
363 std::string Triple::normalize(StringRef Str) {
364 // Parse into components.
365 SmallVector<StringRef, 4> Components;
366 Str.split(Components, "-");
368 // If the first component corresponds to a known architecture, preferentially
369 // use it for the architecture. If the second component corresponds to a
370 // known vendor, preferentially use it for the vendor, etc. This avoids silly
371 // component movement when a component parses as (eg) both a valid arch and a
373 ArchType Arch = UnknownArch;
374 if (Components.size() > 0)
375 Arch = parseArch(Components[0]);
376 VendorType Vendor = UnknownVendor;
377 if (Components.size() > 1)
378 Vendor = parseVendor(Components[1]);
379 OSType OS = UnknownOS;
380 if (Components.size() > 2)
381 OS = parseOS(Components[2]);
382 EnvironmentType Environment = UnknownEnvironment;
383 if (Components.size() > 3)
384 Environment = parseEnvironment(Components[3]);
386 // Note which components are already in their final position. These will not
389 Found[0] = Arch != UnknownArch;
390 Found[1] = Vendor != UnknownVendor;
391 Found[2] = OS != UnknownOS;
392 Found[3] = Environment != UnknownEnvironment;
394 // If they are not there already, permute the components into their canonical
395 // positions by seeing if they parse as a valid architecture, and if so moving
396 // the component to the architecture position etc.
397 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
399 continue; // Already in the canonical position.
401 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
402 // Do not reparse any components that already matched.
403 if (Idx < array_lengthof(Found) && Found[Idx])
406 // Does this component parse as valid for the target position?
408 StringRef Comp = Components[Idx];
410 default: llvm_unreachable("unexpected component type!");
412 Arch = parseArch(Comp);
413 Valid = Arch != UnknownArch;
416 Vendor = parseVendor(Comp);
417 Valid = Vendor != UnknownVendor;
421 Valid = OS != UnknownOS;
424 Environment = parseEnvironment(Comp);
425 Valid = Environment != UnknownEnvironment;
429 continue; // Nope, try the next component.
431 // Move the component to the target position, pushing any non-fixed
432 // components that are in the way to the right. This tends to give
433 // good results in the common cases of a forgotten vendor component
434 // or a wrongly positioned environment.
436 // Insert left, pushing the existing components to the right. For
437 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
438 StringRef CurrentComponent(""); // The empty component.
439 // Replace the component we are moving with an empty component.
440 std::swap(CurrentComponent, Components[Idx]);
441 // Insert the component being moved at Pos, displacing any existing
442 // components to the right.
443 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
444 // Skip over any fixed components.
445 while (i < array_lengthof(Found) && Found[i])
447 // Place the component at the new position, getting the component
448 // that was at this position - it will be moved right.
449 std::swap(CurrentComponent, Components[i]);
451 } else if (Pos > Idx) {
452 // Push right by inserting empty components until the component at Idx
453 // reaches the target position Pos. For example, pc-a -> -pc-a when
454 // moving pc to the second position.
456 // Insert one empty component at Idx.
457 StringRef CurrentComponent(""); // The empty component.
458 for (unsigned i = Idx; i < Components.size();) {
459 // Place the component at the new position, getting the component
460 // that was at this position - it will be moved right.
461 std::swap(CurrentComponent, Components[i]);
462 // If it was placed on top of an empty component then we are done.
463 if (CurrentComponent.empty())
465 // Advance to the next component, skipping any fixed components.
466 while (++i < array_lengthof(Found) && Found[i])
469 // The last component was pushed off the end - append it.
470 if (!CurrentComponent.empty())
471 Components.push_back(CurrentComponent);
473 // Advance Idx to the component's new position.
474 while (++Idx < array_lengthof(Found) && Found[Idx])
476 } while (Idx < Pos); // Add more until the final position is reached.
478 assert(Pos < Components.size() && Components[Pos] == Comp &&
479 "Component moved wrong!");
485 // Special case logic goes here. At this point Arch, Vendor and OS have the
486 // correct values for the computed components.
488 // Stick the corrected components back together to form the normalized string.
489 std::string Normalized;
490 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
491 if (i) Normalized += '-';
492 Normalized += Components[i];
497 StringRef Triple::getArchName() const {
498 return StringRef(Data).split('-').first; // Isolate first component
501 StringRef Triple::getVendorName() const {
502 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
503 return Tmp.split('-').first; // Isolate second component
506 StringRef Triple::getOSName() const {
507 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
508 Tmp = Tmp.split('-').second; // Strip second component
509 return Tmp.split('-').first; // Isolate third component
512 StringRef Triple::getEnvironmentName() const {
513 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
514 Tmp = Tmp.split('-').second; // Strip second component
515 return Tmp.split('-').second; // Strip third component
518 StringRef Triple::getOSAndEnvironmentName() const {
519 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
520 return Tmp.split('-').second; // Strip second component
523 static unsigned EatNumber(StringRef &Str) {
524 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
528 // Consume the leading digit.
529 Result = Result*10 + (Str[0] - '0');
533 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
538 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
539 unsigned &Micro) const {
540 StringRef OSName = getOSName();
542 // Assume that the OS portion of the triple starts with the canonical name.
543 StringRef OSTypeName = getOSTypeName(getOS());
544 if (OSName.startswith(OSTypeName))
545 OSName = OSName.substr(OSTypeName.size());
547 // Any unset version defaults to 0.
548 Major = Minor = Micro = 0;
550 // Parse up to three components.
551 unsigned *Components[3] = { &Major, &Minor, &Micro };
552 for (unsigned i = 0; i != 3; ++i) {
553 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
556 // Consume the leading number.
557 *Components[i] = EatNumber(OSName);
559 // Consume the separator, if present.
560 if (OSName.startswith("."))
561 OSName = OSName.substr(1);
565 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
566 unsigned &Micro) const {
567 getOSVersion(Major, Minor, Micro);
570 default: llvm_unreachable("unexpected OS for Darwin triple");
572 // Default to darwin8, i.e., MacOSX 10.4.
575 // Darwin version numbers are skewed from OS X versions.
592 // Ignore the version from the triple. This is only handled because the
593 // the clang driver combines OS X and IOS support into a common Darwin
594 // toolchain that wants to know the OS X version number even when targeting
604 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
605 unsigned &Micro) const {
607 default: llvm_unreachable("unexpected OS for Darwin triple");
610 // Ignore the version from the triple. This is only handled because the
611 // the clang driver combines OS X and IOS support into a common Darwin
612 // toolchain that wants to know the iOS version number even when targeting
619 getOSVersion(Major, Minor, Micro);
627 void Triple::setTriple(const Twine &Str) {
631 void Triple::setArch(ArchType Kind) {
632 setArchName(getArchTypeName(Kind));
635 void Triple::setVendor(VendorType Kind) {
636 setVendorName(getVendorTypeName(Kind));
639 void Triple::setOS(OSType Kind) {
640 setOSName(getOSTypeName(Kind));
643 void Triple::setEnvironment(EnvironmentType Kind) {
644 setEnvironmentName(getEnvironmentTypeName(Kind));
647 void Triple::setArchName(StringRef Str) {
648 // Work around a miscompilation bug for Twines in gcc 4.0.3.
649 SmallString<64> Triple;
652 Triple += getVendorName();
654 Triple += getOSAndEnvironmentName();
655 setTriple(Triple.str());
658 void Triple::setVendorName(StringRef Str) {
659 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
662 void Triple::setOSName(StringRef Str) {
663 if (hasEnvironment())
664 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
665 "-" + getEnvironmentName());
667 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
670 void Triple::setEnvironmentName(StringRef Str) {
671 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
675 void Triple::setOSAndEnvironmentName(StringRef Str) {
676 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
679 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
681 case llvm::Triple::spir:
682 case llvm::Triple::UnknownArch:
685 case llvm::Triple::msp430:
688 case llvm::Triple::amdil:
689 case llvm::Triple::arm:
690 case llvm::Triple::cellspu:
691 case llvm::Triple::hexagon:
692 case llvm::Triple::le32:
693 case llvm::Triple::mblaze:
694 case llvm::Triple::mips:
695 case llvm::Triple::mipsel:
696 case llvm::Triple::nvptx:
697 case llvm::Triple::ppc:
698 case llvm::Triple::r600:
699 case llvm::Triple::sparc:
700 case llvm::Triple::tce:
701 case llvm::Triple::thumb:
702 case llvm::Triple::x86:
703 case llvm::Triple::xcore:
706 case llvm::Triple::mips64:
707 case llvm::Triple::mips64el:
708 case llvm::Triple::nvptx64:
709 case llvm::Triple::ppc64:
710 case llvm::Triple::sparcv9:
711 case llvm::Triple::x86_64:
714 llvm_unreachable("Invalid architecture value");
717 bool Triple::isArch64Bit() const {
718 return getArchPointerBitWidth(getArch()) == 64;
721 bool Triple::isArch32Bit() const {
722 return getArchPointerBitWidth(getArch()) == 32;
725 bool Triple::isArch16Bit() const {
726 return getArchPointerBitWidth(getArch()) == 16;
729 Triple Triple::get32BitArchVariant() const {
732 case Triple::UnknownArch:
734 T.setArch(UnknownArch);
740 case Triple::cellspu:
741 case Triple::hexagon:
757 case Triple::mips64: T.setArch(Triple::mips); break;
758 case Triple::mips64el: T.setArch(Triple::mipsel); break;
759 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
760 case Triple::ppc64: T.setArch(Triple::ppc); break;
761 case Triple::sparcv9: T.setArch(Triple::sparc); break;
762 case Triple::x86_64: T.setArch(Triple::x86); break;
767 Triple Triple::get64BitArchVariant() const {
770 case Triple::UnknownArch:
773 case Triple::cellspu:
774 case Triple::hexagon:
782 T.setArch(UnknownArch);
787 case Triple::mips64el:
788 case Triple::nvptx64:
790 case Triple::sparcv9:
795 case Triple::mips: T.setArch(Triple::mips64); break;
796 case Triple::mipsel: T.setArch(Triple::mips64el); break;
797 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
798 case Triple::ppc: T.setArch(Triple::ppc64); break;
799 case Triple::sparc: T.setArch(Triple::sparcv9); break;
800 case Triple::x86: T.setArch(Triple::x86_64); break;