blob: a88a82dee89946138ab35dbe8af1b07a401dfd6b [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";
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000026 case bpf: return "bpf";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000027 case hexagon: return "hexagon";
28 case mips: return "mips";
29 case mipsel: return "mipsel";
30 case mips64: return "mips64";
31 case mips64el: return "mips64el";
32 case msp430: return "msp430";
33 case ppc64: return "powerpc64";
34 case ppc64le: return "powerpc64le";
35 case ppc: return "powerpc";
36 case r600: return "r600";
Tom Stellard07f11602015-01-06 18:00:00 +000037 case amdgcn: return "amdgcn";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000038 case sparc: return "sparc";
39 case sparcv9: return "sparcv9";
Douglas Katzmane0ff2822015-04-29 19:15:08 +000040 case sparcel: return "sparcel";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000041 case systemz: return "s390x";
42 case tce: return "tce";
43 case thumb: return "thumb";
Christian Pirker2a111602014-03-28 14:35:30 +000044 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000045 case x86: return "i386";
46 case x86_64: return "x86_64";
47 case xcore: return "xcore";
48 case nvptx: return "nvptx";
49 case nvptx64: return "nvptx64";
50 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +000051 case le64: return "le64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000052 case amdil: return "amdil";
Matt Arsenault3f9b0212014-09-19 19:52:11 +000053 case amdil64: return "amdil64";
54 case hsail: return "hsail";
55 case hsail64: return "hsail64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000056 case spir: return "spir";
57 case spir64: return "spir64";
Eric Christopher54fe1b22014-07-10 17:26:54 +000058 case kalimba: return "kalimba";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000059 }
60
David Blaikie46a9f012012-01-20 21:51:11 +000061 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000062}
63
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000064const char *Triple::getArchTypePrefix(ArchType Kind) {
65 switch (Kind) {
66 default:
Craig Topperc10719f2014-04-07 04:17:22 +000067 return nullptr;
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000068
Christian Pirker6c2f4d42014-02-24 11:34:50 +000069 case aarch64:
70 case aarch64_be: return "aarch64";
Tim Northovere0e3aef2013-01-31 12:12:40 +000071
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000072 case arm:
Christian Pirker2a111602014-03-28 14:35:30 +000073 case armeb:
74 case thumb:
75 case thumbeb: return "arm";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000076
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000077 case ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +000078 case ppc64le:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000079 case ppc: return "ppc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000080
Benjamin Kramerae3c3002012-06-28 19:09:53 +000081 case mips:
82 case mipsel:
83 case mips64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000084 case mips64el: return "mips";
Benjamin Kramerae3c3002012-06-28 19:09:53 +000085
Christian Pirker6c2f4d42014-02-24 11:34:50 +000086 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000087
Tom Stellard07f11602015-01-06 18:00:00 +000088 case amdgcn:
89 case r600: return "amdgpu";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000090
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000091 case bpf: return "bpf";
92
Chris Lattner8228b112010-02-04 06:34:01 +000093 case sparcv9:
Douglas Katzmane0ff2822015-04-29 19:15:08 +000094 case sparcel:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000095 case sparc: return "sparc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000096
Ulrich Weigand57c85f52015-04-01 12:51:43 +000097 case systemz: return "s390";
Richard Sandiforda238c5e2013-05-03 11:05:17 +000098
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000099 case x86:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000100 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +0000101
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000102 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +0000103
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000104 case nvptx: return "nvptx";
105 case nvptx64: return "nvptx";
Tim Northover00ed9962014-03-29 10:18:08 +0000106
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000107 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +0000108 case le64: return "le64";
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000109
110 case amdil:
111 case amdil64: return "amdil";
112
113 case hsail:
114 case hsail64: return "hsail";
115
116 case spir:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000117 case spir64: return "spir";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000118 case kalimba: return "kalimba";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000119 }
120}
121
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000122const char *Triple::getVendorTypeName(VendorType Kind) {
123 switch (Kind) {
124 case UnknownVendor: return "unknown";
125
126 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000127 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000128 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000129 case BGP: return "bgp";
130 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000131 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000132 case IBM: return "ibm";
Daniel Sandersc5626f42014-07-09 16:03:10 +0000133 case ImaginationTechnologies: return "img";
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000134 case MipsTechnologies: return "mti";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000135 case NVIDIA: return "nvidia";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000136 case CSR: return "csr";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000137 }
138
David Blaikie46a9f012012-01-20 21:51:11 +0000139 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000140}
141
142const char *Triple::getOSTypeName(OSType Kind) {
143 switch (Kind) {
144 case UnknownOS: return "unknown";
145
Ed Schoutendae71892015-03-09 18:40:45 +0000146 case CloudABI: return "cloudabi";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000147 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000148 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000149 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000150 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000151 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000152 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000153 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000154 case MacOSX: return "macosx";
Chris Lattner01218d52009-07-13 20:22:23 +0000155 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000156 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000157 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000158 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000159 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000160 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000161 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000162 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000163 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000164 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000165 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000166 case CUDA: return "cuda";
167 case NVCL: return "nvcl";
Tom Stellard4082a6c2014-12-02 16:45:47 +0000168 case AMDHSA: return "amdhsa";
Alex Rosenberg38babd12015-01-25 22:46:59 +0000169 case PS4: return "ps4";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000170 }
171
David Blaikie46a9f012012-01-20 21:51:11 +0000172 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000173}
174
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000175const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
176 switch (Kind) {
177 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000178 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000179 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000180 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000181 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000182 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000183 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000184 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000185 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000186 case MSVC: return "msvc";
187 case Itanium: return "itanium";
188 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000189 }
190
David Blaikie46a9f012012-01-20 21:51:11 +0000191 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000192}
193
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000194Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000195 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000196 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000197 .Case("aarch64_be", aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000198 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
Chandler Carruthff6f3562012-02-12 09:27:38 +0000199 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000200 .Case("armeb", armeb)
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000201 .Case("bpf", bpf)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000202 .Case("mips", mips)
203 .Case("mipsel", mipsel)
204 .Case("mips64", mips64)
205 .Case("mips64el", mips64el)
206 .Case("msp430", msp430)
207 .Case("ppc64", ppc64)
208 .Case("ppc32", ppc)
209 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000210 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000211 .Case("r600", r600)
Tom Stellard07f11602015-01-06 18:00:00 +0000212 .Case("amdgcn", amdgcn)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000213 .Case("hexagon", hexagon)
214 .Case("sparc", sparc)
Douglas Katzman49e96132015-05-01 16:49:58 +0000215 .Case("sparcel", sparcel)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000216 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000217 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000218 .Case("tce", tce)
219 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000220 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000221 .Case("x86", x86)
222 .Case("x86-64", x86_64)
223 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000224 .Case("nvptx", nvptx)
225 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000226 .Case("le32", le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000227 .Case("le64", le64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000228 .Case("amdil", amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000229 .Case("amdil64", amdil64)
230 .Case("hsail", hsail)
231 .Case("hsail64", hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000232 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000233 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000234 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000235 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000236}
237
Renato Golinf5f373f2015-05-08 21:04:27 +0000238// FIXME: Use ARMTargetParser. This would require Triple::arm/thumb
239// to be recogniseable universally.
Renato Golin609bf922014-11-17 14:08:57 +0000240static Triple::ArchType parseARMArch(StringRef ArchName) {
241 size_t offset = StringRef::npos;
242 Triple::ArchType arch = Triple::UnknownArch;
243 bool isThumb = ArchName.startswith("thumb");
244
245 if (ArchName.equals("arm"))
246 return Triple::arm;
247 if (ArchName.equals("armeb"))
248 return Triple::armeb;
249 if (ArchName.equals("thumb"))
250 return Triple::thumb;
251 if (ArchName.equals("thumbeb"))
252 return Triple::thumbeb;
253 if (ArchName.equals("arm64") || ArchName.equals("aarch64"))
254 return Triple::aarch64;
255 if (ArchName.equals("aarch64_be"))
256 return Triple::aarch64_be;
257
258 if (ArchName.startswith("armv")) {
259 offset = 3;
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000260 if (ArchName.endswith("eb")) {
261 arch = Triple::armeb;
262 ArchName = ArchName.substr(0, ArchName.size() - 2);
263 } else
264 arch = Triple::arm;
Renato Golin609bf922014-11-17 14:08:57 +0000265 } else if (ArchName.startswith("armebv")) {
266 offset = 5;
267 arch = Triple::armeb;
268 } else if (ArchName.startswith("thumbv")) {
269 offset = 5;
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000270 if (ArchName.endswith("eb")) {
271 arch = Triple::thumbeb;
272 ArchName = ArchName.substr(0, ArchName.size() - 2);
273 } else
274 arch = Triple::thumb;
Renato Golin609bf922014-11-17 14:08:57 +0000275 } else if (ArchName.startswith("thumbebv")) {
276 offset = 7;
277 arch = Triple::thumbeb;
278 }
279 return StringSwitch<Triple::ArchType>(ArchName.substr(offset))
280 .Cases("v2", "v2a", isThumb ? Triple::UnknownArch : arch)
281 .Cases("v3", "v3m", isThumb ? Triple::UnknownArch : arch)
282 .Cases("v4", "v4t", arch)
283 .Cases("v5", "v5e", "v5t", "v5te", "v5tej", arch)
Ismail Donmez5eb52b72015-05-05 09:29:43 +0000284 .Cases("v6", "v6hl", "v6j", "v6k", arch)
285 .Cases("v6m", "v6sm", arch)
Renato Golin609bf922014-11-17 14:08:57 +0000286 .Cases("v6t2", "v6z", "v6zk", arch)
Ismail Donmez5eb52b72015-05-05 09:29:43 +0000287 .Cases("v7", "v7a", "v7em", "v7hl", "v7l", arch)
Renato Golin609bf922014-11-17 14:08:57 +0000288 .Cases("v7m", "v7r", "v7s", arch)
289 .Cases("v8", "v8a", arch)
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000290 .Cases("v8.1", "v8.1a", arch)
Renato Golin609bf922014-11-17 14:08:57 +0000291 .Default(Triple::UnknownArch);
292}
293
Chandler Carruthaec97082012-02-21 08:53:32 +0000294static Triple::ArchType parseArch(StringRef ArchName) {
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000295 Triple::ArchType ARMArch(parseARMArch(ArchName));
296
Chandler Carruthaec97082012-02-21 08:53:32 +0000297 return StringSwitch<Triple::ArchType>(ArchName)
298 .Cases("i386", "i486", "i586", "i686", Triple::x86)
299 // FIXME: Do we need to support these?
300 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000301 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000302 .Case("powerpc", Triple::ppc)
303 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000304 .Case("powerpc64le", Triple::ppc64le)
Renato Golin609bf922014-11-17 14:08:57 +0000305 .Case("xscale", Triple::arm)
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000306 .Case("xscaleeb", Triple::armeb)
307 .StartsWith("arm", ARMArch)
308 .StartsWith("thumb", ARMArch)
309 .StartsWith("aarch64", ARMArch)
Chandler Carruthaec97082012-02-21 08:53:32 +0000310 .Case("msp430", Triple::msp430)
311 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000312 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000313 .Cases("mips64", "mips64eb", Triple::mips64)
314 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000315 .Case("r600", Triple::r600)
Tom Stellard07f11602015-01-06 18:00:00 +0000316 .Case("amdgcn", Triple::amdgcn)
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000317 .Case("bpf", Triple::bpf)
Chandler Carruthaec97082012-02-21 08:53:32 +0000318 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000319 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000320 .Case("sparc", Triple::sparc)
Douglas Katzmane0ff2822015-04-29 19:15:08 +0000321 .Case("sparcel", Triple::sparcel)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000322 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000323 .Case("tce", Triple::tce)
324 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000325 .Case("nvptx", Triple::nvptx)
326 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000327 .Case("le32", Triple::le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000328 .Case("le64", Triple::le64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000329 .Case("amdil", Triple::amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000330 .Case("amdil64", Triple::amdil64)
331 .Case("hsail", Triple::hsail)
332 .Case("hsail64", Triple::hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000333 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000334 .Case("spir64", Triple::spir64)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000335 .StartsWith("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000336 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000337}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000338
Chandler Carruthaec97082012-02-21 08:53:32 +0000339static Triple::VendorType parseVendor(StringRef VendorName) {
340 return StringSwitch<Triple::VendorType>(VendorName)
341 .Case("apple", Triple::Apple)
342 .Case("pc", Triple::PC)
343 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000344 .Case("bgp", Triple::BGP)
345 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000346 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000347 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000348 .Case("img", Triple::ImaginationTechnologies)
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000349 .Case("mti", Triple::MipsTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000350 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000351 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000352 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000353}
354
Chandler Carruthaec97082012-02-21 08:53:32 +0000355static Triple::OSType parseOS(StringRef OSName) {
356 return StringSwitch<Triple::OSType>(OSName)
Ed Schoutendae71892015-03-09 18:40:45 +0000357 .StartsWith("cloudabi", Triple::CloudABI)
Chandler Carruthaec97082012-02-21 08:53:32 +0000358 .StartsWith("darwin", Triple::Darwin)
359 .StartsWith("dragonfly", Triple::DragonFly)
360 .StartsWith("freebsd", Triple::FreeBSD)
361 .StartsWith("ios", Triple::IOS)
362 .StartsWith("kfreebsd", Triple::KFreeBSD)
363 .StartsWith("linux", Triple::Linux)
364 .StartsWith("lv2", Triple::Lv2)
365 .StartsWith("macosx", Triple::MacOSX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000366 .StartsWith("netbsd", Triple::NetBSD)
367 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000368 .StartsWith("solaris", Triple::Solaris)
369 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000370 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000371 .StartsWith("haiku", Triple::Haiku)
372 .StartsWith("minix", Triple::Minix)
373 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000374 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000375 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000376 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000377 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000378 .StartsWith("cuda", Triple::CUDA)
379 .StartsWith("nvcl", Triple::NVCL)
Tom Stellard4082a6c2014-12-02 16:45:47 +0000380 .StartsWith("amdhsa", Triple::AMDHSA)
Alex Rosenberg38babd12015-01-25 22:46:59 +0000381 .StartsWith("ps4", Triple::PS4)
Chandler Carruthaec97082012-02-21 08:53:32 +0000382 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000383}
384
Chandler Carruthaec97082012-02-21 08:53:32 +0000385static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
386 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000387 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000388 .StartsWith("eabi", Triple::EABI)
389 .StartsWith("gnueabihf", Triple::GNUEABIHF)
390 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000391 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000392 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000393 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000394 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000395 .StartsWith("msvc", Triple::MSVC)
396 .StartsWith("itanium", Triple::Itanium)
397 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000398 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000399}
400
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000401static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
402 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
403 .EndsWith("coff", Triple::COFF)
404 .EndsWith("elf", Triple::ELF)
405 .EndsWith("macho", Triple::MachO)
406 .Default(Triple::UnknownObjectFormat);
407}
408
Renato Golinf5f373f2015-05-08 21:04:27 +0000409// FIXME: Use ARMTargetParser. This would require using Triple::ARMSubArch*
410// in ARMBuildAttrs and in ARCHNames' DefaultArch fields.
Renato Golinc17a07b2014-07-18 12:00:48 +0000411static Triple::SubArchType parseSubArch(StringRef SubArchName) {
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000412 if (SubArchName.endswith("eb"))
413 SubArchName = SubArchName.substr(0, SubArchName.size() - 2);
414
Renato Golinc17a07b2014-07-18 12:00:48 +0000415 return StringSwitch<Triple::SubArchType>(SubArchName)
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000416 .EndsWith("v8.1a", Triple::ARMSubArch_v8_1a)
Renato Golinc17a07b2014-07-18 12:00:48 +0000417 .EndsWith("v8", Triple::ARMSubArch_v8)
418 .EndsWith("v8a", Triple::ARMSubArch_v8)
419 .EndsWith("v7", Triple::ARMSubArch_v7)
420 .EndsWith("v7a", Triple::ARMSubArch_v7)
421 .EndsWith("v7em", Triple::ARMSubArch_v7em)
422 .EndsWith("v7l", Triple::ARMSubArch_v7)
423 .EndsWith("v7m", Triple::ARMSubArch_v7m)
424 .EndsWith("v7r", Triple::ARMSubArch_v7)
425 .EndsWith("v7s", Triple::ARMSubArch_v7s)
426 .EndsWith("v6", Triple::ARMSubArch_v6)
427 .EndsWith("v6m", Triple::ARMSubArch_v6m)
Bradley Smithe997b452015-02-10 15:15:08 +0000428 .EndsWith("v6sm", Triple::ARMSubArch_v6m)
Renato Golin12350602015-03-17 11:55:28 +0000429 .EndsWith("v6k", Triple::ARMSubArch_v6k)
Renato Golinc17a07b2014-07-18 12:00:48 +0000430 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
431 .EndsWith("v5", Triple::ARMSubArch_v5)
432 .EndsWith("v5e", Triple::ARMSubArch_v5)
433 .EndsWith("v5t", Triple::ARMSubArch_v5)
434 .EndsWith("v5te", Triple::ARMSubArch_v5te)
435 .EndsWith("v4t", Triple::ARMSubArch_v4t)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000436 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
437 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
438 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
Renato Golinc17a07b2014-07-18 12:00:48 +0000439 .Default(Triple::NoSubArch);
440}
441
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000442static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
443 switch (Kind) {
444 case Triple::UnknownObjectFormat: return "";
445 case Triple::COFF: return "coff";
446 case Triple::ELF: return "elf";
447 case Triple::MachO: return "macho";
448 }
449 llvm_unreachable("unknown object format type");
450}
451
452static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
Rafael Espindola24254892015-03-18 22:19:16 +0000453 switch (T.getArch()) {
454 default:
455 break;
456 case Triple::hexagon:
457 case Triple::mips:
458 case Triple::mipsel:
459 case Triple::mips64:
460 case Triple::mips64el:
461 case Triple::r600:
462 case Triple::amdgcn:
463 case Triple::sparc:
464 case Triple::sparcv9:
465 case Triple::systemz:
466 case Triple::xcore:
Rafael Espindola7f454402015-03-19 02:40:56 +0000467 case Triple::ppc64le:
468 return Triple::ELF;
469
470 case Triple::ppc:
471 case Triple::ppc64:
472 if (T.isOSDarwin())
473 return Triple::MachO;
Rafael Espindola24254892015-03-18 22:19:16 +0000474 return Triple::ELF;
475 }
476
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000477 if (T.isOSDarwin())
478 return Triple::MachO;
479 else if (T.isOSWindows())
480 return Triple::COFF;
481 return Triple::ELF;
482}
483
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000484/// \brief Construct a triple from the string representation provided.
485///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000486/// This stores the string representation and parses the various pieces into
487/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000488Triple::Triple(const Twine &Str)
489 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000490 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000491 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000492 Vendor(parseVendor(getVendorName())),
493 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000494 Environment(parseEnvironment(getEnvironmentName())),
495 ObjectFormat(parseFormat(getEnvironmentName())) {
496 if (ObjectFormat == Triple::UnknownObjectFormat)
497 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000498}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000499
500/// \brief Construct a triple from string representations of the architecture,
501/// vendor, and OS.
502///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000503/// This joins each argument into a canonical string representation and parses
504/// them into enum members. It leaves the environment unknown and omits it from
505/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000506Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
507 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000508 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000509 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000510 Vendor(parseVendor(VendorStr.str())),
511 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000512 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
513 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000514}
515
516/// \brief Construct a triple from string representations of the architecture,
517/// vendor, OS, and environment.
518///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000519/// This joins each argument into a canonical string representation and parses
520/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000521Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
522 const Twine &EnvironmentStr)
523 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
524 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000525 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000526 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000527 Vendor(parseVendor(VendorStr.str())),
528 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000529 Environment(parseEnvironment(EnvironmentStr.str())),
530 ObjectFormat(parseFormat(EnvironmentStr.str())) {
531 if (ObjectFormat == Triple::UnknownObjectFormat)
532 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000533}
534
Duncan Sands501dff72010-08-12 11:31:39 +0000535std::string Triple::normalize(StringRef Str) {
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000536 bool IsMinGW32 = false;
537 bool IsCygwin = false;
538
Duncan Sands501dff72010-08-12 11:31:39 +0000539 // Parse into components.
540 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000541 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000542
543 // If the first component corresponds to a known architecture, preferentially
544 // use it for the architecture. If the second component corresponds to a
545 // known vendor, preferentially use it for the vendor, etc. This avoids silly
546 // component movement when a component parses as (eg) both a valid arch and a
547 // valid os.
548 ArchType Arch = UnknownArch;
549 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000550 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000551 VendorType Vendor = UnknownVendor;
552 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000553 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000554 OSType OS = UnknownOS;
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000555 if (Components.size() > 2) {
Chandler Carruthaec97082012-02-21 08:53:32 +0000556 OS = parseOS(Components[2]);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000557 IsCygwin = Components[2].startswith("cygwin");
558 IsMinGW32 = Components[2].startswith("mingw");
559 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000560 EnvironmentType Environment = UnknownEnvironment;
561 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000562 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000563 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000564 if (Components.size() > 4)
565 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000566
567 // Note which components are already in their final position. These will not
568 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000569 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000570 Found[0] = Arch != UnknownArch;
571 Found[1] = Vendor != UnknownVendor;
572 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000573 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000574
575 // If they are not there already, permute the components into their canonical
576 // positions by seeing if they parse as a valid architecture, and if so moving
577 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000578 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000579 if (Found[Pos])
580 continue; // Already in the canonical position.
581
582 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
583 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000584 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000585 continue;
586
587 // Does this component parse as valid for the target position?
588 bool Valid = false;
589 StringRef Comp = Components[Idx];
590 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000591 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000592 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000593 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000594 Valid = Arch != UnknownArch;
595 break;
596 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000597 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000598 Valid = Vendor != UnknownVendor;
599 break;
600 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000601 OS = parseOS(Comp);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000602 IsCygwin = Comp.startswith("cygwin");
603 IsMinGW32 = Comp.startswith("mingw");
604 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
Duncan Sands501dff72010-08-12 11:31:39 +0000605 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000606 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000607 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000608 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000609 if (!Valid) {
610 ObjectFormat = parseFormat(Comp);
611 Valid = ObjectFormat != UnknownObjectFormat;
612 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000613 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000614 }
615 if (!Valid)
616 continue; // Nope, try the next component.
617
618 // Move the component to the target position, pushing any non-fixed
619 // components that are in the way to the right. This tends to give
620 // good results in the common cases of a forgotten vendor component
621 // or a wrongly positioned environment.
622 if (Pos < Idx) {
623 // Insert left, pushing the existing components to the right. For
624 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
625 StringRef CurrentComponent(""); // The empty component.
626 // Replace the component we are moving with an empty component.
627 std::swap(CurrentComponent, Components[Idx]);
628 // Insert the component being moved at Pos, displacing any existing
629 // components to the right.
630 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
631 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000632 while (i < array_lengthof(Found) && Found[i])
633 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000634 // Place the component at the new position, getting the component
635 // that was at this position - it will be moved right.
636 std::swap(CurrentComponent, Components[i]);
637 }
638 } else if (Pos > Idx) {
639 // Push right by inserting empty components until the component at Idx
640 // reaches the target position Pos. For example, pc-a -> -pc-a when
641 // moving pc to the second position.
642 do {
643 // Insert one empty component at Idx.
644 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000645 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000646 // Place the component at the new position, getting the component
647 // that was at this position - it will be moved right.
648 std::swap(CurrentComponent, Components[i]);
649 // If it was placed on top of an empty component then we are done.
650 if (CurrentComponent.empty())
651 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000652 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000653 while (++i < array_lengthof(Found) && Found[i])
654 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000655 }
656 // The last component was pushed off the end - append it.
657 if (!CurrentComponent.empty())
658 Components.push_back(CurrentComponent);
659
660 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000661 while (++Idx < array_lengthof(Found) && Found[Idx])
662 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000663 } while (Idx < Pos); // Add more until the final position is reached.
664 }
665 assert(Pos < Components.size() && Components[Pos] == Comp &&
666 "Component moved wrong!");
667 Found[Pos] = true;
668 break;
669 }
670 }
671
672 // Special case logic goes here. At this point Arch, Vendor and OS have the
673 // correct values for the computed components.
674
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000675 if (OS == Triple::Win32) {
676 Components.resize(4);
677 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000678 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000679 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000680 Components[3] = "msvc";
681 else
682 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000683 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000684 } else if (IsMinGW32) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000685 Components.resize(4);
686 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000687 Components[3] = "gnu";
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000688 } else if (IsCygwin) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000689 Components.resize(4);
690 Components[2] = "windows";
691 Components[3] = "cygnus";
692 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000693 if (IsMinGW32 || IsCygwin ||
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000694 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
695 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
696 Components.resize(5);
697 Components[4] = getObjectFormatTypeName(ObjectFormat);
698 }
699 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000700
Duncan Sands501dff72010-08-12 11:31:39 +0000701 // Stick the corrected components back together to form the normalized string.
702 std::string Normalized;
703 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
704 if (i) Normalized += '-';
705 Normalized += Components[i];
706 }
707 return Normalized;
708}
709
Daniel Dunbar19e70762009-07-26 03:31:47 +0000710StringRef Triple::getArchName() const {
711 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000712}
713
Daniel Dunbar19e70762009-07-26 03:31:47 +0000714StringRef Triple::getVendorName() const {
715 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
716 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000717}
718
Daniel Dunbar19e70762009-07-26 03:31:47 +0000719StringRef Triple::getOSName() const {
720 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
721 Tmp = Tmp.split('-').second; // Strip second component
722 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000723}
724
Daniel Dunbar19e70762009-07-26 03:31:47 +0000725StringRef Triple::getEnvironmentName() const {
726 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
727 Tmp = Tmp.split('-').second; // Strip second component
728 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000729}
730
Daniel Dunbar19e70762009-07-26 03:31:47 +0000731StringRef Triple::getOSAndEnvironmentName() const {
732 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
733 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000734}
735
Chris Lattner553c9f32009-08-12 06:19:40 +0000736static unsigned EatNumber(StringRef &Str) {
737 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000738 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000739
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000740 do {
741 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000742 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000743
Chris Lattner553c9f32009-08-12 06:19:40 +0000744 // Eat the digit.
745 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000746 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000747
Chris Lattner553c9f32009-08-12 06:19:40 +0000748 return Result;
749}
750
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000751void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
752 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000753 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000754
Dan Albert675cffc2015-03-03 18:23:51 +0000755 // For Android, we care about the Android version rather than the Linux
756 // version.
757 if (getEnvironment() == Android) {
758 OSName = getEnvironmentName().substr(strlen("android"));
759 if (OSName.startswith("eabi"))
760 OSName = OSName.substr(strlen("eabi"));
761 }
762
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000763 // Assume that the OS portion of the triple starts with the canonical name.
764 StringRef OSTypeName = getOSTypeName(getOS());
765 if (OSName.startswith(OSTypeName))
766 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000767
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000768 // Any unset version defaults to 0.
769 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000770
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000771 // Parse up to three components.
772 unsigned *Components[3] = { &Major, &Minor, &Micro };
773 for (unsigned i = 0; i != 3; ++i) {
774 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
775 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000776
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000777 // Consume the leading number.
778 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000779
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000780 // Consume the separator, if present.
781 if (OSName.startswith("."))
782 OSName = OSName.substr(1);
783 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000784}
785
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000786bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
787 unsigned &Micro) const {
788 getOSVersion(Major, Minor, Micro);
789
790 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000791 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000792 case Darwin:
793 // Default to darwin8, i.e., MacOSX 10.4.
794 if (Major == 0)
795 Major = 8;
796 // Darwin version numbers are skewed from OS X versions.
797 if (Major < 4)
798 return false;
799 Micro = 0;
800 Minor = Major - 4;
801 Major = 10;
802 break;
803 case MacOSX:
804 // Default to 10.4.
805 if (Major == 0) {
806 Major = 10;
807 Minor = 4;
808 }
809 if (Major != 10)
810 return false;
811 break;
812 case IOS:
813 // Ignore the version from the triple. This is only handled because the
814 // the clang driver combines OS X and IOS support into a common Darwin
815 // toolchain that wants to know the OS X version number even when targeting
816 // IOS.
817 Major = 10;
818 Minor = 4;
819 Micro = 0;
820 break;
821 }
822 return true;
823}
824
Chad Rosierd84eaac2012-05-09 17:23:48 +0000825void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
826 unsigned &Micro) const {
827 switch (getOS()) {
828 default: llvm_unreachable("unexpected OS for Darwin triple");
829 case Darwin:
830 case MacOSX:
831 // Ignore the version from the triple. This is only handled because the
832 // the clang driver combines OS X and IOS support into a common Darwin
833 // toolchain that wants to know the iOS version number even when targeting
834 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000835 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000836 Minor = 0;
837 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000838 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000839 case IOS:
840 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000841 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000842 if (Major == 0)
Tim Northovere19bed72014-07-23 12:32:47 +0000843 Major = (getArch() == aarch64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000844 break;
845 }
846}
847
Daniel Dunbar19e70762009-07-26 03:31:47 +0000848void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000849 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000850}
851
852void Triple::setArch(ArchType Kind) {
853 setArchName(getArchTypeName(Kind));
854}
855
856void Triple::setVendor(VendorType Kind) {
857 setVendorName(getVendorTypeName(Kind));
858}
859
860void Triple::setOS(OSType Kind) {
861 setOSName(getOSTypeName(Kind));
862}
863
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000864void Triple::setEnvironment(EnvironmentType Kind) {
Reid Kleckner5fe405d2015-02-13 22:05:50 +0000865 if (ObjectFormat == getDefaultFormat(*this))
866 return setEnvironmentName(getEnvironmentTypeName(Kind));
867
868 setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
869 getObjectFormatTypeName(ObjectFormat)).str());
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000870}
871
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000872void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000873 if (Environment == UnknownEnvironment)
874 return setEnvironmentName(getObjectFormatTypeName(Kind));
875
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000876 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
877 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000878}
879
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000880void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000881 // Work around a miscompilation bug for Twines in gcc 4.0.3.
882 SmallString<64> Triple;
883 Triple += Str;
884 Triple += "-";
885 Triple += getVendorName();
886 Triple += "-";
887 Triple += getOSAndEnvironmentName();
Yaron Keren92e1b622015-03-18 10:17:07 +0000888 setTriple(Triple);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000889}
890
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000891void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000892 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
893}
894
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000895void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000896 if (hasEnvironment())
897 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
898 "-" + getEnvironmentName());
899 else
900 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
901}
902
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000903void Triple::setEnvironmentName(StringRef Str) {
904 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000905 "-" + Str);
906}
907
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000908void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000909 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
910}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000911
912static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
913 switch (Arch) {
914 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000915 return 0;
916
917 case llvm::Triple::msp430:
918 return 16;
919
Chandler Carruthb90c1022012-01-31 04:52:32 +0000920 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000921 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000922 case llvm::Triple::hexagon:
923 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000924 case llvm::Triple::mips:
925 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000926 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000927 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000928 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000929 case llvm::Triple::sparc:
Douglas Katzmane0ff2822015-04-29 19:15:08 +0000930 case llvm::Triple::sparcel:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000931 case llvm::Triple::tce:
932 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000933 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000934 case llvm::Triple::x86:
935 case llvm::Triple::xcore:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000936 case llvm::Triple::amdil:
937 case llvm::Triple::hsail:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000938 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000939 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000940 return 32;
941
Tim Northovere0e3aef2013-01-31 12:12:40 +0000942 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000943 case llvm::Triple::aarch64_be:
Tom Stellard07f11602015-01-06 18:00:00 +0000944 case llvm::Triple::amdgcn:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000945 case llvm::Triple::bpf:
JF Bastien32972ef2014-09-12 17:54:17 +0000946 case llvm::Triple::le64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000947 case llvm::Triple::mips64:
948 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000949 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000950 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000951 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000952 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000953 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000954 case llvm::Triple::x86_64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000955 case llvm::Triple::amdil64:
956 case llvm::Triple::hsail64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000957 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000958 return 64;
959 }
960 llvm_unreachable("Invalid architecture value");
961}
962
963bool Triple::isArch64Bit() const {
964 return getArchPointerBitWidth(getArch()) == 64;
965}
966
967bool Triple::isArch32Bit() const {
968 return getArchPointerBitWidth(getArch()) == 32;
969}
970
971bool Triple::isArch16Bit() const {
972 return getArchPointerBitWidth(getArch()) == 16;
973}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000974
975Triple Triple::get32BitArchVariant() const {
976 Triple T(*this);
977 switch (getArch()) {
978 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000979 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000980 case Triple::aarch64_be:
Tom Stellard07f11602015-01-06 18:00:00 +0000981 case Triple::amdgcn:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000982 case Triple::bpf:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000983 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000984 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000985 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000986 T.setArch(UnknownArch);
987 break;
988
989 case Triple::amdil:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000990 case Triple::hsail:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000991 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000992 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000993 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000994 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000995 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000996 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000997 case Triple::mips:
998 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000999 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001000 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +00001001 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001002 case Triple::sparc:
Douglas Katzmane0ff2822015-04-29 19:15:08 +00001003 case Triple::sparcel:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001004 case Triple::tce:
1005 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +00001006 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001007 case Triple::x86:
1008 case Triple::xcore:
1009 // Already 32-bit.
1010 break;
1011
JF Bastien32972ef2014-09-12 17:54:17 +00001012 case Triple::le64: T.setArch(Triple::le32); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001013 case Triple::mips64: T.setArch(Triple::mips); break;
1014 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001015 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +00001016 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001017 case Triple::sparcv9: T.setArch(Triple::sparc); break;
1018 case Triple::x86_64: T.setArch(Triple::x86); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001019 case Triple::amdil64: T.setArch(Triple::amdil); break;
1020 case Triple::hsail64: T.setArch(Triple::hsail); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +00001021 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001022 }
1023 return T;
1024}
1025
1026Triple Triple::get64BitArchVariant() const {
1027 Triple T(*this);
1028 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001029 case Triple::UnknownArch:
Tim Northoveraf6bfb22014-03-30 07:25:23 +00001030 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +00001031 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001032 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +00001033 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001034 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +00001035 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001036 case Triple::tce:
1037 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +00001038 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001039 case Triple::xcore:
Douglas Katzmane0ff2822015-04-29 19:15:08 +00001040 case Triple::sparcel:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001041 T.setArch(UnknownArch);
1042 break;
1043
Tim Northovere0e3aef2013-01-31 12:12:40 +00001044 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +00001045 case Triple::aarch64_be:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +00001046 case Triple::bpf:
JF Bastien32972ef2014-09-12 17:54:17 +00001047 case Triple::le64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001048 case Triple::amdil64:
Tom Stellard07f11602015-01-06 18:00:00 +00001049 case Triple::amdgcn:
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001050 case Triple::hsail64:
1051 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001052 case Triple::mips64:
1053 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001054 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001055 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001056 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001057 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +00001058 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001059 case Triple::x86_64:
1060 // Already 64-bit.
1061 break;
1062
JF Bastien32972ef2014-09-12 17:54:17 +00001063 case Triple::le32: T.setArch(Triple::le64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001064 case Triple::mips: T.setArch(Triple::mips64); break;
1065 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001066 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001067 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001068 case Triple::sparc: T.setArch(Triple::sparcv9); break;
1069 case Triple::x86: T.setArch(Triple::x86_64); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001070 case Triple::amdil: T.setArch(Triple::amdil64); break;
1071 case Triple::hsail: T.setArch(Triple::hsail64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +00001072 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001073 }
1074 return T;
1075}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001076
Renato Golinf5f373f2015-05-08 21:04:27 +00001077// FIXME: Use ARMTargetParser. This would require ARCHNames to hold
1078// specific CPU names, as well as default CPU arch.
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001079const char *Triple::getARMCPUForArch(StringRef MArch) const {
1080 if (MArch.empty())
1081 MArch = getArchName();
1082
1083 switch (getOS()) {
Ed Masteea0257a2014-09-09 17:47:24 +00001084 case llvm::Triple::FreeBSD:
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001085 case llvm::Triple::NetBSD:
1086 if (MArch == "armv6")
1087 return "arm1176jzf-s";
1088 break;
1089 case llvm::Triple::Win32:
1090 // FIXME: this is invalid for WindowsCE
1091 return "cortex-a9";
1092 default:
1093 break;
1094 }
1095
John Brawn50ed9472015-05-08 12:52:02 +00001096 // MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?
1097 // Only the (v.+) part is relevant for determining the CPU, as it determines
1098 // the architecture version, so we first remove the surrounding parts.
1099 // (ep9312|iwmmxt|xscale)(eb)? is also permitted, so we have to be a bit
1100 // careful when removing the leading (arm|thumb)?(eb)? as we don't want to
1101 // permit things like armep9312.
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001102 const char *result = nullptr;
1103 size_t offset = StringRef::npos;
1104 if (MArch.startswith("arm"))
1105 offset = 3;
John Brawn50ed9472015-05-08 12:52:02 +00001106 else if (MArch.startswith("thumb"))
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001107 offset = 5;
1108 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
1109 offset += 2;
John Brawn50ed9472015-05-08 12:52:02 +00001110 else if (MArch.endswith("eb"))
Joerg Sonnenberger429edc12015-01-26 11:41:48 +00001111 MArch = MArch.substr(0, MArch.size() - 2);
John Brawn50ed9472015-05-08 12:52:02 +00001112 if (offset != StringRef::npos && (offset == MArch.size() || MArch[offset] == 'v'))
1113 MArch = MArch.substr(offset);
1114
1115 if (MArch == "") {
1116 // If no specific architecture version is requested, return the minimum CPU
1117 // required by the OS and environment.
1118 switch (getOS()) {
1119 case llvm::Triple::NetBSD:
1120 switch (getEnvironment()) {
1121 case llvm::Triple::GNUEABIHF:
1122 case llvm::Triple::GNUEABI:
1123 case llvm::Triple::EABIHF:
1124 case llvm::Triple::EABI:
1125 return "arm926ej-s";
1126 default:
1127 return "strongarm";
1128 }
1129 case llvm::Triple::NaCl:
1130 return "cortex-a8";
1131 default:
1132 switch (getEnvironment()) {
1133 case llvm::Triple::EABIHF:
1134 case llvm::Triple::GNUEABIHF:
1135 return "arm1176jzf-s";
1136 default:
1137 return "arm7tdmi";
1138 }
1139 }
1140 } else {
1141 result = llvm::StringSwitch<const char *>(MArch)
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001142 .Cases("v2", "v2a", "arm2")
1143 .Case("v3", "arm6")
1144 .Case("v3m", "arm7m")
1145 .Case("v4", "strongarm")
1146 .Case("v4t", "arm7tdmi")
1147 .Cases("v5", "v5t", "arm10tdmi")
1148 .Cases("v5e", "v5te", "arm1022e")
1149 .Case("v5tej", "arm926ej-s")
Renato Golin12350602015-03-17 11:55:28 +00001150 .Case("v6", "arm1136jf-s")
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001151 .Case("v6j", "arm1136j-s")
Renato Golin12350602015-03-17 11:55:28 +00001152 .Cases("v6k", "v6z", "v6zk", "arm1176jzf-s")
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001153 .Case("v6t2", "arm1156t2-s")
Bradley Smithe997b452015-02-10 15:15:08 +00001154 .Cases("v6m", "v6-m", "v6sm", "v6s-m", "cortex-m0")
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001155 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
1156 .Cases("v7s", "v7-s", "swift")
1157 .Cases("v7r", "v7-r", "cortex-r4")
1158 .Cases("v7m", "v7-m", "cortex-m3")
1159 .Cases("v7em", "v7e-m", "cortex-m4")
1160 .Cases("v8", "v8a", "v8-a", "cortex-a53")
Vladimir Sukharevf3840702015-04-02 09:32:14 +00001161 .Cases("v8.1a", "v8.1-a", "generic")
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001162 .Case("ep9312", "ep9312")
1163 .Case("iwmmxt", "iwmmxt")
1164 .Case("xscale", "xscale")
1165 .Default(nullptr);
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001166 }
John Brawn50ed9472015-05-08 12:52:02 +00001167
1168 return result;
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001169}