1 //===--- Triple.cpp - Target triple helper class --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Triple.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/Twine.h"
21 const char *Triple::getArchTypeName(ArchType Kind
) {
23 case InvalidArch
: return "<invalid>";
24 case UnknownArch
: return "unknown";
26 case alpha
: return "alpha";
27 case arm
: return "arm";
28 case bfin
: return "bfin";
29 case cellspu
: return "cellspu";
30 case mips
: return "mips";
31 case mipsel
: return "mipsel";
32 case msp430
: return "msp430";
33 case pic16
: return "pic16";
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 ptx
: return "ptx";
46 case zpu
: return "zpu";
52 const char *Triple::getArchTypePrefix(ArchType Kind
) {
57 case alpha
: return "alpha";
60 case thumb
: return "arm";
62 case bfin
: return "bfin";
64 case cellspu
: return "spu";
67 case ppc
: return "ppc";
69 case mblaze
: return "mblaze";
72 case sparc
: return "sparc";
75 case x86_64
: return "x86";
77 case xcore
: return "xcore";
79 case ptx
: return "ptx";
80 case zpu
: return "zpu";
84 const char *Triple::getVendorTypeName(VendorType Kind
) {
86 case UnknownVendor
: return "unknown";
88 case Apple
: return "apple";
95 const char *Triple::getOSTypeName(OSType Kind
) {
97 case UnknownOS
: return "unknown";
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 Linux
: return "linux";
105 case Lv2
: return "lv2";
106 case MinGW32
: return "mingw32";
107 case MinGW64
: return "mingw64";
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";
120 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind
) {
122 case UnknownEnvironment
: return "unknown";
128 Triple::ArchType
Triple::getArchTypeForLLVMName(StringRef Name
) {
135 if (Name
== "cellspu")
139 if (Name
== "mipsel")
141 if (Name
== "msp430")
149 if (Name
== "mblaze")
153 if (Name
== "sparcv9")
155 if (Name
== "systemz")
163 if (Name
== "x86-64")
175 Triple::ArchType
Triple::getArchTypeForDarwinArchName(StringRef Str
) {
176 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
177 // archs which Darwin doesn't use.
179 // The matching this routine does is fairly pointless, since it is neither the
180 // complete architecture list, nor a reasonable subset. The problem is that
181 // historically the driver driver accepts this and also ties its -march=
182 // handling to the architecture name, so we need to be careful before removing
185 // This code must be kept in sync with Clang's Darwin specific argument
188 if (Str
== "ppc" || Str
== "ppc601" || Str
== "ppc603" || Str
== "ppc604" ||
189 Str
== "ppc604e" || Str
== "ppc750" || Str
== "ppc7400" ||
190 Str
== "ppc7450" || Str
== "ppc970")
194 return Triple::ppc64
;
196 if (Str
== "i386" || Str
== "i486" || Str
== "i486SX" || Str
== "pentium" ||
197 Str
== "i586" || Str
== "pentpro" || Str
== "i686" || Str
== "pentIIm3" ||
198 Str
== "pentIIm5" || Str
== "pentium4")
202 return Triple::x86_64
;
204 // This is derived from the driver driver.
205 if (Str
== "arm" || Str
== "armv4t" || Str
== "armv5" || Str
== "xscale" ||
206 Str
== "armv6" || Str
== "armv7")
212 return Triple::UnknownArch
;
215 // Returns architecture name that is understood by the target assembler.
216 const char *Triple::getArchNameForAssembler() {
217 if (getOS() != Triple::Darwin
&& getVendor() != Triple::Apple
)
220 StringRef Str
= getArchName();
225 if (Str
== "powerpc")
227 if (Str
== "powerpc64")
229 if (Str
== "mblaze" || Str
== "microblaze")
233 if (Str
== "armv4t" || Str
== "thumbv4t")
235 if (Str
== "armv5" || Str
== "armv5e" || Str
== "thumbv5" || Str
== "thumbv5e")
237 if (Str
== "armv6" || Str
== "thumbv6")
239 if (Str
== "armv7" || Str
== "thumbv7")
250 Triple::ArchType
Triple::ParseArch(StringRef ArchName
) {
251 if (ArchName
.size() == 4 && ArchName
[0] == 'i' &&
252 ArchName
[2] == '8' && ArchName
[3] == '6' &&
253 ArchName
[1] - '3' < 6) // i[3-9]86
255 else if (ArchName
== "amd64" || ArchName
== "x86_64")
257 else if (ArchName
== "bfin")
259 else if (ArchName
== "pic16")
261 else if (ArchName
== "powerpc")
263 else if ((ArchName
== "powerpc64") || (ArchName
== "ppu"))
265 else if (ArchName
== "mblaze")
267 else if (ArchName
== "arm" ||
268 ArchName
.startswith("armv") ||
269 ArchName
== "xscale")
271 else if (ArchName
== "thumb" ||
272 ArchName
.startswith("thumbv"))
274 else if (ArchName
.startswith("alpha"))
276 else if (ArchName
== "spu" || ArchName
== "cellspu")
278 else if (ArchName
== "msp430")
280 else if (ArchName
== "mips" || ArchName
== "mipsallegrex")
282 else if (ArchName
== "mipsel" || ArchName
== "mipsallegrexel" ||
285 else if (ArchName
== "sparc")
287 else if (ArchName
== "sparcv9")
289 else if (ArchName
== "s390x")
291 else if (ArchName
== "tce")
293 else if (ArchName
== "xcore")
295 else if (ArchName
== "ptx")
297 else if (ArchName
== "zpu")
303 Triple::VendorType
Triple::ParseVendor(StringRef VendorName
) {
304 if (VendorName
== "apple")
306 else if (VendorName
== "pc")
309 return UnknownVendor
;
312 Triple::OSType
Triple::ParseOS(StringRef OSName
) {
313 if (OSName
.startswith("auroraux"))
315 else if (OSName
.startswith("cygwin"))
317 else if (OSName
.startswith("darwin"))
319 else if (OSName
.startswith("dragonfly"))
321 else if (OSName
.startswith("freebsd"))
323 else if (OSName
.startswith("linux"))
325 else if (OSName
.startswith("lv2"))
327 else if (OSName
.startswith("mingw32"))
329 else if (OSName
.startswith("mingw64"))
331 else if (OSName
.startswith("netbsd"))
333 else if (OSName
.startswith("openbsd"))
335 else if (OSName
.startswith("psp"))
337 else if (OSName
.startswith("solaris"))
339 else if (OSName
.startswith("win32"))
341 else if (OSName
.startswith("haiku"))
343 else if (OSName
.startswith("minix"))
349 Triple::EnvironmentType
Triple::ParseEnvironment(StringRef EnvironmentName
) {
350 return UnknownEnvironment
;
353 void Triple::Parse() const {
354 assert(!isInitialized() && "Invalid parse call.");
356 Arch
= ParseArch(getArchName());
357 Vendor
= ParseVendor(getVendorName());
358 OS
= ParseOS(getOSName());
359 Environment
= ParseEnvironment(getEnvironmentName());
361 assert(isInitialized() && "Failed to initialize!");
364 std::string
Triple::normalize(StringRef Str
) {
365 // Parse into components.
366 SmallVector
<StringRef
, 4> Components
;
367 for (size_t First
= 0, Last
= 0; Last
!= StringRef::npos
; First
= Last
+ 1) {
368 Last
= Str
.find('-', First
);
369 Components
.push_back(Str
.slice(First
, Last
));
372 // If the first component corresponds to a known architecture, preferentially
373 // use it for the architecture. If the second component corresponds to a
374 // known vendor, preferentially use it for the vendor, etc. This avoids silly
375 // component movement when a component parses as (eg) both a valid arch and a
377 ArchType Arch
= UnknownArch
;
378 if (Components
.size() > 0)
379 Arch
= ParseArch(Components
[0]);
380 VendorType Vendor
= UnknownVendor
;
381 if (Components
.size() > 1)
382 Vendor
= ParseVendor(Components
[1]);
383 OSType OS
= UnknownOS
;
384 if (Components
.size() > 2)
385 OS
= ParseOS(Components
[2]);
386 EnvironmentType Environment
= UnknownEnvironment
;
387 if (Components
.size() > 3)
388 Environment
= ParseEnvironment(Components
[3]);
390 // Note which components are already in their final position. These will not
393 Found
[0] = Arch
!= UnknownArch
;
394 Found
[1] = Vendor
!= UnknownVendor
;
395 Found
[2] = OS
!= UnknownOS
;
396 Found
[3] = Environment
!= UnknownEnvironment
;
398 // If they are not there already, permute the components into their canonical
399 // positions by seeing if they parse as a valid architecture, and if so moving
400 // the component to the architecture position etc.
401 for (unsigned Pos
= 0; Pos
!= array_lengthof(Found
); ++Pos
) {
403 continue; // Already in the canonical position.
405 for (unsigned Idx
= 0; Idx
!= Components
.size(); ++Idx
) {
406 // Do not reparse any components that already matched.
407 if (Idx
< array_lengthof(Found
) && Found
[Idx
])
410 // Does this component parse as valid for the target position?
412 StringRef Comp
= Components
[Idx
];
415 assert(false && "unexpected component type!");
417 Arch
= ParseArch(Comp
);
418 Valid
= Arch
!= UnknownArch
;
421 Vendor
= ParseVendor(Comp
);
422 Valid
= Vendor
!= UnknownVendor
;
426 Valid
= OS
!= UnknownOS
;
429 Environment
= ParseEnvironment(Comp
);
430 Valid
= Environment
!= UnknownEnvironment
;
434 continue; // Nope, try the next component.
436 // Move the component to the target position, pushing any non-fixed
437 // components that are in the way to the right. This tends to give
438 // good results in the common cases of a forgotten vendor component
439 // or a wrongly positioned environment.
441 // Insert left, pushing the existing components to the right. For
442 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
443 StringRef
CurrentComponent(""); // The empty component.
444 // Replace the component we are moving with an empty component.
445 std::swap(CurrentComponent
, Components
[Idx
]);
446 // Insert the component being moved at Pos, displacing any existing
447 // components to the right.
448 for (unsigned i
= Pos
; !CurrentComponent
.empty(); ++i
) {
449 // Skip over any fixed components.
450 while (i
< array_lengthof(Found
) && Found
[i
]) ++i
;
451 // Place the component at the new position, getting the component
452 // that was at this position - it will be moved right.
453 std::swap(CurrentComponent
, Components
[i
]);
455 } else if (Pos
> Idx
) {
456 // Push right by inserting empty components until the component at Idx
457 // reaches the target position Pos. For example, pc-a -> -pc-a when
458 // moving pc to the second position.
460 // Insert one empty component at Idx.
461 StringRef
CurrentComponent(""); // The empty component.
462 for (unsigned i
= Idx
; i
< Components
.size(); ++i
) {
463 // Skip over any fixed components.
464 while (i
< array_lengthof(Found
) && Found
[i
]) ++i
;
465 // Place the component at the new position, getting the component
466 // that was at this position - it will be moved right.
467 std::swap(CurrentComponent
, Components
[i
]);
468 // If it was placed on top of an empty component then we are done.
469 if (CurrentComponent
.empty())
472 // The last component was pushed off the end - append it.
473 if (!CurrentComponent
.empty())
474 Components
.push_back(CurrentComponent
);
476 // Advance Idx to the component's new position.
477 while (++Idx
< array_lengthof(Found
) && Found
[Idx
]) {}
478 } while (Idx
< Pos
); // Add more until the final position is reached.
480 assert(Pos
< Components
.size() && Components
[Pos
] == Comp
&&
481 "Component moved wrong!");
487 // Special case logic goes here. At this point Arch, Vendor and OS have the
488 // correct values for the computed components.
490 // Stick the corrected components back together to form the normalized string.
491 std::string Normalized
;
492 for (unsigned i
= 0, e
= Components
.size(); i
!= e
; ++i
) {
493 if (i
) Normalized
+= '-';
494 Normalized
+= Components
[i
];
499 StringRef
Triple::getArchName() const {
500 return StringRef(Data
).split('-').first
; // Isolate first component
503 StringRef
Triple::getVendorName() const {
504 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
505 return Tmp
.split('-').first
; // Isolate second component
508 StringRef
Triple::getOSName() const {
509 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
510 Tmp
= Tmp
.split('-').second
; // Strip second component
511 return Tmp
.split('-').first
; // Isolate third component
514 StringRef
Triple::getEnvironmentName() const {
515 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
516 Tmp
= Tmp
.split('-').second
; // Strip second component
517 return Tmp
.split('-').second
; // Strip third component
520 StringRef
Triple::getOSAndEnvironmentName() const {
521 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
522 return Tmp
.split('-').second
; // Strip second component
525 static unsigned EatNumber(StringRef
&Str
) {
526 assert(!Str
.empty() && Str
[0] >= '0' && Str
[0] <= '9' && "Not a number");
527 unsigned Result
= Str
[0]-'0';
532 // Handle "darwin11".
533 if (Result
== 1 && !Str
.empty() && Str
[0] >= '0' && Str
[0] <= '9') {
534 Result
= Result
*10 + (Str
[0] - '0');
542 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
543 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
544 /// not defined, return 0's. This requires that the triple have an OSType of
545 /// darwin before it is called.
546 void Triple::getDarwinNumber(unsigned &Maj
, unsigned &Min
,
547 unsigned &Revision
) const {
548 assert(getOS() == Darwin
&& "Not a darwin target triple!");
549 StringRef OSName
= getOSName();
550 assert(OSName
.startswith("darwin") && "Unknown darwin target triple!");
552 // Strip off "darwin".
553 OSName
= OSName
.substr(6);
555 Maj
= Min
= Revision
= 0;
557 if (OSName
.empty() || OSName
[0] < '0' || OSName
[0] > '9')
560 // The major version is the first digit.
561 Maj
= EatNumber(OSName
);
562 if (OSName
.empty()) return;
564 // Handle minor version: 10.4.9 -> darwin8.9.
565 if (OSName
[0] != '.')
569 OSName
= OSName
.substr(1);
571 if (OSName
.empty() || OSName
[0] < '0' || OSName
[0] > '9')
574 Min
= EatNumber(OSName
);
575 if (OSName
.empty()) return;
577 // Handle revision darwin8.9.1
578 if (OSName
[0] != '.')
582 OSName
= OSName
.substr(1);
584 if (OSName
.empty() || OSName
[0] < '0' || OSName
[0] > '9')
587 Revision
= EatNumber(OSName
);
590 void Triple::setTriple(const Twine
&Str
) {
595 void Triple::setArch(ArchType Kind
) {
596 setArchName(getArchTypeName(Kind
));
599 void Triple::setVendor(VendorType Kind
) {
600 setVendorName(getVendorTypeName(Kind
));
603 void Triple::setOS(OSType Kind
) {
604 setOSName(getOSTypeName(Kind
));
607 void Triple::setEnvironment(EnvironmentType Kind
) {
608 setEnvironmentName(getEnvironmentTypeName(Kind
));
611 void Triple::setArchName(StringRef Str
) {
612 // Work around a miscompilation bug for Twines in gcc 4.0.3.
613 SmallString
<64> Triple
;
616 Triple
+= getVendorName();
618 Triple
+= getOSAndEnvironmentName();
619 setTriple(Triple
.str());
622 void Triple::setVendorName(StringRef Str
) {
623 setTriple(getArchName() + "-" + Str
+ "-" + getOSAndEnvironmentName());
626 void Triple::setOSName(StringRef Str
) {
627 if (hasEnvironment())
628 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
+
629 "-" + getEnvironmentName());
631 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
);
634 void Triple::setEnvironmentName(StringRef Str
) {
635 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
639 void Triple::setOSAndEnvironmentName(StringRef Str
) {
640 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
);