blob: 2b8b39782ccdbdfcbb1b5587a0399cb28a2f7a2d [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";
James Molloy09a53b92014-04-14 17:37:53 +000027 case arm64_be: return "arm64_be";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000028 case hexagon: return "hexagon";
29 case mips: return "mips";
30 case mipsel: return "mipsel";
31 case mips64: return "mips64";
32 case mips64el: return "mips64el";
33 case msp430: return "msp430";
34 case ppc64: return "powerpc64";
35 case ppc64le: return "powerpc64le";
36 case ppc: return "powerpc";
37 case r600: return "r600";
38 case sparc: return "sparc";
39 case sparcv9: return "sparcv9";
40 case systemz: return "s390x";
41 case tce: return "tce";
42 case thumb: return "thumb";
Christian Pirker2a111602014-03-28 14:35:30 +000043 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000044 case x86: return "i386";
45 case x86_64: return "x86_64";
46 case xcore: return "xcore";
47 case nvptx: return "nvptx";
48 case nvptx64: return "nvptx64";
49 case le32: return "le32";
50 case amdil: return "amdil";
51 case spir: return "spir";
52 case spir64: return "spir64";
Eric Christopher54fe1b22014-07-10 17:26:54 +000053 case kalimba: return "kalimba";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000054 }
55
David Blaikie46a9f012012-01-20 21:51:11 +000056 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000057}
58
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000059const char *Triple::getArchTypePrefix(ArchType Kind) {
60 switch (Kind) {
61 default:
Craig Topperc10719f2014-04-07 04:17:22 +000062 return nullptr;
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000063
Yi Kong7d78ab52014-07-17 09:43:27 +000064 case arm64:
65 case arm64_be:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000066 case aarch64:
67 case aarch64_be: return "aarch64";
Tim Northovere0e3aef2013-01-31 12:12:40 +000068
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000069 case arm:
Christian Pirker2a111602014-03-28 14:35:30 +000070 case armeb:
71 case thumb:
72 case thumbeb: return "arm";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000073
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000074 case ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +000075 case ppc64le:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000076 case ppc: return "ppc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000077
Benjamin Kramerae3c3002012-06-28 19:09:53 +000078 case mips:
79 case mipsel:
80 case mips64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000081 case mips64el: return "mips";
Benjamin Kramerae3c3002012-06-28 19:09:53 +000082
Christian Pirker6c2f4d42014-02-24 11:34:50 +000083 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084
Christian Pirker6c2f4d42014-02-24 11:34:50 +000085 case r600: return "r600";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000086
Chris Lattner8228b112010-02-04 06:34:01 +000087 case sparcv9:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000088 case sparc: return "sparc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000089
Christian Pirker6c2f4d42014-02-24 11:34:50 +000090 case systemz: return "systemz";
Richard Sandiforda238c5e2013-05-03 11:05:17 +000091
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000092 case x86:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000093 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000094
Christian Pirker6c2f4d42014-02-24 11:34:50 +000095 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000096
Christian Pirker6c2f4d42014-02-24 11:34:50 +000097 case nvptx: return "nvptx";
98 case nvptx64: return "nvptx";
Tim Northover00ed9962014-03-29 10:18:08 +000099
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000100 case le32: return "le32";
101 case amdil: return "amdil";
102 case spir: return "spir";
103 case spir64: return "spir";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000104 case kalimba: return "kalimba";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000105 }
106}
107
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000108const char *Triple::getVendorTypeName(VendorType Kind) {
109 switch (Kind) {
110 case UnknownVendor: return "unknown";
111
112 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000113 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000114 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000115 case BGP: return "bgp";
116 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000117 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000118 case IBM: return "ibm";
Daniel Sandersc5626f42014-07-09 16:03:10 +0000119 case ImaginationTechnologies: return "img";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000120 case NVIDIA: return "nvidia";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000121 case CSR: return "csr";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000122 }
123
David Blaikie46a9f012012-01-20 21:51:11 +0000124 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000125}
126
127const char *Triple::getOSTypeName(OSType Kind) {
128 switch (Kind) {
129 case UnknownOS: return "unknown";
130
Duncan Sands0de39b42009-06-19 14:40:01 +0000131 case AuroraUX: return "auroraux";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000132 case Cygwin: return "cygwin";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000133 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000134 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000135 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000136 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000137 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000138 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000139 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000140 case MacOSX: return "macosx";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000141 case MinGW32: return "mingw32";
Chris Lattner01218d52009-07-13 20:22:23 +0000142 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000143 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000144 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000145 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000146 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000147 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000148 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000149 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000150 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000151 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000152 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000153 case CUDA: return "cuda";
154 case NVCL: return "nvcl";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000155 }
156
David Blaikie46a9f012012-01-20 21:51:11 +0000157 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000158}
159
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000160const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
161 switch (Kind) {
162 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000163 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000164 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000165 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000166 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000167 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000168 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000169 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000170 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000171 case MSVC: return "msvc";
172 case Itanium: return "itanium";
173 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000174 }
175
David Blaikie46a9f012012-01-20 21:51:11 +0000176 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000177}
178
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000179Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000180 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000181 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000182 .Case("aarch64_be", aarch64_be)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000183 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000184 .Case("armeb", armeb)
Tim Northover00ed9962014-03-29 10:18:08 +0000185 .Case("arm64", arm64)
James Molloy09a53b92014-04-14 17:37:53 +0000186 .Case("arm64_be", arm64_be)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000187 .Case("mips", mips)
188 .Case("mipsel", mipsel)
189 .Case("mips64", mips64)
190 .Case("mips64el", mips64el)
191 .Case("msp430", msp430)
192 .Case("ppc64", ppc64)
193 .Case("ppc32", ppc)
194 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000195 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000196 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000197 .Case("hexagon", hexagon)
198 .Case("sparc", sparc)
199 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000200 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000201 .Case("tce", tce)
202 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000203 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000204 .Case("x86", x86)
205 .Case("x86-64", x86_64)
206 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000207 .Case("nvptx", nvptx)
208 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000209 .Case("le32", le32)
210 .Case("amdil", amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000211 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000212 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000213 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000214 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000215}
216
Duncan Sands8712da32010-03-24 09:05:14 +0000217// Returns architecture name that is understood by the target assembler.
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000218const char *Triple::getArchNameForAssembler() {
Daniel Dunbar163a0962011-04-19 21:12:05 +0000219 if (!isOSDarwin() && getVendor() != Triple::Apple)
Craig Topperc10719f2014-04-07 04:17:22 +0000220 return nullptr;
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000221
Chandler Carruthff6f3562012-02-12 09:27:38 +0000222 return StringSwitch<const char*>(getArchName())
223 .Case("i386", "i386")
224 .Case("x86_64", "x86_64")
225 .Case("powerpc", "ppc")
226 .Case("powerpc64", "ppc64")
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000227 .Case("powerpc64le", "ppc64le")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000228 .Case("arm", "arm")
229 .Cases("armv4t", "thumbv4t", "armv4t")
230 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
231 .Cases("armv6", "thumbv6", "armv6")
232 .Cases("armv7", "thumbv7", "armv7")
Christian Pirker2a111602014-03-28 14:35:30 +0000233 .Case("armeb", "armeb")
Tim Northover00ed9962014-03-29 10:18:08 +0000234 .Case("arm64", "arm64")
James Molloy09a53b92014-04-14 17:37:53 +0000235 .Case("arm64_be", "arm64")
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000236 .Case("r600", "r600")
Justin Holewinskiae556d32012-05-04 20:18:50 +0000237 .Case("nvptx", "nvptx")
238 .Case("nvptx64", "nvptx64")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000239 .Case("le32", "le32")
240 .Case("amdil", "amdil")
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000241 .Case("spir", "spir")
Guy Benyeia4d31a32012-11-15 10:35:47 +0000242 .Case("spir64", "spir64")
Craig Topperc10719f2014-04-07 04:17:22 +0000243 .Default(nullptr);
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000244}
245
Chandler Carruthaec97082012-02-21 08:53:32 +0000246static Triple::ArchType parseArch(StringRef ArchName) {
247 return StringSwitch<Triple::ArchType>(ArchName)
248 .Cases("i386", "i486", "i586", "i686", Triple::x86)
249 // FIXME: Do we need to support these?
250 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000251 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000252 .Case("powerpc", Triple::ppc)
253 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000254 .Case("powerpc64le", Triple::ppc64le)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000255 .Case("aarch64", Triple::aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000256 .Case("aarch64_be", Triple::aarch64_be)
Chandler Carruthaec97082012-02-21 08:53:32 +0000257 .Cases("arm", "xscale", Triple::arm)
Chandler Carruthb54950b2012-02-18 04:34:17 +0000258 // FIXME: It would be good to replace these with explicit names for all the
259 // various suffixes supported.
Chandler Carruthaec97082012-02-21 08:53:32 +0000260 .StartsWith("armv", Triple::arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000261 .Case("armeb", Triple::armeb)
262 .StartsWith("armebv", Triple::armeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000263 .Case("thumb", Triple::thumb)
264 .StartsWith("thumbv", Triple::thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000265 .Case("thumbeb", Triple::thumbeb)
266 .StartsWith("thumbebv", Triple::thumbeb)
Tim Northover00ed9962014-03-29 10:18:08 +0000267 .Case("arm64", Triple::arm64)
James Molloy09a53b92014-04-14 17:37:53 +0000268 .Case("arm64_be", Triple::arm64_be)
Chandler Carruthaec97082012-02-21 08:53:32 +0000269 .Case("msp430", Triple::msp430)
270 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000271 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000272 .Cases("mips64", "mips64eb", Triple::mips64)
273 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000274 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000275 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000276 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000277 .Case("sparc", Triple::sparc)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000278 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000279 .Case("tce", Triple::tce)
280 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000281 .Case("nvptx", Triple::nvptx)
282 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000283 .Case("le32", Triple::le32)
284 .Case("amdil", Triple::amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000285 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000286 .Case("spir64", Triple::spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000287 .Case("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000288 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000289}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000290
Chandler Carruthaec97082012-02-21 08:53:32 +0000291static Triple::VendorType parseVendor(StringRef VendorName) {
292 return StringSwitch<Triple::VendorType>(VendorName)
293 .Case("apple", Triple::Apple)
294 .Case("pc", Triple::PC)
295 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000296 .Case("bgp", Triple::BGP)
297 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000298 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000299 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000300 .Case("img", Triple::ImaginationTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000301 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000302 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000303 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000304}
305
Chandler Carruthaec97082012-02-21 08:53:32 +0000306static Triple::OSType parseOS(StringRef OSName) {
307 return StringSwitch<Triple::OSType>(OSName)
308 .StartsWith("auroraux", Triple::AuroraUX)
309 .StartsWith("cygwin", Triple::Cygwin)
310 .StartsWith("darwin", Triple::Darwin)
311 .StartsWith("dragonfly", Triple::DragonFly)
312 .StartsWith("freebsd", Triple::FreeBSD)
313 .StartsWith("ios", Triple::IOS)
314 .StartsWith("kfreebsd", Triple::KFreeBSD)
315 .StartsWith("linux", Triple::Linux)
316 .StartsWith("lv2", Triple::Lv2)
317 .StartsWith("macosx", Triple::MacOSX)
318 .StartsWith("mingw32", Triple::MinGW32)
319 .StartsWith("netbsd", Triple::NetBSD)
320 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000321 .StartsWith("solaris", Triple::Solaris)
322 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000323 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000324 .StartsWith("haiku", Triple::Haiku)
325 .StartsWith("minix", Triple::Minix)
326 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000327 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000328 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000329 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000330 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000331 .StartsWith("cuda", Triple::CUDA)
332 .StartsWith("nvcl", Triple::NVCL)
Chandler Carruthaec97082012-02-21 08:53:32 +0000333 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000334}
335
Chandler Carruthaec97082012-02-21 08:53:32 +0000336static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
337 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000338 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000339 .StartsWith("eabi", Triple::EABI)
340 .StartsWith("gnueabihf", Triple::GNUEABIHF)
341 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000342 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000343 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000344 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000345 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000346 .StartsWith("msvc", Triple::MSVC)
347 .StartsWith("itanium", Triple::Itanium)
348 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000349 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000350}
351
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000352static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
353 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
354 .EndsWith("coff", Triple::COFF)
355 .EndsWith("elf", Triple::ELF)
356 .EndsWith("macho", Triple::MachO)
357 .Default(Triple::UnknownObjectFormat);
358}
359
360static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
361 switch (Kind) {
362 case Triple::UnknownObjectFormat: return "";
363 case Triple::COFF: return "coff";
364 case Triple::ELF: return "elf";
365 case Triple::MachO: return "macho";
366 }
367 llvm_unreachable("unknown object format type");
368}
369
370static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
371 if (T.isOSDarwin())
372 return Triple::MachO;
373 else if (T.isOSWindows())
374 return Triple::COFF;
375 return Triple::ELF;
376}
377
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000378/// \brief Construct a triple from the string representation provided.
379///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000380/// This stores the string representation and parses the various pieces into
381/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000382Triple::Triple(const Twine &Str)
383 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000384 Arch(parseArch(getArchName())),
385 Vendor(parseVendor(getVendorName())),
386 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000387 Environment(parseEnvironment(getEnvironmentName())),
388 ObjectFormat(parseFormat(getEnvironmentName())) {
389 if (ObjectFormat == Triple::UnknownObjectFormat)
390 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000391}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000392
393/// \brief Construct a triple from string representations of the architecture,
394/// vendor, and OS.
395///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000396/// This joins each argument into a canonical string representation and parses
397/// them into enum members. It leaves the environment unknown and omits it from
398/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000399Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
400 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000401 Arch(parseArch(ArchStr.str())),
402 Vendor(parseVendor(VendorStr.str())),
403 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000404 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
405 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000406}
407
408/// \brief Construct a triple from string representations of the architecture,
409/// vendor, OS, and environment.
410///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000411/// This joins each argument into a canonical string representation and parses
412/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000413Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
414 const Twine &EnvironmentStr)
415 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
416 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000417 Arch(parseArch(ArchStr.str())),
418 Vendor(parseVendor(VendorStr.str())),
419 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000420 Environment(parseEnvironment(EnvironmentStr.str())),
421 ObjectFormat(parseFormat(EnvironmentStr.str())) {
422 if (ObjectFormat == Triple::UnknownObjectFormat)
423 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000424}
425
Duncan Sands501dff72010-08-12 11:31:39 +0000426std::string Triple::normalize(StringRef Str) {
427 // Parse into components.
428 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000429 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000430
431 // If the first component corresponds to a known architecture, preferentially
432 // use it for the architecture. If the second component corresponds to a
433 // known vendor, preferentially use it for the vendor, etc. This avoids silly
434 // component movement when a component parses as (eg) both a valid arch and a
435 // valid os.
436 ArchType Arch = UnknownArch;
437 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000438 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000439 VendorType Vendor = UnknownVendor;
440 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000441 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000442 OSType OS = UnknownOS;
443 if (Components.size() > 2)
Chandler Carruthaec97082012-02-21 08:53:32 +0000444 OS = parseOS(Components[2]);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000445 EnvironmentType Environment = UnknownEnvironment;
446 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000447 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000448 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000449 if (Components.size() > 4)
450 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000451
452 // Note which components are already in their final position. These will not
453 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000454 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000455 Found[0] = Arch != UnknownArch;
456 Found[1] = Vendor != UnknownVendor;
457 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000458 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000459
460 // If they are not there already, permute the components into their canonical
461 // positions by seeing if they parse as a valid architecture, and if so moving
462 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000463 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000464 if (Found[Pos])
465 continue; // Already in the canonical position.
466
467 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
468 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000469 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000470 continue;
471
472 // Does this component parse as valid for the target position?
473 bool Valid = false;
474 StringRef Comp = Components[Idx];
475 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000476 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000477 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000478 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000479 Valid = Arch != UnknownArch;
480 break;
481 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000482 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000483 Valid = Vendor != UnknownVendor;
484 break;
485 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000486 OS = parseOS(Comp);
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000487 Valid = OS != UnknownOS;
Duncan Sands501dff72010-08-12 11:31:39 +0000488 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000489 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000490 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000491 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000492 if (!Valid) {
493 ObjectFormat = parseFormat(Comp);
494 Valid = ObjectFormat != UnknownObjectFormat;
495 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000496 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000497 }
498 if (!Valid)
499 continue; // Nope, try the next component.
500
501 // Move the component to the target position, pushing any non-fixed
502 // components that are in the way to the right. This tends to give
503 // good results in the common cases of a forgotten vendor component
504 // or a wrongly positioned environment.
505 if (Pos < Idx) {
506 // Insert left, pushing the existing components to the right. For
507 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
508 StringRef CurrentComponent(""); // The empty component.
509 // Replace the component we are moving with an empty component.
510 std::swap(CurrentComponent, Components[Idx]);
511 // Insert the component being moved at Pos, displacing any existing
512 // components to the right.
513 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
514 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000515 while (i < array_lengthof(Found) && Found[i])
516 ++i;
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 }
521 } else if (Pos > Idx) {
522 // Push right by inserting empty components until the component at Idx
523 // reaches the target position Pos. For example, pc-a -> -pc-a when
524 // moving pc to the second position.
525 do {
526 // Insert one empty component at Idx.
527 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000528 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000529 // Place the component at the new position, getting the component
530 // that was at this position - it will be moved right.
531 std::swap(CurrentComponent, Components[i]);
532 // If it was placed on top of an empty component then we are done.
533 if (CurrentComponent.empty())
534 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000535 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000536 while (++i < array_lengthof(Found) && Found[i])
537 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000538 }
539 // The last component was pushed off the end - append it.
540 if (!CurrentComponent.empty())
541 Components.push_back(CurrentComponent);
542
543 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000544 while (++Idx < array_lengthof(Found) && Found[Idx])
545 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000546 } while (Idx < Pos); // Add more until the final position is reached.
547 }
548 assert(Pos < Components.size() && Components[Pos] == Comp &&
549 "Component moved wrong!");
550 Found[Pos] = true;
551 break;
552 }
553 }
554
555 // Special case logic goes here. At this point Arch, Vendor and OS have the
556 // correct values for the computed components.
557
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000558 if (OS == Triple::Win32) {
559 Components.resize(4);
560 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000561 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000562 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000563 Components[3] = "msvc";
564 else
565 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000566 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000567 } else if (OS == Triple::MinGW32) {
568 Components.resize(4);
569 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000570 Components[3] = "gnu";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000571 } else if (OS == Triple::Cygwin) {
572 Components.resize(4);
573 Components[2] = "windows";
574 Components[3] = "cygnus";
575 }
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000576 if (OS == Triple::MinGW32 || OS == Triple::Cygwin ||
577 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
578 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
579 Components.resize(5);
580 Components[4] = getObjectFormatTypeName(ObjectFormat);
581 }
582 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000583
Duncan Sands501dff72010-08-12 11:31:39 +0000584 // Stick the corrected components back together to form the normalized string.
585 std::string Normalized;
586 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
587 if (i) Normalized += '-';
588 Normalized += Components[i];
589 }
590 return Normalized;
591}
592
Daniel Dunbar19e70762009-07-26 03:31:47 +0000593StringRef Triple::getArchName() const {
594 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000595}
596
Daniel Dunbar19e70762009-07-26 03:31:47 +0000597StringRef Triple::getVendorName() const {
598 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
599 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000600}
601
Daniel Dunbar19e70762009-07-26 03:31:47 +0000602StringRef Triple::getOSName() const {
603 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
604 Tmp = Tmp.split('-').second; // Strip second component
605 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000606}
607
Daniel Dunbar19e70762009-07-26 03:31:47 +0000608StringRef Triple::getEnvironmentName() const {
609 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
610 Tmp = Tmp.split('-').second; // Strip second component
611 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000612}
613
Daniel Dunbar19e70762009-07-26 03:31:47 +0000614StringRef Triple::getOSAndEnvironmentName() const {
615 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
616 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000617}
618
Chris Lattner553c9f32009-08-12 06:19:40 +0000619static unsigned EatNumber(StringRef &Str) {
620 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000621 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000622
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000623 do {
624 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000625 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000626
Chris Lattner553c9f32009-08-12 06:19:40 +0000627 // Eat the digit.
628 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000629 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000630
Chris Lattner553c9f32009-08-12 06:19:40 +0000631 return Result;
632}
633
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000634void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
635 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000636 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000637
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000638 // Assume that the OS portion of the triple starts with the canonical name.
639 StringRef OSTypeName = getOSTypeName(getOS());
640 if (OSName.startswith(OSTypeName))
641 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000642
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000643 // Any unset version defaults to 0.
644 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000645
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000646 // Parse up to three components.
647 unsigned *Components[3] = { &Major, &Minor, &Micro };
648 for (unsigned i = 0; i != 3; ++i) {
649 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
650 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000651
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000652 // Consume the leading number.
653 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000654
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000655 // Consume the separator, if present.
656 if (OSName.startswith("."))
657 OSName = OSName.substr(1);
658 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000659}
660
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000661bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
662 unsigned &Micro) const {
663 getOSVersion(Major, Minor, Micro);
664
665 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000666 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000667 case Darwin:
668 // Default to darwin8, i.e., MacOSX 10.4.
669 if (Major == 0)
670 Major = 8;
671 // Darwin version numbers are skewed from OS X versions.
672 if (Major < 4)
673 return false;
674 Micro = 0;
675 Minor = Major - 4;
676 Major = 10;
677 break;
678 case MacOSX:
679 // Default to 10.4.
680 if (Major == 0) {
681 Major = 10;
682 Minor = 4;
683 }
684 if (Major != 10)
685 return false;
686 break;
687 case IOS:
688 // Ignore the version from the triple. This is only handled because the
689 // the clang driver combines OS X and IOS support into a common Darwin
690 // toolchain that wants to know the OS X version number even when targeting
691 // IOS.
692 Major = 10;
693 Minor = 4;
694 Micro = 0;
695 break;
696 }
697 return true;
698}
699
Chad Rosierd84eaac2012-05-09 17:23:48 +0000700void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
701 unsigned &Micro) const {
702 switch (getOS()) {
703 default: llvm_unreachable("unexpected OS for Darwin triple");
704 case Darwin:
705 case MacOSX:
706 // Ignore the version from the triple. This is only handled because the
707 // the clang driver combines OS X and IOS support into a common Darwin
708 // toolchain that wants to know the iOS version number even when targeting
709 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000710 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000711 Minor = 0;
712 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000713 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000714 case IOS:
715 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000716 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000717 if (Major == 0)
Tim Northover00ed9962014-03-29 10:18:08 +0000718 Major = (getArch() == arm64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000719 break;
720 }
721}
722
Daniel Dunbar19e70762009-07-26 03:31:47 +0000723void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000724 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000725}
726
727void Triple::setArch(ArchType Kind) {
728 setArchName(getArchTypeName(Kind));
729}
730
731void Triple::setVendor(VendorType Kind) {
732 setVendorName(getVendorTypeName(Kind));
733}
734
735void Triple::setOS(OSType Kind) {
736 setOSName(getOSTypeName(Kind));
737}
738
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000739void Triple::setEnvironment(EnvironmentType Kind) {
740 setEnvironmentName(getEnvironmentTypeName(Kind));
741}
742
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000743void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000744 if (Environment == UnknownEnvironment)
745 return setEnvironmentName(getObjectFormatTypeName(Kind));
746
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000747 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
748 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000749}
750
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000751void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000752 // Work around a miscompilation bug for Twines in gcc 4.0.3.
753 SmallString<64> Triple;
754 Triple += Str;
755 Triple += "-";
756 Triple += getVendorName();
757 Triple += "-";
758 Triple += getOSAndEnvironmentName();
759 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000760}
761
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000762void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000763 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
764}
765
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000766void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000767 if (hasEnvironment())
768 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
769 "-" + getEnvironmentName());
770 else
771 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
772}
773
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000774void Triple::setEnvironmentName(StringRef Str) {
775 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000776 "-" + Str);
777}
778
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000779void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000780 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
781}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000782
783static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
784 switch (Arch) {
785 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000786 return 0;
787
788 case llvm::Triple::msp430:
789 return 16;
790
791 case llvm::Triple::amdil:
792 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000793 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000794 case llvm::Triple::hexagon:
795 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000796 case llvm::Triple::mips:
797 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000798 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000799 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000800 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000801 case llvm::Triple::sparc:
802 case llvm::Triple::tce:
803 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000804 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000805 case llvm::Triple::x86:
806 case llvm::Triple::xcore:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000807 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000808 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000809 return 32;
810
Tim Northover00ed9962014-03-29 10:18:08 +0000811 case llvm::Triple::arm64:
James Molloy09a53b92014-04-14 17:37:53 +0000812 case llvm::Triple::arm64_be:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000813 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000814 case llvm::Triple::aarch64_be:
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:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000847 case Triple::arm64:
James Molloy09a53b92014-04-14 17:37:53 +0000848 case Triple::arm64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000849 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000850 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000851 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000852 T.setArch(UnknownArch);
853 break;
854
855 case Triple::amdil:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000856 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000857 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000858 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000859 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000860 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000861 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000862 case Triple::mips:
863 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000864 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000865 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000866 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000867 case Triple::sparc:
868 case Triple::tce:
869 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000870 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000871 case Triple::x86:
872 case Triple::xcore:
873 // Already 32-bit.
874 break;
875
876 case Triple::mips64: T.setArch(Triple::mips); break;
877 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000878 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000879 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000880 case Triple::sparcv9: T.setArch(Triple::sparc); break;
881 case Triple::x86_64: T.setArch(Triple::x86); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000882 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000883 }
884 return T;
885}
886
887Triple Triple::get64BitArchVariant() const {
888 Triple T(*this);
889 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000890 case Triple::UnknownArch:
891 case Triple::amdil:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000892 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000893 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000894 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000895 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000896 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000897 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000898 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000899 case Triple::tce:
900 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000901 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000902 case Triple::xcore:
903 T.setArch(UnknownArch);
904 break;
905
Tim Northovere0e3aef2013-01-31 12:12:40 +0000906 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000907 case Triple::aarch64_be:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000908 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000909 case Triple::mips64:
910 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000911 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000912 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000913 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000914 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000915 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000916 case Triple::x86_64:
Tim Northover00ed9962014-03-29 10:18:08 +0000917 case Triple::arm64:
James Molloy09a53b92014-04-14 17:37:53 +0000918 case Triple::arm64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000919 // Already 64-bit.
920 break;
921
922 case Triple::mips: T.setArch(Triple::mips64); break;
923 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000924 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000925 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000926 case Triple::sparc: T.setArch(Triple::sparcv9); break;
927 case Triple::x86: T.setArch(Triple::x86_64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000928 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000929 }
930 return T;
931}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000932
933// FIXME: tblgen this.
934const char *Triple::getARMCPUForArch(StringRef MArch) const {
935 if (MArch.empty())
936 MArch = getArchName();
937
938 switch (getOS()) {
939 case llvm::Triple::NetBSD:
940 if (MArch == "armv6")
941 return "arm1176jzf-s";
942 break;
943 case llvm::Triple::Win32:
944 // FIXME: this is invalid for WindowsCE
945 return "cortex-a9";
946 default:
947 break;
948 }
949
950 const char *result = nullptr;
951 size_t offset = StringRef::npos;
952 if (MArch.startswith("arm"))
953 offset = 3;
954 if (MArch.startswith("thumb"))
955 offset = 5;
956 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
957 offset += 2;
958 if (offset != StringRef::npos)
959 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
960 .Cases("v2", "v2a", "arm2")
961 .Case("v3", "arm6")
962 .Case("v3m", "arm7m")
963 .Case("v4", "strongarm")
964 .Case("v4t", "arm7tdmi")
965 .Cases("v5", "v5t", "arm10tdmi")
966 .Cases("v5e", "v5te", "arm1022e")
967 .Case("v5tej", "arm926ej-s")
968 .Cases("v6", "v6k", "arm1136jf-s")
969 .Case("v6j", "arm1136j-s")
970 .Cases("v6z", "v6zk", "arm1176jzf-s")
971 .Case("v6t2", "arm1156t2-s")
972 .Cases("v6m", "v6-m", "cortex-m0")
973 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
974 .Cases("v7s", "v7-s", "swift")
975 .Cases("v7r", "v7-r", "cortex-r4")
976 .Cases("v7m", "v7-m", "cortex-m3")
977 .Cases("v7em", "v7e-m", "cortex-m4")
978 .Cases("v8", "v8a", "v8-a", "cortex-a53")
979 .Default(nullptr);
980 else
981 result = llvm::StringSwitch<const char *>(MArch)
982 .Case("ep9312", "ep9312")
983 .Case("iwmmxt", "iwmmxt")
984 .Case("xscale", "xscale")
985 .Default(nullptr);
986
987 if (result)
988 return result;
989
990 // If all else failed, return the most base CPU with thumb interworking
991 // supported by LLVM.
992 // FIXME: Should warn once that we're falling back.
993 switch (getOS()) {
994 case llvm::Triple::NetBSD:
995 switch (getEnvironment()) {
996 case llvm::Triple::GNUEABIHF:
997 case llvm::Triple::GNUEABI:
998 case llvm::Triple::EABIHF:
999 case llvm::Triple::EABI:
1000 return "arm926ej-s";
1001 default:
1002 return "strongarm";
1003 }
1004 default:
1005 switch (getEnvironment()) {
1006 case llvm::Triple::EABIHF:
1007 case llvm::Triple::GNUEABIHF:
1008 return "arm1176jzf-s";
1009 default:
1010 return "arm7tdmi";
1011 }
1012 }
1013}