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