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/Twine.h"
19 const char *Triple::getArchTypeName(ArchType Kind
) {
21 case InvalidArch
: return "<invalid>";
22 case UnknownArch
: return "unknown";
24 case alpha
: return "alpha";
25 case arm
: return "arm";
26 case bfin
: return "bfin";
27 case cellspu
: return "cellspu";
28 case mips
: return "mips";
29 case mipsel
: return "mipsel";
30 case msp430
: return "msp430";
31 case pic16
: return "pic16";
32 case ppc64
: return "powerpc64";
33 case ppc
: return "powerpc";
34 case sparc
: return "sparc";
35 case systemz
: return "s390x";
36 case tce
: return "tce";
37 case thumb
: return "thumb";
38 case x86
: return "i386";
39 case x86_64
: return "x86_64";
40 case xcore
: return "xcore";
46 const char *Triple::getArchTypePrefix(ArchType Kind
) {
51 case alpha
: return "alpha";
54 case thumb
: return "arm";
56 case bfin
: return "bfin";
58 case cellspu
: return "spu";
61 case ppc
: return "ppc";
63 case sparc
: return "sparc";
66 case x86_64
: return "x86";
67 case xcore
: return "xcore";
71 const char *Triple::getVendorTypeName(VendorType Kind
) {
73 case UnknownVendor
: return "unknown";
75 case Apple
: return "apple";
82 const char *Triple::getOSTypeName(OSType Kind
) {
84 case UnknownOS
: return "unknown";
86 case AuroraUX
: return "auroraux";
87 case Cygwin
: return "cygwin";
88 case Darwin
: return "darwin";
89 case DragonFly
: return "dragonfly";
90 case FreeBSD
: return "freebsd";
91 case Linux
: return "linux";
92 case MinGW32
: return "mingw32";
93 case MinGW64
: return "mingw64";
94 case NetBSD
: return "netbsd";
95 case OpenBSD
: return "openbsd";
96 case Solaris
: return "solaris";
97 case Win32
: return "win32";
103 Triple::ArchType
Triple::getArchTypeForLLVMName(const StringRef
&Name
) {
110 if (Name
== "cellspu")
114 if (Name
== "mipsel")
116 if (Name
== "msp430")
126 if (Name
== "systemz")
134 if (Name
== "x86-64")
144 void Triple::Parse() const {
145 assert(!isInitialized() && "Invalid parse call.");
147 StringRef ArchName
= getArchName();
148 StringRef VendorName
= getVendorName();
149 StringRef OSName
= getOSName();
151 if (ArchName
.size() == 4 && ArchName
[0] == 'i' &&
152 ArchName
[2] == '8' && ArchName
[3] == '6' &&
153 ArchName
[1] - '3' < 6) // i[3-9]86
155 else if (ArchName
== "amd64" || ArchName
== "x86_64")
157 else if (ArchName
== "bfin")
159 else if (ArchName
== "pic16")
161 else if (ArchName
== "powerpc")
163 else if (ArchName
== "powerpc64")
165 else if (ArchName
== "arm" ||
166 ArchName
.startswith("armv") ||
167 ArchName
== "xscale")
169 else if (ArchName
== "thumb" ||
170 ArchName
.startswith("thumbv"))
172 else if (ArchName
.startswith("alpha"))
174 else if (ArchName
== "spu" || ArchName
== "cellspu")
176 else if (ArchName
== "msp430")
178 else if (ArchName
== "mips" || ArchName
== "mipsallegrex")
180 else if (ArchName
== "mipsel" || ArchName
== "mipsallegrexel" ||
183 else if (ArchName
== "sparc")
185 else if (ArchName
== "s390x")
187 else if (ArchName
== "tce")
189 else if (ArchName
== "xcore")
195 // Handle some exceptional cases where the OS / environment components are
196 // stuck into the vendor field.
197 if (StringRef(getTriple()).count('-') == 1) {
198 StringRef VendorName
= getVendorName();
200 if (VendorName
.startswith("mingw32")) { // 'i386-mingw32', etc.
206 // arm-elf is another example, but we don't currently parse anything about
210 if (VendorName
== "apple")
212 else if (VendorName
== "pc")
215 Vendor
= UnknownVendor
;
217 if (OSName
.startswith("auroraux"))
219 else if (OSName
.startswith("cygwin"))
221 else if (OSName
.startswith("darwin"))
223 else if (OSName
.startswith("dragonfly"))
225 else if (OSName
.startswith("freebsd"))
227 else if (OSName
.startswith("linux"))
229 else if (OSName
.startswith("mingw32"))
231 else if (OSName
.startswith("mingw64"))
233 else if (OSName
.startswith("netbsd"))
235 else if (OSName
.startswith("openbsd"))
237 else if (OSName
.startswith("solaris"))
239 else if (OSName
.startswith("win32"))
244 assert(isInitialized() && "Failed to initialize!");
247 StringRef
Triple::getArchName() const {
248 return StringRef(Data
).split('-').first
; // Isolate first component
251 StringRef
Triple::getVendorName() const {
252 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
253 return Tmp
.split('-').first
; // Isolate second component
256 StringRef
Triple::getOSName() const {
257 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
258 Tmp
= Tmp
.split('-').second
; // Strip second component
259 return Tmp
.split('-').first
; // Isolate third component
262 StringRef
Triple::getEnvironmentName() const {
263 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
264 Tmp
= Tmp
.split('-').second
; // Strip second component
265 return Tmp
.split('-').second
; // Strip third component
268 StringRef
Triple::getOSAndEnvironmentName() const {
269 StringRef Tmp
= StringRef(Data
).split('-').second
; // Strip first component
270 return Tmp
.split('-').second
; // Strip second component
273 static unsigned EatNumber(StringRef
&Str
) {
274 assert(!Str
.empty() && Str
[0] >= '0' && Str
[0] <= '9' && "Not a number");
275 unsigned Result
= Str
[0]-'0';
280 // Handle "darwin11".
281 if (Result
== 1 && !Str
.empty() && Str
[0] >= '0' && Str
[0] <= '9') {
282 Result
= Result
*10 + (Str
[0] - '0');
290 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
291 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
292 /// not defined, return 0's. This requires that the triple have an OSType of
293 /// darwin before it is called.
294 void Triple::getDarwinNumber(unsigned &Maj
, unsigned &Min
,
295 unsigned &Revision
) const {
296 assert(getOS() == Darwin
&& "Not a darwin target triple!");
297 StringRef OSName
= getOSName();
298 assert(OSName
.startswith("darwin") && "Unknown darwin target triple!");
300 // Strip off "darwin".
301 OSName
= OSName
.substr(6);
303 Maj
= Min
= Revision
= 0;
305 if (OSName
.empty() || OSName
[0] < '0' || OSName
[0] > '9')
308 // The major version is the first digit.
309 Maj
= EatNumber(OSName
);
310 if (OSName
.empty()) return;
312 // Handle minor version: 10.4.9 -> darwin8.9.
313 if (OSName
[0] != '.')
317 OSName
= OSName
.substr(1);
319 if (OSName
.empty() || OSName
[0] < '0' || OSName
[0] > '9')
322 Min
= EatNumber(OSName
);
323 if (OSName
.empty()) return;
325 // Handle revision darwin8.9.1
326 if (OSName
[0] != '.')
330 OSName
= OSName
.substr(1);
332 if (OSName
.empty() || OSName
[0] < '0' || OSName
[0] > '9')
335 Revision
= EatNumber(OSName
);
338 void Triple::setTriple(const Twine
&Str
) {
343 void Triple::setArch(ArchType Kind
) {
344 setArchName(getArchTypeName(Kind
));
347 void Triple::setVendor(VendorType Kind
) {
348 setVendorName(getVendorTypeName(Kind
));
351 void Triple::setOS(OSType Kind
) {
352 setOSName(getOSTypeName(Kind
));
355 void Triple::setArchName(const StringRef
&Str
) {
356 setTriple(Str
+ "-" + getVendorName() + "-" + getOSAndEnvironmentName());
359 void Triple::setVendorName(const StringRef
&Str
) {
360 setTriple(getArchName() + "-" + Str
+ "-" + getOSAndEnvironmentName());
363 void Triple::setOSName(const StringRef
&Str
) {
364 if (hasEnvironment())
365 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
+
366 "-" + getEnvironmentName());
368 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
);
371 void Triple::setEnvironmentName(const StringRef
&Str
) {
372 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
376 void Triple::setOSAndEnvironmentName(const StringRef
&Str
) {
377 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
);