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