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