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