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 return StringSwitch<ArchType>(ArchName)
222 .Cases("i386", "i486", "i586", "i686", x86)
223 .Cases("i786", "i886", "i986", x86) // FIXME: Do we need to support these?
224 .Cases("amd64", "x86_64", x86_64)
225 .Case("powerpc", ppc)
226 .Cases("powerpc64", "ppu", ppc64)
227 .Case("mblaze", mblaze)
228 .Cases("arm", "xscale", arm)
229 // FIXME: It would be good to replace these with explicit names for all the
230 // various suffixes supported.
231 .StartsWith("armv", arm)
232 .Case("thumb", thumb)
233 .StartsWith("thumbv", thumb)
234 .Cases("spu", "cellspu", cellspu)
235 .Case("msp430", msp430)
236 .Cases("mips", "mipseb", "mipsallegrex", mips)
237 .Cases("mipsel", "mipsallegrexel", "psp", mipsel)
238 .Cases("mips64", "mips64eb", mips64)
239 .Case("mips64el", mips64el)
240 .Case("hexagon", hexagon)
241 .Case("sparc", sparc)
242 .Case("sparcv9", sparcv9)
244 .Case("xcore", xcore)
245 .Case("ptx32", ptx32)
246 .Case("ptx64", ptx64)
248 .Case("amdil", amdil)
249 .Default(UnknownArch);
252 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
253 return StringSwitch<VendorType>(VendorName)
254 .Case("apple", Apple)
257 .Default(UnknownVendor);
260 Triple::OSType Triple::ParseOS(StringRef OSName) {
261 return StringSwitch<OSType>(OSName)
262 .StartsWith("auroraux", AuroraUX)
263 .StartsWith("cygwin", Cygwin)
264 .StartsWith("darwin", Darwin)
265 .StartsWith("dragonfly", DragonFly)
266 .StartsWith("freebsd", FreeBSD)
267 .StartsWith("ios", IOS)
268 .StartsWith("kfreebsd", KFreeBSD)
269 .StartsWith("linux", Linux)
270 .StartsWith("lv2", Lv2)
271 .StartsWith("macosx", MacOSX)
272 .StartsWith("mingw32", MinGW32)
273 .StartsWith("netbsd", NetBSD)
274 .StartsWith("openbsd", OpenBSD)
275 .StartsWith("psp", Psp)
276 .StartsWith("solaris", Solaris)
277 .StartsWith("win32", Win32)
278 .StartsWith("haiku", Haiku)
279 .StartsWith("minix", Minix)
280 .StartsWith("rtems", RTEMS)
281 .StartsWith("nacl", NativeClient)
285 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
286 return StringSwitch<EnvironmentType>(EnvironmentName)
287 .StartsWith("eabi", EABI)
288 .StartsWith("gnueabihf", GNUEABIHF)
289 .StartsWith("gnueabi", GNUEABI)
290 .StartsWith("gnu", GNU)
291 .StartsWith("macho", MachO)
292 .StartsWith("androideabi", ANDROIDEABI)
293 .Default(UnknownEnvironment);
296 void Triple::Parse() const {
297 assert(!isInitialized() && "Invalid parse call.");
299 Arch = ParseArch(getArchName());
300 Vendor = ParseVendor(getVendorName());
301 OS = ParseOS(getOSName());
302 Environment = ParseEnvironment(getEnvironmentName());
304 assert(isInitialized() && "Failed to initialize!");
307 std::string Triple::normalize(StringRef Str) {
308 // Parse into components.
309 SmallVector<StringRef, 4> Components;
310 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
311 Last = Str.find('-', First);
312 Components.push_back(Str.slice(First, Last));
315 // If the first component corresponds to a known architecture, preferentially
316 // use it for the architecture. If the second component corresponds to a
317 // known vendor, preferentially use it for the vendor, etc. This avoids silly
318 // component movement when a component parses as (eg) both a valid arch and a
320 ArchType Arch = UnknownArch;
321 if (Components.size() > 0)
322 Arch = ParseArch(Components[0]);
323 VendorType Vendor = UnknownVendor;
324 if (Components.size() > 1)
325 Vendor = ParseVendor(Components[1]);
326 OSType OS = UnknownOS;
327 if (Components.size() > 2)
328 OS = ParseOS(Components[2]);
329 EnvironmentType Environment = UnknownEnvironment;
330 if (Components.size() > 3)
331 Environment = ParseEnvironment(Components[3]);
333 // Note which components are already in their final position. These will not
336 Found[0] = Arch != UnknownArch;
337 Found[1] = Vendor != UnknownVendor;
338 Found[2] = OS != UnknownOS;
339 Found[3] = Environment != UnknownEnvironment;
341 // If they are not there already, permute the components into their canonical
342 // positions by seeing if they parse as a valid architecture, and if so moving
343 // the component to the architecture position etc.
344 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
346 continue; // Already in the canonical position.
348 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
349 // Do not reparse any components that already matched.
350 if (Idx < array_lengthof(Found) && Found[Idx])
353 // Does this component parse as valid for the target position?
355 StringRef Comp = Components[Idx];
357 default: llvm_unreachable("unexpected component type!");
359 Arch = ParseArch(Comp);
360 Valid = Arch != UnknownArch;
363 Vendor = ParseVendor(Comp);
364 Valid = Vendor != UnknownVendor;
368 Valid = OS != UnknownOS;
371 Environment = ParseEnvironment(Comp);
372 Valid = Environment != UnknownEnvironment;
376 continue; // Nope, try the next component.
378 // Move the component to the target position, pushing any non-fixed
379 // components that are in the way to the right. This tends to give
380 // good results in the common cases of a forgotten vendor component
381 // or a wrongly positioned environment.
383 // Insert left, pushing the existing components to the right. For
384 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
385 StringRef CurrentComponent(""); // The empty component.
386 // Replace the component we are moving with an empty component.
387 std::swap(CurrentComponent, Components[Idx]);
388 // Insert the component being moved at Pos, displacing any existing
389 // components to the right.
390 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
391 // Skip over any fixed components.
392 while (i < array_lengthof(Found) && Found[i]) ++i;
393 // Place the component at the new position, getting the component
394 // that was at this position - it will be moved right.
395 std::swap(CurrentComponent, Components[i]);
397 } else if (Pos > Idx) {
398 // Push right by inserting empty components until the component at Idx
399 // reaches the target position Pos. For example, pc-a -> -pc-a when
400 // moving pc to the second position.
402 // Insert one empty component at Idx.
403 StringRef CurrentComponent(""); // The empty component.
404 for (unsigned i = Idx; i < Components.size();) {
405 // Place the component at the new position, getting the component
406 // that was at this position - it will be moved right.
407 std::swap(CurrentComponent, Components[i]);
408 // If it was placed on top of an empty component then we are done.
409 if (CurrentComponent.empty())
411 // Advance to the next component, skipping any fixed components.
412 while (++i < array_lengthof(Found) && Found[i])
415 // The last component was pushed off the end - append it.
416 if (!CurrentComponent.empty())
417 Components.push_back(CurrentComponent);
419 // Advance Idx to the component's new position.
420 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
421 } while (Idx < Pos); // Add more until the final position is reached.
423 assert(Pos < Components.size() && Components[Pos] == Comp &&
424 "Component moved wrong!");
430 // Special case logic goes here. At this point Arch, Vendor and OS have the
431 // correct values for the computed components.
433 // Stick the corrected components back together to form the normalized string.
434 std::string Normalized;
435 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
436 if (i) Normalized += '-';
437 Normalized += Components[i];
442 StringRef Triple::getArchName() const {
443 return StringRef(Data).split('-').first; // Isolate first component
446 StringRef Triple::getVendorName() const {
447 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
448 return Tmp.split('-').first; // Isolate second component
451 StringRef Triple::getOSName() const {
452 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
453 Tmp = Tmp.split('-').second; // Strip second component
454 return Tmp.split('-').first; // Isolate third component
457 StringRef Triple::getEnvironmentName() const {
458 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
459 Tmp = Tmp.split('-').second; // Strip second component
460 return Tmp.split('-').second; // Strip third component
463 StringRef Triple::getOSAndEnvironmentName() const {
464 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
465 return Tmp.split('-').second; // Strip second component
468 static unsigned EatNumber(StringRef &Str) {
469 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
473 // Consume the leading digit.
474 Result = Result*10 + (Str[0] - '0');
478 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
483 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
484 unsigned &Micro) const {
485 StringRef OSName = getOSName();
487 // Assume that the OS portion of the triple starts with the canonical name.
488 StringRef OSTypeName = getOSTypeName(getOS());
489 if (OSName.startswith(OSTypeName))
490 OSName = OSName.substr(OSTypeName.size());
492 // Any unset version defaults to 0.
493 Major = Minor = Micro = 0;
495 // Parse up to three components.
496 unsigned *Components[3] = { &Major, &Minor, &Micro };
497 for (unsigned i = 0; i != 3; ++i) {
498 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
501 // Consume the leading number.
502 *Components[i] = EatNumber(OSName);
504 // Consume the separator, if present.
505 if (OSName.startswith("."))
506 OSName = OSName.substr(1);
510 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
511 unsigned &Micro) const {
512 getOSVersion(Major, Minor, Micro);
515 default: llvm_unreachable("unexpected OS for Darwin triple");
517 // Default to darwin8, i.e., MacOSX 10.4.
520 // Darwin version numbers are skewed from OS X versions.
537 // Ignore the version from the triple. This is only handled because the
538 // the clang driver combines OS X and IOS support into a common Darwin
539 // toolchain that wants to know the OS X version number even when targeting
549 void Triple::setTriple(const Twine &Str) {
554 void Triple::setArch(ArchType Kind) {
555 setArchName(getArchTypeName(Kind));
558 void Triple::setVendor(VendorType Kind) {
559 setVendorName(getVendorTypeName(Kind));
562 void Triple::setOS(OSType Kind) {
563 setOSName(getOSTypeName(Kind));
566 void Triple::setEnvironment(EnvironmentType Kind) {
567 setEnvironmentName(getEnvironmentTypeName(Kind));
570 void Triple::setArchName(StringRef Str) {
571 // Work around a miscompilation bug for Twines in gcc 4.0.3.
572 SmallString<64> Triple;
575 Triple += getVendorName();
577 Triple += getOSAndEnvironmentName();
578 setTriple(Triple.str());
581 void Triple::setVendorName(StringRef Str) {
582 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
585 void Triple::setOSName(StringRef Str) {
586 if (hasEnvironment())
587 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
588 "-" + getEnvironmentName());
590 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
593 void Triple::setEnvironmentName(StringRef Str) {
594 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
598 void Triple::setOSAndEnvironmentName(StringRef Str) {
599 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
602 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
604 case llvm::Triple::UnknownArch:
605 case llvm::Triple::InvalidArch:
608 case llvm::Triple::msp430:
611 case llvm::Triple::amdil:
612 case llvm::Triple::arm:
613 case llvm::Triple::cellspu:
614 case llvm::Triple::hexagon:
615 case llvm::Triple::le32:
616 case llvm::Triple::mblaze:
617 case llvm::Triple::mips:
618 case llvm::Triple::mipsel:
619 case llvm::Triple::ppc:
620 case llvm::Triple::ptx32:
621 case llvm::Triple::sparc:
622 case llvm::Triple::tce:
623 case llvm::Triple::thumb:
624 case llvm::Triple::x86:
625 case llvm::Triple::xcore:
628 case llvm::Triple::mips64:
629 case llvm::Triple::mips64el:
630 case llvm::Triple::ppc64:
631 case llvm::Triple::ptx64:
632 case llvm::Triple::sparcv9:
633 case llvm::Triple::x86_64:
636 llvm_unreachable("Invalid architecture value");
639 bool Triple::isArch64Bit() const {
640 return getArchPointerBitWidth(getArch()) == 64;
643 bool Triple::isArch32Bit() const {
644 return getArchPointerBitWidth(getArch()) == 32;
647 bool Triple::isArch16Bit() const {
648 return getArchPointerBitWidth(getArch()) == 16;
651 Triple Triple::get32BitArchVariant() const {
654 case Triple::UnknownArch:
655 case Triple::InvalidArch:
657 T.setArch(UnknownArch);
662 case Triple::cellspu:
663 case Triple::hexagon:
678 case Triple::mips64: T.setArch(Triple::mips); break;
679 case Triple::mips64el: T.setArch(Triple::mipsel); break;
680 case Triple::ppc64: T.setArch(Triple::ppc); break;
681 case Triple::ptx64: T.setArch(Triple::ptx32); break;
682 case Triple::sparcv9: T.setArch(Triple::sparc); break;
683 case Triple::x86_64: T.setArch(Triple::x86); break;
688 Triple Triple::get64BitArchVariant() const {
691 case Triple::InvalidArch:
692 case Triple::UnknownArch:
695 case Triple::cellspu:
696 case Triple::hexagon:
703 T.setArch(UnknownArch);
707 case Triple::mips64el:
710 case Triple::sparcv9:
715 case Triple::mips: T.setArch(Triple::mips64); break;
716 case Triple::mipsel: T.setArch(Triple::mips64el); break;
717 case Triple::ppc: T.setArch(Triple::ppc64); break;
718 case Triple::ptx32: T.setArch(Triple::ptx64); break;
719 case Triple::sparc: T.setArch(Triple::sparcv9); break;
720 case Triple::x86: T.setArch(Triple::x86_64); break;