blob: 25ddb7689d435c694a1e3d02c25a930e7db98479 [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
Renato Golinc17a07b2014-07-18 12:00:48 +0000360static Triple::SubArchType parseSubArch(StringRef SubArchName) {
361 return StringSwitch<Triple::SubArchType>(SubArchName)
362 .EndsWith("v8", Triple::ARMSubArch_v8)
363 .EndsWith("v8a", Triple::ARMSubArch_v8)
364 .EndsWith("v7", Triple::ARMSubArch_v7)
365 .EndsWith("v7a", Triple::ARMSubArch_v7)
366 .EndsWith("v7em", Triple::ARMSubArch_v7em)
367 .EndsWith("v7l", Triple::ARMSubArch_v7)
368 .EndsWith("v7m", Triple::ARMSubArch_v7m)
369 .EndsWith("v7r", Triple::ARMSubArch_v7)
370 .EndsWith("v7s", Triple::ARMSubArch_v7s)
371 .EndsWith("v6", Triple::ARMSubArch_v6)
372 .EndsWith("v6m", Triple::ARMSubArch_v6m)
373 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
374 .EndsWith("v5", Triple::ARMSubArch_v5)
375 .EndsWith("v5e", Triple::ARMSubArch_v5)
376 .EndsWith("v5t", Triple::ARMSubArch_v5)
377 .EndsWith("v5te", Triple::ARMSubArch_v5te)
378 .EndsWith("v4t", Triple::ARMSubArch_v4t)
379 .Default(Triple::NoSubArch);
380}
381
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000382static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
383 switch (Kind) {
384 case Triple::UnknownObjectFormat: return "";
385 case Triple::COFF: return "coff";
386 case Triple::ELF: return "elf";
387 case Triple::MachO: return "macho";
388 }
389 llvm_unreachable("unknown object format type");
390}
391
392static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
393 if (T.isOSDarwin())
394 return Triple::MachO;
395 else if (T.isOSWindows())
396 return Triple::COFF;
397 return Triple::ELF;
398}
399
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000400/// \brief Construct a triple from the string representation provided.
401///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000402/// This stores the string representation and parses the various pieces into
403/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000404Triple::Triple(const Twine &Str)
405 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000406 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000407 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000408 Vendor(parseVendor(getVendorName())),
409 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000410 Environment(parseEnvironment(getEnvironmentName())),
411 ObjectFormat(parseFormat(getEnvironmentName())) {
412 if (ObjectFormat == Triple::UnknownObjectFormat)
413 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000414}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000415
416/// \brief Construct a triple from string representations of the architecture,
417/// vendor, and OS.
418///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000419/// This joins each argument into a canonical string representation and parses
420/// them into enum members. It leaves the environment unknown and omits it from
421/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000422Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
423 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000424 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000425 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000426 Vendor(parseVendor(VendorStr.str())),
427 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000428 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
429 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000430}
431
432/// \brief Construct a triple from string representations of the architecture,
433/// vendor, OS, and environment.
434///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000435/// This joins each argument into a canonical string representation and parses
436/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000437Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
438 const Twine &EnvironmentStr)
439 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
440 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000441 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000442 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000443 Vendor(parseVendor(VendorStr.str())),
444 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000445 Environment(parseEnvironment(EnvironmentStr.str())),
446 ObjectFormat(parseFormat(EnvironmentStr.str())) {
447 if (ObjectFormat == Triple::UnknownObjectFormat)
448 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000449}
450
Duncan Sands501dff72010-08-12 11:31:39 +0000451std::string Triple::normalize(StringRef Str) {
452 // Parse into components.
453 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000454 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000455
456 // If the first component corresponds to a known architecture, preferentially
457 // use it for the architecture. If the second component corresponds to a
458 // known vendor, preferentially use it for the vendor, etc. This avoids silly
459 // component movement when a component parses as (eg) both a valid arch and a
460 // valid os.
461 ArchType Arch = UnknownArch;
462 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000463 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000464 VendorType Vendor = UnknownVendor;
465 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000466 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000467 OSType OS = UnknownOS;
468 if (Components.size() > 2)
Chandler Carruthaec97082012-02-21 08:53:32 +0000469 OS = parseOS(Components[2]);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000470 EnvironmentType Environment = UnknownEnvironment;
471 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000472 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000473 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000474 if (Components.size() > 4)
475 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000476
477 // Note which components are already in their final position. These will not
478 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000479 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000480 Found[0] = Arch != UnknownArch;
481 Found[1] = Vendor != UnknownVendor;
482 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000483 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000484
485 // If they are not there already, permute the components into their canonical
486 // positions by seeing if they parse as a valid architecture, and if so moving
487 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000488 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000489 if (Found[Pos])
490 continue; // Already in the canonical position.
491
492 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
493 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000494 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000495 continue;
496
497 // Does this component parse as valid for the target position?
498 bool Valid = false;
499 StringRef Comp = Components[Idx];
500 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000501 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000502 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000503 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000504 Valid = Arch != UnknownArch;
505 break;
506 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000507 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000508 Valid = Vendor != UnknownVendor;
509 break;
510 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000511 OS = parseOS(Comp);
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000512 Valid = OS != UnknownOS;
Duncan Sands501dff72010-08-12 11:31:39 +0000513 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000514 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000515 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000516 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000517 if (!Valid) {
518 ObjectFormat = parseFormat(Comp);
519 Valid = ObjectFormat != UnknownObjectFormat;
520 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000521 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000522 }
523 if (!Valid)
524 continue; // Nope, try the next component.
525
526 // Move the component to the target position, pushing any non-fixed
527 // components that are in the way to the right. This tends to give
528 // good results in the common cases of a forgotten vendor component
529 // or a wrongly positioned environment.
530 if (Pos < Idx) {
531 // Insert left, pushing the existing components to the right. For
532 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
533 StringRef CurrentComponent(""); // The empty component.
534 // Replace the component we are moving with an empty component.
535 std::swap(CurrentComponent, Components[Idx]);
536 // Insert the component being moved at Pos, displacing any existing
537 // components to the right.
538 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
539 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000540 while (i < array_lengthof(Found) && Found[i])
541 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000542 // Place the component at the new position, getting the component
543 // that was at this position - it will be moved right.
544 std::swap(CurrentComponent, Components[i]);
545 }
546 } else if (Pos > Idx) {
547 // Push right by inserting empty components until the component at Idx
548 // reaches the target position Pos. For example, pc-a -> -pc-a when
549 // moving pc to the second position.
550 do {
551 // Insert one empty component at Idx.
552 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000553 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000554 // Place the component at the new position, getting the component
555 // that was at this position - it will be moved right.
556 std::swap(CurrentComponent, Components[i]);
557 // If it was placed on top of an empty component then we are done.
558 if (CurrentComponent.empty())
559 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000560 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000561 while (++i < array_lengthof(Found) && Found[i])
562 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000563 }
564 // The last component was pushed off the end - append it.
565 if (!CurrentComponent.empty())
566 Components.push_back(CurrentComponent);
567
568 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000569 while (++Idx < array_lengthof(Found) && Found[Idx])
570 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000571 } while (Idx < Pos); // Add more until the final position is reached.
572 }
573 assert(Pos < Components.size() && Components[Pos] == Comp &&
574 "Component moved wrong!");
575 Found[Pos] = true;
576 break;
577 }
578 }
579
580 // Special case logic goes here. At this point Arch, Vendor and OS have the
581 // correct values for the computed components.
582
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000583 if (OS == Triple::Win32) {
584 Components.resize(4);
585 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000586 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000587 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000588 Components[3] = "msvc";
589 else
590 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000591 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000592 } else if (OS == Triple::MinGW32) {
593 Components.resize(4);
594 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000595 Components[3] = "gnu";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000596 } else if (OS == Triple::Cygwin) {
597 Components.resize(4);
598 Components[2] = "windows";
599 Components[3] = "cygnus";
600 }
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000601 if (OS == Triple::MinGW32 || OS == Triple::Cygwin ||
602 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
603 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
604 Components.resize(5);
605 Components[4] = getObjectFormatTypeName(ObjectFormat);
606 }
607 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000608
Duncan Sands501dff72010-08-12 11:31:39 +0000609 // Stick the corrected components back together to form the normalized string.
610 std::string Normalized;
611 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
612 if (i) Normalized += '-';
613 Normalized += Components[i];
614 }
615 return Normalized;
616}
617
Daniel Dunbar19e70762009-07-26 03:31:47 +0000618StringRef Triple::getArchName() const {
619 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000620}
621
Daniel Dunbar19e70762009-07-26 03:31:47 +0000622StringRef Triple::getVendorName() const {
623 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
624 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000625}
626
Daniel Dunbar19e70762009-07-26 03:31:47 +0000627StringRef Triple::getOSName() const {
628 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
629 Tmp = Tmp.split('-').second; // Strip second component
630 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000631}
632
Daniel Dunbar19e70762009-07-26 03:31:47 +0000633StringRef Triple::getEnvironmentName() const {
634 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
635 Tmp = Tmp.split('-').second; // Strip second component
636 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000637}
638
Daniel Dunbar19e70762009-07-26 03:31:47 +0000639StringRef Triple::getOSAndEnvironmentName() const {
640 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
641 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000642}
643
Chris Lattner553c9f32009-08-12 06:19:40 +0000644static unsigned EatNumber(StringRef &Str) {
645 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000646 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000647
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000648 do {
649 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000650 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000651
Chris Lattner553c9f32009-08-12 06:19:40 +0000652 // Eat the digit.
653 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000654 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000655
Chris Lattner553c9f32009-08-12 06:19:40 +0000656 return Result;
657}
658
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000659void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
660 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000661 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000662
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000663 // Assume that the OS portion of the triple starts with the canonical name.
664 StringRef OSTypeName = getOSTypeName(getOS());
665 if (OSName.startswith(OSTypeName))
666 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000667
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000668 // Any unset version defaults to 0.
669 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000670
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000671 // Parse up to three components.
672 unsigned *Components[3] = { &Major, &Minor, &Micro };
673 for (unsigned i = 0; i != 3; ++i) {
674 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
675 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000676
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000677 // Consume the leading number.
678 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000679
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000680 // Consume the separator, if present.
681 if (OSName.startswith("."))
682 OSName = OSName.substr(1);
683 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000684}
685
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000686bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
687 unsigned &Micro) const {
688 getOSVersion(Major, Minor, Micro);
689
690 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000691 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000692 case Darwin:
693 // Default to darwin8, i.e., MacOSX 10.4.
694 if (Major == 0)
695 Major = 8;
696 // Darwin version numbers are skewed from OS X versions.
697 if (Major < 4)
698 return false;
699 Micro = 0;
700 Minor = Major - 4;
701 Major = 10;
702 break;
703 case MacOSX:
704 // Default to 10.4.
705 if (Major == 0) {
706 Major = 10;
707 Minor = 4;
708 }
709 if (Major != 10)
710 return false;
711 break;
712 case IOS:
713 // Ignore the version from the triple. This is only handled because the
714 // the clang driver combines OS X and IOS support into a common Darwin
715 // toolchain that wants to know the OS X version number even when targeting
716 // IOS.
717 Major = 10;
718 Minor = 4;
719 Micro = 0;
720 break;
721 }
722 return true;
723}
724
Chad Rosierd84eaac2012-05-09 17:23:48 +0000725void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
726 unsigned &Micro) const {
727 switch (getOS()) {
728 default: llvm_unreachable("unexpected OS for Darwin triple");
729 case Darwin:
730 case MacOSX:
731 // Ignore the version from the triple. This is only handled because the
732 // the clang driver combines OS X and IOS support into a common Darwin
733 // toolchain that wants to know the iOS version number even when targeting
734 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000735 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000736 Minor = 0;
737 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000738 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000739 case IOS:
740 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000741 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000742 if (Major == 0)
Tim Northover00ed9962014-03-29 10:18:08 +0000743 Major = (getArch() == arm64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000744 break;
745 }
746}
747
Daniel Dunbar19e70762009-07-26 03:31:47 +0000748void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000749 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000750}
751
752void Triple::setArch(ArchType Kind) {
753 setArchName(getArchTypeName(Kind));
754}
755
756void Triple::setVendor(VendorType Kind) {
757 setVendorName(getVendorTypeName(Kind));
758}
759
760void Triple::setOS(OSType Kind) {
761 setOSName(getOSTypeName(Kind));
762}
763
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000764void Triple::setEnvironment(EnvironmentType Kind) {
765 setEnvironmentName(getEnvironmentTypeName(Kind));
766}
767
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000768void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000769 if (Environment == UnknownEnvironment)
770 return setEnvironmentName(getObjectFormatTypeName(Kind));
771
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000772 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
773 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000774}
775
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000776void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000777 // Work around a miscompilation bug for Twines in gcc 4.0.3.
778 SmallString<64> Triple;
779 Triple += Str;
780 Triple += "-";
781 Triple += getVendorName();
782 Triple += "-";
783 Triple += getOSAndEnvironmentName();
784 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000785}
786
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000787void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000788 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
789}
790
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000791void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000792 if (hasEnvironment())
793 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
794 "-" + getEnvironmentName());
795 else
796 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
797}
798
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000799void Triple::setEnvironmentName(StringRef Str) {
800 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000801 "-" + Str);
802}
803
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000804void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000805 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
806}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000807
808static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
809 switch (Arch) {
810 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000811 return 0;
812
813 case llvm::Triple::msp430:
814 return 16;
815
816 case llvm::Triple::amdil:
817 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000818 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000819 case llvm::Triple::hexagon:
820 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000821 case llvm::Triple::mips:
822 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000823 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000824 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000825 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000826 case llvm::Triple::sparc:
827 case llvm::Triple::tce:
828 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000829 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000830 case llvm::Triple::x86:
831 case llvm::Triple::xcore:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000832 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000833 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000834 return 32;
835
Tim Northover00ed9962014-03-29 10:18:08 +0000836 case llvm::Triple::arm64:
James Molloy09a53b92014-04-14 17:37:53 +0000837 case llvm::Triple::arm64_be:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000838 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000839 case llvm::Triple::aarch64_be:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000840 case llvm::Triple::mips64:
841 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000842 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000843 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000844 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000845 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000846 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000847 case llvm::Triple::x86_64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000848 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000849 return 64;
850 }
851 llvm_unreachable("Invalid architecture value");
852}
853
854bool Triple::isArch64Bit() const {
855 return getArchPointerBitWidth(getArch()) == 64;
856}
857
858bool Triple::isArch32Bit() const {
859 return getArchPointerBitWidth(getArch()) == 32;
860}
861
862bool Triple::isArch16Bit() const {
863 return getArchPointerBitWidth(getArch()) == 16;
864}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000865
866Triple Triple::get32BitArchVariant() const {
867 Triple T(*this);
868 switch (getArch()) {
869 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000870 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000871 case Triple::aarch64_be:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000872 case Triple::arm64:
James Molloy09a53b92014-04-14 17:37:53 +0000873 case Triple::arm64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000874 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000875 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000876 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000877 T.setArch(UnknownArch);
878 break;
879
880 case Triple::amdil:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000881 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000882 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000883 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000884 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000885 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000886 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000887 case Triple::mips:
888 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000889 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000890 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000891 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000892 case Triple::sparc:
893 case Triple::tce:
894 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000895 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000896 case Triple::x86:
897 case Triple::xcore:
898 // Already 32-bit.
899 break;
900
901 case Triple::mips64: T.setArch(Triple::mips); break;
902 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000903 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000904 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000905 case Triple::sparcv9: T.setArch(Triple::sparc); break;
906 case Triple::x86_64: T.setArch(Triple::x86); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000907 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000908 }
909 return T;
910}
911
912Triple Triple::get64BitArchVariant() const {
913 Triple T(*this);
914 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000915 case Triple::UnknownArch:
916 case Triple::amdil:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000917 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000918 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000919 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000920 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000921 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000922 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000923 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000924 case Triple::tce:
925 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000926 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000927 case Triple::xcore:
928 T.setArch(UnknownArch);
929 break;
930
Tim Northovere0e3aef2013-01-31 12:12:40 +0000931 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000932 case Triple::aarch64_be:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000933 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000934 case Triple::mips64:
935 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000936 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000937 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000938 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000939 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000940 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000941 case Triple::x86_64:
Tim Northover00ed9962014-03-29 10:18:08 +0000942 case Triple::arm64:
James Molloy09a53b92014-04-14 17:37:53 +0000943 case Triple::arm64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000944 // Already 64-bit.
945 break;
946
947 case Triple::mips: T.setArch(Triple::mips64); break;
948 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000949 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000950 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000951 case Triple::sparc: T.setArch(Triple::sparcv9); break;
952 case Triple::x86: T.setArch(Triple::x86_64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000953 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000954 }
955 return T;
956}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000957
958// FIXME: tblgen this.
959const char *Triple::getARMCPUForArch(StringRef MArch) const {
960 if (MArch.empty())
961 MArch = getArchName();
962
963 switch (getOS()) {
964 case llvm::Triple::NetBSD:
965 if (MArch == "armv6")
966 return "arm1176jzf-s";
967 break;
968 case llvm::Triple::Win32:
969 // FIXME: this is invalid for WindowsCE
970 return "cortex-a9";
971 default:
972 break;
973 }
974
975 const char *result = nullptr;
976 size_t offset = StringRef::npos;
977 if (MArch.startswith("arm"))
978 offset = 3;
979 if (MArch.startswith("thumb"))
980 offset = 5;
981 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
982 offset += 2;
983 if (offset != StringRef::npos)
984 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
985 .Cases("v2", "v2a", "arm2")
986 .Case("v3", "arm6")
987 .Case("v3m", "arm7m")
988 .Case("v4", "strongarm")
989 .Case("v4t", "arm7tdmi")
990 .Cases("v5", "v5t", "arm10tdmi")
991 .Cases("v5e", "v5te", "arm1022e")
992 .Case("v5tej", "arm926ej-s")
993 .Cases("v6", "v6k", "arm1136jf-s")
994 .Case("v6j", "arm1136j-s")
995 .Cases("v6z", "v6zk", "arm1176jzf-s")
996 .Case("v6t2", "arm1156t2-s")
997 .Cases("v6m", "v6-m", "cortex-m0")
998 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
999 .Cases("v7s", "v7-s", "swift")
1000 .Cases("v7r", "v7-r", "cortex-r4")
1001 .Cases("v7m", "v7-m", "cortex-m3")
1002 .Cases("v7em", "v7e-m", "cortex-m4")
1003 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1004 .Default(nullptr);
1005 else
1006 result = llvm::StringSwitch<const char *>(MArch)
1007 .Case("ep9312", "ep9312")
1008 .Case("iwmmxt", "iwmmxt")
1009 .Case("xscale", "xscale")
1010 .Default(nullptr);
1011
1012 if (result)
1013 return result;
1014
1015 // If all else failed, return the most base CPU with thumb interworking
1016 // supported by LLVM.
1017 // FIXME: Should warn once that we're falling back.
1018 switch (getOS()) {
1019 case llvm::Triple::NetBSD:
1020 switch (getEnvironment()) {
1021 case llvm::Triple::GNUEABIHF:
1022 case llvm::Triple::GNUEABI:
1023 case llvm::Triple::EABIHF:
1024 case llvm::Triple::EABI:
1025 return "arm926ej-s";
1026 default:
1027 return "strongarm";
1028 }
1029 default:
1030 switch (getEnvironment()) {
1031 case llvm::Triple::EABIHF:
1032 case llvm::Triple::GNUEABIHF:
1033 return "arm1176jzf-s";
1034 default:
1035 return "arm7tdmi";
1036 }
1037 }
1038}