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