Clean up comments that I missed when changing the triple representation.
[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/SmallString.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/STLExtras.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 cellspu: return "cellspu";
24   case hexagon: return "hexagon";
25   case mips:    return "mips";
26   case mipsel:  return "mipsel";
27   case mips64:  return "mips64";
28   case mips64el:return "mips64el";
29   case msp430:  return "msp430";
30   case ppc64:   return "powerpc64";
31   case ppc:     return "powerpc";
32   case 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 ptx32:   return "ptx32";
41   case ptx64:   return "ptx64";
42   case le32:    return "le32";
43   case amdil:   return "amdil";
44   }
45
46   llvm_unreachable("Invalid ArchType!");
47 }
48
49 const char *Triple::getArchTypePrefix(ArchType Kind) {
50   switch (Kind) {
51   default:
52     return 0;
53
54   case arm:
55   case thumb:   return "arm";
56
57   case cellspu: return "spu";
58
59   case ppc64:
60   case ppc:     return "ppc";
61
62   case mblaze:  return "mblaze";
63
64   case hexagon:   return "hexagon";
65
66   case sparcv9:
67   case sparc:   return "sparc";
68
69   case x86:
70   case x86_64:  return "x86";
71
72   case xcore:   return "xcore";
73
74   case ptx32:   return "ptx";
75   case ptx64:   return "ptx";
76   case le32:    return "le32";
77   case amdil:   return "amdil";
78   }
79 }
80
81 const char *Triple::getVendorTypeName(VendorType Kind) {
82   switch (Kind) {
83   case UnknownVendor: return "unknown";
84
85   case Apple: return "apple";
86   case PC: return "pc";
87   case SCEI: return "scei";
88   }
89
90   llvm_unreachable("Invalid VendorType!");
91 }
92
93 const char *Triple::getOSTypeName(OSType Kind) {
94   switch (Kind) {
95   case UnknownOS: return "unknown";
96
97   case AuroraUX: return "auroraux";
98   case Cygwin: return "cygwin";
99   case Darwin: return "darwin";
100   case DragonFly: return "dragonfly";
101   case FreeBSD: return "freebsd";
102   case IOS: return "ios";
103   case KFreeBSD: return "kfreebsd";
104   case Linux: return "linux";
105   case Lv2: return "lv2";
106   case MacOSX: return "macosx";
107   case MinGW32: return "mingw32";
108   case NetBSD: return "netbsd";
109   case OpenBSD: return "openbsd";
110   case Psp: return "psp";
111   case Solaris: return "solaris";
112   case Win32: return "win32";
113   case Haiku: return "haiku";
114   case Minix: return "minix";
115   case RTEMS: return "rtems";
116   case NativeClient: return "nacl";
117   }
118
119   llvm_unreachable("Invalid OSType");
120 }
121
122 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
123   switch (Kind) {
124   case UnknownEnvironment: return "unknown";
125   case GNU: return "gnu";
126   case GNUEABIHF: return "gnueabihf";
127   case GNUEABI: return "gnueabi";
128   case EABI: return "eabi";
129   case MachO: return "macho";
130   case ANDROIDEABI: return "androideabi";
131   }
132
133   llvm_unreachable("Invalid EnvironmentType!");
134 }
135
136 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
137   return StringSwitch<Triple::ArchType>(Name)
138     .Case("arm", arm)
139     .Case("cellspu", cellspu)
140     .Case("mips", mips)
141     .Case("mipsel", mipsel)
142     .Case("mips64", mips64)
143     .Case("mips64el", mips64el)
144     .Case("msp430", msp430)
145     .Case("ppc64", ppc64)
146     .Case("ppc32", ppc)
147     .Case("ppc", ppc)
148     .Case("mblaze", mblaze)
149     .Case("hexagon", hexagon)
150     .Case("sparc", sparc)
151     .Case("sparcv9", sparcv9)
152     .Case("tce", tce)
153     .Case("thumb", thumb)
154     .Case("x86", x86)
155     .Case("x86-64", x86_64)
156     .Case("xcore", xcore)
157     .Case("ptx32", ptx32)
158     .Case("ptx64", ptx64)
159     .Case("le32", le32)
160     .Case("amdil", amdil)
161     .Default(UnknownArch);
162 }
163
164 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
165   // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
166   // archs which Darwin doesn't use.
167
168   // The matching this routine does is fairly pointless, since it is neither the
169   // complete architecture list, nor a reasonable subset. The problem is that
170   // historically the driver driver accepts this and also ties its -march=
171   // handling to the architecture name, so we need to be careful before removing
172   // support for it.
173
174   // This code must be kept in sync with Clang's Darwin specific argument
175   // translation.
176
177   return StringSwitch<ArchType>(Str)
178     .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
179     .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
180     .Case("ppc64", Triple::ppc64)
181     .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
182     .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
183            Triple::x86)
184     .Case("x86_64", Triple::x86_64)
185     // This is derived from the driver driver.
186     .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
187     .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
188     .Case("ptx32", Triple::ptx32)
189     .Case("ptx64", Triple::ptx64)
190     .Case("amdil", Triple::amdil)
191     .Default(Triple::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     .Cases("mblaze", "microblaze", "mblaze")
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("ptx32", "ptx32")
211     .Case("ptx64", "ptx64")
212     .Case("le32", "le32")
213     .Case("amdil", "amdil")
214     .Default(NULL);
215 }
216
217 Triple::ArchType Triple::ParseArch(StringRef ArchName) {
218   return StringSwitch<ArchType>(ArchName)
219     .Cases("i386", "i486", "i586", "i686", x86)
220     .Cases("i786", "i886", "i986", x86) // FIXME: Do we need to support these?
221     .Cases("amd64", "x86_64", x86_64)
222     .Case("powerpc", ppc)
223     .Cases("powerpc64", "ppu", ppc64)
224     .Case("mblaze", mblaze)
225     .Cases("arm", "xscale", arm)
226     // FIXME: It would be good to replace these with explicit names for all the
227     // various suffixes supported.
228     .StartsWith("armv", arm)
229     .Case("thumb", thumb)
230     .StartsWith("thumbv", thumb)
231     .Cases("spu", "cellspu", cellspu)
232     .Case("msp430", msp430)
233     .Cases("mips", "mipseb", "mipsallegrex", mips)
234     .Cases("mipsel", "mipsallegrexel", "psp", mipsel)
235     .Cases("mips64", "mips64eb", mips64)
236     .Case("mips64el", mips64el)
237     .Case("hexagon", hexagon)
238     .Case("sparc", sparc)
239     .Case("sparcv9", sparcv9)
240     .Case("tce", tce)
241     .Case("xcore", xcore)
242     .Case("ptx32", ptx32)
243     .Case("ptx64", ptx64)
244     .Case("le32", le32)
245     .Case("amdil", amdil)
246     .Default(UnknownArch);
247 }
248
249 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
250   return StringSwitch<VendorType>(VendorName)
251     .Case("apple", Apple)
252     .Case("pc", PC)
253     .Case("scei", SCEI)
254     .Default(UnknownVendor);
255 }
256
257 Triple::OSType Triple::ParseOS(StringRef OSName) {
258   return StringSwitch<OSType>(OSName)
259     .StartsWith("auroraux", AuroraUX)
260     .StartsWith("cygwin", Cygwin)
261     .StartsWith("darwin", Darwin)
262     .StartsWith("dragonfly", DragonFly)
263     .StartsWith("freebsd", FreeBSD)
264     .StartsWith("ios", IOS)
265     .StartsWith("kfreebsd", KFreeBSD)
266     .StartsWith("linux", Linux)
267     .StartsWith("lv2", Lv2)
268     .StartsWith("macosx", MacOSX)
269     .StartsWith("mingw32", MinGW32)
270     .StartsWith("netbsd", NetBSD)
271     .StartsWith("openbsd", OpenBSD)
272     .StartsWith("psp", Psp)
273     .StartsWith("solaris", Solaris)
274     .StartsWith("win32", Win32)
275     .StartsWith("haiku", Haiku)
276     .StartsWith("minix", Minix)
277     .StartsWith("rtems", RTEMS)
278     .StartsWith("nacl", NativeClient)
279     .Default(UnknownOS);
280 }
281
282 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
283   return StringSwitch<EnvironmentType>(EnvironmentName)
284     .StartsWith("eabi", EABI)
285     .StartsWith("gnueabihf", GNUEABIHF)
286     .StartsWith("gnueabi", GNUEABI)
287     .StartsWith("gnu", GNU)
288     .StartsWith("macho", MachO)
289     .StartsWith("androideabi", ANDROIDEABI)
290     .Default(UnknownEnvironment);
291 }
292
293 /// \brief Construct a triple from the string representation provided.
294 ///
295 /// This stores the string representation and parses the various pieces into
296 /// enum members.
297 Triple::Triple(const Twine &Str)
298     : Data(Str.str()),
299       Arch(ParseArch(getArchName())),
300       Vendor(ParseVendor(getVendorName())),
301       OS(ParseOS(getOSName())),
302       Environment(ParseEnvironment(getEnvironmentName())) {
303 }
304
305 /// \brief Construct a triple from string representations of the architecture,
306 /// vendor, and OS.
307 ///
308 /// This joins each argument into a canonical string representation and parses
309 /// them into enum members. It leaves the environment unknown and omits it from
310 /// the string representation.
311 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
312     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
313       Arch(ParseArch(ArchStr.str())),
314       Vendor(ParseVendor(VendorStr.str())),
315       OS(ParseOS(OSStr.str())),
316       Environment() {
317 }
318
319 /// \brief Construct a triple from string representations of the architecture,
320 /// vendor, OS, and environment.
321 ///
322 /// This joins each argument into a canonical string representation and parses
323 /// them into enum members.
324 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
325                const Twine &EnvironmentStr)
326     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
327             EnvironmentStr).str()),
328       Arch(ParseArch(ArchStr.str())),
329       Vendor(ParseVendor(VendorStr.str())),
330       OS(ParseOS(OSStr.str())),
331       Environment(ParseEnvironment(EnvironmentStr.str())) {
332 }
333
334 std::string Triple::normalize(StringRef Str) {
335   // Parse into components.
336   SmallVector<StringRef, 4> Components;
337   for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
338     Last = Str.find('-', First);
339     Components.push_back(Str.slice(First, Last));
340   }
341
342   // If the first component corresponds to a known architecture, preferentially
343   // use it for the architecture.  If the second component corresponds to a
344   // known vendor, preferentially use it for the vendor, etc.  This avoids silly
345   // component movement when a component parses as (eg) both a valid arch and a
346   // valid os.
347   ArchType Arch = UnknownArch;
348   if (Components.size() > 0)
349     Arch = ParseArch(Components[0]);
350   VendorType Vendor = UnknownVendor;
351   if (Components.size() > 1)
352     Vendor = ParseVendor(Components[1]);
353   OSType OS = UnknownOS;
354   if (Components.size() > 2)
355     OS = ParseOS(Components[2]);
356   EnvironmentType Environment = UnknownEnvironment;
357   if (Components.size() > 3)
358     Environment = ParseEnvironment(Components[3]);
359
360   // Note which components are already in their final position.  These will not
361   // be moved.
362   bool Found[4];
363   Found[0] = Arch != UnknownArch;
364   Found[1] = Vendor != UnknownVendor;
365   Found[2] = OS != UnknownOS;
366   Found[3] = Environment != UnknownEnvironment;
367
368   // If they are not there already, permute the components into their canonical
369   // positions by seeing if they parse as a valid architecture, and if so moving
370   // the component to the architecture position etc.
371   for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
372     if (Found[Pos])
373       continue; // Already in the canonical position.
374
375     for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
376       // Do not reparse any components that already matched.
377       if (Idx < array_lengthof(Found) && Found[Idx])
378         continue;
379
380       // Does this component parse as valid for the target position?
381       bool Valid = false;
382       StringRef Comp = Components[Idx];
383       switch (Pos) {
384       default: llvm_unreachable("unexpected component type!");
385       case 0:
386         Arch = ParseArch(Comp);
387         Valid = Arch != UnknownArch;
388         break;
389       case 1:
390         Vendor = ParseVendor(Comp);
391         Valid = Vendor != UnknownVendor;
392         break;
393       case 2:
394         OS = ParseOS(Comp);
395         Valid = OS != UnknownOS;
396         break;
397       case 3:
398         Environment = ParseEnvironment(Comp);
399         Valid = Environment != UnknownEnvironment;
400         break;
401       }
402       if (!Valid)
403         continue; // Nope, try the next component.
404
405       // Move the component to the target position, pushing any non-fixed
406       // components that are in the way to the right.  This tends to give
407       // good results in the common cases of a forgotten vendor component
408       // or a wrongly positioned environment.
409       if (Pos < Idx) {
410         // Insert left, pushing the existing components to the right.  For
411         // example, a-b-i386 -> i386-a-b when moving i386 to the front.
412         StringRef CurrentComponent(""); // The empty component.
413         // Replace the component we are moving with an empty component.
414         std::swap(CurrentComponent, Components[Idx]);
415         // Insert the component being moved at Pos, displacing any existing
416         // components to the right.
417         for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
418           // Skip over any fixed components.
419           while (i < array_lengthof(Found) && Found[i]) ++i;
420           // Place the component at the new position, getting the component
421           // that was at this position - it will be moved right.
422           std::swap(CurrentComponent, Components[i]);
423         }
424       } else if (Pos > Idx) {
425         // Push right by inserting empty components until the component at Idx
426         // reaches the target position Pos.  For example, pc-a -> -pc-a when
427         // moving pc to the second position.
428         do {
429           // Insert one empty component at Idx.
430           StringRef CurrentComponent(""); // The empty component.
431           for (unsigned i = Idx; i < Components.size();) {
432             // Place the component at the new position, getting the component
433             // that was at this position - it will be moved right.
434             std::swap(CurrentComponent, Components[i]);
435             // If it was placed on top of an empty component then we are done.
436             if (CurrentComponent.empty())
437               break;
438             // Advance to the next component, skipping any fixed components.
439             while (++i < array_lengthof(Found) && Found[i])
440               ;
441           }
442           // The last component was pushed off the end - append it.
443           if (!CurrentComponent.empty())
444             Components.push_back(CurrentComponent);
445
446           // Advance Idx to the component's new position.
447           while (++Idx < array_lengthof(Found) && Found[Idx]) {}
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::setTriple(const Twine &Str) {
577   *this = Triple(Str);
578 }
579
580 void Triple::setArch(ArchType Kind) {
581   setArchName(getArchTypeName(Kind));
582 }
583
584 void Triple::setVendor(VendorType Kind) {
585   setVendorName(getVendorTypeName(Kind));
586 }
587
588 void Triple::setOS(OSType Kind) {
589   setOSName(getOSTypeName(Kind));
590 }
591
592 void Triple::setEnvironment(EnvironmentType Kind) {
593   setEnvironmentName(getEnvironmentTypeName(Kind));
594 }
595
596 void Triple::setArchName(StringRef Str) {
597   // Work around a miscompilation bug for Twines in gcc 4.0.3.
598   SmallString<64> Triple;
599   Triple += Str;
600   Triple += "-";
601   Triple += getVendorName();
602   Triple += "-";
603   Triple += getOSAndEnvironmentName();
604   setTriple(Triple.str());
605 }
606
607 void Triple::setVendorName(StringRef Str) {
608   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
609 }
610
611 void Triple::setOSName(StringRef Str) {
612   if (hasEnvironment())
613     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
614               "-" + getEnvironmentName());
615   else
616     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
617 }
618
619 void Triple::setEnvironmentName(StringRef Str) {
620   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
621             "-" + Str);
622 }
623
624 void Triple::setOSAndEnvironmentName(StringRef Str) {
625   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
626 }
627
628 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
629   switch (Arch) {
630   case llvm::Triple::UnknownArch:
631     return 0;
632
633   case llvm::Triple::msp430:
634     return 16;
635
636   case llvm::Triple::amdil:
637   case llvm::Triple::arm:
638   case llvm::Triple::cellspu:
639   case llvm::Triple::hexagon:
640   case llvm::Triple::le32:
641   case llvm::Triple::mblaze:
642   case llvm::Triple::mips:
643   case llvm::Triple::mipsel:
644   case llvm::Triple::ppc:
645   case llvm::Triple::ptx32:
646   case llvm::Triple::sparc:
647   case llvm::Triple::tce:
648   case llvm::Triple::thumb:
649   case llvm::Triple::x86:
650   case llvm::Triple::xcore:
651     return 32;
652
653   case llvm::Triple::mips64:
654   case llvm::Triple::mips64el:
655   case llvm::Triple::ppc64:
656   case llvm::Triple::ptx64:
657   case llvm::Triple::sparcv9:
658   case llvm::Triple::x86_64:
659     return 64;
660   }
661   llvm_unreachable("Invalid architecture value");
662 }
663
664 bool Triple::isArch64Bit() const {
665   return getArchPointerBitWidth(getArch()) == 64;
666 }
667
668 bool Triple::isArch32Bit() const {
669   return getArchPointerBitWidth(getArch()) == 32;
670 }
671
672 bool Triple::isArch16Bit() const {
673   return getArchPointerBitWidth(getArch()) == 16;
674 }
675
676 Triple Triple::get32BitArchVariant() const {
677   Triple T(*this);
678   switch (getArch()) {
679   case Triple::UnknownArch:
680   case Triple::msp430:
681     T.setArch(UnknownArch);
682     break;
683
684   case Triple::amdil:
685   case Triple::arm:
686   case Triple::cellspu:
687   case Triple::hexagon:
688   case Triple::le32:
689   case Triple::mblaze:
690   case Triple::mips:
691   case Triple::mipsel:
692   case Triple::ppc:
693   case Triple::ptx32:
694   case Triple::sparc:
695   case Triple::tce:
696   case Triple::thumb:
697   case Triple::x86:
698   case Triple::xcore:
699     // Already 32-bit.
700     break;
701
702   case Triple::mips64:    T.setArch(Triple::mips);    break;
703   case Triple::mips64el:  T.setArch(Triple::mipsel);  break;
704   case Triple::ppc64:     T.setArch(Triple::ppc);   break;
705   case Triple::ptx64:     T.setArch(Triple::ptx32);   break;
706   case Triple::sparcv9:   T.setArch(Triple::sparc);   break;
707   case Triple::x86_64:    T.setArch(Triple::x86);     break;
708   }
709   return T;
710 }
711
712 Triple Triple::get64BitArchVariant() const {
713   Triple T(*this);
714   switch (getArch()) {
715   case Triple::UnknownArch:
716   case Triple::amdil:
717   case Triple::arm:
718   case Triple::cellspu:
719   case Triple::hexagon:
720   case Triple::le32:
721   case Triple::mblaze:
722   case Triple::msp430:
723   case Triple::tce:
724   case Triple::thumb:
725   case Triple::xcore:
726     T.setArch(UnknownArch);
727     break;
728
729   case Triple::mips64:
730   case Triple::mips64el:
731   case Triple::ppc64:
732   case Triple::ptx64:
733   case Triple::sparcv9:
734   case Triple::x86_64:
735     // Already 64-bit.
736     break;
737
738   case Triple::mips:    T.setArch(Triple::mips64);    break;
739   case Triple::mipsel:  T.setArch(Triple::mips64el);  break;
740   case Triple::ppc:     T.setArch(Triple::ppc64);     break;
741   case Triple::ptx32:   T.setArch(Triple::ptx64);     break;
742   case Triple::sparc:   T.setArch(Triple::sparcv9);   break;
743   case Triple::x86:     T.setArch(Triple::x86_64);    break;
744   }
745   return T;
746 }