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