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