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