blob: 72cc2a8eb9aca7b0158b0c04c1258ebc215c55f2 [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";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000026 case hexagon: return "hexagon";
27 case mips: return "mips";
28 case mipsel: return "mipsel";
29 case mips64: return "mips64";
30 case mips64el: return "mips64el";
31 case msp430: return "msp430";
32 case ppc64: return "powerpc64";
33 case ppc64le: return "powerpc64le";
34 case ppc: return "powerpc";
35 case r600: return "r600";
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";
Christian Pirker2a111602014-03-28 14:35:30 +000041 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000042 case x86: return "i386";
43 case x86_64: return "x86_64";
44 case xcore: return "xcore";
45 case nvptx: return "nvptx";
46 case nvptx64: return "nvptx64";
47 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +000048 case le64: return "le64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000049 case amdil: return "amdil";
50 case spir: return "spir";
51 case spir64: return "spir64";
Eric Christopher54fe1b22014-07-10 17:26:54 +000052 case kalimba: return "kalimba";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000053 }
54
David Blaikie46a9f012012-01-20 21:51:11 +000055 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000056}
57
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000058const char *Triple::getArchTypePrefix(ArchType Kind) {
59 switch (Kind) {
60 default:
Craig Topperc10719f2014-04-07 04:17:22 +000061 return nullptr;
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000062
Christian Pirker6c2f4d42014-02-24 11:34:50 +000063 case aarch64:
64 case aarch64_be: return "aarch64";
Tim Northovere0e3aef2013-01-31 12:12:40 +000065
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000066 case arm:
Christian Pirker2a111602014-03-28 14:35:30 +000067 case armeb:
68 case thumb:
69 case thumbeb: return "arm";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000070
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000071 case ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +000072 case ppc64le:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000073 case ppc: return "ppc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000074
Benjamin Kramerae3c3002012-06-28 19:09:53 +000075 case mips:
76 case mipsel:
77 case mips64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000078 case mips64el: return "mips";
Benjamin Kramerae3c3002012-06-28 19:09:53 +000079
Christian Pirker6c2f4d42014-02-24 11:34:50 +000080 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000081
Christian Pirker6c2f4d42014-02-24 11:34:50 +000082 case r600: return "r600";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000083
Chris Lattner8228b112010-02-04 06:34:01 +000084 case sparcv9:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000085 case sparc: return "sparc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000086
Christian Pirker6c2f4d42014-02-24 11:34:50 +000087 case systemz: return "systemz";
Richard Sandiforda238c5e2013-05-03 11:05:17 +000088
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000089 case x86:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000090 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000091
Christian Pirker6c2f4d42014-02-24 11:34:50 +000092 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000093
Christian Pirker6c2f4d42014-02-24 11:34:50 +000094 case nvptx: return "nvptx";
95 case nvptx64: return "nvptx";
Tim Northover00ed9962014-03-29 10:18:08 +000096
Christian Pirker6c2f4d42014-02-24 11:34:50 +000097 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +000098 case le64: return "le64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000099 case amdil: return "amdil";
100 case spir: return "spir";
101 case spir64: return "spir";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000102 case kalimba: return "kalimba";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000103 }
104}
105
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000106const char *Triple::getVendorTypeName(VendorType Kind) {
107 switch (Kind) {
108 case UnknownVendor: return "unknown";
109
110 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000111 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000112 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000113 case BGP: return "bgp";
114 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000115 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000116 case IBM: return "ibm";
Daniel Sandersc5626f42014-07-09 16:03:10 +0000117 case ImaginationTechnologies: return "img";
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000118 case MipsTechnologies: return "mti";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000119 case NVIDIA: return "nvidia";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000120 case CSR: return "csr";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000121 }
122
David Blaikie46a9f012012-01-20 21:51:11 +0000123 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000124}
125
126const char *Triple::getOSTypeName(OSType Kind) {
127 switch (Kind) {
128 case UnknownOS: return "unknown";
129
130 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000131 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000132 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000133 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000134 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000135 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000136 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000137 case MacOSX: return "macosx";
Chris Lattner01218d52009-07-13 20:22:23 +0000138 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000139 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000140 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000141 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000142 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000143 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000144 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000145 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000146 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000147 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000148 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000149 case CUDA: return "cuda";
150 case NVCL: return "nvcl";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000151 }
152
David Blaikie46a9f012012-01-20 21:51:11 +0000153 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000154}
155
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000156const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
157 switch (Kind) {
158 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000159 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000160 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000161 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000162 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000163 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000164 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000165 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000166 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000167 case MSVC: return "msvc";
168 case Itanium: return "itanium";
169 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000170 }
171
David Blaikie46a9f012012-01-20 21:51:11 +0000172 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000173}
174
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000175Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000176 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000177 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000178 .Case("aarch64_be", aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000179 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
Chandler Carruthff6f3562012-02-12 09:27:38 +0000180 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000181 .Case("armeb", armeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000182 .Case("mips", mips)
183 .Case("mipsel", mipsel)
184 .Case("mips64", mips64)
185 .Case("mips64el", mips64el)
186 .Case("msp430", msp430)
187 .Case("ppc64", ppc64)
188 .Case("ppc32", ppc)
189 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000190 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000191 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000192 .Case("hexagon", hexagon)
193 .Case("sparc", sparc)
194 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000195 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000196 .Case("tce", tce)
197 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000198 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000199 .Case("x86", x86)
200 .Case("x86-64", x86_64)
201 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000202 .Case("nvptx", nvptx)
203 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000204 .Case("le32", le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000205 .Case("le64", le64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000206 .Case("amdil", amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000207 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000208 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000209 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000210 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000211}
212
Chandler Carruthaec97082012-02-21 08:53:32 +0000213static Triple::ArchType parseArch(StringRef ArchName) {
214 return StringSwitch<Triple::ArchType>(ArchName)
215 .Cases("i386", "i486", "i586", "i686", Triple::x86)
216 // FIXME: Do we need to support these?
217 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000218 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000219 .Case("powerpc", Triple::ppc)
220 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000221 .Case("powerpc64le", Triple::ppc64le)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000222 .Case("aarch64", Triple::aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000223 .Case("aarch64_be", Triple::aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000224 .Case("arm64", Triple::aarch64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000225 .Cases("arm", "xscale", Triple::arm)
Chandler Carruthb54950b2012-02-18 04:34:17 +0000226 // FIXME: It would be good to replace these with explicit names for all the
227 // various suffixes supported.
Chandler Carruthaec97082012-02-21 08:53:32 +0000228 .StartsWith("armv", Triple::arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000229 .Case("armeb", Triple::armeb)
230 .StartsWith("armebv", Triple::armeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000231 .Case("thumb", Triple::thumb)
232 .StartsWith("thumbv", Triple::thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000233 .Case("thumbeb", Triple::thumbeb)
234 .StartsWith("thumbebv", Triple::thumbeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000235 .Case("msp430", Triple::msp430)
236 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000237 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000238 .Cases("mips64", "mips64eb", Triple::mips64)
239 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000240 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000241 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000242 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000243 .Case("sparc", Triple::sparc)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000244 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000245 .Case("tce", Triple::tce)
246 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000247 .Case("nvptx", Triple::nvptx)
248 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000249 .Case("le32", Triple::le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000250 .Case("le64", Triple::le64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000251 .Case("amdil", Triple::amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000252 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000253 .Case("spir64", Triple::spir64)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000254 .StartsWith("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000255 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000256}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000257
Chandler Carruthaec97082012-02-21 08:53:32 +0000258static Triple::VendorType parseVendor(StringRef VendorName) {
259 return StringSwitch<Triple::VendorType>(VendorName)
260 .Case("apple", Triple::Apple)
261 .Case("pc", Triple::PC)
262 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000263 .Case("bgp", Triple::BGP)
264 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000265 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000266 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000267 .Case("img", Triple::ImaginationTechnologies)
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000268 .Case("mti", Triple::MipsTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000269 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000270 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000271 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000272}
273
Chandler Carruthaec97082012-02-21 08:53:32 +0000274static Triple::OSType parseOS(StringRef OSName) {
275 return StringSwitch<Triple::OSType>(OSName)
Chandler Carruthaec97082012-02-21 08:53:32 +0000276 .StartsWith("darwin", Triple::Darwin)
277 .StartsWith("dragonfly", Triple::DragonFly)
278 .StartsWith("freebsd", Triple::FreeBSD)
279 .StartsWith("ios", Triple::IOS)
280 .StartsWith("kfreebsd", Triple::KFreeBSD)
281 .StartsWith("linux", Triple::Linux)
282 .StartsWith("lv2", Triple::Lv2)
283 .StartsWith("macosx", Triple::MacOSX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000284 .StartsWith("netbsd", Triple::NetBSD)
285 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000286 .StartsWith("solaris", Triple::Solaris)
287 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000288 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000289 .StartsWith("haiku", Triple::Haiku)
290 .StartsWith("minix", Triple::Minix)
291 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000292 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000293 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000294 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000295 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000296 .StartsWith("cuda", Triple::CUDA)
297 .StartsWith("nvcl", Triple::NVCL)
Chandler Carruthaec97082012-02-21 08:53:32 +0000298 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000299}
300
Chandler Carruthaec97082012-02-21 08:53:32 +0000301static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
302 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000303 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000304 .StartsWith("eabi", Triple::EABI)
305 .StartsWith("gnueabihf", Triple::GNUEABIHF)
306 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000307 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000308 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000309 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000310 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000311 .StartsWith("msvc", Triple::MSVC)
312 .StartsWith("itanium", Triple::Itanium)
313 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000314 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000315}
316
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000317static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
318 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
319 .EndsWith("coff", Triple::COFF)
320 .EndsWith("elf", Triple::ELF)
321 .EndsWith("macho", Triple::MachO)
322 .Default(Triple::UnknownObjectFormat);
323}
324
Renato Golinc17a07b2014-07-18 12:00:48 +0000325static Triple::SubArchType parseSubArch(StringRef SubArchName) {
326 return StringSwitch<Triple::SubArchType>(SubArchName)
327 .EndsWith("v8", Triple::ARMSubArch_v8)
328 .EndsWith("v8a", Triple::ARMSubArch_v8)
329 .EndsWith("v7", Triple::ARMSubArch_v7)
330 .EndsWith("v7a", Triple::ARMSubArch_v7)
331 .EndsWith("v7em", Triple::ARMSubArch_v7em)
332 .EndsWith("v7l", Triple::ARMSubArch_v7)
333 .EndsWith("v7m", Triple::ARMSubArch_v7m)
334 .EndsWith("v7r", Triple::ARMSubArch_v7)
335 .EndsWith("v7s", Triple::ARMSubArch_v7s)
336 .EndsWith("v6", Triple::ARMSubArch_v6)
337 .EndsWith("v6m", Triple::ARMSubArch_v6m)
338 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
339 .EndsWith("v5", Triple::ARMSubArch_v5)
340 .EndsWith("v5e", Triple::ARMSubArch_v5)
341 .EndsWith("v5t", Triple::ARMSubArch_v5)
342 .EndsWith("v5te", Triple::ARMSubArch_v5te)
343 .EndsWith("v4t", Triple::ARMSubArch_v4t)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000344 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
345 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
346 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
Renato Golinc17a07b2014-07-18 12:00:48 +0000347 .Default(Triple::NoSubArch);
348}
349
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000350static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
351 switch (Kind) {
352 case Triple::UnknownObjectFormat: return "";
353 case Triple::COFF: return "coff";
354 case Triple::ELF: return "elf";
355 case Triple::MachO: return "macho";
356 }
357 llvm_unreachable("unknown object format type");
358}
359
360static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
361 if (T.isOSDarwin())
362 return Triple::MachO;
363 else if (T.isOSWindows())
364 return Triple::COFF;
365 return Triple::ELF;
366}
367
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000368/// \brief Construct a triple from the string representation provided.
369///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000370/// This stores the string representation and parses the various pieces into
371/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000372Triple::Triple(const Twine &Str)
373 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000374 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000375 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000376 Vendor(parseVendor(getVendorName())),
377 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000378 Environment(parseEnvironment(getEnvironmentName())),
379 ObjectFormat(parseFormat(getEnvironmentName())) {
380 if (ObjectFormat == Triple::UnknownObjectFormat)
381 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000382}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000383
384/// \brief Construct a triple from string representations of the architecture,
385/// vendor, and OS.
386///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000387/// This joins each argument into a canonical string representation and parses
388/// them into enum members. It leaves the environment unknown and omits it from
389/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000390Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
391 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000392 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000393 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000394 Vendor(parseVendor(VendorStr.str())),
395 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000396 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
397 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000398}
399
400/// \brief Construct a triple from string representations of the architecture,
401/// vendor, OS, and environment.
402///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000403/// This joins each argument into a canonical string representation and parses
404/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000405Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
406 const Twine &EnvironmentStr)
407 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
408 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000409 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000410 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000411 Vendor(parseVendor(VendorStr.str())),
412 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000413 Environment(parseEnvironment(EnvironmentStr.str())),
414 ObjectFormat(parseFormat(EnvironmentStr.str())) {
415 if (ObjectFormat == Triple::UnknownObjectFormat)
416 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000417}
418
Duncan Sands501dff72010-08-12 11:31:39 +0000419std::string Triple::normalize(StringRef Str) {
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000420 bool IsMinGW32 = false;
421 bool IsCygwin = false;
422
Duncan Sands501dff72010-08-12 11:31:39 +0000423 // Parse into components.
424 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000425 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000426
427 // If the first component corresponds to a known architecture, preferentially
428 // use it for the architecture. If the second component corresponds to a
429 // known vendor, preferentially use it for the vendor, etc. This avoids silly
430 // component movement when a component parses as (eg) both a valid arch and a
431 // valid os.
432 ArchType Arch = UnknownArch;
433 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000434 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000435 VendorType Vendor = UnknownVendor;
436 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000437 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000438 OSType OS = UnknownOS;
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000439 if (Components.size() > 2) {
Chandler Carruthaec97082012-02-21 08:53:32 +0000440 OS = parseOS(Components[2]);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000441 IsCygwin = Components[2].startswith("cygwin");
442 IsMinGW32 = Components[2].startswith("mingw");
443 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000444 EnvironmentType Environment = UnknownEnvironment;
445 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000446 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000447 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000448 if (Components.size() > 4)
449 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000450
451 // Note which components are already in their final position. These will not
452 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000453 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000454 Found[0] = Arch != UnknownArch;
455 Found[1] = Vendor != UnknownVendor;
456 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000457 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000458
459 // If they are not there already, permute the components into their canonical
460 // positions by seeing if they parse as a valid architecture, and if so moving
461 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000462 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000463 if (Found[Pos])
464 continue; // Already in the canonical position.
465
466 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
467 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000468 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000469 continue;
470
471 // Does this component parse as valid for the target position?
472 bool Valid = false;
473 StringRef Comp = Components[Idx];
474 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000475 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000476 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000477 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000478 Valid = Arch != UnknownArch;
479 break;
480 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000481 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000482 Valid = Vendor != UnknownVendor;
483 break;
484 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000485 OS = parseOS(Comp);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000486 IsCygwin = Comp.startswith("cygwin");
487 IsMinGW32 = Comp.startswith("mingw");
488 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
Duncan Sands501dff72010-08-12 11:31:39 +0000489 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000490 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000491 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000492 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000493 if (!Valid) {
494 ObjectFormat = parseFormat(Comp);
495 Valid = ObjectFormat != UnknownObjectFormat;
496 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000497 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000498 }
499 if (!Valid)
500 continue; // Nope, try the next component.
501
502 // Move the component to the target position, pushing any non-fixed
503 // components that are in the way to the right. This tends to give
504 // good results in the common cases of a forgotten vendor component
505 // or a wrongly positioned environment.
506 if (Pos < Idx) {
507 // Insert left, pushing the existing components to the right. For
508 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
509 StringRef CurrentComponent(""); // The empty component.
510 // Replace the component we are moving with an empty component.
511 std::swap(CurrentComponent, Components[Idx]);
512 // Insert the component being moved at Pos, displacing any existing
513 // components to the right.
514 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
515 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000516 while (i < array_lengthof(Found) && Found[i])
517 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000518 // Place the component at the new position, getting the component
519 // that was at this position - it will be moved right.
520 std::swap(CurrentComponent, Components[i]);
521 }
522 } else if (Pos > Idx) {
523 // Push right by inserting empty components until the component at Idx
524 // reaches the target position Pos. For example, pc-a -> -pc-a when
525 // moving pc to the second position.
526 do {
527 // Insert one empty component at Idx.
528 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000529 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000530 // Place the component at the new position, getting the component
531 // that was at this position - it will be moved right.
532 std::swap(CurrentComponent, Components[i]);
533 // If it was placed on top of an empty component then we are done.
534 if (CurrentComponent.empty())
535 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000536 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000537 while (++i < array_lengthof(Found) && Found[i])
538 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000539 }
540 // The last component was pushed off the end - append it.
541 if (!CurrentComponent.empty())
542 Components.push_back(CurrentComponent);
543
544 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000545 while (++Idx < array_lengthof(Found) && Found[Idx])
546 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000547 } while (Idx < Pos); // Add more until the final position is reached.
548 }
549 assert(Pos < Components.size() && Components[Pos] == Comp &&
550 "Component moved wrong!");
551 Found[Pos] = true;
552 break;
553 }
554 }
555
556 // Special case logic goes here. At this point Arch, Vendor and OS have the
557 // correct values for the computed components.
558
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000559 if (OS == Triple::Win32) {
560 Components.resize(4);
561 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000562 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000563 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000564 Components[3] = "msvc";
565 else
566 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000567 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000568 } else if (IsMinGW32) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000569 Components.resize(4);
570 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000571 Components[3] = "gnu";
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000572 } else if (IsCygwin) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000573 Components.resize(4);
574 Components[2] = "windows";
575 Components[3] = "cygnus";
576 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000577 if (IsMinGW32 || IsCygwin ||
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000578 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
579 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
580 Components.resize(5);
581 Components[4] = getObjectFormatTypeName(ObjectFormat);
582 }
583 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000584
Duncan Sands501dff72010-08-12 11:31:39 +0000585 // Stick the corrected components back together to form the normalized string.
586 std::string Normalized;
587 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
588 if (i) Normalized += '-';
589 Normalized += Components[i];
590 }
591 return Normalized;
592}
593
Daniel Dunbar19e70762009-07-26 03:31:47 +0000594StringRef Triple::getArchName() const {
595 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000596}
597
Daniel Dunbar19e70762009-07-26 03:31:47 +0000598StringRef Triple::getVendorName() const {
599 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
600 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000601}
602
Daniel Dunbar19e70762009-07-26 03:31:47 +0000603StringRef Triple::getOSName() const {
604 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
605 Tmp = Tmp.split('-').second; // Strip second component
606 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000607}
608
Daniel Dunbar19e70762009-07-26 03:31:47 +0000609StringRef Triple::getEnvironmentName() const {
610 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
611 Tmp = Tmp.split('-').second; // Strip second component
612 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000613}
614
Daniel Dunbar19e70762009-07-26 03:31:47 +0000615StringRef Triple::getOSAndEnvironmentName() const {
616 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
617 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000618}
619
Chris Lattner553c9f32009-08-12 06:19:40 +0000620static unsigned EatNumber(StringRef &Str) {
621 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000622 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000623
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000624 do {
625 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000626 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000627
Chris Lattner553c9f32009-08-12 06:19:40 +0000628 // Eat the digit.
629 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000630 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000631
Chris Lattner553c9f32009-08-12 06:19:40 +0000632 return Result;
633}
634
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000635void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
636 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000637 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000638
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000639 // Assume that the OS portion of the triple starts with the canonical name.
640 StringRef OSTypeName = getOSTypeName(getOS());
641 if (OSName.startswith(OSTypeName))
642 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000643
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000644 // Any unset version defaults to 0.
645 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000646
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000647 // Parse up to three components.
648 unsigned *Components[3] = { &Major, &Minor, &Micro };
649 for (unsigned i = 0; i != 3; ++i) {
650 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
651 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000652
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000653 // Consume the leading number.
654 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000655
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000656 // Consume the separator, if present.
657 if (OSName.startswith("."))
658 OSName = OSName.substr(1);
659 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000660}
661
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000662bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
663 unsigned &Micro) const {
664 getOSVersion(Major, Minor, Micro);
665
666 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000667 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000668 case Darwin:
669 // Default to darwin8, i.e., MacOSX 10.4.
670 if (Major == 0)
671 Major = 8;
672 // Darwin version numbers are skewed from OS X versions.
673 if (Major < 4)
674 return false;
675 Micro = 0;
676 Minor = Major - 4;
677 Major = 10;
678 break;
679 case MacOSX:
680 // Default to 10.4.
681 if (Major == 0) {
682 Major = 10;
683 Minor = 4;
684 }
685 if (Major != 10)
686 return false;
687 break;
688 case IOS:
689 // Ignore the version from the triple. This is only handled because the
690 // the clang driver combines OS X and IOS support into a common Darwin
691 // toolchain that wants to know the OS X version number even when targeting
692 // IOS.
693 Major = 10;
694 Minor = 4;
695 Micro = 0;
696 break;
697 }
698 return true;
699}
700
Chad Rosierd84eaac2012-05-09 17:23:48 +0000701void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
702 unsigned &Micro) const {
703 switch (getOS()) {
704 default: llvm_unreachable("unexpected OS for Darwin triple");
705 case Darwin:
706 case MacOSX:
707 // Ignore the version from the triple. This is only handled because the
708 // the clang driver combines OS X and IOS support into a common Darwin
709 // toolchain that wants to know the iOS version number even when targeting
710 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000711 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000712 Minor = 0;
713 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000714 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000715 case IOS:
716 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000717 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000718 if (Major == 0)
Tim Northovere19bed72014-07-23 12:32:47 +0000719 Major = (getArch() == aarch64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000720 break;
721 }
722}
723
Daniel Dunbar19e70762009-07-26 03:31:47 +0000724void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000725 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000726}
727
728void Triple::setArch(ArchType Kind) {
729 setArchName(getArchTypeName(Kind));
730}
731
732void Triple::setVendor(VendorType Kind) {
733 setVendorName(getVendorTypeName(Kind));
734}
735
736void Triple::setOS(OSType Kind) {
737 setOSName(getOSTypeName(Kind));
738}
739
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000740void Triple::setEnvironment(EnvironmentType Kind) {
741 setEnvironmentName(getEnvironmentTypeName(Kind));
742}
743
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000744void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000745 if (Environment == UnknownEnvironment)
746 return setEnvironmentName(getObjectFormatTypeName(Kind));
747
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000748 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
749 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000750}
751
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000752void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000753 // Work around a miscompilation bug for Twines in gcc 4.0.3.
754 SmallString<64> Triple;
755 Triple += Str;
756 Triple += "-";
757 Triple += getVendorName();
758 Triple += "-";
759 Triple += getOSAndEnvironmentName();
760 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000761}
762
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000763void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000764 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
765}
766
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000767void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000768 if (hasEnvironment())
769 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
770 "-" + getEnvironmentName());
771 else
772 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
773}
774
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000775void Triple::setEnvironmentName(StringRef Str) {
776 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000777 "-" + Str);
778}
779
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000780void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000781 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
782}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000783
784static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
785 switch (Arch) {
786 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000787 return 0;
788
789 case llvm::Triple::msp430:
790 return 16;
791
792 case llvm::Triple::amdil:
793 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000794 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000795 case llvm::Triple::hexagon:
796 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000797 case llvm::Triple::mips:
798 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000799 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000800 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000801 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000802 case llvm::Triple::sparc:
803 case llvm::Triple::tce:
804 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000805 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000806 case llvm::Triple::x86:
807 case llvm::Triple::xcore:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000808 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000809 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000810 return 32;
811
Tim Northovere0e3aef2013-01-31 12:12:40 +0000812 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000813 case llvm::Triple::aarch64_be:
JF Bastien32972ef2014-09-12 17:54:17 +0000814 case llvm::Triple::le64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000815 case llvm::Triple::mips64:
816 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000817 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000818 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000819 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000820 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000821 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000822 case llvm::Triple::x86_64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000823 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000824 return 64;
825 }
826 llvm_unreachable("Invalid architecture value");
827}
828
829bool Triple::isArch64Bit() const {
830 return getArchPointerBitWidth(getArch()) == 64;
831}
832
833bool Triple::isArch32Bit() const {
834 return getArchPointerBitWidth(getArch()) == 32;
835}
836
837bool Triple::isArch16Bit() const {
838 return getArchPointerBitWidth(getArch()) == 16;
839}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000840
841Triple Triple::get32BitArchVariant() const {
842 Triple T(*this);
843 switch (getArch()) {
844 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000845 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000846 case Triple::aarch64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000847 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000848 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000849 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000850 T.setArch(UnknownArch);
851 break;
852
853 case Triple::amdil:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000854 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000855 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000856 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000857 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000858 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000859 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000860 case Triple::mips:
861 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000862 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000863 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000864 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000865 case Triple::sparc:
866 case Triple::tce:
867 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000868 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000869 case Triple::x86:
870 case Triple::xcore:
871 // Already 32-bit.
872 break;
873
JF Bastien32972ef2014-09-12 17:54:17 +0000874 case Triple::le64: T.setArch(Triple::le32); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000875 case Triple::mips64: T.setArch(Triple::mips); break;
876 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000877 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000878 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000879 case Triple::sparcv9: T.setArch(Triple::sparc); break;
880 case Triple::x86_64: T.setArch(Triple::x86); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000881 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000882 }
883 return T;
884}
885
886Triple Triple::get64BitArchVariant() const {
887 Triple T(*this);
888 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000889 case Triple::UnknownArch:
890 case Triple::amdil:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000891 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000892 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000893 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000894 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000895 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000896 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000897 case Triple::tce:
898 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000899 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000900 case Triple::xcore:
901 T.setArch(UnknownArch);
902 break;
903
Tim Northovere0e3aef2013-01-31 12:12:40 +0000904 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000905 case Triple::aarch64_be:
JF Bastien32972ef2014-09-12 17:54:17 +0000906 case Triple::le64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000907 case Triple::mips64:
908 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000909 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000910 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000911 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000912 case Triple::sparcv9:
JF Bastien32972ef2014-09-12 17:54:17 +0000913 case Triple::spir64:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000914 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000915 case Triple::x86_64:
916 // Already 64-bit.
917 break;
918
JF Bastien32972ef2014-09-12 17:54:17 +0000919 case Triple::le32: T.setArch(Triple::le64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000920 case Triple::mips: T.setArch(Triple::mips64); break;
921 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000922 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000923 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000924 case Triple::sparc: T.setArch(Triple::sparcv9); break;
925 case Triple::x86: T.setArch(Triple::x86_64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000926 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000927 }
928 return T;
929}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000930
931// FIXME: tblgen this.
932const char *Triple::getARMCPUForArch(StringRef MArch) const {
933 if (MArch.empty())
934 MArch = getArchName();
935
936 switch (getOS()) {
Ed Masteea0257a2014-09-09 17:47:24 +0000937 case llvm::Triple::FreeBSD:
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000938 case llvm::Triple::NetBSD:
939 if (MArch == "armv6")
940 return "arm1176jzf-s";
941 break;
942 case llvm::Triple::Win32:
943 // FIXME: this is invalid for WindowsCE
944 return "cortex-a9";
945 default:
946 break;
947 }
948
949 const char *result = nullptr;
950 size_t offset = StringRef::npos;
951 if (MArch.startswith("arm"))
952 offset = 3;
953 if (MArch.startswith("thumb"))
954 offset = 5;
955 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
956 offset += 2;
957 if (offset != StringRef::npos)
958 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
959 .Cases("v2", "v2a", "arm2")
960 .Case("v3", "arm6")
961 .Case("v3m", "arm7m")
962 .Case("v4", "strongarm")
963 .Case("v4t", "arm7tdmi")
964 .Cases("v5", "v5t", "arm10tdmi")
965 .Cases("v5e", "v5te", "arm1022e")
966 .Case("v5tej", "arm926ej-s")
967 .Cases("v6", "v6k", "arm1136jf-s")
968 .Case("v6j", "arm1136j-s")
969 .Cases("v6z", "v6zk", "arm1176jzf-s")
970 .Case("v6t2", "arm1156t2-s")
971 .Cases("v6m", "v6-m", "cortex-m0")
972 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
973 .Cases("v7s", "v7-s", "swift")
974 .Cases("v7r", "v7-r", "cortex-r4")
975 .Cases("v7m", "v7-m", "cortex-m3")
976 .Cases("v7em", "v7e-m", "cortex-m4")
977 .Cases("v8", "v8a", "v8-a", "cortex-a53")
978 .Default(nullptr);
979 else
980 result = llvm::StringSwitch<const char *>(MArch)
981 .Case("ep9312", "ep9312")
982 .Case("iwmmxt", "iwmmxt")
983 .Case("xscale", "xscale")
984 .Default(nullptr);
985
986 if (result)
987 return result;
988
989 // If all else failed, return the most base CPU with thumb interworking
990 // supported by LLVM.
991 // FIXME: Should warn once that we're falling back.
992 switch (getOS()) {
993 case llvm::Triple::NetBSD:
994 switch (getEnvironment()) {
995 case llvm::Triple::GNUEABIHF:
996 case llvm::Triple::GNUEABI:
997 case llvm::Triple::EABIHF:
998 case llvm::Triple::EABI:
999 return "arm926ej-s";
1000 default:
1001 return "strongarm";
1002 }
1003 default:
1004 switch (getEnvironment()) {
1005 case llvm::Triple::EABIHF:
1006 case llvm::Triple::GNUEABIHF:
1007 return "arm1176jzf-s";
1008 default:
1009 return "arm7tdmi";
1010 }
1011 }
1012}