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 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";
67 case r600: return "r600";
70 case sparc: return "sparc";
73 case x86_64: return "x86";
75 case xcore: return "xcore";
77 case ptx32: return "ptx";
78 case ptx64: return "ptx";
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";
93 llvm_unreachable("Invalid VendorType!");
96 const char *Triple::getOSTypeName(OSType Kind) {
98 case UnknownOS: return "unknown";
100 case AuroraUX: return "auroraux";
101 case Cygwin: return "cygwin";
102 case Darwin: return "darwin";
103 case DragonFly: return "dragonfly";
104 case FreeBSD: return "freebsd";
105 case IOS: return "ios";
106 case KFreeBSD: return "kfreebsd";
107 case Linux: return "linux";
108 case Lv2: return "lv2";
109 case MacOSX: return "macosx";
110 case MinGW32: return "mingw32";
111 case NetBSD: return "netbsd";
112 case OpenBSD: return "openbsd";
113 case Solaris: return "solaris";
114 case Win32: return "win32";
115 case Haiku: return "haiku";
116 case Minix: return "minix";
117 case RTEMS: return "rtems";
118 case NativeClient: return "nacl";
121 llvm_unreachable("Invalid OSType");
124 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
126 case UnknownEnvironment: return "unknown";
127 case GNU: return "gnu";
128 case GNUEABIHF: return "gnueabihf";
129 case GNUEABI: return "gnueabi";
130 case EABI: return "eabi";
131 case MachO: return "macho";
132 case ANDROIDEABI: return "androideabi";
135 llvm_unreachable("Invalid EnvironmentType!");
138 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
139 return StringSwitch<Triple::ArchType>(Name)
141 .Case("cellspu", cellspu)
143 .Case("mipsel", mipsel)
144 .Case("mips64", mips64)
145 .Case("mips64el", mips64el)
146 .Case("msp430", msp430)
147 .Case("ppc64", ppc64)
150 .Case("mblaze", mblaze)
152 .Case("hexagon", hexagon)
153 .Case("sparc", sparc)
154 .Case("sparcv9", sparcv9)
156 .Case("thumb", thumb)
158 .Case("x86-64", x86_64)
159 .Case("xcore", xcore)
160 .Case("ptx32", ptx32)
161 .Case("ptx64", ptx64)
163 .Case("amdil", amdil)
164 .Default(UnknownArch);
167 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
168 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
169 // archs which Darwin doesn't use.
171 // The matching this routine does is fairly pointless, since it is neither the
172 // complete architecture list, nor a reasonable subset. The problem is that
173 // historically the driver driver accepts this and also ties its -march=
174 // handling to the architecture name, so we need to be careful before removing
177 // This code must be kept in sync with Clang's Darwin specific argument
180 return StringSwitch<ArchType>(Str)
181 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
182 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
183 .Case("ppc64", Triple::ppc64)
184 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
185 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
187 .Case("x86_64", Triple::x86_64)
188 // This is derived from the driver driver.
189 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
190 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
191 .Case("r600", Triple::r600)
192 .Case("ptx32", Triple::ptx32)
193 .Case("ptx64", Triple::ptx64)
194 .Case("amdil", Triple::amdil)
195 .Default(Triple::UnknownArch);
198 // Returns architecture name that is understood by the target assembler.
199 const char *Triple::getArchNameForAssembler() {
200 if (!isOSDarwin() && getVendor() != Triple::Apple)
203 return StringSwitch<const char*>(getArchName())
204 .Case("i386", "i386")
205 .Case("x86_64", "x86_64")
206 .Case("powerpc", "ppc")
207 .Case("powerpc64", "ppc64")
208 .Cases("mblaze", "microblaze", "mblaze")
210 .Cases("armv4t", "thumbv4t", "armv4t")
211 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
212 .Cases("armv6", "thumbv6", "armv6")
213 .Cases("armv7", "thumbv7", "armv7")
214 .Case("r600", "r600")
215 .Case("ptx32", "ptx32")
216 .Case("ptx64", "ptx64")
217 .Case("le32", "le32")
218 .Case("amdil", "amdil")
222 static Triple::ArchType parseArch(StringRef ArchName) {
223 return StringSwitch<Triple::ArchType>(ArchName)
224 .Cases("i386", "i486", "i586", "i686", Triple::x86)
225 // FIXME: Do we need to support these?
226 .Cases("i786", "i886", "i986", Triple::x86)
227 .Cases("amd64", "x86_64", Triple::x86_64)
228 .Case("powerpc", Triple::ppc)
229 .Cases("powerpc64", "ppu", Triple::ppc64)
230 .Case("mblaze", Triple::mblaze)
231 .Cases("arm", "xscale", Triple::arm)
232 // FIXME: It would be good to replace these with explicit names for all the
233 // various suffixes supported.
234 .StartsWith("armv", Triple::arm)
235 .Case("thumb", Triple::thumb)
236 .StartsWith("thumbv", Triple::thumb)
237 .Cases("spu", "cellspu", Triple::cellspu)
238 .Case("msp430", Triple::msp430)
239 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
240 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
241 .Cases("mips64", "mips64eb", Triple::mips64)
242 .Case("mips64el", Triple::mips64el)
243 .Case("r600", Triple::r600)
244 .Case("hexagon", Triple::hexagon)
245 .Case("sparc", Triple::sparc)
246 .Case("sparcv9", Triple::sparcv9)
247 .Case("tce", Triple::tce)
248 .Case("xcore", Triple::xcore)
249 .Case("ptx32", Triple::ptx32)
250 .Case("ptx64", Triple::ptx64)
251 .Case("le32", Triple::le32)
252 .Case("amdil", Triple::amdil)
253 .Default(Triple::UnknownArch);
256 static Triple::VendorType parseVendor(StringRef VendorName) {
257 return StringSwitch<Triple::VendorType>(VendorName)
258 .Case("apple", Triple::Apple)
259 .Case("pc", Triple::PC)
260 .Case("scei", Triple::SCEI)
261 .Default(Triple::UnknownVendor);
264 static Triple::OSType parseOS(StringRef OSName) {
265 return StringSwitch<Triple::OSType>(OSName)
266 .StartsWith("auroraux", Triple::AuroraUX)
267 .StartsWith("cygwin", Triple::Cygwin)
268 .StartsWith("darwin", Triple::Darwin)
269 .StartsWith("dragonfly", Triple::DragonFly)
270 .StartsWith("freebsd", Triple::FreeBSD)
271 .StartsWith("ios", Triple::IOS)
272 .StartsWith("kfreebsd", Triple::KFreeBSD)
273 .StartsWith("linux", Triple::Linux)
274 .StartsWith("lv2", Triple::Lv2)
275 .StartsWith("macosx", Triple::MacOSX)
276 .StartsWith("mingw32", Triple::MinGW32)
277 .StartsWith("netbsd", Triple::NetBSD)
278 .StartsWith("openbsd", Triple::OpenBSD)
279 .StartsWith("solaris", Triple::Solaris)
280 .StartsWith("win32", Triple::Win32)
281 .StartsWith("haiku", Triple::Haiku)
282 .StartsWith("minix", Triple::Minix)
283 .StartsWith("rtems", Triple::RTEMS)
284 .StartsWith("nacl", Triple::NativeClient)
285 .Default(Triple::UnknownOS);
288 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
289 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
290 .StartsWith("eabi", Triple::EABI)
291 .StartsWith("gnueabihf", Triple::GNUEABIHF)
292 .StartsWith("gnueabi", Triple::GNUEABI)
293 .StartsWith("gnu", Triple::GNU)
294 .StartsWith("macho", Triple::MachO)
295 .StartsWith("androideabi", Triple::ANDROIDEABI)
296 .Default(Triple::UnknownEnvironment);
299 /// \brief Construct a triple from the string representation provided.
301 /// This stores the string representation and parses the various pieces into
303 Triple::Triple(const Twine &Str)
305 Arch(parseArch(getArchName())),
306 Vendor(parseVendor(getVendorName())),
307 OS(parseOS(getOSName())),
308 Environment(parseEnvironment(getEnvironmentName())) {
311 /// \brief Construct a triple from string representations of the architecture,
314 /// This joins each argument into a canonical string representation and parses
315 /// them into enum members. It leaves the environment unknown and omits it from
316 /// the string representation.
317 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
318 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
319 Arch(parseArch(ArchStr.str())),
320 Vendor(parseVendor(VendorStr.str())),
321 OS(parseOS(OSStr.str())),
325 /// \brief Construct a triple from string representations of the architecture,
326 /// vendor, OS, and environment.
328 /// This joins each argument into a canonical string representation and parses
329 /// them into enum members.
330 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
331 const Twine &EnvironmentStr)
332 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
333 EnvironmentStr).str()),
334 Arch(parseArch(ArchStr.str())),
335 Vendor(parseVendor(VendorStr.str())),
336 OS(parseOS(OSStr.str())),
337 Environment(parseEnvironment(EnvironmentStr.str())) {
340 std::string Triple::normalize(StringRef Str) {
341 // Parse into components.
342 SmallVector<StringRef, 4> Components;
343 Str.split(Components, "-");
345 // If the first component corresponds to a known architecture, preferentially
346 // use it for the architecture. If the second component corresponds to a
347 // known vendor, preferentially use it for the vendor, etc. This avoids silly
348 // component movement when a component parses as (eg) both a valid arch and a
350 ArchType Arch = UnknownArch;
351 if (Components.size() > 0)
352 Arch = parseArch(Components[0]);
353 VendorType Vendor = UnknownVendor;
354 if (Components.size() > 1)
355 Vendor = parseVendor(Components[1]);
356 OSType OS = UnknownOS;
357 if (Components.size() > 2)
358 OS = parseOS(Components[2]);
359 EnvironmentType Environment = UnknownEnvironment;
360 if (Components.size() > 3)
361 Environment = parseEnvironment(Components[3]);
363 // Note which components are already in their final position. These will not
366 Found[0] = Arch != UnknownArch;
367 Found[1] = Vendor != UnknownVendor;
368 Found[2] = OS != UnknownOS;
369 Found[3] = Environment != UnknownEnvironment;
371 // If they are not there already, permute the components into their canonical
372 // positions by seeing if they parse as a valid architecture, and if so moving
373 // the component to the architecture position etc.
374 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
376 continue; // Already in the canonical position.
378 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
379 // Do not reparse any components that already matched.
380 if (Idx < array_lengthof(Found) && Found[Idx])
383 // Does this component parse as valid for the target position?
385 StringRef Comp = Components[Idx];
387 default: llvm_unreachable("unexpected component type!");
389 Arch = parseArch(Comp);
390 Valid = Arch != UnknownArch;
393 Vendor = parseVendor(Comp);
394 Valid = Vendor != UnknownVendor;
398 Valid = OS != UnknownOS;
401 Environment = parseEnvironment(Comp);
402 Valid = Environment != UnknownEnvironment;
406 continue; // Nope, try the next component.
408 // Move the component to the target position, pushing any non-fixed
409 // components that are in the way to the right. This tends to give
410 // good results in the common cases of a forgotten vendor component
411 // or a wrongly positioned environment.
413 // Insert left, pushing the existing components to the right. For
414 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
415 StringRef CurrentComponent(""); // The empty component.
416 // Replace the component we are moving with an empty component.
417 std::swap(CurrentComponent, Components[Idx]);
418 // Insert the component being moved at Pos, displacing any existing
419 // components to the right.
420 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
421 // Skip over any fixed components.
422 while (i < array_lengthof(Found) && Found[i])
424 // Place the component at the new position, getting the component
425 // that was at this position - it will be moved right.
426 std::swap(CurrentComponent, Components[i]);
428 } else if (Pos > Idx) {
429 // Push right by inserting empty components until the component at Idx
430 // reaches the target position Pos. For example, pc-a -> -pc-a when
431 // moving pc to the second position.
433 // Insert one empty component at Idx.
434 StringRef CurrentComponent(""); // The empty component.
435 for (unsigned i = Idx; i < Components.size();) {
436 // Place the component at the new position, getting the component
437 // that was at this position - it will be moved right.
438 std::swap(CurrentComponent, Components[i]);
439 // If it was placed on top of an empty component then we are done.
440 if (CurrentComponent.empty())
442 // Advance to the next component, skipping any fixed components.
443 while (++i < array_lengthof(Found) && Found[i])
446 // The last component was pushed off the end - append it.
447 if (!CurrentComponent.empty())
448 Components.push_back(CurrentComponent);
450 // Advance Idx to the component's new position.
451 while (++Idx < array_lengthof(Found) && Found[Idx])
453 } while (Idx < Pos); // Add more until the final position is reached.
455 assert(Pos < Components.size() && Components[Pos] == Comp &&
456 "Component moved wrong!");
462 // Special case logic goes here. At this point Arch, Vendor and OS have the
463 // correct values for the computed components.
465 // Stick the corrected components back together to form the normalized string.
466 std::string Normalized;
467 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
468 if (i) Normalized += '-';
469 Normalized += Components[i];
474 StringRef Triple::getArchName() const {
475 return StringRef(Data).split('-').first; // Isolate first component
478 StringRef Triple::getVendorName() const {
479 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
480 return Tmp.split('-').first; // Isolate second component
483 StringRef Triple::getOSName() const {
484 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
485 Tmp = Tmp.split('-').second; // Strip second component
486 return Tmp.split('-').first; // Isolate third component
489 StringRef Triple::getEnvironmentName() const {
490 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
491 Tmp = Tmp.split('-').second; // Strip second component
492 return Tmp.split('-').second; // Strip third component
495 StringRef Triple::getOSAndEnvironmentName() const {
496 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
497 return Tmp.split('-').second; // Strip second component
500 static unsigned EatNumber(StringRef &Str) {
501 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
505 // Consume the leading digit.
506 Result = Result*10 + (Str[0] - '0');
510 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
515 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
516 unsigned &Micro) const {
517 StringRef OSName = getOSName();
519 // Assume that the OS portion of the triple starts with the canonical name.
520 StringRef OSTypeName = getOSTypeName(getOS());
521 if (OSName.startswith(OSTypeName))
522 OSName = OSName.substr(OSTypeName.size());
524 // Any unset version defaults to 0.
525 Major = Minor = Micro = 0;
527 // Parse up to three components.
528 unsigned *Components[3] = { &Major, &Minor, &Micro };
529 for (unsigned i = 0; i != 3; ++i) {
530 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
533 // Consume the leading number.
534 *Components[i] = EatNumber(OSName);
536 // Consume the separator, if present.
537 if (OSName.startswith("."))
538 OSName = OSName.substr(1);
542 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
543 unsigned &Micro) const {
544 getOSVersion(Major, Minor, Micro);
547 default: llvm_unreachable("unexpected OS for Darwin triple");
549 // Default to darwin8, i.e., MacOSX 10.4.
552 // Darwin version numbers are skewed from OS X versions.
569 // Ignore the version from the triple. This is only handled because the
570 // the clang driver combines OS X and IOS support into a common Darwin
571 // toolchain that wants to know the OS X version number even when targeting
581 void Triple::setTriple(const Twine &Str) {
585 void Triple::setArch(ArchType Kind) {
586 setArchName(getArchTypeName(Kind));
589 void Triple::setVendor(VendorType Kind) {
590 setVendorName(getVendorTypeName(Kind));
593 void Triple::setOS(OSType Kind) {
594 setOSName(getOSTypeName(Kind));
597 void Triple::setEnvironment(EnvironmentType Kind) {
598 setEnvironmentName(getEnvironmentTypeName(Kind));
601 void Triple::setArchName(StringRef Str) {
602 // Work around a miscompilation bug for Twines in gcc 4.0.3.
603 SmallString<64> Triple;
606 Triple += getVendorName();
608 Triple += getOSAndEnvironmentName();
609 setTriple(Triple.str());
612 void Triple::setVendorName(StringRef Str) {
613 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
616 void Triple::setOSName(StringRef Str) {
617 if (hasEnvironment())
618 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
619 "-" + getEnvironmentName());
621 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
624 void Triple::setEnvironmentName(StringRef Str) {
625 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
629 void Triple::setOSAndEnvironmentName(StringRef Str) {
630 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
633 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
635 case llvm::Triple::UnknownArch:
638 case llvm::Triple::msp430:
641 case llvm::Triple::amdil:
642 case llvm::Triple::arm:
643 case llvm::Triple::cellspu:
644 case llvm::Triple::hexagon:
645 case llvm::Triple::le32:
646 case llvm::Triple::mblaze:
647 case llvm::Triple::mips:
648 case llvm::Triple::mipsel:
649 case llvm::Triple::ppc:
650 case llvm::Triple::ptx32:
651 case llvm::Triple::r600:
652 case llvm::Triple::sparc:
653 case llvm::Triple::tce:
654 case llvm::Triple::thumb:
655 case llvm::Triple::x86:
656 case llvm::Triple::xcore:
659 case llvm::Triple::mips64:
660 case llvm::Triple::mips64el:
661 case llvm::Triple::ppc64:
662 case llvm::Triple::ptx64:
663 case llvm::Triple::sparcv9:
664 case llvm::Triple::x86_64:
667 llvm_unreachable("Invalid architecture value");
670 bool Triple::isArch64Bit() const {
671 return getArchPointerBitWidth(getArch()) == 64;
674 bool Triple::isArch32Bit() const {
675 return getArchPointerBitWidth(getArch()) == 32;
678 bool Triple::isArch16Bit() const {
679 return getArchPointerBitWidth(getArch()) == 16;
682 Triple Triple::get32BitArchVariant() const {
685 case Triple::UnknownArch:
687 T.setArch(UnknownArch);
692 case Triple::cellspu:
693 case Triple::hexagon:
709 case Triple::mips64: T.setArch(Triple::mips); break;
710 case Triple::mips64el: T.setArch(Triple::mipsel); break;
711 case Triple::ppc64: T.setArch(Triple::ppc); break;
712 case Triple::ptx64: T.setArch(Triple::ptx32); break;
713 case Triple::sparcv9: T.setArch(Triple::sparc); break;
714 case Triple::x86_64: T.setArch(Triple::x86); break;
719 Triple Triple::get64BitArchVariant() const {
722 case Triple::UnknownArch:
725 case Triple::cellspu:
726 case Triple::hexagon:
734 T.setArch(UnknownArch);
738 case Triple::mips64el:
741 case Triple::sparcv9:
746 case Triple::mips: T.setArch(Triple::mips64); break;
747 case Triple::mipsel: T.setArch(Triple::mips64el); break;
748 case Triple::ppc: T.setArch(Triple::ppc64); break;
749 case Triple::ptx32: T.setArch(Triple::ptx64); break;
750 case Triple::sparc: T.setArch(Triple::sparcv9); break;
751 case Triple::x86: T.setArch(Triple::x86_64); break;