blob: f923a9aa87aeeb0932059cd19dfca6e4d1061a45 [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";
Tom Stellard07f11602015-01-06 18:00:00 +000036 case amdgcn: return "amdgcn";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000037 case sparc: return "sparc";
38 case sparcv9: return "sparcv9";
39 case systemz: return "s390x";
40 case tce: return "tce";
41 case thumb: return "thumb";
Christian Pirker2a111602014-03-28 14:35:30 +000042 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000043 case x86: return "i386";
44 case x86_64: return "x86_64";
45 case xcore: return "xcore";
46 case nvptx: return "nvptx";
47 case nvptx64: return "nvptx64";
48 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +000049 case le64: return "le64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000050 case amdil: return "amdil";
Matt Arsenault3f9b0212014-09-19 19:52:11 +000051 case amdil64: return "amdil64";
52 case hsail: return "hsail";
53 case hsail64: return "hsail64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000054 case spir: return "spir";
55 case spir64: return "spir64";
Eric Christopher54fe1b22014-07-10 17:26:54 +000056 case kalimba: return "kalimba";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000057 }
58
David Blaikie46a9f012012-01-20 21:51:11 +000059 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000060}
61
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000062const char *Triple::getArchTypePrefix(ArchType Kind) {
63 switch (Kind) {
64 default:
Craig Topperc10719f2014-04-07 04:17:22 +000065 return nullptr;
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000066
Christian Pirker6c2f4d42014-02-24 11:34:50 +000067 case aarch64:
68 case aarch64_be: return "aarch64";
Tim Northovere0e3aef2013-01-31 12:12:40 +000069
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000070 case arm:
Christian Pirker2a111602014-03-28 14:35:30 +000071 case armeb:
72 case thumb:
73 case thumbeb: return "arm";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000074
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000075 case ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +000076 case ppc64le:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000077 case ppc: return "ppc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000078
Benjamin Kramerae3c3002012-06-28 19:09:53 +000079 case mips:
80 case mipsel:
81 case mips64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000082 case mips64el: return "mips";
Benjamin Kramerae3c3002012-06-28 19:09:53 +000083
Christian Pirker6c2f4d42014-02-24 11:34:50 +000084 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085
Tom Stellard07f11602015-01-06 18:00:00 +000086 case amdgcn:
87 case r600: return "amdgpu";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000088
Chris Lattner8228b112010-02-04 06:34:01 +000089 case sparcv9:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000090 case sparc: return "sparc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000091
Christian Pirker6c2f4d42014-02-24 11:34:50 +000092 case systemz: return "systemz";
Richard Sandiforda238c5e2013-05-03 11:05:17 +000093
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000094 case x86:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000095 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000096
Christian Pirker6c2f4d42014-02-24 11:34:50 +000097 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000098
Christian Pirker6c2f4d42014-02-24 11:34:50 +000099 case nvptx: return "nvptx";
100 case nvptx64: return "nvptx";
Tim Northover00ed9962014-03-29 10:18:08 +0000101
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000102 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +0000103 case le64: return "le64";
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000104
105 case amdil:
106 case amdil64: return "amdil";
107
108 case hsail:
109 case hsail64: return "hsail";
110
111 case spir:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000112 case spir64: return "spir";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000113 case kalimba: return "kalimba";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000114 }
115}
116
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000117const char *Triple::getVendorTypeName(VendorType Kind) {
118 switch (Kind) {
119 case UnknownVendor: return "unknown";
120
121 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000122 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000123 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000124 case BGP: return "bgp";
125 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000126 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000127 case IBM: return "ibm";
Daniel Sandersc5626f42014-07-09 16:03:10 +0000128 case ImaginationTechnologies: return "img";
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000129 case MipsTechnologies: return "mti";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000130 case NVIDIA: return "nvidia";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000131 case CSR: return "csr";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000132 }
133
David Blaikie46a9f012012-01-20 21:51:11 +0000134 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000135}
136
137const char *Triple::getOSTypeName(OSType Kind) {
138 switch (Kind) {
139 case UnknownOS: return "unknown";
140
141 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000142 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000143 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000144 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000145 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000146 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000147 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000148 case MacOSX: return "macosx";
Chris Lattner01218d52009-07-13 20:22:23 +0000149 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000150 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000151 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000152 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000153 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000154 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000155 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000156 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000157 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000158 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000159 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000160 case CUDA: return "cuda";
161 case NVCL: return "nvcl";
Tom Stellard4082a6c2014-12-02 16:45:47 +0000162 case AMDHSA: return "amdhsa";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000163 }
164
David Blaikie46a9f012012-01-20 21:51:11 +0000165 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000166}
167
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000168const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
169 switch (Kind) {
170 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000171 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000172 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000173 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000174 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000175 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000176 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000177 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000178 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000179 case MSVC: return "msvc";
180 case Itanium: return "itanium";
181 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000182 }
183
David Blaikie46a9f012012-01-20 21:51:11 +0000184 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000185}
186
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000187Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000188 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000189 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000190 .Case("aarch64_be", aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000191 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
Chandler Carruthff6f3562012-02-12 09:27:38 +0000192 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000193 .Case("armeb", armeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000194 .Case("mips", mips)
195 .Case("mipsel", mipsel)
196 .Case("mips64", mips64)
197 .Case("mips64el", mips64el)
198 .Case("msp430", msp430)
199 .Case("ppc64", ppc64)
200 .Case("ppc32", ppc)
201 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000202 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000203 .Case("r600", r600)
Tom Stellard07f11602015-01-06 18:00:00 +0000204 .Case("amdgcn", amdgcn)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000205 .Case("hexagon", hexagon)
206 .Case("sparc", sparc)
207 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000208 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000209 .Case("tce", tce)
210 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000211 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000212 .Case("x86", x86)
213 .Case("x86-64", x86_64)
214 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000215 .Case("nvptx", nvptx)
216 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000217 .Case("le32", le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000218 .Case("le64", le64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000219 .Case("amdil", amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000220 .Case("amdil64", amdil64)
221 .Case("hsail", hsail)
222 .Case("hsail64", hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000223 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000224 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000225 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000226 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000227}
228
Renato Golin609bf922014-11-17 14:08:57 +0000229static Triple::ArchType parseARMArch(StringRef ArchName) {
230 size_t offset = StringRef::npos;
231 Triple::ArchType arch = Triple::UnknownArch;
232 bool isThumb = ArchName.startswith("thumb");
233
234 if (ArchName.equals("arm"))
235 return Triple::arm;
236 if (ArchName.equals("armeb"))
237 return Triple::armeb;
238 if (ArchName.equals("thumb"))
239 return Triple::thumb;
240 if (ArchName.equals("thumbeb"))
241 return Triple::thumbeb;
242 if (ArchName.equals("arm64") || ArchName.equals("aarch64"))
243 return Triple::aarch64;
244 if (ArchName.equals("aarch64_be"))
245 return Triple::aarch64_be;
246
247 if (ArchName.startswith("armv")) {
248 offset = 3;
249 arch = Triple::arm;
250 } else if (ArchName.startswith("armebv")) {
251 offset = 5;
252 arch = Triple::armeb;
253 } else if (ArchName.startswith("thumbv")) {
254 offset = 5;
255 arch = Triple::thumb;
256 } else if (ArchName.startswith("thumbebv")) {
257 offset = 7;
258 arch = Triple::thumbeb;
259 }
260 return StringSwitch<Triple::ArchType>(ArchName.substr(offset))
261 .Cases("v2", "v2a", isThumb ? Triple::UnknownArch : arch)
262 .Cases("v3", "v3m", isThumb ? Triple::UnknownArch : arch)
263 .Cases("v4", "v4t", arch)
264 .Cases("v5", "v5e", "v5t", "v5te", "v5tej", arch)
265 .Cases("v6", "v6j", "v6k", "v6m", arch)
266 .Cases("v6t2", "v6z", "v6zk", arch)
267 .Cases("v7", "v7a", "v7em", "v7l", arch)
268 .Cases("v7m", "v7r", "v7s", arch)
269 .Cases("v8", "v8a", arch)
270 .Default(Triple::UnknownArch);
271}
272
Chandler Carruthaec97082012-02-21 08:53:32 +0000273static Triple::ArchType parseArch(StringRef ArchName) {
274 return StringSwitch<Triple::ArchType>(ArchName)
275 .Cases("i386", "i486", "i586", "i686", Triple::x86)
276 // FIXME: Do we need to support these?
277 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000278 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000279 .Case("powerpc", Triple::ppc)
280 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000281 .Case("powerpc64le", Triple::ppc64le)
Renato Golin609bf922014-11-17 14:08:57 +0000282 .Case("xscale", Triple::arm)
283 .StartsWith("arm", parseARMArch(ArchName))
284 .StartsWith("thumb", parseARMArch(ArchName))
285 .StartsWith("aarch64", parseARMArch(ArchName))
Chandler Carruthaec97082012-02-21 08:53:32 +0000286 .Case("msp430", Triple::msp430)
287 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000288 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000289 .Cases("mips64", "mips64eb", Triple::mips64)
290 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000291 .Case("r600", Triple::r600)
Tom Stellard07f11602015-01-06 18:00:00 +0000292 .Case("amdgcn", Triple::amdgcn)
Chandler Carruthaec97082012-02-21 08:53:32 +0000293 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000294 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000295 .Case("sparc", Triple::sparc)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000296 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000297 .Case("tce", Triple::tce)
298 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000299 .Case("nvptx", Triple::nvptx)
300 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000301 .Case("le32", Triple::le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000302 .Case("le64", Triple::le64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000303 .Case("amdil", Triple::amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000304 .Case("amdil64", Triple::amdil64)
305 .Case("hsail", Triple::hsail)
306 .Case("hsail64", Triple::hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000307 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000308 .Case("spir64", Triple::spir64)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000309 .StartsWith("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000310 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000311}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000312
Chandler Carruthaec97082012-02-21 08:53:32 +0000313static Triple::VendorType parseVendor(StringRef VendorName) {
314 return StringSwitch<Triple::VendorType>(VendorName)
315 .Case("apple", Triple::Apple)
316 .Case("pc", Triple::PC)
317 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000318 .Case("bgp", Triple::BGP)
319 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000320 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000321 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000322 .Case("img", Triple::ImaginationTechnologies)
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000323 .Case("mti", Triple::MipsTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000324 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000325 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000326 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000327}
328
Chandler Carruthaec97082012-02-21 08:53:32 +0000329static Triple::OSType parseOS(StringRef OSName) {
330 return StringSwitch<Triple::OSType>(OSName)
Chandler Carruthaec97082012-02-21 08:53:32 +0000331 .StartsWith("darwin", Triple::Darwin)
332 .StartsWith("dragonfly", Triple::DragonFly)
333 .StartsWith("freebsd", Triple::FreeBSD)
334 .StartsWith("ios", Triple::IOS)
335 .StartsWith("kfreebsd", Triple::KFreeBSD)
336 .StartsWith("linux", Triple::Linux)
337 .StartsWith("lv2", Triple::Lv2)
338 .StartsWith("macosx", Triple::MacOSX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000339 .StartsWith("netbsd", Triple::NetBSD)
340 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000341 .StartsWith("solaris", Triple::Solaris)
342 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000343 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000344 .StartsWith("haiku", Triple::Haiku)
345 .StartsWith("minix", Triple::Minix)
346 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000347 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000348 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000349 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000350 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000351 .StartsWith("cuda", Triple::CUDA)
352 .StartsWith("nvcl", Triple::NVCL)
Tom Stellard4082a6c2014-12-02 16:45:47 +0000353 .StartsWith("amdhsa", Triple::AMDHSA)
Chandler Carruthaec97082012-02-21 08:53:32 +0000354 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000355}
356
Chandler Carruthaec97082012-02-21 08:53:32 +0000357static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
358 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000359 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000360 .StartsWith("eabi", Triple::EABI)
361 .StartsWith("gnueabihf", Triple::GNUEABIHF)
362 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000363 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000364 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000365 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000366 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000367 .StartsWith("msvc", Triple::MSVC)
368 .StartsWith("itanium", Triple::Itanium)
369 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000370 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000371}
372
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000373static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
374 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
375 .EndsWith("coff", Triple::COFF)
376 .EndsWith("elf", Triple::ELF)
377 .EndsWith("macho", Triple::MachO)
378 .Default(Triple::UnknownObjectFormat);
379}
380
Renato Golinc17a07b2014-07-18 12:00:48 +0000381static Triple::SubArchType parseSubArch(StringRef SubArchName) {
382 return StringSwitch<Triple::SubArchType>(SubArchName)
383 .EndsWith("v8", Triple::ARMSubArch_v8)
384 .EndsWith("v8a", Triple::ARMSubArch_v8)
385 .EndsWith("v7", Triple::ARMSubArch_v7)
386 .EndsWith("v7a", Triple::ARMSubArch_v7)
387 .EndsWith("v7em", Triple::ARMSubArch_v7em)
388 .EndsWith("v7l", Triple::ARMSubArch_v7)
389 .EndsWith("v7m", Triple::ARMSubArch_v7m)
390 .EndsWith("v7r", Triple::ARMSubArch_v7)
391 .EndsWith("v7s", Triple::ARMSubArch_v7s)
392 .EndsWith("v6", Triple::ARMSubArch_v6)
393 .EndsWith("v6m", Triple::ARMSubArch_v6m)
394 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
395 .EndsWith("v5", Triple::ARMSubArch_v5)
396 .EndsWith("v5e", Triple::ARMSubArch_v5)
397 .EndsWith("v5t", Triple::ARMSubArch_v5)
398 .EndsWith("v5te", Triple::ARMSubArch_v5te)
399 .EndsWith("v4t", Triple::ARMSubArch_v4t)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000400 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
401 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
402 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
Renato Golinc17a07b2014-07-18 12:00:48 +0000403 .Default(Triple::NoSubArch);
404}
405
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000406static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
407 switch (Kind) {
408 case Triple::UnknownObjectFormat: return "";
409 case Triple::COFF: return "coff";
410 case Triple::ELF: return "elf";
411 case Triple::MachO: return "macho";
412 }
413 llvm_unreachable("unknown object format type");
414}
415
416static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
417 if (T.isOSDarwin())
418 return Triple::MachO;
419 else if (T.isOSWindows())
420 return Triple::COFF;
421 return Triple::ELF;
422}
423
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000424/// \brief Construct a triple from the string representation provided.
425///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000426/// This stores the string representation and parses the various pieces into
427/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000428Triple::Triple(const Twine &Str)
429 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000430 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000431 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000432 Vendor(parseVendor(getVendorName())),
433 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000434 Environment(parseEnvironment(getEnvironmentName())),
435 ObjectFormat(parseFormat(getEnvironmentName())) {
436 if (ObjectFormat == Triple::UnknownObjectFormat)
437 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000438}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000439
440/// \brief Construct a triple from string representations of the architecture,
441/// vendor, and OS.
442///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000443/// This joins each argument into a canonical string representation and parses
444/// them into enum members. It leaves the environment unknown and omits it from
445/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000446Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
447 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000448 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000449 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000450 Vendor(parseVendor(VendorStr.str())),
451 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000452 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
453 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000454}
455
456/// \brief Construct a triple from string representations of the architecture,
457/// vendor, OS, and environment.
458///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000459/// This joins each argument into a canonical string representation and parses
460/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000461Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
462 const Twine &EnvironmentStr)
463 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
464 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000465 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000466 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000467 Vendor(parseVendor(VendorStr.str())),
468 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000469 Environment(parseEnvironment(EnvironmentStr.str())),
470 ObjectFormat(parseFormat(EnvironmentStr.str())) {
471 if (ObjectFormat == Triple::UnknownObjectFormat)
472 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000473}
474
Duncan Sands501dff72010-08-12 11:31:39 +0000475std::string Triple::normalize(StringRef Str) {
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000476 bool IsMinGW32 = false;
477 bool IsCygwin = false;
478
Duncan Sands501dff72010-08-12 11:31:39 +0000479 // Parse into components.
480 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000481 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000482
483 // If the first component corresponds to a known architecture, preferentially
484 // use it for the architecture. If the second component corresponds to a
485 // known vendor, preferentially use it for the vendor, etc. This avoids silly
486 // component movement when a component parses as (eg) both a valid arch and a
487 // valid os.
488 ArchType Arch = UnknownArch;
489 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000490 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000491 VendorType Vendor = UnknownVendor;
492 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000493 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000494 OSType OS = UnknownOS;
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000495 if (Components.size() > 2) {
Chandler Carruthaec97082012-02-21 08:53:32 +0000496 OS = parseOS(Components[2]);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000497 IsCygwin = Components[2].startswith("cygwin");
498 IsMinGW32 = Components[2].startswith("mingw");
499 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000500 EnvironmentType Environment = UnknownEnvironment;
501 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000502 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000503 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000504 if (Components.size() > 4)
505 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000506
507 // Note which components are already in their final position. These will not
508 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000509 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000510 Found[0] = Arch != UnknownArch;
511 Found[1] = Vendor != UnknownVendor;
512 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000513 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000514
515 // If they are not there already, permute the components into their canonical
516 // positions by seeing if they parse as a valid architecture, and if so moving
517 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000518 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000519 if (Found[Pos])
520 continue; // Already in the canonical position.
521
522 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
523 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000524 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000525 continue;
526
527 // Does this component parse as valid for the target position?
528 bool Valid = false;
529 StringRef Comp = Components[Idx];
530 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000531 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000532 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000533 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000534 Valid = Arch != UnknownArch;
535 break;
536 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000537 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000538 Valid = Vendor != UnknownVendor;
539 break;
540 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000541 OS = parseOS(Comp);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000542 IsCygwin = Comp.startswith("cygwin");
543 IsMinGW32 = Comp.startswith("mingw");
544 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
Duncan Sands501dff72010-08-12 11:31:39 +0000545 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000546 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000547 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000548 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000549 if (!Valid) {
550 ObjectFormat = parseFormat(Comp);
551 Valid = ObjectFormat != UnknownObjectFormat;
552 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000553 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000554 }
555 if (!Valid)
556 continue; // Nope, try the next component.
557
558 // Move the component to the target position, pushing any non-fixed
559 // components that are in the way to the right. This tends to give
560 // good results in the common cases of a forgotten vendor component
561 // or a wrongly positioned environment.
562 if (Pos < Idx) {
563 // Insert left, pushing the existing components to the right. For
564 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
565 StringRef CurrentComponent(""); // The empty component.
566 // Replace the component we are moving with an empty component.
567 std::swap(CurrentComponent, Components[Idx]);
568 // Insert the component being moved at Pos, displacing any existing
569 // components to the right.
570 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
571 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000572 while (i < array_lengthof(Found) && Found[i])
573 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000574 // Place the component at the new position, getting the component
575 // that was at this position - it will be moved right.
576 std::swap(CurrentComponent, Components[i]);
577 }
578 } else if (Pos > Idx) {
579 // Push right by inserting empty components until the component at Idx
580 // reaches the target position Pos. For example, pc-a -> -pc-a when
581 // moving pc to the second position.
582 do {
583 // Insert one empty component at Idx.
584 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000585 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000586 // Place the component at the new position, getting the component
587 // that was at this position - it will be moved right.
588 std::swap(CurrentComponent, Components[i]);
589 // If it was placed on top of an empty component then we are done.
590 if (CurrentComponent.empty())
591 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000592 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000593 while (++i < array_lengthof(Found) && Found[i])
594 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000595 }
596 // The last component was pushed off the end - append it.
597 if (!CurrentComponent.empty())
598 Components.push_back(CurrentComponent);
599
600 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000601 while (++Idx < array_lengthof(Found) && Found[Idx])
602 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000603 } while (Idx < Pos); // Add more until the final position is reached.
604 }
605 assert(Pos < Components.size() && Components[Pos] == Comp &&
606 "Component moved wrong!");
607 Found[Pos] = true;
608 break;
609 }
610 }
611
612 // Special case logic goes here. At this point Arch, Vendor and OS have the
613 // correct values for the computed components.
614
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000615 if (OS == Triple::Win32) {
616 Components.resize(4);
617 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000618 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000619 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000620 Components[3] = "msvc";
621 else
622 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000623 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000624 } else if (IsMinGW32) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000625 Components.resize(4);
626 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000627 Components[3] = "gnu";
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000628 } else if (IsCygwin) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000629 Components.resize(4);
630 Components[2] = "windows";
631 Components[3] = "cygnus";
632 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000633 if (IsMinGW32 || IsCygwin ||
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000634 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
635 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
636 Components.resize(5);
637 Components[4] = getObjectFormatTypeName(ObjectFormat);
638 }
639 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000640
Duncan Sands501dff72010-08-12 11:31:39 +0000641 // Stick the corrected components back together to form the normalized string.
642 std::string Normalized;
643 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
644 if (i) Normalized += '-';
645 Normalized += Components[i];
646 }
647 return Normalized;
648}
649
Daniel Dunbar19e70762009-07-26 03:31:47 +0000650StringRef Triple::getArchName() const {
651 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000652}
653
Daniel Dunbar19e70762009-07-26 03:31:47 +0000654StringRef Triple::getVendorName() const {
655 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
656 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000657}
658
Daniel Dunbar19e70762009-07-26 03:31:47 +0000659StringRef Triple::getOSName() const {
660 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
661 Tmp = Tmp.split('-').second; // Strip second component
662 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000663}
664
Daniel Dunbar19e70762009-07-26 03:31:47 +0000665StringRef Triple::getEnvironmentName() const {
666 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
667 Tmp = Tmp.split('-').second; // Strip second component
668 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000669}
670
Daniel Dunbar19e70762009-07-26 03:31:47 +0000671StringRef Triple::getOSAndEnvironmentName() const {
672 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
673 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000674}
675
Chris Lattner553c9f32009-08-12 06:19:40 +0000676static unsigned EatNumber(StringRef &Str) {
677 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000678 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000679
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000680 do {
681 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000682 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000683
Chris Lattner553c9f32009-08-12 06:19:40 +0000684 // Eat the digit.
685 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000686 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000687
Chris Lattner553c9f32009-08-12 06:19:40 +0000688 return Result;
689}
690
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000691void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
692 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000693 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000694
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000695 // Assume that the OS portion of the triple starts with the canonical name.
696 StringRef OSTypeName = getOSTypeName(getOS());
697 if (OSName.startswith(OSTypeName))
698 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000699
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000700 // Any unset version defaults to 0.
701 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000702
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000703 // Parse up to three components.
704 unsigned *Components[3] = { &Major, &Minor, &Micro };
705 for (unsigned i = 0; i != 3; ++i) {
706 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
707 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000708
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000709 // Consume the leading number.
710 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000711
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000712 // Consume the separator, if present.
713 if (OSName.startswith("."))
714 OSName = OSName.substr(1);
715 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000716}
717
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000718bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
719 unsigned &Micro) const {
720 getOSVersion(Major, Minor, Micro);
721
722 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000723 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000724 case Darwin:
725 // Default to darwin8, i.e., MacOSX 10.4.
726 if (Major == 0)
727 Major = 8;
728 // Darwin version numbers are skewed from OS X versions.
729 if (Major < 4)
730 return false;
731 Micro = 0;
732 Minor = Major - 4;
733 Major = 10;
734 break;
735 case MacOSX:
736 // Default to 10.4.
737 if (Major == 0) {
738 Major = 10;
739 Minor = 4;
740 }
741 if (Major != 10)
742 return false;
743 break;
744 case IOS:
745 // Ignore the version from the triple. This is only handled because the
746 // the clang driver combines OS X and IOS support into a common Darwin
747 // toolchain that wants to know the OS X version number even when targeting
748 // IOS.
749 Major = 10;
750 Minor = 4;
751 Micro = 0;
752 break;
753 }
754 return true;
755}
756
Chad Rosierd84eaac2012-05-09 17:23:48 +0000757void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
758 unsigned &Micro) const {
759 switch (getOS()) {
760 default: llvm_unreachable("unexpected OS for Darwin triple");
761 case Darwin:
762 case MacOSX:
763 // Ignore the version from the triple. This is only handled because the
764 // the clang driver combines OS X and IOS support into a common Darwin
765 // toolchain that wants to know the iOS version number even when targeting
766 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000767 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000768 Minor = 0;
769 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000770 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000771 case IOS:
772 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000773 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000774 if (Major == 0)
Tim Northovere19bed72014-07-23 12:32:47 +0000775 Major = (getArch() == aarch64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000776 break;
777 }
778}
779
Daniel Dunbar19e70762009-07-26 03:31:47 +0000780void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000781 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000782}
783
784void Triple::setArch(ArchType Kind) {
785 setArchName(getArchTypeName(Kind));
786}
787
788void Triple::setVendor(VendorType Kind) {
789 setVendorName(getVendorTypeName(Kind));
790}
791
792void Triple::setOS(OSType Kind) {
793 setOSName(getOSTypeName(Kind));
794}
795
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000796void Triple::setEnvironment(EnvironmentType Kind) {
797 setEnvironmentName(getEnvironmentTypeName(Kind));
798}
799
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000800void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000801 if (Environment == UnknownEnvironment)
802 return setEnvironmentName(getObjectFormatTypeName(Kind));
803
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000804 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
805 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000806}
807
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000808void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000809 // Work around a miscompilation bug for Twines in gcc 4.0.3.
810 SmallString<64> Triple;
811 Triple += Str;
812 Triple += "-";
813 Triple += getVendorName();
814 Triple += "-";
815 Triple += getOSAndEnvironmentName();
816 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000817}
818
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000819void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000820 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
821}
822
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000823void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000824 if (hasEnvironment())
825 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
826 "-" + getEnvironmentName());
827 else
828 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
829}
830
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000831void Triple::setEnvironmentName(StringRef Str) {
832 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000833 "-" + Str);
834}
835
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000836void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000837 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
838}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000839
840static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
841 switch (Arch) {
842 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000843 return 0;
844
845 case llvm::Triple::msp430:
846 return 16;
847
Chandler Carruthb90c1022012-01-31 04:52:32 +0000848 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000849 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000850 case llvm::Triple::hexagon:
851 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000852 case llvm::Triple::mips:
853 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000854 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000855 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000856 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000857 case llvm::Triple::sparc:
858 case llvm::Triple::tce:
859 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000860 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000861 case llvm::Triple::x86:
862 case llvm::Triple::xcore:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000863 case llvm::Triple::amdil:
864 case llvm::Triple::hsail:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000865 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000866 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000867 return 32;
868
Tim Northovere0e3aef2013-01-31 12:12:40 +0000869 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000870 case llvm::Triple::aarch64_be:
Tom Stellard07f11602015-01-06 18:00:00 +0000871 case llvm::Triple::amdgcn:
JF Bastien32972ef2014-09-12 17:54:17 +0000872 case llvm::Triple::le64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000873 case llvm::Triple::mips64:
874 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000875 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000876 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000877 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000878 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000879 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000880 case llvm::Triple::x86_64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000881 case llvm::Triple::amdil64:
882 case llvm::Triple::hsail64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000883 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000884 return 64;
885 }
886 llvm_unreachable("Invalid architecture value");
887}
888
889bool Triple::isArch64Bit() const {
890 return getArchPointerBitWidth(getArch()) == 64;
891}
892
893bool Triple::isArch32Bit() const {
894 return getArchPointerBitWidth(getArch()) == 32;
895}
896
897bool Triple::isArch16Bit() const {
898 return getArchPointerBitWidth(getArch()) == 16;
899}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000900
901Triple Triple::get32BitArchVariant() const {
902 Triple T(*this);
903 switch (getArch()) {
904 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000905 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000906 case Triple::aarch64_be:
Tom Stellard07f11602015-01-06 18:00:00 +0000907 case Triple::amdgcn:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000908 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000909 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000910 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000911 T.setArch(UnknownArch);
912 break;
913
914 case Triple::amdil:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000915 case Triple::hsail:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000916 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000917 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000918 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000919 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000920 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000921 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000922 case Triple::mips:
923 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000924 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000925 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000926 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000927 case Triple::sparc:
928 case Triple::tce:
929 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000930 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000931 case Triple::x86:
932 case Triple::xcore:
933 // Already 32-bit.
934 break;
935
JF Bastien32972ef2014-09-12 17:54:17 +0000936 case Triple::le64: T.setArch(Triple::le32); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000937 case Triple::mips64: T.setArch(Triple::mips); break;
938 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000939 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000940 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000941 case Triple::sparcv9: T.setArch(Triple::sparc); break;
942 case Triple::x86_64: T.setArch(Triple::x86); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000943 case Triple::amdil64: T.setArch(Triple::amdil); break;
944 case Triple::hsail64: T.setArch(Triple::hsail); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000945 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000946 }
947 return T;
948}
949
950Triple Triple::get64BitArchVariant() const {
951 Triple T(*this);
952 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000953 case Triple::UnknownArch:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000954 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000955 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000956 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000957 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000958 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000959 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000960 case Triple::tce:
961 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000962 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000963 case Triple::xcore:
964 T.setArch(UnknownArch);
965 break;
966
Tim Northovere0e3aef2013-01-31 12:12:40 +0000967 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000968 case Triple::aarch64_be:
JF Bastien32972ef2014-09-12 17:54:17 +0000969 case Triple::le64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000970 case Triple::amdil64:
Tom Stellard07f11602015-01-06 18:00:00 +0000971 case Triple::amdgcn:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000972 case Triple::hsail64:
973 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000974 case Triple::mips64:
975 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000976 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000977 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000978 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000979 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000980 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000981 case Triple::x86_64:
982 // Already 64-bit.
983 break;
984
JF Bastien32972ef2014-09-12 17:54:17 +0000985 case Triple::le32: T.setArch(Triple::le64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000986 case Triple::mips: T.setArch(Triple::mips64); break;
987 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000988 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000989 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000990 case Triple::sparc: T.setArch(Triple::sparcv9); break;
991 case Triple::x86: T.setArch(Triple::x86_64); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000992 case Triple::amdil: T.setArch(Triple::amdil64); break;
993 case Triple::hsail: T.setArch(Triple::hsail64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000994 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000995 }
996 return T;
997}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000998
999// FIXME: tblgen this.
1000const char *Triple::getARMCPUForArch(StringRef MArch) const {
1001 if (MArch.empty())
1002 MArch = getArchName();
1003
1004 switch (getOS()) {
Ed Masteea0257a2014-09-09 17:47:24 +00001005 case llvm::Triple::FreeBSD:
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001006 case llvm::Triple::NetBSD:
1007 if (MArch == "armv6")
1008 return "arm1176jzf-s";
1009 break;
1010 case llvm::Triple::Win32:
1011 // FIXME: this is invalid for WindowsCE
1012 return "cortex-a9";
1013 default:
1014 break;
1015 }
1016
1017 const char *result = nullptr;
1018 size_t offset = StringRef::npos;
1019 if (MArch.startswith("arm"))
1020 offset = 3;
1021 if (MArch.startswith("thumb"))
1022 offset = 5;
1023 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
1024 offset += 2;
1025 if (offset != StringRef::npos)
1026 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
1027 .Cases("v2", "v2a", "arm2")
1028 .Case("v3", "arm6")
1029 .Case("v3m", "arm7m")
1030 .Case("v4", "strongarm")
1031 .Case("v4t", "arm7tdmi")
1032 .Cases("v5", "v5t", "arm10tdmi")
1033 .Cases("v5e", "v5te", "arm1022e")
1034 .Case("v5tej", "arm926ej-s")
1035 .Cases("v6", "v6k", "arm1136jf-s")
1036 .Case("v6j", "arm1136j-s")
1037 .Cases("v6z", "v6zk", "arm1176jzf-s")
1038 .Case("v6t2", "arm1156t2-s")
1039 .Cases("v6m", "v6-m", "cortex-m0")
1040 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
1041 .Cases("v7s", "v7-s", "swift")
1042 .Cases("v7r", "v7-r", "cortex-r4")
1043 .Cases("v7m", "v7-m", "cortex-m3")
1044 .Cases("v7em", "v7e-m", "cortex-m4")
1045 .Cases("v8", "v8a", "v8-a", "cortex-a53")
1046 .Default(nullptr);
1047 else
1048 result = llvm::StringSwitch<const char *>(MArch)
1049 .Case("ep9312", "ep9312")
1050 .Case("iwmmxt", "iwmmxt")
1051 .Case("xscale", "xscale")
1052 .Default(nullptr);
1053
1054 if (result)
1055 return result;
1056
1057 // If all else failed, return the most base CPU with thumb interworking
1058 // supported by LLVM.
1059 // FIXME: Should warn once that we're falling back.
1060 switch (getOS()) {
1061 case llvm::Triple::NetBSD:
1062 switch (getEnvironment()) {
1063 case llvm::Triple::GNUEABIHF:
1064 case llvm::Triple::GNUEABI:
1065 case llvm::Triple::EABIHF:
1066 case llvm::Triple::EABI:
1067 return "arm926ej-s";
1068 default:
1069 return "strongarm";
1070 }
1071 default:
1072 switch (getEnvironment()) {
1073 case llvm::Triple::EABIHF:
1074 case llvm::Triple::GNUEABIHF:
1075 return "arm1176jzf-s";
1076 default:
1077 return "arm7tdmi";
1078 }
1079 }
1080}