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