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