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