blob: 7a9dc39369fada4b0dadcaf285265bf3fc692d5b [file] [log] [blame]
Daniel Dunbar4abd5662009-04-01 21:53:23 +00001//===--- Triple.cpp - Target triple helper class --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskine2595b52009-10-06 21:45:26 +000012#include "llvm/ADT/SmallString.h"
Chandler Carruthff6f3562012-02-12 09:27:38 +000013#include "llvm/ADT/StringSwitch.h"
David Blaikie46a9f012012-01-20 21:51:11 +000014#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov12062ba2009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar4abd5662009-04-01 21:53:23 +000016using namespace llvm;
17
Daniel Dunbar4abd5662009-04-01 21:53:23 +000018const char *Triple::getArchTypeName(ArchType Kind) {
19 switch (Kind) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +000020 case UnknownArch: return "unknown";
Jim Grosbachf638b262010-12-17 02:10:59 +000021
Christian Pirker6c2f4d42014-02-24 11:34:50 +000022 case aarch64: return "aarch64";
23 case aarch64_be: return "aarch64_be";
24 case arm: return "arm";
Christian Pirker2a111602014-03-28 14:35:30 +000025 case armeb: return "armeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000026 case hexagon: return "hexagon";
27 case mips: return "mips";
28 case mipsel: return "mipsel";
29 case mips64: return "mips64";
30 case mips64el: return "mips64el";
31 case msp430: return "msp430";
32 case ppc64: return "powerpc64";
33 case ppc64le: return "powerpc64le";
34 case ppc: return "powerpc";
35 case r600: return "r600";
36 case sparc: return "sparc";
37 case sparcv9: return "sparcv9";
38 case systemz: return "s390x";
39 case tce: return "tce";
40 case thumb: return "thumb";
Christian Pirker2a111602014-03-28 14:35:30 +000041 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000042 case x86: return "i386";
43 case x86_64: return "x86_64";
44 case xcore: return "xcore";
45 case nvptx: return "nvptx";
46 case nvptx64: return "nvptx64";
47 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +000048 case le64: return "le64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000049 case amdil: return "amdil";
Matt Arsenault3f9b0212014-09-19 19:52:11 +000050 case amdil64: return "amdil64";
51 case hsail: return "hsail";
52 case hsail64: return "hsail64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000053 case spir: return "spir";
54 case spir64: return "spir64";
Eric Christopher54fe1b22014-07-10 17:26:54 +000055 case kalimba: return "kalimba";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000056 }
57
David Blaikie46a9f012012-01-20 21:51:11 +000058 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000059}
60
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000061const char *Triple::getArchTypePrefix(ArchType Kind) {
62 switch (Kind) {
63 default:
Craig Topperc10719f2014-04-07 04:17:22 +000064 return nullptr;
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000065
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";
JF Bastien32972ef2014-09-12 17:54:17 +0000101 case le64: return "le64";
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000102
103 case amdil:
104 case amdil64: return "amdil";
105
106 case hsail:
107 case hsail64: return "hsail";
108
109 case spir:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000110 case spir64: return "spir";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000111 case kalimba: return "kalimba";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000112 }
113}
114
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000115const char *Triple::getVendorTypeName(VendorType Kind) {
116 switch (Kind) {
117 case UnknownVendor: return "unknown";
118
119 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000120 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000121 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000122 case BGP: return "bgp";
123 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000124 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000125 case IBM: return "ibm";
Daniel Sandersc5626f42014-07-09 16:03:10 +0000126 case ImaginationTechnologies: return "img";
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000127 case MipsTechnologies: return "mti";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000128 case NVIDIA: return "nvidia";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000129 case CSR: return "csr";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000130 }
131
David Blaikie46a9f012012-01-20 21:51:11 +0000132 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000133}
134
135const char *Triple::getOSTypeName(OSType Kind) {
136 switch (Kind) {
137 case UnknownOS: return "unknown";
138
139 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000140 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000141 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000142 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000143 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000144 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000145 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000146 case MacOSX: return "macosx";
Chris Lattner01218d52009-07-13 20:22:23 +0000147 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000148 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000149 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000150 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000151 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000152 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000153 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000154 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000155 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000156 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000157 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000158 case CUDA: return "cuda";
159 case NVCL: return "nvcl";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000160 }
161
David Blaikie46a9f012012-01-20 21:51:11 +0000162 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000163}
164
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000165const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
166 switch (Kind) {
167 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000168 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000169 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000170 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000171 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000172 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000173 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000174 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000175 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000176 case MSVC: return "msvc";
177 case Itanium: return "itanium";
178 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000179 }
180
David Blaikie46a9f012012-01-20 21:51:11 +0000181 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000182}
183
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000184Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000185 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000186 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000187 .Case("aarch64_be", aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000188 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
Chandler Carruthff6f3562012-02-12 09:27:38 +0000189 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000190 .Case("armeb", armeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000191 .Case("mips", mips)
192 .Case("mipsel", mipsel)
193 .Case("mips64", mips64)
194 .Case("mips64el", mips64el)
195 .Case("msp430", msp430)
196 .Case("ppc64", ppc64)
197 .Case("ppc32", ppc)
198 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000199 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000200 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000201 .Case("hexagon", hexagon)
202 .Case("sparc", sparc)
203 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000204 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000205 .Case("tce", tce)
206 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000207 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000208 .Case("x86", x86)
209 .Case("x86-64", x86_64)
210 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000211 .Case("nvptx", nvptx)
212 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000213 .Case("le32", le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000214 .Case("le64", le64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000215 .Case("amdil", amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000216 .Case("amdil64", amdil64)
217 .Case("hsail", hsail)
218 .Case("hsail64", hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000219 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000220 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000221 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000222 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000223}
224
Chandler Carruthaec97082012-02-21 08:53:32 +0000225static Triple::ArchType parseArch(StringRef ArchName) {
226 return StringSwitch<Triple::ArchType>(ArchName)
227 .Cases("i386", "i486", "i586", "i686", Triple::x86)
228 // FIXME: Do we need to support these?
229 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000230 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000231 .Case("powerpc", Triple::ppc)
232 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000233 .Case("powerpc64le", Triple::ppc64le)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000234 .Case("aarch64", Triple::aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000235 .Case("aarch64_be", Triple::aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000236 .Case("arm64", Triple::aarch64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000237 .Cases("arm", "xscale", Triple::arm)
Chandler Carruthb54950b2012-02-18 04:34:17 +0000238 // FIXME: It would be good to replace these with explicit names for all the
239 // various suffixes supported.
Chandler Carruthaec97082012-02-21 08:53:32 +0000240 .StartsWith("armv", Triple::arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000241 .Case("armeb", Triple::armeb)
242 .StartsWith("armebv", Triple::armeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000243 .Case("thumb", Triple::thumb)
244 .StartsWith("thumbv", Triple::thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000245 .Case("thumbeb", Triple::thumbeb)
246 .StartsWith("thumbebv", Triple::thumbeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000247 .Case("msp430", Triple::msp430)
248 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000249 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000250 .Cases("mips64", "mips64eb", Triple::mips64)
251 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000252 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000253 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000254 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000255 .Case("sparc", Triple::sparc)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000256 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000257 .Case("tce", Triple::tce)
258 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000259 .Case("nvptx", Triple::nvptx)
260 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000261 .Case("le32", Triple::le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000262 .Case("le64", Triple::le64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000263 .Case("amdil", Triple::amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000264 .Case("amdil64", Triple::amdil64)
265 .Case("hsail", Triple::hsail)
266 .Case("hsail64", Triple::hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000267 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000268 .Case("spir64", Triple::spir64)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000269 .StartsWith("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000270 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000271}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000272
Chandler Carruthaec97082012-02-21 08:53:32 +0000273static Triple::VendorType parseVendor(StringRef VendorName) {
274 return StringSwitch<Triple::VendorType>(VendorName)
275 .Case("apple", Triple::Apple)
276 .Case("pc", Triple::PC)
277 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000278 .Case("bgp", Triple::BGP)
279 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000280 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000281 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000282 .Case("img", Triple::ImaginationTechnologies)
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000283 .Case("mti", Triple::MipsTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000284 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000285 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000286 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000287}
288
Chandler Carruthaec97082012-02-21 08:53:32 +0000289static Triple::OSType parseOS(StringRef OSName) {
290 return StringSwitch<Triple::OSType>(OSName)
Chandler Carruthaec97082012-02-21 08:53:32 +0000291 .StartsWith("darwin", Triple::Darwin)
292 .StartsWith("dragonfly", Triple::DragonFly)
293 .StartsWith("freebsd", Triple::FreeBSD)
294 .StartsWith("ios", Triple::IOS)
295 .StartsWith("kfreebsd", Triple::KFreeBSD)
296 .StartsWith("linux", Triple::Linux)
297 .StartsWith("lv2", Triple::Lv2)
298 .StartsWith("macosx", Triple::MacOSX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000299 .StartsWith("netbsd", Triple::NetBSD)
300 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000301 .StartsWith("solaris", Triple::Solaris)
302 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000303 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000304 .StartsWith("haiku", Triple::Haiku)
305 .StartsWith("minix", Triple::Minix)
306 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000307 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000308 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000309 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000310 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000311 .StartsWith("cuda", Triple::CUDA)
312 .StartsWith("nvcl", Triple::NVCL)
Chandler Carruthaec97082012-02-21 08:53:32 +0000313 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000314}
315
Chandler Carruthaec97082012-02-21 08:53:32 +0000316static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
317 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000318 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000319 .StartsWith("eabi", Triple::EABI)
320 .StartsWith("gnueabihf", Triple::GNUEABIHF)
321 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000322 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000323 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000324 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000325 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000326 .StartsWith("msvc", Triple::MSVC)
327 .StartsWith("itanium", Triple::Itanium)
328 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000329 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000330}
331
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000332static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
333 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
334 .EndsWith("coff", Triple::COFF)
335 .EndsWith("elf", Triple::ELF)
336 .EndsWith("macho", Triple::MachO)
337 .Default(Triple::UnknownObjectFormat);
338}
339
Renato Golinc17a07b2014-07-18 12:00:48 +0000340static Triple::SubArchType parseSubArch(StringRef SubArchName) {
341 return StringSwitch<Triple::SubArchType>(SubArchName)
342 .EndsWith("v8", Triple::ARMSubArch_v8)
343 .EndsWith("v8a", Triple::ARMSubArch_v8)
344 .EndsWith("v7", Triple::ARMSubArch_v7)
345 .EndsWith("v7a", Triple::ARMSubArch_v7)
346 .EndsWith("v7em", Triple::ARMSubArch_v7em)
347 .EndsWith("v7l", Triple::ARMSubArch_v7)
348 .EndsWith("v7m", Triple::ARMSubArch_v7m)
349 .EndsWith("v7r", Triple::ARMSubArch_v7)
350 .EndsWith("v7s", Triple::ARMSubArch_v7s)
351 .EndsWith("v6", Triple::ARMSubArch_v6)
352 .EndsWith("v6m", Triple::ARMSubArch_v6m)
353 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
354 .EndsWith("v5", Triple::ARMSubArch_v5)
355 .EndsWith("v5e", Triple::ARMSubArch_v5)
356 .EndsWith("v5t", Triple::ARMSubArch_v5)
357 .EndsWith("v5te", Triple::ARMSubArch_v5te)
358 .EndsWith("v4t", Triple::ARMSubArch_v4t)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000359 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
360 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
361 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
Renato Golinc17a07b2014-07-18 12:00:48 +0000362 .Default(Triple::NoSubArch);
363}
364
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000365static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
366 switch (Kind) {
367 case Triple::UnknownObjectFormat: return "";
368 case Triple::COFF: return "coff";
369 case Triple::ELF: return "elf";
370 case Triple::MachO: return "macho";
371 }
372 llvm_unreachable("unknown object format type");
373}
374
375static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
376 if (T.isOSDarwin())
377 return Triple::MachO;
378 else if (T.isOSWindows())
379 return Triple::COFF;
380 return Triple::ELF;
381}
382
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000383/// \brief Construct a triple from the string representation provided.
384///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000385/// This stores the string representation and parses the various pieces into
386/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000387Triple::Triple(const Twine &Str)
388 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000389 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000390 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000391 Vendor(parseVendor(getVendorName())),
392 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000393 Environment(parseEnvironment(getEnvironmentName())),
394 ObjectFormat(parseFormat(getEnvironmentName())) {
395 if (ObjectFormat == Triple::UnknownObjectFormat)
396 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000397}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000398
399/// \brief Construct a triple from string representations of the architecture,
400/// vendor, and OS.
401///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000402/// This joins each argument into a canonical string representation and parses
403/// them into enum members. It leaves the environment unknown and omits it from
404/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000405Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
406 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000407 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000408 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000409 Vendor(parseVendor(VendorStr.str())),
410 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000411 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
412 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000413}
414
415/// \brief Construct a triple from string representations of the architecture,
416/// vendor, OS, and environment.
417///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000418/// This joins each argument into a canonical string representation and parses
419/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000420Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
421 const Twine &EnvironmentStr)
422 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
423 EnvironmentStr).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(parseEnvironment(EnvironmentStr.str())),
429 ObjectFormat(parseFormat(EnvironmentStr.str())) {
430 if (ObjectFormat == Triple::UnknownObjectFormat)
431 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000432}
433
Duncan Sands501dff72010-08-12 11:31:39 +0000434std::string Triple::normalize(StringRef Str) {
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000435 bool IsMinGW32 = false;
436 bool IsCygwin = false;
437
Duncan Sands501dff72010-08-12 11:31:39 +0000438 // Parse into components.
439 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000440 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000441
442 // If the first component corresponds to a known architecture, preferentially
443 // use it for the architecture. If the second component corresponds to a
444 // known vendor, preferentially use it for the vendor, etc. This avoids silly
445 // component movement when a component parses as (eg) both a valid arch and a
446 // valid os.
447 ArchType Arch = UnknownArch;
448 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000449 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000450 VendorType Vendor = UnknownVendor;
451 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000452 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000453 OSType OS = UnknownOS;
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000454 if (Components.size() > 2) {
Chandler Carruthaec97082012-02-21 08:53:32 +0000455 OS = parseOS(Components[2]);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000456 IsCygwin = Components[2].startswith("cygwin");
457 IsMinGW32 = Components[2].startswith("mingw");
458 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000459 EnvironmentType Environment = UnknownEnvironment;
460 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000461 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000462 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000463 if (Components.size() > 4)
464 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000465
466 // Note which components are already in their final position. These will not
467 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000468 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000469 Found[0] = Arch != UnknownArch;
470 Found[1] = Vendor != UnknownVendor;
471 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000472 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000473
474 // If they are not there already, permute the components into their canonical
475 // positions by seeing if they parse as a valid architecture, and if so moving
476 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000477 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000478 if (Found[Pos])
479 continue; // Already in the canonical position.
480
481 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
482 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000483 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000484 continue;
485
486 // Does this component parse as valid for the target position?
487 bool Valid = false;
488 StringRef Comp = Components[Idx];
489 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000490 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000491 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000492 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000493 Valid = Arch != UnknownArch;
494 break;
495 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000496 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000497 Valid = Vendor != UnknownVendor;
498 break;
499 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000500 OS = parseOS(Comp);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000501 IsCygwin = Comp.startswith("cygwin");
502 IsMinGW32 = Comp.startswith("mingw");
503 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
Duncan Sands501dff72010-08-12 11:31:39 +0000504 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000505 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000506 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000507 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000508 if (!Valid) {
509 ObjectFormat = parseFormat(Comp);
510 Valid = ObjectFormat != UnknownObjectFormat;
511 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000512 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000513 }
514 if (!Valid)
515 continue; // Nope, try the next component.
516
517 // Move the component to the target position, pushing any non-fixed
518 // components that are in the way to the right. This tends to give
519 // good results in the common cases of a forgotten vendor component
520 // or a wrongly positioned environment.
521 if (Pos < Idx) {
522 // Insert left, pushing the existing components to the right. For
523 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
524 StringRef CurrentComponent(""); // The empty component.
525 // Replace the component we are moving with an empty component.
526 std::swap(CurrentComponent, Components[Idx]);
527 // Insert the component being moved at Pos, displacing any existing
528 // components to the right.
529 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
530 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000531 while (i < array_lengthof(Found) && Found[i])
532 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000533 // Place the component at the new position, getting the component
534 // that was at this position - it will be moved right.
535 std::swap(CurrentComponent, Components[i]);
536 }
537 } else if (Pos > Idx) {
538 // Push right by inserting empty components until the component at Idx
539 // reaches the target position Pos. For example, pc-a -> -pc-a when
540 // moving pc to the second position.
541 do {
542 // Insert one empty component at Idx.
543 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000544 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000545 // Place the component at the new position, getting the component
546 // that was at this position - it will be moved right.
547 std::swap(CurrentComponent, Components[i]);
548 // If it was placed on top of an empty component then we are done.
549 if (CurrentComponent.empty())
550 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000551 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000552 while (++i < array_lengthof(Found) && Found[i])
553 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000554 }
555 // The last component was pushed off the end - append it.
556 if (!CurrentComponent.empty())
557 Components.push_back(CurrentComponent);
558
559 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000560 while (++Idx < array_lengthof(Found) && Found[Idx])
561 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000562 } while (Idx < Pos); // Add more until the final position is reached.
563 }
564 assert(Pos < Components.size() && Components[Pos] == Comp &&
565 "Component moved wrong!");
566 Found[Pos] = true;
567 break;
568 }
569 }
570
571 // Special case logic goes here. At this point Arch, Vendor and OS have the
572 // correct values for the computed components.
573
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000574 if (OS == Triple::Win32) {
575 Components.resize(4);
576 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000577 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000578 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000579 Components[3] = "msvc";
580 else
581 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000582 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000583 } else if (IsMinGW32) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000584 Components.resize(4);
585 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000586 Components[3] = "gnu";
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000587 } else if (IsCygwin) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000588 Components.resize(4);
589 Components[2] = "windows";
590 Components[3] = "cygnus";
591 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000592 if (IsMinGW32 || IsCygwin ||
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000593 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
594 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
595 Components.resize(5);
596 Components[4] = getObjectFormatTypeName(ObjectFormat);
597 }
598 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000599
Duncan Sands501dff72010-08-12 11:31:39 +0000600 // Stick the corrected components back together to form the normalized string.
601 std::string Normalized;
602 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
603 if (i) Normalized += '-';
604 Normalized += Components[i];
605 }
606 return Normalized;
607}
608
Daniel Dunbar19e70762009-07-26 03:31:47 +0000609StringRef Triple::getArchName() const {
610 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000611}
612
Daniel Dunbar19e70762009-07-26 03:31:47 +0000613StringRef Triple::getVendorName() const {
614 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
615 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000616}
617
Daniel Dunbar19e70762009-07-26 03:31:47 +0000618StringRef Triple::getOSName() const {
619 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
620 Tmp = Tmp.split('-').second; // Strip second component
621 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000622}
623
Daniel Dunbar19e70762009-07-26 03:31:47 +0000624StringRef Triple::getEnvironmentName() const {
625 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
626 Tmp = Tmp.split('-').second; // Strip second component
627 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000628}
629
Daniel Dunbar19e70762009-07-26 03:31:47 +0000630StringRef Triple::getOSAndEnvironmentName() const {
631 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
632 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000633}
634
Chris Lattner553c9f32009-08-12 06:19:40 +0000635static unsigned EatNumber(StringRef &Str) {
636 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000637 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000638
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000639 do {
640 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000641 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000642
Chris Lattner553c9f32009-08-12 06:19:40 +0000643 // Eat the digit.
644 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000645 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000646
Chris Lattner553c9f32009-08-12 06:19:40 +0000647 return Result;
648}
649
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000650void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
651 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000652 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000653
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000654 // Assume that the OS portion of the triple starts with the canonical name.
655 StringRef OSTypeName = getOSTypeName(getOS());
656 if (OSName.startswith(OSTypeName))
657 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000658
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000659 // Any unset version defaults to 0.
660 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000661
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000662 // Parse up to three components.
663 unsigned *Components[3] = { &Major, &Minor, &Micro };
664 for (unsigned i = 0; i != 3; ++i) {
665 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
666 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000667
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000668 // Consume the leading number.
669 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000670
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000671 // Consume the separator, if present.
672 if (OSName.startswith("."))
673 OSName = OSName.substr(1);
674 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000675}
676
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000677bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
678 unsigned &Micro) const {
679 getOSVersion(Major, Minor, Micro);
680
681 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000682 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000683 case Darwin:
684 // Default to darwin8, i.e., MacOSX 10.4.
685 if (Major == 0)
686 Major = 8;
687 // Darwin version numbers are skewed from OS X versions.
688 if (Major < 4)
689 return false;
690 Micro = 0;
691 Minor = Major - 4;
692 Major = 10;
693 break;
694 case MacOSX:
695 // Default to 10.4.
696 if (Major == 0) {
697 Major = 10;
698 Minor = 4;
699 }
700 if (Major != 10)
701 return false;
702 break;
703 case IOS:
704 // Ignore the version from the triple. This is only handled because the
705 // the clang driver combines OS X and IOS support into a common Darwin
706 // toolchain that wants to know the OS X version number even when targeting
707 // IOS.
708 Major = 10;
709 Minor = 4;
710 Micro = 0;
711 break;
712 }
713 return true;
714}
715
Chad Rosierd84eaac2012-05-09 17:23:48 +0000716void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
717 unsigned &Micro) const {
718 switch (getOS()) {
719 default: llvm_unreachable("unexpected OS for Darwin triple");
720 case Darwin:
721 case MacOSX:
722 // Ignore the version from the triple. This is only handled because the
723 // the clang driver combines OS X and IOS support into a common Darwin
724 // toolchain that wants to know the iOS version number even when targeting
725 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000726 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000727 Minor = 0;
728 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000729 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000730 case IOS:
731 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000732 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000733 if (Major == 0)
Tim Northovere19bed72014-07-23 12:32:47 +0000734 Major = (getArch() == aarch64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000735 break;
736 }
737}
738
Daniel Dunbar19e70762009-07-26 03:31:47 +0000739void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000740 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000741}
742
743void Triple::setArch(ArchType Kind) {
744 setArchName(getArchTypeName(Kind));
745}
746
747void Triple::setVendor(VendorType Kind) {
748 setVendorName(getVendorTypeName(Kind));
749}
750
751void Triple::setOS(OSType Kind) {
752 setOSName(getOSTypeName(Kind));
753}
754
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000755void Triple::setEnvironment(EnvironmentType Kind) {
756 setEnvironmentName(getEnvironmentTypeName(Kind));
757}
758
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000759void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000760 if (Environment == UnknownEnvironment)
761 return setEnvironmentName(getObjectFormatTypeName(Kind));
762
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000763 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
764 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000765}
766
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000767void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000768 // Work around a miscompilation bug for Twines in gcc 4.0.3.
769 SmallString<64> Triple;
770 Triple += Str;
771 Triple += "-";
772 Triple += getVendorName();
773 Triple += "-";
774 Triple += getOSAndEnvironmentName();
775 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000776}
777
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000778void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000779 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
780}
781
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000782void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000783 if (hasEnvironment())
784 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
785 "-" + getEnvironmentName());
786 else
787 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
788}
789
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000790void Triple::setEnvironmentName(StringRef Str) {
791 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000792 "-" + Str);
793}
794
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000795void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000796 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
797}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000798
799static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
800 switch (Arch) {
801 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000802 return 0;
803
804 case llvm::Triple::msp430:
805 return 16;
806
Chandler Carruthb90c1022012-01-31 04:52:32 +0000807 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000808 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000809 case llvm::Triple::hexagon:
810 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000811 case llvm::Triple::mips:
812 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000813 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000814 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000815 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000816 case llvm::Triple::sparc:
817 case llvm::Triple::tce:
818 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000819 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000820 case llvm::Triple::x86:
821 case llvm::Triple::xcore:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000822 case llvm::Triple::amdil:
823 case llvm::Triple::hsail:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000824 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000825 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000826 return 32;
827
Tim Northovere0e3aef2013-01-31 12:12:40 +0000828 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000829 case llvm::Triple::aarch64_be:
JF Bastien32972ef2014-09-12 17:54:17 +0000830 case llvm::Triple::le64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000831 case llvm::Triple::mips64:
832 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000833 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000834 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000835 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000836 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000837 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000838 case llvm::Triple::x86_64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000839 case llvm::Triple::amdil64:
840 case llvm::Triple::hsail64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000841 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000842 return 64;
843 }
844 llvm_unreachable("Invalid architecture value");
845}
846
847bool Triple::isArch64Bit() const {
848 return getArchPointerBitWidth(getArch()) == 64;
849}
850
851bool Triple::isArch32Bit() const {
852 return getArchPointerBitWidth(getArch()) == 32;
853}
854
855bool Triple::isArch16Bit() const {
856 return getArchPointerBitWidth(getArch()) == 16;
857}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000858
859Triple Triple::get32BitArchVariant() const {
860 Triple T(*this);
861 switch (getArch()) {
862 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000863 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000864 case Triple::aarch64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000865 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000866 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000867 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000868 T.setArch(UnknownArch);
869 break;
870
871 case Triple::amdil:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000872 case Triple::hsail:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000873 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000874 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000875 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000876 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000877 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000878 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000879 case Triple::mips:
880 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000881 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000882 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000883 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000884 case Triple::sparc:
885 case Triple::tce:
886 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000887 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000888 case Triple::x86:
889 case Triple::xcore:
890 // Already 32-bit.
891 break;
892
JF Bastien32972ef2014-09-12 17:54:17 +0000893 case Triple::le64: T.setArch(Triple::le32); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000894 case Triple::mips64: T.setArch(Triple::mips); break;
895 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000896 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000897 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000898 case Triple::sparcv9: T.setArch(Triple::sparc); break;
899 case Triple::x86_64: T.setArch(Triple::x86); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000900 case Triple::amdil64: T.setArch(Triple::amdil); break;
901 case Triple::hsail64: T.setArch(Triple::hsail); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000902 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000903 }
904 return T;
905}
906
907Triple Triple::get64BitArchVariant() const {
908 Triple T(*this);
909 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000910 case Triple::UnknownArch:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000911 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000912 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000913 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000914 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000915 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000916 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000917 case Triple::tce:
918 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000919 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000920 case Triple::xcore:
921 T.setArch(UnknownArch);
922 break;
923
Tim Northovere0e3aef2013-01-31 12:12:40 +0000924 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000925 case Triple::aarch64_be:
JF Bastien32972ef2014-09-12 17:54:17 +0000926 case Triple::le64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000927 case Triple::amdil64:
928 case Triple::hsail64:
929 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000930 case Triple::mips64:
931 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000932 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000933 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000934 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000935 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000936 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000937 case Triple::x86_64:
938 // Already 64-bit.
939 break;
940
JF Bastien32972ef2014-09-12 17:54:17 +0000941 case Triple::le32: T.setArch(Triple::le64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000942 case Triple::mips: T.setArch(Triple::mips64); break;
943 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000944 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000945 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000946 case Triple::sparc: T.setArch(Triple::sparcv9); break;
947 case Triple::x86: T.setArch(Triple::x86_64); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000948 case Triple::amdil: T.setArch(Triple::amdil64); break;
949 case Triple::hsail: T.setArch(Triple::hsail64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000950 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000951 }
952 return T;
953}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000954
955// FIXME: tblgen this.
956const char *Triple::getARMCPUForArch(StringRef MArch) const {
957 if (MArch.empty())
958 MArch = getArchName();
959
960 switch (getOS()) {
Ed Masteea0257a2014-09-09 17:47:24 +0000961 case llvm::Triple::FreeBSD:
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000962 case llvm::Triple::NetBSD:
963 if (MArch == "armv6")
964 return "arm1176jzf-s";
965 break;
966 case llvm::Triple::Win32:
967 // FIXME: this is invalid for WindowsCE
968 return "cortex-a9";
969 default:
970 break;
971 }
972
973 const char *result = nullptr;
974 size_t offset = StringRef::npos;
975 if (MArch.startswith("arm"))
976 offset = 3;
977 if (MArch.startswith("thumb"))
978 offset = 5;
979 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
980 offset += 2;
981 if (offset != StringRef::npos)
982 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
983 .Cases("v2", "v2a", "arm2")
984 .Case("v3", "arm6")
985 .Case("v3m", "arm7m")
986 .Case("v4", "strongarm")
987 .Case("v4t", "arm7tdmi")
988 .Cases("v5", "v5t", "arm10tdmi")
989 .Cases("v5e", "v5te", "arm1022e")
990 .Case("v5tej", "arm926ej-s")
991 .Cases("v6", "v6k", "arm1136jf-s")
992 .Case("v6j", "arm1136j-s")
993 .Cases("v6z", "v6zk", "arm1176jzf-s")
994 .Case("v6t2", "arm1156t2-s")
995 .Cases("v6m", "v6-m", "cortex-m0")
996 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
997 .Cases("v7s", "v7-s", "swift")
998 .Cases("v7r", "v7-r", "cortex-r4")
999 .Cases("v7m", "v7-m", "cortex-m3")
1000 .Cases("v7em", "v7e-m", "cortex-m4")
1001 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1002 .Default(nullptr);
1003 else
1004 result = llvm::StringSwitch<const char *>(MArch)
1005 .Case("ep9312", "ep9312")
1006 .Case("iwmmxt", "iwmmxt")
1007 .Case("xscale", "xscale")
1008 .Default(nullptr);
1009
1010 if (result)
1011 return result;
1012
1013 // If all else failed, return the most base CPU with thumb interworking
1014 // supported by LLVM.
1015 // FIXME: Should warn once that we're falling back.
1016 switch (getOS()) {
1017 case llvm::Triple::NetBSD:
1018 switch (getEnvironment()) {
1019 case llvm::Triple::GNUEABIHF:
1020 case llvm::Triple::GNUEABI:
1021 case llvm::Triple::EABIHF:
1022 case llvm::Triple::EABI:
1023 return "arm926ej-s";
1024 default:
1025 return "strongarm";
1026 }
1027 default:
1028 switch (getEnvironment()) {
1029 case llvm::Triple::EABIHF:
1030 case llvm::Triple::GNUEABIHF:
1031 return "arm1176jzf-s";
1032 default:
1033 return "arm7tdmi";
1034 }
1035 }
1036}