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