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