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