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