Fix ThreadLocalImpl::getInstance for --disable-threads.
[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/SmallString.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/STLExtras.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 arm:     return "arm";
23   case cellspu: return "cellspu";
24   case hexagon: return "hexagon";
25   case mips:    return "mips";
26   case mipsel:  return "mipsel";
27   case mips64:  return "mips64";
28   case mips64el:return "mips64el";
29   case msp430:  return "msp430";
30   case ppc64:   return "powerpc64";
31   case ppc:     return "powerpc";
32   case r600:    return "r600";
33   case sparc:   return "sparc";
34   case sparcv9: return "sparcv9";
35   case tce:     return "tce";
36   case thumb:   return "thumb";
37   case x86:     return "i386";
38   case x86_64:  return "x86_64";
39   case xcore:   return "xcore";
40   case mblaze:  return "mblaze";
41   case nvptx:   return "nvptx";
42   case nvptx64: return "nvptx64";
43   case le32:    return "le32";
44   case amdil:   return "amdil";
45   }
46
47   llvm_unreachable("Invalid ArchType!");
48 }
49
50 const char *Triple::getArchTypePrefix(ArchType Kind) {
51   switch (Kind) {
52   default:
53     return 0;
54
55   case arm:
56   case thumb:   return "arm";
57
58   case cellspu: return "spu";
59
60   case ppc64:
61   case ppc:     return "ppc";
62
63   case mblaze:  return "mblaze";
64
65   case hexagon:   return "hexagon";
66
67   case r600:    return "r600";
68
69   case sparcv9:
70   case sparc:   return "sparc";
71
72   case x86:
73   case x86_64:  return "x86";
74
75   case xcore:   return "xcore";
76
77   case nvptx:   return "nvptx";
78   case nvptx64: return "nvptx";
79   case le32:    return "le32";
80   case amdil:   return "amdil";
81   }
82 }
83
84 const char *Triple::getVendorTypeName(VendorType Kind) {
85   switch (Kind) {
86   case UnknownVendor: return "unknown";
87
88   case Apple: return "apple";
89   case PC: return "pc";
90   case SCEI: return "scei";
91   case BGP: return "bgp";
92   case BGQ: return "bgq";
93   }
94
95   llvm_unreachable("Invalid VendorType!");
96 }
97
98 const char *Triple::getOSTypeName(OSType Kind) {
99   switch (Kind) {
100   case UnknownOS: return "unknown";
101
102   case AuroraUX: return "auroraux";
103   case Cygwin: return "cygwin";
104   case Darwin: return "darwin";
105   case DragonFly: return "dragonfly";
106   case FreeBSD: return "freebsd";
107   case IOS: return "ios";
108   case KFreeBSD: return "kfreebsd";
109   case Linux: return "linux";
110   case Lv2: return "lv2";
111   case MacOSX: return "macosx";
112   case MinGW32: return "mingw32";
113   case NetBSD: return "netbsd";
114   case OpenBSD: return "openbsd";
115   case Solaris: return "solaris";
116   case Win32: return "win32";
117   case Haiku: return "haiku";
118   case Minix: return "minix";
119   case RTEMS: return "rtems";
120   case NativeClient: return "nacl";
121   case CNK: return "cnk";
122   }
123
124   llvm_unreachable("Invalid OSType");
125 }
126
127 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
128   switch (Kind) {
129   case UnknownEnvironment: return "unknown";
130   case GNU: return "gnu";
131   case GNUEABIHF: return "gnueabihf";
132   case GNUEABI: return "gnueabi";
133   case EABI: return "eabi";
134   case MachO: return "macho";
135   case ANDROIDEABI: return "androideabi";
136   }
137
138   llvm_unreachable("Invalid EnvironmentType!");
139 }
140
141 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
142   return StringSwitch<Triple::ArchType>(Name)
143     .Case("arm", arm)
144     .Case("cellspu", cellspu)
145     .Case("mips", mips)
146     .Case("mipsel", mipsel)
147     .Case("mips64", mips64)
148     .Case("mips64el", mips64el)
149     .Case("msp430", msp430)
150     .Case("ppc64", ppc64)
151     .Case("ppc32", ppc)
152     .Case("ppc", ppc)
153     .Case("mblaze", mblaze)
154     .Case("r600", r600)
155     .Case("hexagon", hexagon)
156     .Case("sparc", sparc)
157     .Case("sparcv9", sparcv9)
158     .Case("tce", tce)
159     .Case("thumb", thumb)
160     .Case("x86", x86)
161     .Case("x86-64", x86_64)
162     .Case("xcore", xcore)
163     .Case("nvptx", nvptx)
164     .Case("nvptx64", nvptx64)
165     .Case("le32", le32)
166     .Case("amdil", amdil)
167     .Default(UnknownArch);
168 }
169
170 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
171   // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
172   // archs which Darwin doesn't use.
173
174   // The matching this routine does is fairly pointless, since it is neither the
175   // complete architecture list, nor a reasonable subset. The problem is that
176   // historically the driver driver accepts this and also ties its -march=
177   // handling to the architecture name, so we need to be careful before removing
178   // support for it.
179
180   // This code must be kept in sync with Clang's Darwin specific argument
181   // translation.
182
183   return StringSwitch<ArchType>(Str)
184     .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
185     .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
186     .Case("ppc64", Triple::ppc64)
187     .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
188     .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
189            Triple::x86)
190     .Case("x86_64", Triple::x86_64)
191     // This is derived from the driver driver.
192     .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
193     .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
194     .Case("r600", Triple::r600)
195     .Case("nvptx", Triple::nvptx)
196     .Case("nvptx64", Triple::nvptx64)
197     .Case("amdil", Triple::amdil)
198     .Default(Triple::UnknownArch);
199 }
200
201 // Returns architecture name that is understood by the target assembler.
202 const char *Triple::getArchNameForAssembler() {
203   if (!isOSDarwin() && getVendor() != Triple::Apple)
204     return NULL;
205
206   return StringSwitch<const char*>(getArchName())
207     .Case("i386", "i386")
208     .Case("x86_64", "x86_64")
209     .Case("powerpc", "ppc")
210     .Case("powerpc64", "ppc64")
211     .Cases("mblaze", "microblaze", "mblaze")
212     .Case("arm", "arm")
213     .Cases("armv4t", "thumbv4t", "armv4t")
214     .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
215     .Cases("armv6", "thumbv6", "armv6")
216     .Cases("armv7", "thumbv7", "armv7")
217     .Case("r600", "r600")
218     .Case("nvptx", "nvptx")
219     .Case("nvptx64", "nvptx64")
220     .Case("le32", "le32")
221     .Case("amdil", "amdil")
222     .Default(NULL);
223 }
224
225 static Triple::ArchType parseArch(StringRef ArchName) {
226   return StringSwitch<Triple::ArchType>(ArchName)
227     .Cases("i386", "i486", "i586", "i686", Triple::x86)
228     // FIXME: Do we need to support these?
229     .Cases("i786", "i886", "i986", Triple::x86)
230     .Cases("amd64", "x86_64", Triple::x86_64)
231     .Case("powerpc", Triple::ppc)
232     .Cases("powerpc64", "ppu", Triple::ppc64)
233     .Case("mblaze", Triple::mblaze)
234     .Cases("arm", "xscale", Triple::arm)
235     // FIXME: It would be good to replace these with explicit names for all the
236     // various suffixes supported.
237     .StartsWith("armv", Triple::arm)
238     .Case("thumb", Triple::thumb)
239     .StartsWith("thumbv", Triple::thumb)
240     .Cases("spu", "cellspu", Triple::cellspu)
241     .Case("msp430", Triple::msp430)
242     .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
243     .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
244     .Cases("mips64", "mips64eb", Triple::mips64)
245     .Case("mips64el", Triple::mips64el)
246     .Case("r600", Triple::r600)
247     .Case("hexagon", Triple::hexagon)
248     .Case("sparc", Triple::sparc)
249     .Case("sparcv9", Triple::sparcv9)
250     .Case("tce", Triple::tce)
251     .Case("xcore", Triple::xcore)
252     .Case("nvptx", Triple::nvptx)
253     .Case("nvptx64", Triple::nvptx64)
254     .Case("le32", Triple::le32)
255     .Case("amdil", Triple::amdil)
256     .Default(Triple::UnknownArch);
257 }
258
259 static Triple::VendorType parseVendor(StringRef VendorName) {
260   return StringSwitch<Triple::VendorType>(VendorName)
261     .Case("apple", Triple::Apple)
262     .Case("pc", Triple::PC)
263     .Case("scei", Triple::SCEI)
264     .Case("bgp", Triple::BGP)
265     .Case("bgq", Triple::BGQ)
266     .Default(Triple::UnknownVendor);
267 }
268
269 static Triple::OSType parseOS(StringRef OSName) {
270   return StringSwitch<Triple::OSType>(OSName)
271     .StartsWith("auroraux", Triple::AuroraUX)
272     .StartsWith("cygwin", Triple::Cygwin)
273     .StartsWith("darwin", Triple::Darwin)
274     .StartsWith("dragonfly", Triple::DragonFly)
275     .StartsWith("freebsd", Triple::FreeBSD)
276     .StartsWith("ios", Triple::IOS)
277     .StartsWith("kfreebsd", Triple::KFreeBSD)
278     .StartsWith("linux", Triple::Linux)
279     .StartsWith("lv2", Triple::Lv2)
280     .StartsWith("macosx", Triple::MacOSX)
281     .StartsWith("mingw32", Triple::MinGW32)
282     .StartsWith("netbsd", Triple::NetBSD)
283     .StartsWith("openbsd", Triple::OpenBSD)
284     .StartsWith("solaris", Triple::Solaris)
285     .StartsWith("win32", Triple::Win32)
286     .StartsWith("haiku", Triple::Haiku)
287     .StartsWith("minix", Triple::Minix)
288     .StartsWith("rtems", Triple::RTEMS)
289     .StartsWith("nacl", Triple::NativeClient)
290     .StartsWith("cnk", Triple::CNK)
291     .Default(Triple::UnknownOS);
292 }
293
294 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
295   return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
296     .StartsWith("eabi", Triple::EABI)
297     .StartsWith("gnueabihf", Triple::GNUEABIHF)
298     .StartsWith("gnueabi", Triple::GNUEABI)
299     .StartsWith("gnu", Triple::GNU)
300     .StartsWith("macho", Triple::MachO)
301     .StartsWith("androideabi", Triple::ANDROIDEABI)
302     .Default(Triple::UnknownEnvironment);
303 }
304
305 /// \brief Construct a triple from the string representation provided.
306 ///
307 /// This stores the string representation and parses the various pieces into
308 /// enum members.
309 Triple::Triple(const Twine &Str)
310     : Data(Str.str()),
311       Arch(parseArch(getArchName())),
312       Vendor(parseVendor(getVendorName())),
313       OS(parseOS(getOSName())),
314       Environment(parseEnvironment(getEnvironmentName())) {
315 }
316
317 /// \brief Construct a triple from string representations of the architecture,
318 /// vendor, and OS.
319 ///
320 /// This joins each argument into a canonical string representation and parses
321 /// them into enum members. It leaves the environment unknown and omits it from
322 /// the string representation.
323 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
324     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
325       Arch(parseArch(ArchStr.str())),
326       Vendor(parseVendor(VendorStr.str())),
327       OS(parseOS(OSStr.str())),
328       Environment() {
329 }
330
331 /// \brief Construct a triple from string representations of the architecture,
332 /// vendor, OS, and environment.
333 ///
334 /// This joins each argument into a canonical string representation and parses
335 /// them into enum members.
336 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
337                const Twine &EnvironmentStr)
338     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
339             EnvironmentStr).str()),
340       Arch(parseArch(ArchStr.str())),
341       Vendor(parseVendor(VendorStr.str())),
342       OS(parseOS(OSStr.str())),
343       Environment(parseEnvironment(EnvironmentStr.str())) {
344 }
345
346 std::string Triple::normalize(StringRef Str) {
347   // Parse into components.
348   SmallVector<StringRef, 4> Components;
349   Str.split(Components, "-");
350
351   // If the first component corresponds to a known architecture, preferentially
352   // use it for the architecture.  If the second component corresponds to a
353   // known vendor, preferentially use it for the vendor, etc.  This avoids silly
354   // component movement when a component parses as (eg) both a valid arch and a
355   // valid os.
356   ArchType Arch = UnknownArch;
357   if (Components.size() > 0)
358     Arch = parseArch(Components[0]);
359   VendorType Vendor = UnknownVendor;
360   if (Components.size() > 1)
361     Vendor = parseVendor(Components[1]);
362   OSType OS = UnknownOS;
363   if (Components.size() > 2)
364     OS = parseOS(Components[2]);
365   EnvironmentType Environment = UnknownEnvironment;
366   if (Components.size() > 3)
367     Environment = parseEnvironment(Components[3]);
368
369   // Note which components are already in their final position.  These will not
370   // be moved.
371   bool Found[4];
372   Found[0] = Arch != UnknownArch;
373   Found[1] = Vendor != UnknownVendor;
374   Found[2] = OS != UnknownOS;
375   Found[3] = Environment != UnknownEnvironment;
376
377   // If they are not there already, permute the components into their canonical
378   // positions by seeing if they parse as a valid architecture, and if so moving
379   // the component to the architecture position etc.
380   for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
381     if (Found[Pos])
382       continue; // Already in the canonical position.
383
384     for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
385       // Do not reparse any components that already matched.
386       if (Idx < array_lengthof(Found) && Found[Idx])
387         continue;
388
389       // Does this component parse as valid for the target position?
390       bool Valid = false;
391       StringRef Comp = Components[Idx];
392       switch (Pos) {
393       default: llvm_unreachable("unexpected component type!");
394       case 0:
395         Arch = parseArch(Comp);
396         Valid = Arch != UnknownArch;
397         break;
398       case 1:
399         Vendor = parseVendor(Comp);
400         Valid = Vendor != UnknownVendor;
401         break;
402       case 2:
403         OS = parseOS(Comp);
404         Valid = OS != UnknownOS;
405         break;
406       case 3:
407         Environment = parseEnvironment(Comp);
408         Valid = Environment != UnknownEnvironment;
409         break;
410       }
411       if (!Valid)
412         continue; // Nope, try the next component.
413
414       // Move the component to the target position, pushing any non-fixed
415       // components that are in the way to the right.  This tends to give
416       // good results in the common cases of a forgotten vendor component
417       // or a wrongly positioned environment.
418       if (Pos < Idx) {
419         // Insert left, pushing the existing components to the right.  For
420         // example, a-b-i386 -> i386-a-b when moving i386 to the front.
421         StringRef CurrentComponent(""); // The empty component.
422         // Replace the component we are moving with an empty component.
423         std::swap(CurrentComponent, Components[Idx]);
424         // Insert the component being moved at Pos, displacing any existing
425         // components to the right.
426         for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
427           // Skip over any fixed components.
428           while (i < array_lengthof(Found) && Found[i])
429             ++i;
430           // Place the component at the new position, getting the component
431           // that was at this position - it will be moved right.
432           std::swap(CurrentComponent, Components[i]);
433         }
434       } else if (Pos > Idx) {
435         // Push right by inserting empty components until the component at Idx
436         // reaches the target position Pos.  For example, pc-a -> -pc-a when
437         // moving pc to the second position.
438         do {
439           // Insert one empty component at Idx.
440           StringRef CurrentComponent(""); // The empty component.
441           for (unsigned i = Idx; i < Components.size();) {
442             // Place the component at the new position, getting the component
443             // that was at this position - it will be moved right.
444             std::swap(CurrentComponent, Components[i]);
445             // If it was placed on top of an empty component then we are done.
446             if (CurrentComponent.empty())
447               break;
448             // Advance to the next component, skipping any fixed components.
449             while (++i < array_lengthof(Found) && Found[i])
450               ;
451           }
452           // The last component was pushed off the end - append it.
453           if (!CurrentComponent.empty())
454             Components.push_back(CurrentComponent);
455
456           // Advance Idx to the component's new position.
457           while (++Idx < array_lengthof(Found) && Found[Idx])
458             ;
459         } while (Idx < Pos); // Add more until the final position is reached.
460       }
461       assert(Pos < Components.size() && Components[Pos] == Comp &&
462              "Component moved wrong!");
463       Found[Pos] = true;
464       break;
465     }
466   }
467
468   // Special case logic goes here.  At this point Arch, Vendor and OS have the
469   // correct values for the computed components.
470
471   // Stick the corrected components back together to form the normalized string.
472   std::string Normalized;
473   for (unsigned i = 0, e = Components.size(); i != e; ++i) {
474     if (i) Normalized += '-';
475     Normalized += Components[i];
476   }
477   return Normalized;
478 }
479
480 StringRef Triple::getArchName() const {
481   return StringRef(Data).split('-').first;           // Isolate first component
482 }
483
484 StringRef Triple::getVendorName() const {
485   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
486   return Tmp.split('-').first;                       // Isolate second component
487 }
488
489 StringRef Triple::getOSName() const {
490   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
491   Tmp = Tmp.split('-').second;                       // Strip second component
492   return Tmp.split('-').first;                       // Isolate third component
493 }
494
495 StringRef Triple::getEnvironmentName() const {
496   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
497   Tmp = Tmp.split('-').second;                       // Strip second component
498   return Tmp.split('-').second;                      // Strip third component
499 }
500
501 StringRef Triple::getOSAndEnvironmentName() const {
502   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
503   return Tmp.split('-').second;                      // Strip second component
504 }
505
506 static unsigned EatNumber(StringRef &Str) {
507   assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
508   unsigned Result = 0;
509
510   do {
511     // Consume the leading digit.
512     Result = Result*10 + (Str[0] - '0');
513
514     // Eat the digit.
515     Str = Str.substr(1);
516   } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
517
518   return Result;
519 }
520
521 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
522                           unsigned &Micro) const {
523   StringRef OSName = getOSName();
524
525   // Assume that the OS portion of the triple starts with the canonical name.
526   StringRef OSTypeName = getOSTypeName(getOS());
527   if (OSName.startswith(OSTypeName))
528     OSName = OSName.substr(OSTypeName.size());
529
530   // Any unset version defaults to 0.
531   Major = Minor = Micro = 0;
532
533   // Parse up to three components.
534   unsigned *Components[3] = { &Major, &Minor, &Micro };
535   for (unsigned i = 0; i != 3; ++i) {
536     if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
537       break;
538
539     // Consume the leading number.
540     *Components[i] = EatNumber(OSName);
541
542     // Consume the separator, if present.
543     if (OSName.startswith("."))
544       OSName = OSName.substr(1);
545   }
546 }
547
548 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
549                               unsigned &Micro) const {
550   getOSVersion(Major, Minor, Micro);
551
552   switch (getOS()) {
553   default: llvm_unreachable("unexpected OS for Darwin triple");
554   case Darwin:
555     // Default to darwin8, i.e., MacOSX 10.4.
556     if (Major == 0)
557       Major = 8;
558     // Darwin version numbers are skewed from OS X versions.
559     if (Major < 4)
560       return false;
561     Micro = 0;
562     Minor = Major - 4;
563     Major = 10;
564     break;
565   case MacOSX:
566     // Default to 10.4.
567     if (Major == 0) {
568       Major = 10;
569       Minor = 4;
570     }
571     if (Major != 10)
572       return false;
573     break;
574   case IOS:
575     // Ignore the version from the triple.  This is only handled because the
576     // the clang driver combines OS X and IOS support into a common Darwin
577     // toolchain that wants to know the OS X version number even when targeting
578     // IOS.
579     Major = 10;
580     Minor = 4;
581     Micro = 0;
582     break;
583   }
584   return true;
585 }
586
587 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
588                            unsigned &Micro) const {
589   switch (getOS()) {
590   default: llvm_unreachable("unexpected OS for Darwin triple");
591   case Darwin:
592   case MacOSX:
593     // Ignore the version from the triple.  This is only handled because the
594     // the clang driver combines OS X and IOS support into a common Darwin
595     // toolchain that wants to know the iOS version number even when targeting
596     // OS X.
597     Major = 3;
598     Minor = 0;
599     Micro = 0;
600     break;
601   case IOS:
602     getOSVersion(Major, Minor, Micro);
603     // Default to 3.0.
604     if (Major == 0)
605       Major = 3;
606     break;
607   }
608 }
609
610 void Triple::setTriple(const Twine &Str) {
611   *this = Triple(Str);
612 }
613
614 void Triple::setArch(ArchType Kind) {
615   setArchName(getArchTypeName(Kind));
616 }
617
618 void Triple::setVendor(VendorType Kind) {
619   setVendorName(getVendorTypeName(Kind));
620 }
621
622 void Triple::setOS(OSType Kind) {
623   setOSName(getOSTypeName(Kind));
624 }
625
626 void Triple::setEnvironment(EnvironmentType Kind) {
627   setEnvironmentName(getEnvironmentTypeName(Kind));
628 }
629
630 void Triple::setArchName(StringRef Str) {
631   // Work around a miscompilation bug for Twines in gcc 4.0.3.
632   SmallString<64> Triple;
633   Triple += Str;
634   Triple += "-";
635   Triple += getVendorName();
636   Triple += "-";
637   Triple += getOSAndEnvironmentName();
638   setTriple(Triple.str());
639 }
640
641 void Triple::setVendorName(StringRef Str) {
642   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
643 }
644
645 void Triple::setOSName(StringRef Str) {
646   if (hasEnvironment())
647     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
648               "-" + getEnvironmentName());
649   else
650     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
651 }
652
653 void Triple::setEnvironmentName(StringRef Str) {
654   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
655             "-" + Str);
656 }
657
658 void Triple::setOSAndEnvironmentName(StringRef Str) {
659   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
660 }
661
662 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
663   switch (Arch) {
664   case llvm::Triple::UnknownArch:
665     return 0;
666
667   case llvm::Triple::msp430:
668     return 16;
669
670   case llvm::Triple::amdil:
671   case llvm::Triple::arm:
672   case llvm::Triple::cellspu:
673   case llvm::Triple::hexagon:
674   case llvm::Triple::le32:
675   case llvm::Triple::mblaze:
676   case llvm::Triple::mips:
677   case llvm::Triple::mipsel:
678   case llvm::Triple::nvptx:
679   case llvm::Triple::ppc:
680   case llvm::Triple::r600:
681   case llvm::Triple::sparc:
682   case llvm::Triple::tce:
683   case llvm::Triple::thumb:
684   case llvm::Triple::x86:
685   case llvm::Triple::xcore:
686     return 32;
687
688   case llvm::Triple::mips64:
689   case llvm::Triple::mips64el:
690   case llvm::Triple::nvptx64:
691   case llvm::Triple::ppc64:
692   case llvm::Triple::sparcv9:
693   case llvm::Triple::x86_64:
694     return 64;
695   }
696   llvm_unreachable("Invalid architecture value");
697 }
698
699 bool Triple::isArch64Bit() const {
700   return getArchPointerBitWidth(getArch()) == 64;
701 }
702
703 bool Triple::isArch32Bit() const {
704   return getArchPointerBitWidth(getArch()) == 32;
705 }
706
707 bool Triple::isArch16Bit() const {
708   return getArchPointerBitWidth(getArch()) == 16;
709 }
710
711 Triple Triple::get32BitArchVariant() const {
712   Triple T(*this);
713   switch (getArch()) {
714   case Triple::UnknownArch:
715   case Triple::msp430:
716     T.setArch(UnknownArch);
717     break;
718
719   case Triple::amdil:
720   case Triple::arm:
721   case Triple::cellspu:
722   case Triple::hexagon:
723   case Triple::le32:
724   case Triple::mblaze:
725   case Triple::mips:
726   case Triple::mipsel:
727   case Triple::nvptx:
728   case Triple::ppc:
729   case Triple::r600:
730   case Triple::sparc:
731   case Triple::tce:
732   case Triple::thumb:
733   case Triple::x86:
734   case Triple::xcore:
735     // Already 32-bit.
736     break;
737
738   case Triple::mips64:    T.setArch(Triple::mips);    break;
739   case Triple::mips64el:  T.setArch(Triple::mipsel);  break;
740   case Triple::nvptx64:   T.setArch(Triple::nvptx);   break;
741   case Triple::ppc64:     T.setArch(Triple::ppc);   break;
742   case Triple::sparcv9:   T.setArch(Triple::sparc);   break;
743   case Triple::x86_64:    T.setArch(Triple::x86);     break;
744   }
745   return T;
746 }
747
748 Triple Triple::get64BitArchVariant() const {
749   Triple T(*this);
750   switch (getArch()) {
751   case Triple::UnknownArch:
752   case Triple::amdil:
753   case Triple::arm:
754   case Triple::cellspu:
755   case Triple::hexagon:
756   case Triple::le32:
757   case Triple::mblaze:
758   case Triple::msp430:
759   case Triple::r600:
760   case Triple::tce:
761   case Triple::thumb:
762   case Triple::xcore:
763     T.setArch(UnknownArch);
764     break;
765
766   case Triple::mips64:
767   case Triple::mips64el:
768   case Triple::nvptx64:
769   case Triple::ppc64:
770   case Triple::sparcv9:
771   case Triple::x86_64:
772     // Already 64-bit.
773     break;
774
775   case Triple::mips:    T.setArch(Triple::mips64);    break;
776   case Triple::mipsel:  T.setArch(Triple::mips64el);  break;
777   case Triple::nvptx:   T.setArch(Triple::nvptx64);   break;
778   case Triple::ppc:     T.setArch(Triple::ppc64);     break;
779   case Triple::sparc:   T.setArch(Triple::sparcv9);   break;
780   case Triple::x86:     T.setArch(Triple::x86_64);    break;
781   }
782   return T;
783 }