1947f43aa11a3bc905967d7ada3fa0fd0e98c5c3
[oota-llvm.git] / lib / Support / Triple.cpp
1 //===--- Triple.cpp - Target triple helper class --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include <cstring>
16 using namespace llvm;
17
18 const char *Triple::getArchTypeName(ArchType Kind) {
19   switch (Kind) {
20   case UnknownArch: return "unknown";
21
22   case aarch64: return "aarch64";
23   case arm:     return "arm";
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 ppc64le: return "powerpc64le";
32   case ppc:     return "powerpc";
33   case r600:    return "r600";
34   case sparc:   return "sparc";
35   case sparcv9: return "sparcv9";
36   case systemz: return "s390x";
37   case tce:     return "tce";
38   case thumb:   return "thumb";
39   case x86:     return "i386";
40   case x86_64:  return "x86_64";
41   case xcore:   return "xcore";
42   case nvptx:   return "nvptx";
43   case nvptx64: return "nvptx64";
44   case le32:    return "le32";
45   case amdil:   return "amdil";
46   case spir:    return "spir";
47   case spir64:  return "spir64";
48   }
49
50   llvm_unreachable("Invalid ArchType!");
51 }
52
53 const char *Triple::getArchTypePrefix(ArchType Kind) {
54   switch (Kind) {
55   default:
56     return 0;
57
58   case aarch64: return "aarch64";
59
60   case arm:
61   case thumb:   return "arm";
62
63   case ppc64:
64   case ppc64le:
65   case ppc:     return "ppc";
66
67   case mips:
68   case mipsel:
69   case mips64:
70   case mips64el:return "mips";
71
72   case hexagon: return "hexagon";
73
74   case r600:    return "r600";
75
76   case sparcv9:
77   case sparc:   return "sparc";
78
79   case systemz: return "systemz";
80
81   case x86:
82   case x86_64:  return "x86";
83
84   case xcore:   return "xcore";
85
86   case nvptx:   return "nvptx";
87   case nvptx64: return "nvptx";
88   case le32:    return "le32";
89   case amdil:   return "amdil";
90   case spir:    return "spir";
91   case spir64:  return "spir";
92   }
93 }
94
95 const char *Triple::getVendorTypeName(VendorType Kind) {
96   switch (Kind) {
97   case UnknownVendor: return "unknown";
98
99   case Apple: return "apple";
100   case PC: return "pc";
101   case SCEI: return "scei";
102   case BGP: return "bgp";
103   case BGQ: return "bgq";
104   case Freescale: return "fsl";
105   case IBM: return "ibm";
106   case NVIDIA: return "nvidia";
107   }
108
109   llvm_unreachable("Invalid VendorType!");
110 }
111
112 const char *Triple::getOSTypeName(OSType Kind) {
113   switch (Kind) {
114   case UnknownOS: return "unknown";
115
116   case AuroraUX: return "auroraux";
117   case Cygwin: return "cygwin";
118   case Darwin: return "darwin";
119   case DragonFly: return "dragonfly";
120   case FreeBSD: return "freebsd";
121   case IOS: return "ios";
122   case KFreeBSD: return "kfreebsd";
123   case Linux: return "linux";
124   case Lv2: return "lv2";
125   case MacOSX: return "macosx";
126   case MinGW32: return "mingw32";
127   case NetBSD: return "netbsd";
128   case OpenBSD: return "openbsd";
129   case Solaris: return "solaris";
130   case Win32: return "win32";
131   case Haiku: return "haiku";
132   case Minix: return "minix";
133   case RTEMS: return "rtems";
134   case NaCl: return "nacl";
135   case CNK: return "cnk";
136   case Bitrig: return "bitrig";
137   case AIX: return "aix";
138   case CUDA: return "cuda";
139   case NVCL: return "nvcl";
140   }
141
142   llvm_unreachable("Invalid OSType");
143 }
144
145 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
146   switch (Kind) {
147   case UnknownEnvironment: return "unknown";
148   case GNU: return "gnu";
149   case GNUEABIHF: return "gnueabihf";
150   case GNUEABI: return "gnueabi";
151   case GNUX32: return "gnux32";
152   case CODE16: return "code16";
153   case EABI: return "eabi";
154   case EABIHF: return "eabihf";
155   case MachO: return "macho";
156   case Android: return "android";
157   case ELF: return "elf";
158   }
159
160   llvm_unreachable("Invalid EnvironmentType!");
161 }
162
163 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
164   return StringSwitch<Triple::ArchType>(Name)
165     .Case("aarch64", aarch64)
166     .Case("arm", arm)
167     .Case("mips", mips)
168     .Case("mipsel", mipsel)
169     .Case("mips64", mips64)
170     .Case("mips64el", mips64el)
171     .Case("msp430", msp430)
172     .Case("ppc64", ppc64)
173     .Case("ppc32", ppc)
174     .Case("ppc", ppc)
175     .Case("ppc64le", ppc64le)
176     .Case("r600", r600)
177     .Case("hexagon", hexagon)
178     .Case("sparc", sparc)
179     .Case("sparcv9", sparcv9)
180     .Case("systemz", systemz)
181     .Case("tce", tce)
182     .Case("thumb", thumb)
183     .Case("x86", x86)
184     .Case("x86-64", x86_64)
185     .Case("xcore", xcore)
186     .Case("nvptx", nvptx)
187     .Case("nvptx64", nvptx64)
188     .Case("le32", le32)
189     .Case("amdil", amdil)
190     .Case("spir", spir)
191     .Case("spir64", spir64)
192     .Default(UnknownArch);
193 }
194
195 // Returns architecture name that is understood by the target assembler.
196 const char *Triple::getArchNameForAssembler() {
197   if (!isOSDarwin() && getVendor() != Triple::Apple)
198     return NULL;
199
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     .Case("powerpc64le", "ppc64le")
206     .Case("arm", "arm")
207     .Cases("armv4t", "thumbv4t", "armv4t")
208     .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
209     .Cases("armv6", "thumbv6", "armv6")
210     .Cases("armv7", "thumbv7", "armv7")
211     .Case("r600", "r600")
212     .Case("nvptx", "nvptx")
213     .Case("nvptx64", "nvptx64")
214     .Case("le32", "le32")
215     .Case("amdil", "amdil")
216     .Case("spir", "spir")
217     .Case("spir64", "spir64")
218     .Default(NULL);
219 }
220
221 static Triple::ArchType parseArch(StringRef ArchName) {
222   return StringSwitch<Triple::ArchType>(ArchName)
223     .Cases("i386", "i486", "i586", "i686", Triple::x86)
224     // FIXME: Do we need to support these?
225     .Cases("i786", "i886", "i986", Triple::x86)
226     .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
227     .Case("powerpc", Triple::ppc)
228     .Cases("powerpc64", "ppu", Triple::ppc64)
229     .Case("powerpc64le", Triple::ppc64le)
230     .Case("aarch64", Triple::aarch64)
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     .Case("msp430", Triple::msp430)
238     .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
239     .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
240     .Cases("mips64", "mips64eb", Triple::mips64)
241     .Case("mips64el", Triple::mips64el)
242     .Case("r600", Triple::r600)
243     .Case("hexagon", Triple::hexagon)
244     .Case("s390x", Triple::systemz)
245     .Case("sparc", Triple::sparc)
246     .Cases("sparcv9", "sparc64", Triple::sparcv9)
247     .Case("tce", Triple::tce)
248     .Case("xcore", Triple::xcore)
249     .Case("nvptx", Triple::nvptx)
250     .Case("nvptx64", Triple::nvptx64)
251     .Case("le32", Triple::le32)
252     .Case("amdil", Triple::amdil)
253     .Case("spir", Triple::spir)
254     .Case("spir64", Triple::spir64)
255     .Default(Triple::UnknownArch);
256 }
257
258 static Triple::VendorType parseVendor(StringRef VendorName) {
259   return StringSwitch<Triple::VendorType>(VendorName)
260     .Case("apple", Triple::Apple)
261     .Case("pc", Triple::PC)
262     .Case("scei", Triple::SCEI)
263     .Case("bgp", Triple::BGP)
264     .Case("bgq", Triple::BGQ)
265     .Case("fsl", Triple::Freescale)
266     .Case("ibm", Triple::IBM)
267     .Case("nvidia", Triple::NVIDIA)
268     .Default(Triple::UnknownVendor);
269 }
270
271 static Triple::OSType parseOS(StringRef OSName) {
272   return StringSwitch<Triple::OSType>(OSName)
273     .StartsWith("auroraux", Triple::AuroraUX)
274     .StartsWith("cygwin", Triple::Cygwin)
275     .StartsWith("darwin", Triple::Darwin)
276     .StartsWith("dragonfly", Triple::DragonFly)
277     .StartsWith("freebsd", Triple::FreeBSD)
278     .StartsWith("ios", Triple::IOS)
279     .StartsWith("kfreebsd", Triple::KFreeBSD)
280     .StartsWith("linux", Triple::Linux)
281     .StartsWith("lv2", Triple::Lv2)
282     .StartsWith("macosx", Triple::MacOSX)
283     .StartsWith("mingw32", Triple::MinGW32)
284     .StartsWith("netbsd", Triple::NetBSD)
285     .StartsWith("openbsd", Triple::OpenBSD)
286     .StartsWith("solaris", Triple::Solaris)
287     .StartsWith("win32", Triple::Win32)
288     .StartsWith("haiku", Triple::Haiku)
289     .StartsWith("minix", Triple::Minix)
290     .StartsWith("rtems", Triple::RTEMS)
291     .StartsWith("nacl", Triple::NaCl)
292     .StartsWith("cnk", Triple::CNK)
293     .StartsWith("bitrig", Triple::Bitrig)
294     .StartsWith("aix", Triple::AIX)
295     .StartsWith("cuda", Triple::CUDA)
296     .StartsWith("nvcl", Triple::NVCL)
297     .Default(Triple::UnknownOS);
298 }
299
300 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
301   return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
302     .StartsWith("eabihf", Triple::EABIHF)
303     .StartsWith("eabi", Triple::EABI)
304     .StartsWith("gnueabihf", Triple::GNUEABIHF)
305     .StartsWith("gnueabi", Triple::GNUEABI)
306     .StartsWith("gnux32", Triple::GNUX32)
307     .StartsWith("code16", Triple::CODE16)
308     .StartsWith("gnu", Triple::GNU)
309     .StartsWith("macho", Triple::MachO)
310     .StartsWith("android", Triple::Android)
311     .StartsWith("elf", Triple::ELF)
312     .Default(Triple::UnknownEnvironment);
313 }
314
315 /// \brief Construct a triple from the string representation provided.
316 ///
317 /// This stores the string representation and parses the various pieces into
318 /// enum members.
319 Triple::Triple(const Twine &Str)
320     : Data(Str.str()),
321       Arch(parseArch(getArchName())),
322       Vendor(parseVendor(getVendorName())),
323       OS(parseOS(getOSName())),
324       Environment(parseEnvironment(getEnvironmentName())) {
325 }
326
327 /// \brief Construct a triple from string representations of the architecture,
328 /// vendor, and OS.
329 ///
330 /// This joins each argument into a canonical string representation and parses
331 /// them into enum members. It leaves the environment unknown and omits it from
332 /// the string representation.
333 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
334     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
335       Arch(parseArch(ArchStr.str())),
336       Vendor(parseVendor(VendorStr.str())),
337       OS(parseOS(OSStr.str())),
338       Environment() {
339 }
340
341 /// \brief Construct a triple from string representations of the architecture,
342 /// vendor, OS, and environment.
343 ///
344 /// This joins each argument into a canonical string representation and parses
345 /// them into enum members.
346 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
347                const Twine &EnvironmentStr)
348     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
349             EnvironmentStr).str()),
350       Arch(parseArch(ArchStr.str())),
351       Vendor(parseVendor(VendorStr.str())),
352       OS(parseOS(OSStr.str())),
353       Environment(parseEnvironment(EnvironmentStr.str())) {
354 }
355
356 std::string Triple::normalize(StringRef Str) {
357   // Parse into components.
358   SmallVector<StringRef, 4> Components;
359   Str.split(Components, "-");
360
361   // If the first component corresponds to a known architecture, preferentially
362   // use it for the architecture.  If the second component corresponds to a
363   // known vendor, preferentially use it for the vendor, etc.  This avoids silly
364   // component movement when a component parses as (eg) both a valid arch and a
365   // valid os.
366   ArchType Arch = UnknownArch;
367   if (Components.size() > 0)
368     Arch = parseArch(Components[0]);
369   VendorType Vendor = UnknownVendor;
370   if (Components.size() > 1)
371     Vendor = parseVendor(Components[1]);
372   OSType OS = UnknownOS;
373   if (Components.size() > 2)
374     OS = parseOS(Components[2]);
375   EnvironmentType Environment = UnknownEnvironment;
376   if (Components.size() > 3)
377     Environment = parseEnvironment(Components[3]);
378
379   // Note which components are already in their final position.  These will not
380   // be moved.
381   bool Found[4];
382   Found[0] = Arch != UnknownArch;
383   Found[1] = Vendor != UnknownVendor;
384   Found[2] = OS != UnknownOS;
385   Found[3] = Environment != UnknownEnvironment;
386
387   // If they are not there already, permute the components into their canonical
388   // positions by seeing if they parse as a valid architecture, and if so moving
389   // the component to the architecture position etc.
390   for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
391     if (Found[Pos])
392       continue; // Already in the canonical position.
393
394     for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
395       // Do not reparse any components that already matched.
396       if (Idx < array_lengthof(Found) && Found[Idx])
397         continue;
398
399       // Does this component parse as valid for the target position?
400       bool Valid = false;
401       StringRef Comp = Components[Idx];
402       switch (Pos) {
403       default: llvm_unreachable("unexpected component type!");
404       case 0:
405         Arch = parseArch(Comp);
406         Valid = Arch != UnknownArch;
407         break;
408       case 1:
409         Vendor = parseVendor(Comp);
410         Valid = Vendor != UnknownVendor;
411         break;
412       case 2:
413         OS = parseOS(Comp);
414         Valid = OS != UnknownOS;
415         break;
416       case 3:
417         Environment = parseEnvironment(Comp);
418         Valid = Environment != UnknownEnvironment;
419         break;
420       }
421       if (!Valid)
422         continue; // Nope, try the next component.
423
424       // Move the component to the target position, pushing any non-fixed
425       // components that are in the way to the right.  This tends to give
426       // good results in the common cases of a forgotten vendor component
427       // or a wrongly positioned environment.
428       if (Pos < Idx) {
429         // Insert left, pushing the existing components to the right.  For
430         // example, a-b-i386 -> i386-a-b when moving i386 to the front.
431         StringRef CurrentComponent(""); // The empty component.
432         // Replace the component we are moving with an empty component.
433         std::swap(CurrentComponent, Components[Idx]);
434         // Insert the component being moved at Pos, displacing any existing
435         // components to the right.
436         for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
437           // Skip over any fixed components.
438           while (i < array_lengthof(Found) && Found[i])
439             ++i;
440           // Place the component at the new position, getting the component
441           // that was at this position - it will be moved right.
442           std::swap(CurrentComponent, Components[i]);
443         }
444       } else if (Pos > Idx) {
445         // Push right by inserting empty components until the component at Idx
446         // reaches the target position Pos.  For example, pc-a -> -pc-a when
447         // moving pc to the second position.
448         do {
449           // Insert one empty component at Idx.
450           StringRef CurrentComponent(""); // The empty component.
451           for (unsigned i = Idx; i < Components.size();) {
452             // Place the component at the new position, getting the component
453             // that was at this position - it will be moved right.
454             std::swap(CurrentComponent, Components[i]);
455             // If it was placed on top of an empty component then we are done.
456             if (CurrentComponent.empty())
457               break;
458             // Advance to the next component, skipping any fixed components.
459             while (++i < array_lengthof(Found) && Found[i])
460               ;
461           }
462           // The last component was pushed off the end - append it.
463           if (!CurrentComponent.empty())
464             Components.push_back(CurrentComponent);
465
466           // Advance Idx to the component's new position.
467           while (++Idx < array_lengthof(Found) && Found[Idx])
468             ;
469         } while (Idx < Pos); // Add more until the final position is reached.
470       }
471       assert(Pos < Components.size() && Components[Pos] == Comp &&
472              "Component moved wrong!");
473       Found[Pos] = true;
474       break;
475     }
476   }
477
478   // Special case logic goes here.  At this point Arch, Vendor and OS have the
479   // correct values for the computed components.
480
481   // Stick the corrected components back together to form the normalized string.
482   std::string Normalized;
483   for (unsigned i = 0, e = Components.size(); i != e; ++i) {
484     if (i) Normalized += '-';
485     Normalized += Components[i];
486   }
487   return Normalized;
488 }
489
490 StringRef Triple::getArchName() const {
491   return StringRef(Data).split('-').first;           // Isolate first component
492 }
493
494 StringRef Triple::getVendorName() const {
495   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
496   return Tmp.split('-').first;                       // Isolate second component
497 }
498
499 StringRef Triple::getOSName() const {
500   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
501   Tmp = Tmp.split('-').second;                       // Strip second component
502   return Tmp.split('-').first;                       // Isolate third component
503 }
504
505 StringRef Triple::getEnvironmentName() const {
506   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
507   Tmp = Tmp.split('-').second;                       // Strip second component
508   return Tmp.split('-').second;                      // Strip third component
509 }
510
511 StringRef Triple::getOSAndEnvironmentName() const {
512   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
513   return Tmp.split('-').second;                      // Strip second component
514 }
515
516 static unsigned EatNumber(StringRef &Str) {
517   assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
518   unsigned Result = 0;
519
520   do {
521     // Consume the leading digit.
522     Result = Result*10 + (Str[0] - '0');
523
524     // Eat the digit.
525     Str = Str.substr(1);
526   } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
527
528   return Result;
529 }
530
531 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
532                           unsigned &Micro) const {
533   StringRef OSName = getOSName();
534
535   // Assume that the OS portion of the triple starts with the canonical name.
536   StringRef OSTypeName = getOSTypeName(getOS());
537   if (OSName.startswith(OSTypeName))
538     OSName = OSName.substr(OSTypeName.size());
539
540   // Any unset version defaults to 0.
541   Major = Minor = Micro = 0;
542
543   // Parse up to three components.
544   unsigned *Components[3] = { &Major, &Minor, &Micro };
545   for (unsigned i = 0; i != 3; ++i) {
546     if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
547       break;
548
549     // Consume the leading number.
550     *Components[i] = EatNumber(OSName);
551
552     // Consume the separator, if present.
553     if (OSName.startswith("."))
554       OSName = OSName.substr(1);
555   }
556 }
557
558 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
559                               unsigned &Micro) const {
560   getOSVersion(Major, Minor, Micro);
561
562   switch (getOS()) {
563   default: llvm_unreachable("unexpected OS for Darwin triple");
564   case Darwin:
565     // Default to darwin8, i.e., MacOSX 10.4.
566     if (Major == 0)
567       Major = 8;
568     // Darwin version numbers are skewed from OS X versions.
569     if (Major < 4)
570       return false;
571     Micro = 0;
572     Minor = Major - 4;
573     Major = 10;
574     break;
575   case MacOSX:
576     // Default to 10.4.
577     if (Major == 0) {
578       Major = 10;
579       Minor = 4;
580     }
581     if (Major != 10)
582       return false;
583     break;
584   case IOS:
585     // Ignore the version from the triple.  This is only handled because the
586     // the clang driver combines OS X and IOS support into a common Darwin
587     // toolchain that wants to know the OS X version number even when targeting
588     // IOS.
589     Major = 10;
590     Minor = 4;
591     Micro = 0;
592     break;
593   }
594   return true;
595 }
596
597 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
598                            unsigned &Micro) const {
599   switch (getOS()) {
600   default: llvm_unreachable("unexpected OS for Darwin triple");
601   case Darwin:
602   case MacOSX:
603     // Ignore the version from the triple.  This is only handled because the
604     // the clang driver combines OS X and IOS support into a common Darwin
605     // toolchain that wants to know the iOS version number even when targeting
606     // OS X.
607     Major = 5;
608     Minor = 0;
609     Micro = 0;
610     break;
611   case IOS:
612     getOSVersion(Major, Minor, Micro);
613     // Default to 5.0.
614     if (Major == 0)
615       Major = 5;
616     break;
617   }
618 }
619
620 void Triple::setTriple(const Twine &Str) {
621   *this = Triple(Str);
622 }
623
624 void Triple::setArch(ArchType Kind) {
625   setArchName(getArchTypeName(Kind));
626 }
627
628 void Triple::setVendor(VendorType Kind) {
629   setVendorName(getVendorTypeName(Kind));
630 }
631
632 void Triple::setOS(OSType Kind) {
633   setOSName(getOSTypeName(Kind));
634 }
635
636 void Triple::setEnvironment(EnvironmentType Kind) {
637   setEnvironmentName(getEnvironmentTypeName(Kind));
638 }
639
640 void Triple::setArchName(StringRef Str) {
641   // Work around a miscompilation bug for Twines in gcc 4.0.3.
642   SmallString<64> Triple;
643   Triple += Str;
644   Triple += "-";
645   Triple += getVendorName();
646   Triple += "-";
647   Triple += getOSAndEnvironmentName();
648   setTriple(Triple.str());
649 }
650
651 void Triple::setVendorName(StringRef Str) {
652   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
653 }
654
655 void Triple::setOSName(StringRef Str) {
656   if (hasEnvironment())
657     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
658               "-" + getEnvironmentName());
659   else
660     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
661 }
662
663 void Triple::setEnvironmentName(StringRef Str) {
664   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
665             "-" + Str);
666 }
667
668 void Triple::setOSAndEnvironmentName(StringRef Str) {
669   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
670 }
671
672 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
673   switch (Arch) {
674   case llvm::Triple::UnknownArch:
675     return 0;
676
677   case llvm::Triple::msp430:
678     return 16;
679
680   case llvm::Triple::amdil:
681   case llvm::Triple::arm:
682   case llvm::Triple::hexagon:
683   case llvm::Triple::le32:
684   case llvm::Triple::mips:
685   case llvm::Triple::mipsel:
686   case llvm::Triple::nvptx:
687   case llvm::Triple::ppc:
688   case llvm::Triple::r600:
689   case llvm::Triple::sparc:
690   case llvm::Triple::tce:
691   case llvm::Triple::thumb:
692   case llvm::Triple::x86:
693   case llvm::Triple::xcore:
694   case llvm::Triple::spir:
695     return 32;
696
697   case llvm::Triple::aarch64:
698   case llvm::Triple::mips64:
699   case llvm::Triple::mips64el:
700   case llvm::Triple::nvptx64:
701   case llvm::Triple::ppc64:
702   case llvm::Triple::ppc64le:
703   case llvm::Triple::sparcv9:
704   case llvm::Triple::systemz:
705   case llvm::Triple::x86_64:
706   case llvm::Triple::spir64:
707     return 64;
708   }
709   llvm_unreachable("Invalid architecture value");
710 }
711
712 bool Triple::isArch64Bit() const {
713   return getArchPointerBitWidth(getArch()) == 64;
714 }
715
716 bool Triple::isArch32Bit() const {
717   return getArchPointerBitWidth(getArch()) == 32;
718 }
719
720 bool Triple::isArch16Bit() const {
721   return getArchPointerBitWidth(getArch()) == 16;
722 }
723
724 Triple Triple::get32BitArchVariant() const {
725   Triple T(*this);
726   switch (getArch()) {
727   case Triple::UnknownArch:
728   case Triple::aarch64:
729   case Triple::msp430:
730   case Triple::systemz:
731   case Triple::ppc64le:
732     T.setArch(UnknownArch);
733     break;
734
735   case Triple::amdil:
736   case Triple::spir:
737   case Triple::arm:
738   case Triple::hexagon:
739   case Triple::le32:
740   case Triple::mips:
741   case Triple::mipsel:
742   case Triple::nvptx:
743   case Triple::ppc:
744   case Triple::r600:
745   case Triple::sparc:
746   case Triple::tce:
747   case Triple::thumb:
748   case Triple::x86:
749   case Triple::xcore:
750     // Already 32-bit.
751     break;
752
753   case Triple::mips64:    T.setArch(Triple::mips);    break;
754   case Triple::mips64el:  T.setArch(Triple::mipsel);  break;
755   case Triple::nvptx64:   T.setArch(Triple::nvptx);   break;
756   case Triple::ppc64:     T.setArch(Triple::ppc);   break;
757   case Triple::sparcv9:   T.setArch(Triple::sparc);   break;
758   case Triple::x86_64:    T.setArch(Triple::x86);     break;
759   case Triple::spir64:    T.setArch(Triple::spir);    break;
760   }
761   return T;
762 }
763
764 Triple Triple::get64BitArchVariant() const {
765   Triple T(*this);
766   switch (getArch()) {
767   case Triple::UnknownArch:
768   case Triple::amdil:
769   case Triple::arm:
770   case Triple::hexagon:
771   case Triple::le32:
772   case Triple::msp430:
773   case Triple::r600:
774   case Triple::tce:
775   case Triple::thumb:
776   case Triple::xcore:
777     T.setArch(UnknownArch);
778     break;
779
780   case Triple::aarch64:
781   case Triple::spir64:
782   case Triple::mips64:
783   case Triple::mips64el:
784   case Triple::nvptx64:
785   case Triple::ppc64:
786   case Triple::ppc64le:
787   case Triple::sparcv9:
788   case Triple::systemz:
789   case Triple::x86_64:
790     // Already 64-bit.
791     break;
792
793   case Triple::mips:    T.setArch(Triple::mips64);    break;
794   case Triple::mipsel:  T.setArch(Triple::mips64el);  break;
795   case Triple::nvptx:   T.setArch(Triple::nvptx64);   break;
796   case Triple::ppc:     T.setArch(Triple::ppc64);     break;
797   case Triple::sparc:   T.setArch(Triple::sparcv9);   break;
798   case Triple::x86:     T.setArch(Triple::x86_64);    break;
799   case Triple::spir:    T.setArch(Triple::spir64);    break;
800   }
801   return T;
802 }