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