blob: 9e91642d9752733e0700aa8ba1c37bbb4a85a077 [file] [log] [blame]
Daniel Dunbar4abd5662009-04-01 21:53:23 +00001//===--- 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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskine2595b52009-10-06 21:45:26 +000012#include "llvm/ADT/SmallString.h"
Chandler Carruthff6f3562012-02-12 09:27:38 +000013#include "llvm/ADT/StringSwitch.h"
David Blaikie46a9f012012-01-20 21:51:11 +000014#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov12062ba2009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar4abd5662009-04-01 21:53:23 +000016using namespace llvm;
17
Daniel Dunbar4abd5662009-04-01 21:53:23 +000018const char *Triple::getArchTypeName(ArchType Kind) {
19 switch (Kind) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +000020 case UnknownArch: return "unknown";
Jim Grosbachf638b262010-12-17 02:10:59 +000021
Christian Pirker6c2f4d42014-02-24 11:34:50 +000022 case aarch64: return "aarch64";
23 case aarch64_be: return "aarch64_be";
24 case arm: return "arm";
Christian Pirker2a111602014-03-28 14:35:30 +000025 case armeb: return "armeb";
Tim Northover00ed9962014-03-29 10:18:08 +000026 case arm64: return "arm64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000027 case hexagon: return "hexagon";
28 case mips: return "mips";
29 case mipsel: return "mipsel";
30 case mips64: return "mips64";
31 case mips64el: return "mips64el";
32 case msp430: return "msp430";
33 case ppc64: return "powerpc64";
34 case ppc64le: return "powerpc64le";
35 case ppc: return "powerpc";
36 case r600: return "r600";
37 case sparc: return "sparc";
38 case sparcv9: return "sparcv9";
39 case systemz: return "s390x";
40 case tce: return "tce";
41 case thumb: return "thumb";
Christian Pirker2a111602014-03-28 14:35:30 +000042 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000043 case x86: return "i386";
44 case x86_64: return "x86_64";
45 case xcore: return "xcore";
46 case nvptx: return "nvptx";
47 case nvptx64: return "nvptx64";
48 case le32: return "le32";
49 case amdil: return "amdil";
50 case spir: return "spir";
51 case spir64: return "spir64";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000052 }
53
David Blaikie46a9f012012-01-20 21:51:11 +000054 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000055}
56
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000057const char *Triple::getArchTypePrefix(ArchType Kind) {
58 switch (Kind) {
59 default:
60 return 0;
61
Christian Pirker6c2f4d42014-02-24 11:34:50 +000062 case aarch64:
63 case aarch64_be: return "aarch64";
Tim Northovere0e3aef2013-01-31 12:12:40 +000064
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000065 case arm:
Christian Pirker2a111602014-03-28 14:35:30 +000066 case armeb:
67 case thumb:
68 case thumbeb: return "arm";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000069
Tim Northover00ed9962014-03-29 10:18:08 +000070 case arm64: return "arm64";
71
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000072 case ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +000073 case ppc64le:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000074 case ppc: return "ppc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000075
Benjamin Kramerae3c3002012-06-28 19:09:53 +000076 case mips:
77 case mipsel:
78 case mips64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000079 case mips64el: return "mips";
Benjamin Kramerae3c3002012-06-28 19:09:53 +000080
Christian Pirker6c2f4d42014-02-24 11:34:50 +000081 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082
Christian Pirker6c2f4d42014-02-24 11:34:50 +000083 case r600: return "r600";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000084
Chris Lattner8228b112010-02-04 06:34:01 +000085 case sparcv9:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000086 case sparc: return "sparc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000087
Christian Pirker6c2f4d42014-02-24 11:34:50 +000088 case systemz: return "systemz";
Richard Sandiforda238c5e2013-05-03 11:05:17 +000089
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000090 case x86:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000091 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000092
Christian Pirker6c2f4d42014-02-24 11:34:50 +000093 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000094
Christian Pirker6c2f4d42014-02-24 11:34:50 +000095 case nvptx: return "nvptx";
96 case nvptx64: return "nvptx";
Tim Northover00ed9962014-03-29 10:18:08 +000097
Christian Pirker6c2f4d42014-02-24 11:34:50 +000098 case le32: return "le32";
99 case amdil: return "amdil";
100 case spir: return "spir";
101 case spir64: return "spir";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000102 }
103}
104
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000105const char *Triple::getVendorTypeName(VendorType Kind) {
106 switch (Kind) {
107 case UnknownVendor: return "unknown";
108
109 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000110 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000111 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000112 case BGP: return "bgp";
113 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000114 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000115 case IBM: return "ibm";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000116 case NVIDIA: return "nvidia";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000117 }
118
David Blaikie46a9f012012-01-20 21:51:11 +0000119 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000120}
121
122const char *Triple::getOSTypeName(OSType Kind) {
123 switch (Kind) {
124 case UnknownOS: return "unknown";
125
Duncan Sands0de39b42009-06-19 14:40:01 +0000126 case AuroraUX: return "auroraux";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000127 case Cygwin: return "cygwin";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000128 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000129 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000130 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000131 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000132 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000133 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000134 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000135 case MacOSX: return "macosx";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000136 case MinGW32: return "mingw32";
Chris Lattner01218d52009-07-13 20:22:23 +0000137 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000138 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000139 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000140 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000141 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000142 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000143 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000144 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000145 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000146 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000147 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000148 case CUDA: return "cuda";
149 case NVCL: return "nvcl";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000150 }
151
David Blaikie46a9f012012-01-20 21:51:11 +0000152 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000153}
154
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000155const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
156 switch (Kind) {
157 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000158 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000159 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000160 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000161 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000162 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000163 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000164 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000165 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000166 case MSVC: return "msvc";
167 case Itanium: return "itanium";
168 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000169 }
170
David Blaikie46a9f012012-01-20 21:51:11 +0000171 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000172}
173
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000174Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000175 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000176 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000177 .Case("aarch64_be", aarch64_be)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000178 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000179 .Case("armeb", armeb)
Tim Northover00ed9962014-03-29 10:18:08 +0000180 .Case("arm64", arm64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000181 .Case("mips", mips)
182 .Case("mipsel", mipsel)
183 .Case("mips64", mips64)
184 .Case("mips64el", mips64el)
185 .Case("msp430", msp430)
186 .Case("ppc64", ppc64)
187 .Case("ppc32", ppc)
188 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000189 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000190 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000191 .Case("hexagon", hexagon)
192 .Case("sparc", sparc)
193 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000194 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000195 .Case("tce", tce)
196 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000197 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000198 .Case("x86", x86)
199 .Case("x86-64", x86_64)
200 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000201 .Case("nvptx", nvptx)
202 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000203 .Case("le32", le32)
204 .Case("amdil", amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000205 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000206 .Case("spir64", spir64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000207 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000208}
209
Duncan Sands8712da32010-03-24 09:05:14 +0000210// Returns architecture name that is understood by the target assembler.
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000211const char *Triple::getArchNameForAssembler() {
Daniel Dunbar163a0962011-04-19 21:12:05 +0000212 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000213 return NULL;
214
Chandler Carruthff6f3562012-02-12 09:27:38 +0000215 return StringSwitch<const char*>(getArchName())
216 .Case("i386", "i386")
217 .Case("x86_64", "x86_64")
218 .Case("powerpc", "ppc")
219 .Case("powerpc64", "ppc64")
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000220 .Case("powerpc64le", "ppc64le")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000221 .Case("arm", "arm")
222 .Cases("armv4t", "thumbv4t", "armv4t")
223 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
224 .Cases("armv6", "thumbv6", "armv6")
225 .Cases("armv7", "thumbv7", "armv7")
Christian Pirker2a111602014-03-28 14:35:30 +0000226 .Case("armeb", "armeb")
Tim Northover00ed9962014-03-29 10:18:08 +0000227 .Case("arm64", "arm64")
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000228 .Case("r600", "r600")
Justin Holewinskiae556d32012-05-04 20:18:50 +0000229 .Case("nvptx", "nvptx")
230 .Case("nvptx64", "nvptx64")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000231 .Case("le32", "le32")
232 .Case("amdil", "amdil")
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000233 .Case("spir", "spir")
Guy Benyeia4d31a32012-11-15 10:35:47 +0000234 .Case("spir64", "spir64")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000235 .Default(NULL);
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000236}
237
Chandler Carruthaec97082012-02-21 08:53:32 +0000238static Triple::ArchType parseArch(StringRef ArchName) {
239 return StringSwitch<Triple::ArchType>(ArchName)
240 .Cases("i386", "i486", "i586", "i686", Triple::x86)
241 // FIXME: Do we need to support these?
242 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000243 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000244 .Case("powerpc", Triple::ppc)
245 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000246 .Case("powerpc64le", Triple::ppc64le)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000247 .Case("aarch64", Triple::aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000248 .Case("aarch64_be", Triple::aarch64_be)
Chandler Carruthaec97082012-02-21 08:53:32 +0000249 .Cases("arm", "xscale", Triple::arm)
Chandler Carruthb54950b2012-02-18 04:34:17 +0000250 // FIXME: It would be good to replace these with explicit names for all the
251 // various suffixes supported.
Chandler Carruthaec97082012-02-21 08:53:32 +0000252 .StartsWith("armv", Triple::arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000253 .Case("armeb", Triple::armeb)
254 .StartsWith("armebv", Triple::armeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000255 .Case("thumb", Triple::thumb)
256 .StartsWith("thumbv", Triple::thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000257 .Case("thumbeb", Triple::thumbeb)
258 .StartsWith("thumbebv", Triple::thumbeb)
Tim Northover00ed9962014-03-29 10:18:08 +0000259 .Case("arm64", Triple::arm64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000260 .Case("msp430", Triple::msp430)
261 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000262 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000263 .Cases("mips64", "mips64eb", Triple::mips64)
264 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000265 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000266 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000267 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000268 .Case("sparc", Triple::sparc)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000269 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000270 .Case("tce", Triple::tce)
271 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000272 .Case("nvptx", Triple::nvptx)
273 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000274 .Case("le32", Triple::le32)
275 .Case("amdil", Triple::amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000276 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000277 .Case("spir64", Triple::spir64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000278 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000279}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000280
Chandler Carruthaec97082012-02-21 08:53:32 +0000281static Triple::VendorType parseVendor(StringRef VendorName) {
282 return StringSwitch<Triple::VendorType>(VendorName)
283 .Case("apple", Triple::Apple)
284 .Case("pc", Triple::PC)
285 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000286 .Case("bgp", Triple::BGP)
287 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000288 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000289 .Case("ibm", Triple::IBM)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000290 .Case("nvidia", Triple::NVIDIA)
Chandler Carruthaec97082012-02-21 08:53:32 +0000291 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000292}
293
Chandler Carruthaec97082012-02-21 08:53:32 +0000294static Triple::OSType parseOS(StringRef OSName) {
295 return StringSwitch<Triple::OSType>(OSName)
296 .StartsWith("auroraux", Triple::AuroraUX)
297 .StartsWith("cygwin", Triple::Cygwin)
298 .StartsWith("darwin", Triple::Darwin)
299 .StartsWith("dragonfly", Triple::DragonFly)
300 .StartsWith("freebsd", Triple::FreeBSD)
301 .StartsWith("ios", Triple::IOS)
302 .StartsWith("kfreebsd", Triple::KFreeBSD)
303 .StartsWith("linux", Triple::Linux)
304 .StartsWith("lv2", Triple::Lv2)
305 .StartsWith("macosx", Triple::MacOSX)
306 .StartsWith("mingw32", Triple::MinGW32)
307 .StartsWith("netbsd", Triple::NetBSD)
308 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000309 .StartsWith("solaris", Triple::Solaris)
310 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000311 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000312 .StartsWith("haiku", Triple::Haiku)
313 .StartsWith("minix", Triple::Minix)
314 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000315 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000316 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000317 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000318 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000319 .StartsWith("cuda", Triple::CUDA)
320 .StartsWith("nvcl", Triple::NVCL)
Chandler Carruthaec97082012-02-21 08:53:32 +0000321 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000322}
323
Chandler Carruthaec97082012-02-21 08:53:32 +0000324static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
325 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000326 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000327 .StartsWith("eabi", Triple::EABI)
328 .StartsWith("gnueabihf", Triple::GNUEABIHF)
329 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000330 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000331 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000332 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000333 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000334 .StartsWith("msvc", Triple::MSVC)
335 .StartsWith("itanium", Triple::Itanium)
336 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000337 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000338}
339
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000340static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
341 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
342 .EndsWith("coff", Triple::COFF)
343 .EndsWith("elf", Triple::ELF)
344 .EndsWith("macho", Triple::MachO)
345 .Default(Triple::UnknownObjectFormat);
346}
347
348static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
349 switch (Kind) {
350 case Triple::UnknownObjectFormat: return "";
351 case Triple::COFF: return "coff";
352 case Triple::ELF: return "elf";
353 case Triple::MachO: return "macho";
354 }
355 llvm_unreachable("unknown object format type");
356}
357
358static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
359 if (T.isOSDarwin())
360 return Triple::MachO;
361 else if (T.isOSWindows())
362 return Triple::COFF;
363 return Triple::ELF;
364}
365
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000366/// \brief Construct a triple from the string representation provided.
367///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000368/// This stores the string representation and parses the various pieces into
369/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000370Triple::Triple(const Twine &Str)
371 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000372 Arch(parseArch(getArchName())),
373 Vendor(parseVendor(getVendorName())),
374 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000375 Environment(parseEnvironment(getEnvironmentName())),
376 ObjectFormat(parseFormat(getEnvironmentName())) {
377 if (ObjectFormat == Triple::UnknownObjectFormat)
378 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000379}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000380
381/// \brief Construct a triple from string representations of the architecture,
382/// vendor, and OS.
383///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000384/// This joins each argument into a canonical string representation and parses
385/// them into enum members. It leaves the environment unknown and omits it from
386/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000387Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
388 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000389 Arch(parseArch(ArchStr.str())),
390 Vendor(parseVendor(VendorStr.str())),
391 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000392 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
393 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000394}
395
396/// \brief Construct a triple from string representations of the architecture,
397/// vendor, OS, and environment.
398///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000399/// This joins each argument into a canonical string representation and parses
400/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000401Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
402 const Twine &EnvironmentStr)
403 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
404 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000405 Arch(parseArch(ArchStr.str())),
406 Vendor(parseVendor(VendorStr.str())),
407 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000408 Environment(parseEnvironment(EnvironmentStr.str())),
409 ObjectFormat(parseFormat(EnvironmentStr.str())) {
410 if (ObjectFormat == Triple::UnknownObjectFormat)
411 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000412}
413
Duncan Sands501dff72010-08-12 11:31:39 +0000414std::string Triple::normalize(StringRef Str) {
415 // Parse into components.
416 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000417 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000418
419 // If the first component corresponds to a known architecture, preferentially
420 // use it for the architecture. If the second component corresponds to a
421 // known vendor, preferentially use it for the vendor, etc. This avoids silly
422 // component movement when a component parses as (eg) both a valid arch and a
423 // valid os.
424 ArchType Arch = UnknownArch;
425 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000426 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000427 VendorType Vendor = UnknownVendor;
428 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000429 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000430 OSType OS = UnknownOS;
431 if (Components.size() > 2)
Chandler Carruthaec97082012-02-21 08:53:32 +0000432 OS = parseOS(Components[2]);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000433 EnvironmentType Environment = UnknownEnvironment;
434 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000435 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000436 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000437 if (Components.size() > 4)
438 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000439
440 // Note which components are already in their final position. These will not
441 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000442 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000443 Found[0] = Arch != UnknownArch;
444 Found[1] = Vendor != UnknownVendor;
445 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000446 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000447
448 // If they are not there already, permute the components into their canonical
449 // positions by seeing if they parse as a valid architecture, and if so moving
450 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000451 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000452 if (Found[Pos])
453 continue; // Already in the canonical position.
454
455 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
456 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000457 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000458 continue;
459
460 // Does this component parse as valid for the target position?
461 bool Valid = false;
462 StringRef Comp = Components[Idx];
463 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000464 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000465 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000466 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000467 Valid = Arch != UnknownArch;
468 break;
469 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000470 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000471 Valid = Vendor != UnknownVendor;
472 break;
473 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000474 OS = parseOS(Comp);
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000475 Valid = OS != UnknownOS;
Duncan Sands501dff72010-08-12 11:31:39 +0000476 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000477 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000478 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000479 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000480 if (!Valid) {
481 ObjectFormat = parseFormat(Comp);
482 Valid = ObjectFormat != UnknownObjectFormat;
483 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000484 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000485 }
486 if (!Valid)
487 continue; // Nope, try the next component.
488
489 // Move the component to the target position, pushing any non-fixed
490 // components that are in the way to the right. This tends to give
491 // good results in the common cases of a forgotten vendor component
492 // or a wrongly positioned environment.
493 if (Pos < Idx) {
494 // Insert left, pushing the existing components to the right. For
495 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
496 StringRef CurrentComponent(""); // The empty component.
497 // Replace the component we are moving with an empty component.
498 std::swap(CurrentComponent, Components[Idx]);
499 // Insert the component being moved at Pos, displacing any existing
500 // components to the right.
501 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
502 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000503 while (i < array_lengthof(Found) && Found[i])
504 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000505 // Place the component at the new position, getting the component
506 // that was at this position - it will be moved right.
507 std::swap(CurrentComponent, Components[i]);
508 }
509 } else if (Pos > Idx) {
510 // Push right by inserting empty components until the component at Idx
511 // reaches the target position Pos. For example, pc-a -> -pc-a when
512 // moving pc to the second position.
513 do {
514 // Insert one empty component at Idx.
515 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000516 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000517 // Place the component at the new position, getting the component
518 // that was at this position - it will be moved right.
519 std::swap(CurrentComponent, Components[i]);
520 // If it was placed on top of an empty component then we are done.
521 if (CurrentComponent.empty())
522 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000523 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000524 while (++i < array_lengthof(Found) && Found[i])
525 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000526 }
527 // The last component was pushed off the end - append it.
528 if (!CurrentComponent.empty())
529 Components.push_back(CurrentComponent);
530
531 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000532 while (++Idx < array_lengthof(Found) && Found[Idx])
533 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000534 } while (Idx < Pos); // Add more until the final position is reached.
535 }
536 assert(Pos < Components.size() && Components[Pos] == Comp &&
537 "Component moved wrong!");
538 Found[Pos] = true;
539 break;
540 }
541 }
542
543 // Special case logic goes here. At this point Arch, Vendor and OS have the
544 // correct values for the computed components.
545
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000546 if (OS == Triple::Win32) {
547 Components.resize(4);
548 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000549 if (Environment == UnknownEnvironment) {
550 if (ObjectFormat == UnknownObjectFormat)
551 Components[3] = "msvc";
552 else
553 Components[3] = getObjectFormatTypeName(ObjectFormat);
554 } else if (ObjectFormat != UnknownObjectFormat &&
555 ObjectFormat != Triple::COFF) {
556 Components.resize(5);
557 Components[4] = getObjectFormatTypeName(ObjectFormat);
558 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000559 } else if (OS == Triple::MinGW32) {
560 Components.resize(4);
561 Components[2] = "windows";
562 Components[3] = (ObjectFormat == Triple::ELF) ? "gnuelf" : "gnu";
563 } else if (OS == Triple::Cygwin) {
564 Components.resize(4);
565 Components[2] = "windows";
566 Components[3] = "cygnus";
567 }
568
Duncan Sands501dff72010-08-12 11:31:39 +0000569 // Stick the corrected components back together to form the normalized string.
570 std::string Normalized;
571 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
572 if (i) Normalized += '-';
573 Normalized += Components[i];
574 }
575 return Normalized;
576}
577
Daniel Dunbar19e70762009-07-26 03:31:47 +0000578StringRef Triple::getArchName() const {
579 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000580}
581
Daniel Dunbar19e70762009-07-26 03:31:47 +0000582StringRef Triple::getVendorName() const {
583 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
584 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000585}
586
Daniel Dunbar19e70762009-07-26 03:31:47 +0000587StringRef Triple::getOSName() const {
588 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
589 Tmp = Tmp.split('-').second; // Strip second component
590 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000591}
592
Daniel Dunbar19e70762009-07-26 03:31:47 +0000593StringRef Triple::getEnvironmentName() const {
594 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
595 Tmp = Tmp.split('-').second; // Strip second component
596 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000597}
598
Daniel Dunbar19e70762009-07-26 03:31:47 +0000599StringRef Triple::getOSAndEnvironmentName() const {
600 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
601 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000602}
603
Chris Lattner553c9f32009-08-12 06:19:40 +0000604static unsigned EatNumber(StringRef &Str) {
605 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000606 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000607
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000608 do {
609 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000610 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000611
Chris Lattner553c9f32009-08-12 06:19:40 +0000612 // Eat the digit.
613 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000614 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000615
Chris Lattner553c9f32009-08-12 06:19:40 +0000616 return Result;
617}
618
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000619void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
620 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000621 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000622
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000623 // Assume that the OS portion of the triple starts with the canonical name.
624 StringRef OSTypeName = getOSTypeName(getOS());
625 if (OSName.startswith(OSTypeName))
626 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000627
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000628 // Any unset version defaults to 0.
629 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000630
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000631 // Parse up to three components.
632 unsigned *Components[3] = { &Major, &Minor, &Micro };
633 for (unsigned i = 0; i != 3; ++i) {
634 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
635 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000636
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000637 // Consume the leading number.
638 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000639
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000640 // Consume the separator, if present.
641 if (OSName.startswith("."))
642 OSName = OSName.substr(1);
643 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000644}
645
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000646bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
647 unsigned &Micro) const {
648 getOSVersion(Major, Minor, Micro);
649
650 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000651 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000652 case Darwin:
653 // Default to darwin8, i.e., MacOSX 10.4.
654 if (Major == 0)
655 Major = 8;
656 // Darwin version numbers are skewed from OS X versions.
657 if (Major < 4)
658 return false;
659 Micro = 0;
660 Minor = Major - 4;
661 Major = 10;
662 break;
663 case MacOSX:
664 // Default to 10.4.
665 if (Major == 0) {
666 Major = 10;
667 Minor = 4;
668 }
669 if (Major != 10)
670 return false;
671 break;
672 case IOS:
673 // Ignore the version from the triple. This is only handled because the
674 // the clang driver combines OS X and IOS support into a common Darwin
675 // toolchain that wants to know the OS X version number even when targeting
676 // IOS.
677 Major = 10;
678 Minor = 4;
679 Micro = 0;
680 break;
681 }
682 return true;
683}
684
Chad Rosierd84eaac2012-05-09 17:23:48 +0000685void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
686 unsigned &Micro) const {
687 switch (getOS()) {
688 default: llvm_unreachable("unexpected OS for Darwin triple");
689 case Darwin:
690 case MacOSX:
691 // Ignore the version from the triple. This is only handled because the
692 // the clang driver combines OS X and IOS support into a common Darwin
693 // toolchain that wants to know the iOS version number even when targeting
694 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000695 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000696 Minor = 0;
697 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000698 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000699 case IOS:
700 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000701 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000702 if (Major == 0)
Tim Northover00ed9962014-03-29 10:18:08 +0000703 Major = (getArch() == arm64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000704 break;
705 }
706}
707
Daniel Dunbar19e70762009-07-26 03:31:47 +0000708void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000709 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000710}
711
712void Triple::setArch(ArchType Kind) {
713 setArchName(getArchTypeName(Kind));
714}
715
716void Triple::setVendor(VendorType Kind) {
717 setVendorName(getVendorTypeName(Kind));
718}
719
720void Triple::setOS(OSType Kind) {
721 setOSName(getOSTypeName(Kind));
722}
723
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000724void Triple::setEnvironment(EnvironmentType Kind) {
725 setEnvironmentName(getEnvironmentTypeName(Kind));
726}
727
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000728void Triple::setObjectFormat(ObjectFormatType Kind) {
729 setEnvironmentName(getObjectFormatTypeName(Kind));
730}
731
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000732void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000733 // Work around a miscompilation bug for Twines in gcc 4.0.3.
734 SmallString<64> Triple;
735 Triple += Str;
736 Triple += "-";
737 Triple += getVendorName();
738 Triple += "-";
739 Triple += getOSAndEnvironmentName();
740 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000741}
742
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000743void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000744 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
745}
746
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000747void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000748 if (hasEnvironment())
749 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
750 "-" + getEnvironmentName());
751 else
752 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
753}
754
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000755void Triple::setEnvironmentName(StringRef Str) {
756 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000757 "-" + Str);
758}
759
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000760void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000761 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
762}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000763
764static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
765 switch (Arch) {
766 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000767 return 0;
768
769 case llvm::Triple::msp430:
770 return 16;
771
772 case llvm::Triple::amdil:
773 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000774 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000775 case llvm::Triple::hexagon:
776 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000777 case llvm::Triple::mips:
778 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000779 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000780 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000781 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000782 case llvm::Triple::sparc:
783 case llvm::Triple::tce:
784 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000785 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000786 case llvm::Triple::x86:
787 case llvm::Triple::xcore:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000788 case llvm::Triple::spir:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000789 return 32;
790
Tim Northover00ed9962014-03-29 10:18:08 +0000791 case llvm::Triple::arm64:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000792 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000793 case llvm::Triple::aarch64_be:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000794 case llvm::Triple::mips64:
795 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000796 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000797 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000798 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000799 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000800 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000801 case llvm::Triple::x86_64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000802 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000803 return 64;
804 }
805 llvm_unreachable("Invalid architecture value");
806}
807
808bool Triple::isArch64Bit() const {
809 return getArchPointerBitWidth(getArch()) == 64;
810}
811
812bool Triple::isArch32Bit() const {
813 return getArchPointerBitWidth(getArch()) == 32;
814}
815
816bool Triple::isArch16Bit() const {
817 return getArchPointerBitWidth(getArch()) == 16;
818}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000819
820Triple Triple::get32BitArchVariant() const {
821 Triple T(*this);
822 switch (getArch()) {
823 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000824 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000825 case Triple::aarch64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000826 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000827 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000828 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000829 T.setArch(UnknownArch);
830 break;
831
832 case Triple::amdil:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000833 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000834 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000835 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000836 case Triple::hexagon:
837 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000838 case Triple::mips:
839 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000840 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000841 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000842 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000843 case Triple::sparc:
844 case Triple::tce:
845 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000846 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000847 case Triple::x86:
848 case Triple::xcore:
849 // Already 32-bit.
850 break;
851
852 case Triple::mips64: T.setArch(Triple::mips); break;
853 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000854 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000855 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000856 case Triple::sparcv9: T.setArch(Triple::sparc); break;
857 case Triple::x86_64: T.setArch(Triple::x86); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000858 case Triple::spir64: T.setArch(Triple::spir); break;
Tim Northover00ed9962014-03-29 10:18:08 +0000859 case Triple::arm64: T.setArch(Triple::arm); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000860 }
861 return T;
862}
863
864Triple Triple::get64BitArchVariant() const {
865 Triple T(*this);
866 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000867 case Triple::UnknownArch:
868 case Triple::amdil:
Christian Pirker2a111602014-03-28 14:35:30 +0000869 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000870 case Triple::hexagon:
871 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000872 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000873 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000874 case Triple::tce:
875 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000876 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000877 case Triple::xcore:
878 T.setArch(UnknownArch);
879 break;
880
Tim Northovere0e3aef2013-01-31 12:12:40 +0000881 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000882 case Triple::aarch64_be:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000883 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000884 case Triple::mips64:
885 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000886 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000887 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000888 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000889 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000890 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000891 case Triple::x86_64:
Tim Northover00ed9962014-03-29 10:18:08 +0000892 case Triple::arm64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000893 // Already 64-bit.
894 break;
895
896 case Triple::mips: T.setArch(Triple::mips64); break;
897 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000898 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000899 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000900 case Triple::sparc: T.setArch(Triple::sparcv9); break;
901 case Triple::x86: T.setArch(Triple::x86_64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000902 case Triple::spir: T.setArch(Triple::spir64); break;
Tim Northover00ed9962014-03-29 10:18:08 +0000903 case Triple::arm: T.setArch(Triple::arm64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000904 }
905 return T;
906}