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