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