blob: 64cd6e73e9e4ec520b8e3e4071ac66e19e5c53f7 [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";
Tom Stellard4082a6c2014-12-02 16:45:47 +0000160 case AMDHSA: return "amdhsa";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000161 }
162
David Blaikie46a9f012012-01-20 21:51:11 +0000163 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000164}
165
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000166const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
167 switch (Kind) {
168 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000169 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000170 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000171 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000172 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000173 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000174 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000175 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000176 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000177 case MSVC: return "msvc";
178 case Itanium: return "itanium";
179 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000180 }
181
David Blaikie46a9f012012-01-20 21:51:11 +0000182 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000183}
184
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000185Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000186 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000187 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000188 .Case("aarch64_be", aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000189 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
Chandler Carruthff6f3562012-02-12 09:27:38 +0000190 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000191 .Case("armeb", armeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000192 .Case("mips", mips)
193 .Case("mipsel", mipsel)
194 .Case("mips64", mips64)
195 .Case("mips64el", mips64el)
196 .Case("msp430", msp430)
197 .Case("ppc64", ppc64)
198 .Case("ppc32", ppc)
199 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000200 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000201 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000202 .Case("hexagon", hexagon)
203 .Case("sparc", sparc)
204 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000205 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000206 .Case("tce", tce)
207 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000208 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000209 .Case("x86", x86)
210 .Case("x86-64", x86_64)
211 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000212 .Case("nvptx", nvptx)
213 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000214 .Case("le32", le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000215 .Case("le64", le64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000216 .Case("amdil", amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000217 .Case("amdil64", amdil64)
218 .Case("hsail", hsail)
219 .Case("hsail64", hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000220 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000221 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000222 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000223 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000224}
225
Renato Golin609bf922014-11-17 14:08:57 +0000226static Triple::ArchType parseARMArch(StringRef ArchName) {
227 size_t offset = StringRef::npos;
228 Triple::ArchType arch = Triple::UnknownArch;
229 bool isThumb = ArchName.startswith("thumb");
230
231 if (ArchName.equals("arm"))
232 return Triple::arm;
233 if (ArchName.equals("armeb"))
234 return Triple::armeb;
235 if (ArchName.equals("thumb"))
236 return Triple::thumb;
237 if (ArchName.equals("thumbeb"))
238 return Triple::thumbeb;
239 if (ArchName.equals("arm64") || ArchName.equals("aarch64"))
240 return Triple::aarch64;
241 if (ArchName.equals("aarch64_be"))
242 return Triple::aarch64_be;
243
244 if (ArchName.startswith("armv")) {
245 offset = 3;
246 arch = Triple::arm;
247 } else if (ArchName.startswith("armebv")) {
248 offset = 5;
249 arch = Triple::armeb;
250 } else if (ArchName.startswith("thumbv")) {
251 offset = 5;
252 arch = Triple::thumb;
253 } else if (ArchName.startswith("thumbebv")) {
254 offset = 7;
255 arch = Triple::thumbeb;
256 }
257 return StringSwitch<Triple::ArchType>(ArchName.substr(offset))
258 .Cases("v2", "v2a", isThumb ? Triple::UnknownArch : arch)
259 .Cases("v3", "v3m", isThumb ? Triple::UnknownArch : arch)
260 .Cases("v4", "v4t", arch)
261 .Cases("v5", "v5e", "v5t", "v5te", "v5tej", arch)
262 .Cases("v6", "v6j", "v6k", "v6m", arch)
263 .Cases("v6t2", "v6z", "v6zk", arch)
264 .Cases("v7", "v7a", "v7em", "v7l", arch)
265 .Cases("v7m", "v7r", "v7s", arch)
266 .Cases("v8", "v8a", arch)
267 .Default(Triple::UnknownArch);
268}
269
Chandler Carruthaec97082012-02-21 08:53:32 +0000270static Triple::ArchType parseArch(StringRef ArchName) {
271 return StringSwitch<Triple::ArchType>(ArchName)
272 .Cases("i386", "i486", "i586", "i686", Triple::x86)
273 // FIXME: Do we need to support these?
274 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000275 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000276 .Case("powerpc", Triple::ppc)
277 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000278 .Case("powerpc64le", Triple::ppc64le)
Renato Golin609bf922014-11-17 14:08:57 +0000279 .Case("xscale", Triple::arm)
280 .StartsWith("arm", parseARMArch(ArchName))
281 .StartsWith("thumb", parseARMArch(ArchName))
282 .StartsWith("aarch64", parseARMArch(ArchName))
Chandler Carruthaec97082012-02-21 08:53:32 +0000283 .Case("msp430", Triple::msp430)
284 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000285 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000286 .Cases("mips64", "mips64eb", Triple::mips64)
287 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000288 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000289 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000290 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000291 .Case("sparc", Triple::sparc)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000292 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000293 .Case("tce", Triple::tce)
294 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000295 .Case("nvptx", Triple::nvptx)
296 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000297 .Case("le32", Triple::le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000298 .Case("le64", Triple::le64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000299 .Case("amdil", Triple::amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000300 .Case("amdil64", Triple::amdil64)
301 .Case("hsail", Triple::hsail)
302 .Case("hsail64", Triple::hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000303 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000304 .Case("spir64", Triple::spir64)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000305 .StartsWith("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000306 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000307}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000308
Chandler Carruthaec97082012-02-21 08:53:32 +0000309static Triple::VendorType parseVendor(StringRef VendorName) {
310 return StringSwitch<Triple::VendorType>(VendorName)
311 .Case("apple", Triple::Apple)
312 .Case("pc", Triple::PC)
313 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000314 .Case("bgp", Triple::BGP)
315 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000316 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000317 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000318 .Case("img", Triple::ImaginationTechnologies)
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000319 .Case("mti", Triple::MipsTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000320 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000321 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000322 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000323}
324
Chandler Carruthaec97082012-02-21 08:53:32 +0000325static Triple::OSType parseOS(StringRef OSName) {
326 return StringSwitch<Triple::OSType>(OSName)
Chandler Carruthaec97082012-02-21 08:53:32 +0000327 .StartsWith("darwin", Triple::Darwin)
328 .StartsWith("dragonfly", Triple::DragonFly)
329 .StartsWith("freebsd", Triple::FreeBSD)
330 .StartsWith("ios", Triple::IOS)
331 .StartsWith("kfreebsd", Triple::KFreeBSD)
332 .StartsWith("linux", Triple::Linux)
333 .StartsWith("lv2", Triple::Lv2)
334 .StartsWith("macosx", Triple::MacOSX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000335 .StartsWith("netbsd", Triple::NetBSD)
336 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000337 .StartsWith("solaris", Triple::Solaris)
338 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000339 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000340 .StartsWith("haiku", Triple::Haiku)
341 .StartsWith("minix", Triple::Minix)
342 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000343 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000344 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000345 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000346 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000347 .StartsWith("cuda", Triple::CUDA)
348 .StartsWith("nvcl", Triple::NVCL)
Tom Stellard4082a6c2014-12-02 16:45:47 +0000349 .StartsWith("amdhsa", Triple::AMDHSA)
Chandler Carruthaec97082012-02-21 08:53:32 +0000350 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000351}
352
Chandler Carruthaec97082012-02-21 08:53:32 +0000353static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
354 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000355 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000356 .StartsWith("eabi", Triple::EABI)
357 .StartsWith("gnueabihf", Triple::GNUEABIHF)
358 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000359 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000360 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000361 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000362 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000363 .StartsWith("msvc", Triple::MSVC)
364 .StartsWith("itanium", Triple::Itanium)
365 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000366 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000367}
368
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000369static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
370 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
371 .EndsWith("coff", Triple::COFF)
372 .EndsWith("elf", Triple::ELF)
373 .EndsWith("macho", Triple::MachO)
374 .Default(Triple::UnknownObjectFormat);
375}
376
Renato Golinc17a07b2014-07-18 12:00:48 +0000377static Triple::SubArchType parseSubArch(StringRef SubArchName) {
378 return StringSwitch<Triple::SubArchType>(SubArchName)
379 .EndsWith("v8", Triple::ARMSubArch_v8)
380 .EndsWith("v8a", Triple::ARMSubArch_v8)
381 .EndsWith("v7", Triple::ARMSubArch_v7)
382 .EndsWith("v7a", Triple::ARMSubArch_v7)
383 .EndsWith("v7em", Triple::ARMSubArch_v7em)
384 .EndsWith("v7l", Triple::ARMSubArch_v7)
385 .EndsWith("v7m", Triple::ARMSubArch_v7m)
386 .EndsWith("v7r", Triple::ARMSubArch_v7)
387 .EndsWith("v7s", Triple::ARMSubArch_v7s)
388 .EndsWith("v6", Triple::ARMSubArch_v6)
389 .EndsWith("v6m", Triple::ARMSubArch_v6m)
390 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
391 .EndsWith("v5", Triple::ARMSubArch_v5)
392 .EndsWith("v5e", Triple::ARMSubArch_v5)
393 .EndsWith("v5t", Triple::ARMSubArch_v5)
394 .EndsWith("v5te", Triple::ARMSubArch_v5te)
395 .EndsWith("v4t", Triple::ARMSubArch_v4t)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000396 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
397 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
398 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
Renato Golinc17a07b2014-07-18 12:00:48 +0000399 .Default(Triple::NoSubArch);
400}
401
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000402static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
403 switch (Kind) {
404 case Triple::UnknownObjectFormat: return "";
405 case Triple::COFF: return "coff";
406 case Triple::ELF: return "elf";
407 case Triple::MachO: return "macho";
408 }
409 llvm_unreachable("unknown object format type");
410}
411
412static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
413 if (T.isOSDarwin())
414 return Triple::MachO;
415 else if (T.isOSWindows())
416 return Triple::COFF;
417 return Triple::ELF;
418}
419
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000420/// \brief Construct a triple from the string representation provided.
421///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000422/// This stores the string representation and parses the various pieces into
423/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000424Triple::Triple(const Twine &Str)
425 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000426 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000427 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000428 Vendor(parseVendor(getVendorName())),
429 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000430 Environment(parseEnvironment(getEnvironmentName())),
431 ObjectFormat(parseFormat(getEnvironmentName())) {
432 if (ObjectFormat == Triple::UnknownObjectFormat)
433 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000434}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000435
436/// \brief Construct a triple from string representations of the architecture,
437/// vendor, and OS.
438///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000439/// This joins each argument into a canonical string representation and parses
440/// them into enum members. It leaves the environment unknown and omits it from
441/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000442Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
443 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000444 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000445 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000446 Vendor(parseVendor(VendorStr.str())),
447 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000448 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
449 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000450}
451
452/// \brief Construct a triple from string representations of the architecture,
453/// vendor, OS, and environment.
454///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000455/// This joins each argument into a canonical string representation and parses
456/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000457Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
458 const Twine &EnvironmentStr)
459 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
460 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000461 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000462 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000463 Vendor(parseVendor(VendorStr.str())),
464 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000465 Environment(parseEnvironment(EnvironmentStr.str())),
466 ObjectFormat(parseFormat(EnvironmentStr.str())) {
467 if (ObjectFormat == Triple::UnknownObjectFormat)
468 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000469}
470
Duncan Sands501dff72010-08-12 11:31:39 +0000471std::string Triple::normalize(StringRef Str) {
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000472 bool IsMinGW32 = false;
473 bool IsCygwin = false;
474
Duncan Sands501dff72010-08-12 11:31:39 +0000475 // Parse into components.
476 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000477 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000478
479 // If the first component corresponds to a known architecture, preferentially
480 // use it for the architecture. If the second component corresponds to a
481 // known vendor, preferentially use it for the vendor, etc. This avoids silly
482 // component movement when a component parses as (eg) both a valid arch and a
483 // valid os.
484 ArchType Arch = UnknownArch;
485 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000486 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000487 VendorType Vendor = UnknownVendor;
488 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000489 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000490 OSType OS = UnknownOS;
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000491 if (Components.size() > 2) {
Chandler Carruthaec97082012-02-21 08:53:32 +0000492 OS = parseOS(Components[2]);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000493 IsCygwin = Components[2].startswith("cygwin");
494 IsMinGW32 = Components[2].startswith("mingw");
495 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000496 EnvironmentType Environment = UnknownEnvironment;
497 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000498 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000499 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000500 if (Components.size() > 4)
501 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000502
503 // Note which components are already in their final position. These will not
504 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000505 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000506 Found[0] = Arch != UnknownArch;
507 Found[1] = Vendor != UnknownVendor;
508 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000509 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000510
511 // If they are not there already, permute the components into their canonical
512 // positions by seeing if they parse as a valid architecture, and if so moving
513 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000514 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000515 if (Found[Pos])
516 continue; // Already in the canonical position.
517
518 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
519 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000520 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000521 continue;
522
523 // Does this component parse as valid for the target position?
524 bool Valid = false;
525 StringRef Comp = Components[Idx];
526 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000527 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000528 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000529 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000530 Valid = Arch != UnknownArch;
531 break;
532 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000533 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000534 Valid = Vendor != UnknownVendor;
535 break;
536 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000537 OS = parseOS(Comp);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000538 IsCygwin = Comp.startswith("cygwin");
539 IsMinGW32 = Comp.startswith("mingw");
540 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
Duncan Sands501dff72010-08-12 11:31:39 +0000541 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000542 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000543 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000544 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000545 if (!Valid) {
546 ObjectFormat = parseFormat(Comp);
547 Valid = ObjectFormat != UnknownObjectFormat;
548 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000549 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000550 }
551 if (!Valid)
552 continue; // Nope, try the next component.
553
554 // Move the component to the target position, pushing any non-fixed
555 // components that are in the way to the right. This tends to give
556 // good results in the common cases of a forgotten vendor component
557 // or a wrongly positioned environment.
558 if (Pos < Idx) {
559 // Insert left, pushing the existing components to the right. For
560 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
561 StringRef CurrentComponent(""); // The empty component.
562 // Replace the component we are moving with an empty component.
563 std::swap(CurrentComponent, Components[Idx]);
564 // Insert the component being moved at Pos, displacing any existing
565 // components to the right.
566 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
567 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000568 while (i < array_lengthof(Found) && Found[i])
569 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000570 // Place the component at the new position, getting the component
571 // that was at this position - it will be moved right.
572 std::swap(CurrentComponent, Components[i]);
573 }
574 } else if (Pos > Idx) {
575 // Push right by inserting empty components until the component at Idx
576 // reaches the target position Pos. For example, pc-a -> -pc-a when
577 // moving pc to the second position.
578 do {
579 // Insert one empty component at Idx.
580 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000581 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000582 // Place the component at the new position, getting the component
583 // that was at this position - it will be moved right.
584 std::swap(CurrentComponent, Components[i]);
585 // If it was placed on top of an empty component then we are done.
586 if (CurrentComponent.empty())
587 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000588 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000589 while (++i < array_lengthof(Found) && Found[i])
590 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000591 }
592 // The last component was pushed off the end - append it.
593 if (!CurrentComponent.empty())
594 Components.push_back(CurrentComponent);
595
596 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000597 while (++Idx < array_lengthof(Found) && Found[Idx])
598 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000599 } while (Idx < Pos); // Add more until the final position is reached.
600 }
601 assert(Pos < Components.size() && Components[Pos] == Comp &&
602 "Component moved wrong!");
603 Found[Pos] = true;
604 break;
605 }
606 }
607
608 // Special case logic goes here. At this point Arch, Vendor and OS have the
609 // correct values for the computed components.
610
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000611 if (OS == Triple::Win32) {
612 Components.resize(4);
613 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000614 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000615 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000616 Components[3] = "msvc";
617 else
618 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000619 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000620 } else if (IsMinGW32) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000621 Components.resize(4);
622 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000623 Components[3] = "gnu";
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000624 } else if (IsCygwin) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000625 Components.resize(4);
626 Components[2] = "windows";
627 Components[3] = "cygnus";
628 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000629 if (IsMinGW32 || IsCygwin ||
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000630 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
631 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
632 Components.resize(5);
633 Components[4] = getObjectFormatTypeName(ObjectFormat);
634 }
635 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000636
Duncan Sands501dff72010-08-12 11:31:39 +0000637 // Stick the corrected components back together to form the normalized string.
638 std::string Normalized;
639 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
640 if (i) Normalized += '-';
641 Normalized += Components[i];
642 }
643 return Normalized;
644}
645
Daniel Dunbar19e70762009-07-26 03:31:47 +0000646StringRef Triple::getArchName() const {
647 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000648}
649
Daniel Dunbar19e70762009-07-26 03:31:47 +0000650StringRef Triple::getVendorName() const {
651 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
652 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000653}
654
Daniel Dunbar19e70762009-07-26 03:31:47 +0000655StringRef Triple::getOSName() const {
656 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
657 Tmp = Tmp.split('-').second; // Strip second component
658 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000659}
660
Daniel Dunbar19e70762009-07-26 03:31:47 +0000661StringRef Triple::getEnvironmentName() const {
662 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
663 Tmp = Tmp.split('-').second; // Strip second component
664 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000665}
666
Daniel Dunbar19e70762009-07-26 03:31:47 +0000667StringRef Triple::getOSAndEnvironmentName() const {
668 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
669 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000670}
671
Chris Lattner553c9f32009-08-12 06:19:40 +0000672static unsigned EatNumber(StringRef &Str) {
673 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000674 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000675
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000676 do {
677 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000678 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000679
Chris Lattner553c9f32009-08-12 06:19:40 +0000680 // Eat the digit.
681 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000682 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000683
Chris Lattner553c9f32009-08-12 06:19:40 +0000684 return Result;
685}
686
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000687void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
688 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000689 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000690
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000691 // Assume that the OS portion of the triple starts with the canonical name.
692 StringRef OSTypeName = getOSTypeName(getOS());
693 if (OSName.startswith(OSTypeName))
694 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000695
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000696 // Any unset version defaults to 0.
697 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000698
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000699 // Parse up to three components.
700 unsigned *Components[3] = { &Major, &Minor, &Micro };
701 for (unsigned i = 0; i != 3; ++i) {
702 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
703 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000704
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000705 // Consume the leading number.
706 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000707
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000708 // Consume the separator, if present.
709 if (OSName.startswith("."))
710 OSName = OSName.substr(1);
711 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000712}
713
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000714bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
715 unsigned &Micro) const {
716 getOSVersion(Major, Minor, Micro);
717
718 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000719 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000720 case Darwin:
721 // Default to darwin8, i.e., MacOSX 10.4.
722 if (Major == 0)
723 Major = 8;
724 // Darwin version numbers are skewed from OS X versions.
725 if (Major < 4)
726 return false;
727 Micro = 0;
728 Minor = Major - 4;
729 Major = 10;
730 break;
731 case MacOSX:
732 // Default to 10.4.
733 if (Major == 0) {
734 Major = 10;
735 Minor = 4;
736 }
737 if (Major != 10)
738 return false;
739 break;
740 case IOS:
741 // Ignore the version from the triple. This is only handled because the
742 // the clang driver combines OS X and IOS support into a common Darwin
743 // toolchain that wants to know the OS X version number even when targeting
744 // IOS.
745 Major = 10;
746 Minor = 4;
747 Micro = 0;
748 break;
749 }
750 return true;
751}
752
Chad Rosierd84eaac2012-05-09 17:23:48 +0000753void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
754 unsigned &Micro) const {
755 switch (getOS()) {
756 default: llvm_unreachable("unexpected OS for Darwin triple");
757 case Darwin:
758 case MacOSX:
759 // Ignore the version from the triple. This is only handled because the
760 // the clang driver combines OS X and IOS support into a common Darwin
761 // toolchain that wants to know the iOS version number even when targeting
762 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000763 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000764 Minor = 0;
765 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000766 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000767 case IOS:
768 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000769 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000770 if (Major == 0)
Tim Northovere19bed72014-07-23 12:32:47 +0000771 Major = (getArch() == aarch64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000772 break;
773 }
774}
775
Daniel Dunbar19e70762009-07-26 03:31:47 +0000776void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000777 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000778}
779
780void Triple::setArch(ArchType Kind) {
781 setArchName(getArchTypeName(Kind));
782}
783
784void Triple::setVendor(VendorType Kind) {
785 setVendorName(getVendorTypeName(Kind));
786}
787
788void Triple::setOS(OSType Kind) {
789 setOSName(getOSTypeName(Kind));
790}
791
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000792void Triple::setEnvironment(EnvironmentType Kind) {
793 setEnvironmentName(getEnvironmentTypeName(Kind));
794}
795
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000796void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000797 if (Environment == UnknownEnvironment)
798 return setEnvironmentName(getObjectFormatTypeName(Kind));
799
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000800 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
801 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000802}
803
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000804void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000805 // Work around a miscompilation bug for Twines in gcc 4.0.3.
806 SmallString<64> Triple;
807 Triple += Str;
808 Triple += "-";
809 Triple += getVendorName();
810 Triple += "-";
811 Triple += getOSAndEnvironmentName();
812 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000813}
814
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000815void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000816 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
817}
818
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000819void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000820 if (hasEnvironment())
821 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
822 "-" + getEnvironmentName());
823 else
824 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
825}
826
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000827void Triple::setEnvironmentName(StringRef Str) {
828 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000829 "-" + Str);
830}
831
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000832void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000833 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
834}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000835
836static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
837 switch (Arch) {
838 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000839 return 0;
840
841 case llvm::Triple::msp430:
842 return 16;
843
Chandler Carruthb90c1022012-01-31 04:52:32 +0000844 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000845 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000846 case llvm::Triple::hexagon:
847 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000848 case llvm::Triple::mips:
849 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000850 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000851 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000852 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000853 case llvm::Triple::sparc:
854 case llvm::Triple::tce:
855 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000856 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000857 case llvm::Triple::x86:
858 case llvm::Triple::xcore:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000859 case llvm::Triple::amdil:
860 case llvm::Triple::hsail:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000861 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000862 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000863 return 32;
864
Tim Northovere0e3aef2013-01-31 12:12:40 +0000865 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000866 case llvm::Triple::aarch64_be:
JF Bastien32972ef2014-09-12 17:54:17 +0000867 case llvm::Triple::le64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000868 case llvm::Triple::mips64:
869 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000870 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000871 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000872 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000873 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000874 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000875 case llvm::Triple::x86_64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000876 case llvm::Triple::amdil64:
877 case llvm::Triple::hsail64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000878 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000879 return 64;
880 }
881 llvm_unreachable("Invalid architecture value");
882}
883
884bool Triple::isArch64Bit() const {
885 return getArchPointerBitWidth(getArch()) == 64;
886}
887
888bool Triple::isArch32Bit() const {
889 return getArchPointerBitWidth(getArch()) == 32;
890}
891
892bool Triple::isArch16Bit() const {
893 return getArchPointerBitWidth(getArch()) == 16;
894}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000895
896Triple Triple::get32BitArchVariant() const {
897 Triple T(*this);
898 switch (getArch()) {
899 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000900 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000901 case Triple::aarch64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000902 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000903 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000904 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000905 T.setArch(UnknownArch);
906 break;
907
908 case Triple::amdil:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000909 case Triple::hsail:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000910 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +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::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000916 case Triple::mips:
917 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000918 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000919 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000920 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000921 case Triple::sparc:
922 case Triple::tce:
923 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000924 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000925 case Triple::x86:
926 case Triple::xcore:
927 // Already 32-bit.
928 break;
929
JF Bastien32972ef2014-09-12 17:54:17 +0000930 case Triple::le64: T.setArch(Triple::le32); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000931 case Triple::mips64: T.setArch(Triple::mips); break;
932 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000933 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000934 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000935 case Triple::sparcv9: T.setArch(Triple::sparc); break;
936 case Triple::x86_64: T.setArch(Triple::x86); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000937 case Triple::amdil64: T.setArch(Triple::amdil); break;
938 case Triple::hsail64: T.setArch(Triple::hsail); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000939 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000940 }
941 return T;
942}
943
944Triple Triple::get64BitArchVariant() const {
945 Triple T(*this);
946 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000947 case Triple::UnknownArch:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000948 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000949 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000950 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000951 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000952 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000953 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000954 case Triple::tce:
955 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000956 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000957 case Triple::xcore:
958 T.setArch(UnknownArch);
959 break;
960
Tim Northovere0e3aef2013-01-31 12:12:40 +0000961 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000962 case Triple::aarch64_be:
JF Bastien32972ef2014-09-12 17:54:17 +0000963 case Triple::le64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000964 case Triple::amdil64:
965 case Triple::hsail64:
966 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000967 case Triple::mips64:
968 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000969 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000970 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000971 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000972 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000973 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000974 case Triple::x86_64:
975 // Already 64-bit.
976 break;
977
JF Bastien32972ef2014-09-12 17:54:17 +0000978 case Triple::le32: T.setArch(Triple::le64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000979 case Triple::mips: T.setArch(Triple::mips64); break;
980 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000981 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000982 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000983 case Triple::sparc: T.setArch(Triple::sparcv9); break;
984 case Triple::x86: T.setArch(Triple::x86_64); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000985 case Triple::amdil: T.setArch(Triple::amdil64); break;
986 case Triple::hsail: T.setArch(Triple::hsail64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000987 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000988 }
989 return T;
990}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000991
992// FIXME: tblgen this.
993const char *Triple::getARMCPUForArch(StringRef MArch) const {
994 if (MArch.empty())
995 MArch = getArchName();
996
997 switch (getOS()) {
Ed Masteea0257a2014-09-09 17:47:24 +0000998 case llvm::Triple::FreeBSD:
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000999 case llvm::Triple::NetBSD:
1000 if (MArch == "armv6")
1001 return "arm1176jzf-s";
1002 break;
1003 case llvm::Triple::Win32:
1004 // FIXME: this is invalid for WindowsCE
1005 return "cortex-a9";
1006 default:
1007 break;
1008 }
1009
1010 const char *result = nullptr;
1011 size_t offset = StringRef::npos;
1012 if (MArch.startswith("arm"))
1013 offset = 3;
1014 if (MArch.startswith("thumb"))
1015 offset = 5;
1016 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
1017 offset += 2;
1018 if (offset != StringRef::npos)
1019 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
1020 .Cases("v2", "v2a", "arm2")
1021 .Case("v3", "arm6")
1022 .Case("v3m", "arm7m")
1023 .Case("v4", "strongarm")
1024 .Case("v4t", "arm7tdmi")
1025 .Cases("v5", "v5t", "arm10tdmi")
1026 .Cases("v5e", "v5te", "arm1022e")
1027 .Case("v5tej", "arm926ej-s")
1028 .Cases("v6", "v6k", "arm1136jf-s")
1029 .Case("v6j", "arm1136j-s")
1030 .Cases("v6z", "v6zk", "arm1176jzf-s")
1031 .Case("v6t2", "arm1156t2-s")
1032 .Cases("v6m", "v6-m", "cortex-m0")
1033 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
1034 .Cases("v7s", "v7-s", "swift")
1035 .Cases("v7r", "v7-r", "cortex-r4")
1036 .Cases("v7m", "v7-m", "cortex-m3")
1037 .Cases("v7em", "v7e-m", "cortex-m4")
1038 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1039 .Default(nullptr);
1040 else
1041 result = llvm::StringSwitch<const char *>(MArch)
1042 .Case("ep9312", "ep9312")
1043 .Case("iwmmxt", "iwmmxt")
1044 .Case("xscale", "xscale")
1045 .Default(nullptr);
1046
1047 if (result)
1048 return result;
1049
1050 // If all else failed, return the most base CPU with thumb interworking
1051 // supported by LLVM.
1052 // FIXME: Should warn once that we're falling back.
1053 switch (getOS()) {
1054 case llvm::Triple::NetBSD:
1055 switch (getEnvironment()) {
1056 case llvm::Triple::GNUEABIHF:
1057 case llvm::Triple::GNUEABI:
1058 case llvm::Triple::EABIHF:
1059 case llvm::Triple::EABI:
1060 return "arm926ej-s";
1061 default:
1062 return "strongarm";
1063 }
1064 default:
1065 switch (getEnvironment()) {
1066 case llvm::Triple::EABIHF:
1067 case llvm::Triple::GNUEABIHF:
1068 return "arm1176jzf-s";
1069 default:
1070 return "arm7tdmi";
1071 }
1072 }
1073}