blob: 7f08ce0de473517c2608564082622f5104b374a4 [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"
Renato Goline8048f02015-05-20 15:05:07 +000015#include "llvm/Support/TargetParser.h"
Mikhail Glushenkov12062ba2009-04-02 01:11:37 +000016#include <cstring>
Daniel Dunbar4abd5662009-04-01 21:53:23 +000017using namespace llvm;
18
Daniel Dunbar4abd5662009-04-01 21:53:23 +000019const char *Triple::getArchTypeName(ArchType Kind) {
20 switch (Kind) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +000021 case UnknownArch: return "unknown";
Jim Grosbachf638b262010-12-17 02:10:59 +000022
Christian Pirker6c2f4d42014-02-24 11:34:50 +000023 case aarch64: return "aarch64";
24 case aarch64_be: return "aarch64_be";
25 case arm: return "arm";
Christian Pirker2a111602014-03-28 14:35:30 +000026 case armeb: return "armeb";
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000027 case bpf: return "bpf";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000028 case hexagon: return "hexagon";
29 case mips: return "mips";
30 case mipsel: return "mipsel";
31 case mips64: return "mips64";
32 case mips64el: return "mips64el";
33 case msp430: return "msp430";
34 case ppc64: return "powerpc64";
35 case ppc64le: return "powerpc64le";
36 case ppc: return "powerpc";
37 case r600: return "r600";
Tom Stellard07f11602015-01-06 18:00:00 +000038 case amdgcn: return "amdgcn";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000039 case sparc: return "sparc";
40 case sparcv9: return "sparcv9";
Douglas Katzmane0ff2822015-04-29 19:15:08 +000041 case sparcel: return "sparcel";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000042 case systemz: return "s390x";
43 case tce: return "tce";
44 case thumb: return "thumb";
Christian Pirker2a111602014-03-28 14:35:30 +000045 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000046 case x86: return "i386";
47 case x86_64: return "x86_64";
48 case xcore: return "xcore";
49 case nvptx: return "nvptx";
50 case nvptx64: return "nvptx64";
51 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +000052 case le64: return "le64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000053 case amdil: return "amdil";
Matt Arsenault3f9b0212014-09-19 19:52:11 +000054 case amdil64: return "amdil64";
55 case hsail: return "hsail";
56 case hsail64: return "hsail64";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000057 case spir: return "spir";
58 case spir64: return "spir64";
Eric Christopher54fe1b22014-07-10 17:26:54 +000059 case kalimba: return "kalimba";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000060 }
61
David Blaikie46a9f012012-01-20 21:51:11 +000062 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000063}
64
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000065const char *Triple::getArchTypePrefix(ArchType Kind) {
66 switch (Kind) {
67 default:
Craig Topperc10719f2014-04-07 04:17:22 +000068 return nullptr;
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000069
Christian Pirker6c2f4d42014-02-24 11:34:50 +000070 case aarch64:
71 case aarch64_be: return "aarch64";
Tim Northovere0e3aef2013-01-31 12:12:40 +000072
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000073 case arm:
Christian Pirker2a111602014-03-28 14:35:30 +000074 case armeb:
75 case thumb:
76 case thumbeb: return "arm";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000077
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000078 case ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +000079 case ppc64le:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000080 case ppc: return "ppc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000081
Benjamin Kramerae3c3002012-06-28 19:09:53 +000082 case mips:
83 case mipsel:
84 case mips64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000085 case mips64el: return "mips";
Benjamin Kramerae3c3002012-06-28 19:09:53 +000086
Christian Pirker6c2f4d42014-02-24 11:34:50 +000087 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088
Tom Stellard07f11602015-01-06 18:00:00 +000089 case amdgcn:
90 case r600: return "amdgpu";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000091
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000092 case bpf: return "bpf";
93
Chris Lattner8228b112010-02-04 06:34:01 +000094 case sparcv9:
Douglas Katzmane0ff2822015-04-29 19:15:08 +000095 case sparcel:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000096 case sparc: return "sparc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000097
Ulrich Weigand57c85f52015-04-01 12:51:43 +000098 case systemz: return "s390";
Richard Sandiforda238c5e2013-05-03 11:05:17 +000099
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000100 case x86:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000101 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +0000102
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000103 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +0000104
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000105 case nvptx: return "nvptx";
106 case nvptx64: return "nvptx";
Tim Northover00ed9962014-03-29 10:18:08 +0000107
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000108 case le32: return "le32";
JF Bastien32972ef2014-09-12 17:54:17 +0000109 case le64: return "le64";
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000110
111 case amdil:
112 case amdil64: return "amdil";
113
114 case hsail:
115 case hsail64: return "hsail";
116
117 case spir:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000118 case spir64: return "spir";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000119 case kalimba: return "kalimba";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000120 }
121}
122
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000123const char *Triple::getVendorTypeName(VendorType Kind) {
124 switch (Kind) {
125 case UnknownVendor: return "unknown";
126
127 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000128 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000129 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000130 case BGP: return "bgp";
131 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000132 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000133 case IBM: return "ibm";
Daniel Sandersc5626f42014-07-09 16:03:10 +0000134 case ImaginationTechnologies: return "img";
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000135 case MipsTechnologies: return "mti";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000136 case NVIDIA: return "nvidia";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000137 case CSR: return "csr";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000138 }
139
David Blaikie46a9f012012-01-20 21:51:11 +0000140 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000141}
142
143const char *Triple::getOSTypeName(OSType Kind) {
144 switch (Kind) {
145 case UnknownOS: return "unknown";
146
Ed Schoutendae71892015-03-09 18:40:45 +0000147 case CloudABI: return "cloudabi";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000148 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000149 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000150 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000151 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000152 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000153 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000154 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000155 case MacOSX: return "macosx";
Chris Lattner01218d52009-07-13 20:22:23 +0000156 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000157 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000158 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000159 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000160 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000161 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000162 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000163 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000164 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000165 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000166 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000167 case CUDA: return "cuda";
168 case NVCL: return "nvcl";
Tom Stellard4082a6c2014-12-02 16:45:47 +0000169 case AMDHSA: return "amdhsa";
Alex Rosenberg38babd12015-01-25 22:46:59 +0000170 case PS4: return "ps4";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000171 }
172
David Blaikie46a9f012012-01-20 21:51:11 +0000173 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000174}
175
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000176const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
177 switch (Kind) {
178 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000179 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000180 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000181 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000182 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000183 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000184 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000185 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000186 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000187 case MSVC: return "msvc";
188 case Itanium: return "itanium";
189 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000190 }
191
David Blaikie46a9f012012-01-20 21:51:11 +0000192 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000193}
194
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000195Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000196 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000197 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000198 .Case("aarch64_be", aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000199 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
Chandler Carruthff6f3562012-02-12 09:27:38 +0000200 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000201 .Case("armeb", armeb)
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000202 .Case("bpf", bpf)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000203 .Case("mips", mips)
204 .Case("mipsel", mipsel)
205 .Case("mips64", mips64)
206 .Case("mips64el", mips64el)
207 .Case("msp430", msp430)
208 .Case("ppc64", ppc64)
209 .Case("ppc32", ppc)
210 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000211 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000212 .Case("r600", r600)
Tom Stellard07f11602015-01-06 18:00:00 +0000213 .Case("amdgcn", amdgcn)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000214 .Case("hexagon", hexagon)
215 .Case("sparc", sparc)
Douglas Katzman49e96132015-05-01 16:49:58 +0000216 .Case("sparcel", sparcel)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000217 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000218 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000219 .Case("tce", tce)
220 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000221 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000222 .Case("x86", x86)
223 .Case("x86-64", x86_64)
224 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000225 .Case("nvptx", nvptx)
226 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000227 .Case("le32", le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000228 .Case("le64", le64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000229 .Case("amdil", amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000230 .Case("amdil64", amdil64)
231 .Case("hsail", hsail)
232 .Case("hsail64", hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000233 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000234 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000235 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000236 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000237}
238
Renato Golinf5f373f2015-05-08 21:04:27 +0000239// FIXME: Use ARMTargetParser. This would require Triple::arm/thumb
240// to be recogniseable universally.
Renato Golin609bf922014-11-17 14:08:57 +0000241static Triple::ArchType parseARMArch(StringRef ArchName) {
242 size_t offset = StringRef::npos;
243 Triple::ArchType arch = Triple::UnknownArch;
244 bool isThumb = ArchName.startswith("thumb");
245
246 if (ArchName.equals("arm"))
247 return Triple::arm;
248 if (ArchName.equals("armeb"))
249 return Triple::armeb;
250 if (ArchName.equals("thumb"))
251 return Triple::thumb;
252 if (ArchName.equals("thumbeb"))
253 return Triple::thumbeb;
254 if (ArchName.equals("arm64") || ArchName.equals("aarch64"))
255 return Triple::aarch64;
256 if (ArchName.equals("aarch64_be"))
257 return Triple::aarch64_be;
258
259 if (ArchName.startswith("armv")) {
260 offset = 3;
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000261 if (ArchName.endswith("eb")) {
262 arch = Triple::armeb;
263 ArchName = ArchName.substr(0, ArchName.size() - 2);
264 } else
265 arch = Triple::arm;
Renato Golin609bf922014-11-17 14:08:57 +0000266 } else if (ArchName.startswith("armebv")) {
267 offset = 5;
268 arch = Triple::armeb;
269 } else if (ArchName.startswith("thumbv")) {
270 offset = 5;
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000271 if (ArchName.endswith("eb")) {
272 arch = Triple::thumbeb;
273 ArchName = ArchName.substr(0, ArchName.size() - 2);
274 } else
275 arch = Triple::thumb;
Renato Golin609bf922014-11-17 14:08:57 +0000276 } else if (ArchName.startswith("thumbebv")) {
277 offset = 7;
278 arch = Triple::thumbeb;
279 }
280 return StringSwitch<Triple::ArchType>(ArchName.substr(offset))
281 .Cases("v2", "v2a", isThumb ? Triple::UnknownArch : arch)
282 .Cases("v3", "v3m", isThumb ? Triple::UnknownArch : arch)
283 .Cases("v4", "v4t", arch)
284 .Cases("v5", "v5e", "v5t", "v5te", "v5tej", arch)
Ismail Donmez5eb52b72015-05-05 09:29:43 +0000285 .Cases("v6", "v6hl", "v6j", "v6k", arch)
286 .Cases("v6m", "v6sm", arch)
Renato Golin609bf922014-11-17 14:08:57 +0000287 .Cases("v6t2", "v6z", "v6zk", arch)
Ismail Donmez5eb52b72015-05-05 09:29:43 +0000288 .Cases("v7", "v7a", "v7em", "v7hl", "v7l", arch)
Renato Golin609bf922014-11-17 14:08:57 +0000289 .Cases("v7m", "v7r", "v7s", arch)
290 .Cases("v8", "v8a", arch)
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000291 .Cases("v8.1", "v8.1a", arch)
Renato Golin609bf922014-11-17 14:08:57 +0000292 .Default(Triple::UnknownArch);
293}
294
Chandler Carruthaec97082012-02-21 08:53:32 +0000295static Triple::ArchType parseArch(StringRef ArchName) {
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000296 Triple::ArchType ARMArch(parseARMArch(ArchName));
297
Chandler Carruthaec97082012-02-21 08:53:32 +0000298 return StringSwitch<Triple::ArchType>(ArchName)
299 .Cases("i386", "i486", "i586", "i686", Triple::x86)
300 // FIXME: Do we need to support these?
301 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000302 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000303 .Case("powerpc", Triple::ppc)
304 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000305 .Case("powerpc64le", Triple::ppc64le)
Renato Golin609bf922014-11-17 14:08:57 +0000306 .Case("xscale", Triple::arm)
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000307 .Case("xscaleeb", Triple::armeb)
308 .StartsWith("arm", ARMArch)
309 .StartsWith("thumb", ARMArch)
310 .StartsWith("aarch64", ARMArch)
Chandler Carruthaec97082012-02-21 08:53:32 +0000311 .Case("msp430", Triple::msp430)
312 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000313 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000314 .Cases("mips64", "mips64eb", Triple::mips64)
315 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000316 .Case("r600", Triple::r600)
Tom Stellard07f11602015-01-06 18:00:00 +0000317 .Case("amdgcn", Triple::amdgcn)
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000318 .Case("bpf", Triple::bpf)
Chandler Carruthaec97082012-02-21 08:53:32 +0000319 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000320 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000321 .Case("sparc", Triple::sparc)
Douglas Katzmane0ff2822015-04-29 19:15:08 +0000322 .Case("sparcel", Triple::sparcel)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000323 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000324 .Case("tce", Triple::tce)
325 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000326 .Case("nvptx", Triple::nvptx)
327 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000328 .Case("le32", Triple::le32)
JF Bastien32972ef2014-09-12 17:54:17 +0000329 .Case("le64", Triple::le64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000330 .Case("amdil", Triple::amdil)
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000331 .Case("amdil64", Triple::amdil64)
332 .Case("hsail", Triple::hsail)
333 .Case("hsail64", Triple::hsail64)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000334 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000335 .Case("spir64", Triple::spir64)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000336 .StartsWith("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000337 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000338}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000339
Chandler Carruthaec97082012-02-21 08:53:32 +0000340static Triple::VendorType parseVendor(StringRef VendorName) {
341 return StringSwitch<Triple::VendorType>(VendorName)
342 .Case("apple", Triple::Apple)
343 .Case("pc", Triple::PC)
344 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000345 .Case("bgp", Triple::BGP)
346 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000347 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000348 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000349 .Case("img", Triple::ImaginationTechnologies)
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000350 .Case("mti", Triple::MipsTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000351 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000352 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000353 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000354}
355
Chandler Carruthaec97082012-02-21 08:53:32 +0000356static Triple::OSType parseOS(StringRef OSName) {
357 return StringSwitch<Triple::OSType>(OSName)
Ed Schoutendae71892015-03-09 18:40:45 +0000358 .StartsWith("cloudabi", Triple::CloudABI)
Chandler Carruthaec97082012-02-21 08:53:32 +0000359 .StartsWith("darwin", Triple::Darwin)
360 .StartsWith("dragonfly", Triple::DragonFly)
361 .StartsWith("freebsd", Triple::FreeBSD)
362 .StartsWith("ios", Triple::IOS)
363 .StartsWith("kfreebsd", Triple::KFreeBSD)
364 .StartsWith("linux", Triple::Linux)
365 .StartsWith("lv2", Triple::Lv2)
366 .StartsWith("macosx", Triple::MacOSX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000367 .StartsWith("netbsd", Triple::NetBSD)
368 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000369 .StartsWith("solaris", Triple::Solaris)
370 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000371 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000372 .StartsWith("haiku", Triple::Haiku)
373 .StartsWith("minix", Triple::Minix)
374 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000375 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000376 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000377 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000378 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000379 .StartsWith("cuda", Triple::CUDA)
380 .StartsWith("nvcl", Triple::NVCL)
Tom Stellard4082a6c2014-12-02 16:45:47 +0000381 .StartsWith("amdhsa", Triple::AMDHSA)
Alex Rosenberg38babd12015-01-25 22:46:59 +0000382 .StartsWith("ps4", Triple::PS4)
Chandler Carruthaec97082012-02-21 08:53:32 +0000383 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000384}
385
Chandler Carruthaec97082012-02-21 08:53:32 +0000386static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
387 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000388 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000389 .StartsWith("eabi", Triple::EABI)
390 .StartsWith("gnueabihf", Triple::GNUEABIHF)
391 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000392 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000393 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000394 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000395 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000396 .StartsWith("msvc", Triple::MSVC)
397 .StartsWith("itanium", Triple::Itanium)
398 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000399 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000400}
401
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000402static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
403 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
404 .EndsWith("coff", Triple::COFF)
405 .EndsWith("elf", Triple::ELF)
406 .EndsWith("macho", Triple::MachO)
407 .Default(Triple::UnknownObjectFormat);
408}
409
Renato Golinf5f373f2015-05-08 21:04:27 +0000410// FIXME: Use ARMTargetParser. This would require using Triple::ARMSubArch*
411// in ARMBuildAttrs and in ARCHNames' DefaultArch fields.
Renato Golinc17a07b2014-07-18 12:00:48 +0000412static Triple::SubArchType parseSubArch(StringRef SubArchName) {
Joerg Sonnenberger429edc12015-01-26 11:41:48 +0000413 if (SubArchName.endswith("eb"))
414 SubArchName = SubArchName.substr(0, SubArchName.size() - 2);
415
Renato Golinc17a07b2014-07-18 12:00:48 +0000416 return StringSwitch<Triple::SubArchType>(SubArchName)
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000417 .EndsWith("v8.1a", Triple::ARMSubArch_v8_1a)
Renato Golinc17a07b2014-07-18 12:00:48 +0000418 .EndsWith("v8", Triple::ARMSubArch_v8)
419 .EndsWith("v8a", Triple::ARMSubArch_v8)
420 .EndsWith("v7", Triple::ARMSubArch_v7)
421 .EndsWith("v7a", Triple::ARMSubArch_v7)
422 .EndsWith("v7em", Triple::ARMSubArch_v7em)
423 .EndsWith("v7l", Triple::ARMSubArch_v7)
424 .EndsWith("v7m", Triple::ARMSubArch_v7m)
425 .EndsWith("v7r", Triple::ARMSubArch_v7)
426 .EndsWith("v7s", Triple::ARMSubArch_v7s)
427 .EndsWith("v6", Triple::ARMSubArch_v6)
428 .EndsWith("v6m", Triple::ARMSubArch_v6m)
Bradley Smithe997b452015-02-10 15:15:08 +0000429 .EndsWith("v6sm", Triple::ARMSubArch_v6m)
Renato Golin12350602015-03-17 11:55:28 +0000430 .EndsWith("v6k", Triple::ARMSubArch_v6k)
Renato Golinc17a07b2014-07-18 12:00:48 +0000431 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
432 .EndsWith("v5", Triple::ARMSubArch_v5)
433 .EndsWith("v5e", Triple::ARMSubArch_v5)
434 .EndsWith("v5t", Triple::ARMSubArch_v5)
435 .EndsWith("v5te", Triple::ARMSubArch_v5te)
436 .EndsWith("v4t", Triple::ARMSubArch_v4t)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000437 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
438 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
439 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
Renato Golinc17a07b2014-07-18 12:00:48 +0000440 .Default(Triple::NoSubArch);
441}
442
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000443static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
444 switch (Kind) {
445 case Triple::UnknownObjectFormat: return "";
446 case Triple::COFF: return "coff";
447 case Triple::ELF: return "elf";
448 case Triple::MachO: return "macho";
449 }
450 llvm_unreachable("unknown object format type");
451}
452
453static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
Rafael Espindola24254892015-03-18 22:19:16 +0000454 switch (T.getArch()) {
455 default:
456 break;
457 case Triple::hexagon:
458 case Triple::mips:
459 case Triple::mipsel:
460 case Triple::mips64:
461 case Triple::mips64el:
462 case Triple::r600:
463 case Triple::amdgcn:
464 case Triple::sparc:
465 case Triple::sparcv9:
466 case Triple::systemz:
467 case Triple::xcore:
Rafael Espindola7f454402015-03-19 02:40:56 +0000468 case Triple::ppc64le:
469 return Triple::ELF;
470
471 case Triple::ppc:
472 case Triple::ppc64:
473 if (T.isOSDarwin())
474 return Triple::MachO;
Rafael Espindola24254892015-03-18 22:19:16 +0000475 return Triple::ELF;
476 }
477
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000478 if (T.isOSDarwin())
479 return Triple::MachO;
480 else if (T.isOSWindows())
481 return Triple::COFF;
482 return Triple::ELF;
483}
484
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000485/// \brief Construct a triple from the string representation provided.
486///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000487/// This stores the string representation and parses the various pieces into
488/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000489Triple::Triple(const Twine &Str)
490 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000491 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000492 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000493 Vendor(parseVendor(getVendorName())),
494 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000495 Environment(parseEnvironment(getEnvironmentName())),
496 ObjectFormat(parseFormat(getEnvironmentName())) {
497 if (ObjectFormat == Triple::UnknownObjectFormat)
498 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000499}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000500
501/// \brief Construct a triple from string representations of the architecture,
502/// vendor, and OS.
503///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000504/// This joins each argument into a canonical string representation and parses
505/// them into enum members. It leaves the environment unknown and omits it from
506/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000507Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
508 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000509 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000510 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000511 Vendor(parseVendor(VendorStr.str())),
512 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000513 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
514 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000515}
516
517/// \brief Construct a triple from string representations of the architecture,
518/// vendor, OS, and environment.
519///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000520/// This joins each argument into a canonical string representation and parses
521/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000522Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
523 const Twine &EnvironmentStr)
524 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
525 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000526 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000527 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000528 Vendor(parseVendor(VendorStr.str())),
529 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000530 Environment(parseEnvironment(EnvironmentStr.str())),
531 ObjectFormat(parseFormat(EnvironmentStr.str())) {
532 if (ObjectFormat == Triple::UnknownObjectFormat)
533 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000534}
535
Duncan Sands501dff72010-08-12 11:31:39 +0000536std::string Triple::normalize(StringRef Str) {
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000537 bool IsMinGW32 = false;
538 bool IsCygwin = false;
539
Duncan Sands501dff72010-08-12 11:31:39 +0000540 // Parse into components.
541 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000542 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000543
544 // If the first component corresponds to a known architecture, preferentially
545 // use it for the architecture. If the second component corresponds to a
546 // known vendor, preferentially use it for the vendor, etc. This avoids silly
547 // component movement when a component parses as (eg) both a valid arch and a
548 // valid os.
549 ArchType Arch = UnknownArch;
550 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000551 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000552 VendorType Vendor = UnknownVendor;
553 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000554 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000555 OSType OS = UnknownOS;
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000556 if (Components.size() > 2) {
Chandler Carruthaec97082012-02-21 08:53:32 +0000557 OS = parseOS(Components[2]);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000558 IsCygwin = Components[2].startswith("cygwin");
559 IsMinGW32 = Components[2].startswith("mingw");
560 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000561 EnvironmentType Environment = UnknownEnvironment;
562 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000563 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000564 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000565 if (Components.size() > 4)
566 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000567
568 // Note which components are already in their final position. These will not
569 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000570 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000571 Found[0] = Arch != UnknownArch;
572 Found[1] = Vendor != UnknownVendor;
573 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000574 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000575
576 // If they are not there already, permute the components into their canonical
577 // positions by seeing if they parse as a valid architecture, and if so moving
578 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000579 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000580 if (Found[Pos])
581 continue; // Already in the canonical position.
582
583 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
584 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000585 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000586 continue;
587
588 // Does this component parse as valid for the target position?
589 bool Valid = false;
590 StringRef Comp = Components[Idx];
591 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000592 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000593 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000594 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000595 Valid = Arch != UnknownArch;
596 break;
597 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000598 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000599 Valid = Vendor != UnknownVendor;
600 break;
601 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000602 OS = parseOS(Comp);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000603 IsCygwin = Comp.startswith("cygwin");
604 IsMinGW32 = Comp.startswith("mingw");
605 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
Duncan Sands501dff72010-08-12 11:31:39 +0000606 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000607 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000608 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000609 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000610 if (!Valid) {
611 ObjectFormat = parseFormat(Comp);
612 Valid = ObjectFormat != UnknownObjectFormat;
613 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000614 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000615 }
616 if (!Valid)
617 continue; // Nope, try the next component.
618
619 // Move the component to the target position, pushing any non-fixed
620 // components that are in the way to the right. This tends to give
621 // good results in the common cases of a forgotten vendor component
622 // or a wrongly positioned environment.
623 if (Pos < Idx) {
624 // Insert left, pushing the existing components to the right. For
625 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
626 StringRef CurrentComponent(""); // The empty component.
627 // Replace the component we are moving with an empty component.
628 std::swap(CurrentComponent, Components[Idx]);
629 // Insert the component being moved at Pos, displacing any existing
630 // components to the right.
631 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
632 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000633 while (i < array_lengthof(Found) && Found[i])
634 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000635 // Place the component at the new position, getting the component
636 // that was at this position - it will be moved right.
637 std::swap(CurrentComponent, Components[i]);
638 }
639 } else if (Pos > Idx) {
640 // Push right by inserting empty components until the component at Idx
641 // reaches the target position Pos. For example, pc-a -> -pc-a when
642 // moving pc to the second position.
643 do {
644 // Insert one empty component at Idx.
645 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000646 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000647 // Place the component at the new position, getting the component
648 // that was at this position - it will be moved right.
649 std::swap(CurrentComponent, Components[i]);
650 // If it was placed on top of an empty component then we are done.
651 if (CurrentComponent.empty())
652 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000653 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000654 while (++i < array_lengthof(Found) && Found[i])
655 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000656 }
657 // The last component was pushed off the end - append it.
658 if (!CurrentComponent.empty())
659 Components.push_back(CurrentComponent);
660
661 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000662 while (++Idx < array_lengthof(Found) && Found[Idx])
663 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000664 } while (Idx < Pos); // Add more until the final position is reached.
665 }
666 assert(Pos < Components.size() && Components[Pos] == Comp &&
667 "Component moved wrong!");
668 Found[Pos] = true;
669 break;
670 }
671 }
672
673 // Special case logic goes here. At this point Arch, Vendor and OS have the
674 // correct values for the computed components.
675
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000676 if (OS == Triple::Win32) {
677 Components.resize(4);
678 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000679 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000680 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000681 Components[3] = "msvc";
682 else
683 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000684 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000685 } else if (IsMinGW32) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000686 Components.resize(4);
687 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000688 Components[3] = "gnu";
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000689 } else if (IsCygwin) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000690 Components.resize(4);
691 Components[2] = "windows";
692 Components[3] = "cygnus";
693 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000694 if (IsMinGW32 || IsCygwin ||
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000695 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
696 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
697 Components.resize(5);
698 Components[4] = getObjectFormatTypeName(ObjectFormat);
699 }
700 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000701
Duncan Sands501dff72010-08-12 11:31:39 +0000702 // Stick the corrected components back together to form the normalized string.
703 std::string Normalized;
704 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
705 if (i) Normalized += '-';
706 Normalized += Components[i];
707 }
708 return Normalized;
709}
710
Daniel Dunbar19e70762009-07-26 03:31:47 +0000711StringRef Triple::getArchName() const {
712 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000713}
714
Daniel Dunbar19e70762009-07-26 03:31:47 +0000715StringRef Triple::getVendorName() const {
716 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
717 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000718}
719
Daniel Dunbar19e70762009-07-26 03:31:47 +0000720StringRef Triple::getOSName() const {
721 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
722 Tmp = Tmp.split('-').second; // Strip second component
723 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000724}
725
Daniel Dunbar19e70762009-07-26 03:31:47 +0000726StringRef Triple::getEnvironmentName() const {
727 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
728 Tmp = Tmp.split('-').second; // Strip second component
729 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000730}
731
Daniel Dunbar19e70762009-07-26 03:31:47 +0000732StringRef Triple::getOSAndEnvironmentName() const {
733 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
734 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000735}
736
Chris Lattner553c9f32009-08-12 06:19:40 +0000737static unsigned EatNumber(StringRef &Str) {
738 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000739 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000740
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000741 do {
742 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000743 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000744
Chris Lattner553c9f32009-08-12 06:19:40 +0000745 // Eat the digit.
746 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000747 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000748
Chris Lattner553c9f32009-08-12 06:19:40 +0000749 return Result;
750}
751
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000752void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
753 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000754 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000755
Dan Albert675cffc2015-03-03 18:23:51 +0000756 // For Android, we care about the Android version rather than the Linux
757 // version.
758 if (getEnvironment() == Android) {
759 OSName = getEnvironmentName().substr(strlen("android"));
760 if (OSName.startswith("eabi"))
761 OSName = OSName.substr(strlen("eabi"));
762 }
763
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000764 // Assume that the OS portion of the triple starts with the canonical name.
765 StringRef OSTypeName = getOSTypeName(getOS());
766 if (OSName.startswith(OSTypeName))
767 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000768
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000769 // Any unset version defaults to 0.
770 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000771
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000772 // Parse up to three components.
773 unsigned *Components[3] = { &Major, &Minor, &Micro };
774 for (unsigned i = 0; i != 3; ++i) {
775 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
776 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000777
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000778 // Consume the leading number.
779 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000780
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000781 // Consume the separator, if present.
782 if (OSName.startswith("."))
783 OSName = OSName.substr(1);
784 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000785}
786
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000787bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
788 unsigned &Micro) const {
789 getOSVersion(Major, Minor, Micro);
790
791 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000792 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000793 case Darwin:
794 // Default to darwin8, i.e., MacOSX 10.4.
795 if (Major == 0)
796 Major = 8;
797 // Darwin version numbers are skewed from OS X versions.
798 if (Major < 4)
799 return false;
800 Micro = 0;
801 Minor = Major - 4;
802 Major = 10;
803 break;
804 case MacOSX:
805 // Default to 10.4.
806 if (Major == 0) {
807 Major = 10;
808 Minor = 4;
809 }
810 if (Major != 10)
811 return false;
812 break;
813 case IOS:
814 // Ignore the version from the triple. This is only handled because the
815 // the clang driver combines OS X and IOS support into a common Darwin
816 // toolchain that wants to know the OS X version number even when targeting
817 // IOS.
818 Major = 10;
819 Minor = 4;
820 Micro = 0;
821 break;
822 }
823 return true;
824}
825
Chad Rosierd84eaac2012-05-09 17:23:48 +0000826void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
827 unsigned &Micro) const {
828 switch (getOS()) {
829 default: llvm_unreachable("unexpected OS for Darwin triple");
830 case Darwin:
831 case MacOSX:
832 // Ignore the version from the triple. This is only handled because the
833 // the clang driver combines OS X and IOS support into a common Darwin
834 // toolchain that wants to know the iOS version number even when targeting
835 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000836 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000837 Minor = 0;
838 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000839 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000840 case IOS:
841 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000842 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000843 if (Major == 0)
Tim Northovere19bed72014-07-23 12:32:47 +0000844 Major = (getArch() == aarch64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000845 break;
846 }
847}
848
Daniel Dunbar19e70762009-07-26 03:31:47 +0000849void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000850 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000851}
852
853void Triple::setArch(ArchType Kind) {
854 setArchName(getArchTypeName(Kind));
855}
856
857void Triple::setVendor(VendorType Kind) {
858 setVendorName(getVendorTypeName(Kind));
859}
860
861void Triple::setOS(OSType Kind) {
862 setOSName(getOSTypeName(Kind));
863}
864
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000865void Triple::setEnvironment(EnvironmentType Kind) {
Reid Kleckner5fe405d2015-02-13 22:05:50 +0000866 if (ObjectFormat == getDefaultFormat(*this))
867 return setEnvironmentName(getEnvironmentTypeName(Kind));
868
869 setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
870 getObjectFormatTypeName(ObjectFormat)).str());
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000871}
872
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000873void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000874 if (Environment == UnknownEnvironment)
875 return setEnvironmentName(getObjectFormatTypeName(Kind));
876
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000877 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
878 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000879}
880
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000881void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000882 // Work around a miscompilation bug for Twines in gcc 4.0.3.
883 SmallString<64> Triple;
884 Triple += Str;
885 Triple += "-";
886 Triple += getVendorName();
887 Triple += "-";
888 Triple += getOSAndEnvironmentName();
Yaron Keren92e1b622015-03-18 10:17:07 +0000889 setTriple(Triple);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000890}
891
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000892void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000893 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
894}
895
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000896void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000897 if (hasEnvironment())
898 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
899 "-" + getEnvironmentName());
900 else
901 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
902}
903
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000904void Triple::setEnvironmentName(StringRef Str) {
905 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000906 "-" + Str);
907}
908
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000909void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000910 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
911}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000912
913static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
914 switch (Arch) {
915 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000916 return 0;
917
918 case llvm::Triple::msp430:
919 return 16;
920
Chandler Carruthb90c1022012-01-31 04:52:32 +0000921 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000922 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000923 case llvm::Triple::hexagon:
924 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000925 case llvm::Triple::mips:
926 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000927 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000928 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000929 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000930 case llvm::Triple::sparc:
Douglas Katzmane0ff2822015-04-29 19:15:08 +0000931 case llvm::Triple::sparcel:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000932 case llvm::Triple::tce:
933 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000934 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000935 case llvm::Triple::x86:
936 case llvm::Triple::xcore:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000937 case llvm::Triple::amdil:
938 case llvm::Triple::hsail:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000939 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000940 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000941 return 32;
942
Tim Northovere0e3aef2013-01-31 12:12:40 +0000943 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000944 case llvm::Triple::aarch64_be:
Tom Stellard07f11602015-01-06 18:00:00 +0000945 case llvm::Triple::amdgcn:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000946 case llvm::Triple::bpf:
JF Bastien32972ef2014-09-12 17:54:17 +0000947 case llvm::Triple::le64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000948 case llvm::Triple::mips64:
949 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000950 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000951 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000952 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000953 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000954 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000955 case llvm::Triple::x86_64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000956 case llvm::Triple::amdil64:
957 case llvm::Triple::hsail64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000958 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000959 return 64;
960 }
961 llvm_unreachable("Invalid architecture value");
962}
963
964bool Triple::isArch64Bit() const {
965 return getArchPointerBitWidth(getArch()) == 64;
966}
967
968bool Triple::isArch32Bit() const {
969 return getArchPointerBitWidth(getArch()) == 32;
970}
971
972bool Triple::isArch16Bit() const {
973 return getArchPointerBitWidth(getArch()) == 16;
974}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000975
976Triple Triple::get32BitArchVariant() const {
977 Triple T(*this);
978 switch (getArch()) {
979 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000980 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000981 case Triple::aarch64_be:
Tom Stellard07f11602015-01-06 18:00:00 +0000982 case Triple::amdgcn:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000983 case Triple::bpf:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000984 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000985 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000986 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000987 T.setArch(UnknownArch);
988 break;
989
990 case Triple::amdil:
Matt Arsenault3f9b0212014-09-19 19:52:11 +0000991 case Triple::hsail:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000992 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000993 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000994 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000995 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000996 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000997 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000998 case Triple::mips:
999 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001000 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001001 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +00001002 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001003 case Triple::sparc:
Douglas Katzmane0ff2822015-04-29 19:15:08 +00001004 case Triple::sparcel:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001005 case Triple::tce:
1006 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +00001007 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001008 case Triple::x86:
1009 case Triple::xcore:
1010 // Already 32-bit.
1011 break;
1012
JF Bastien32972ef2014-09-12 17:54:17 +00001013 case Triple::le64: T.setArch(Triple::le32); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001014 case Triple::mips64: T.setArch(Triple::mips); break;
1015 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001016 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +00001017 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001018 case Triple::sparcv9: T.setArch(Triple::sparc); break;
1019 case Triple::x86_64: T.setArch(Triple::x86); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001020 case Triple::amdil64: T.setArch(Triple::amdil); break;
1021 case Triple::hsail64: T.setArch(Triple::hsail); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +00001022 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001023 }
1024 return T;
1025}
1026
1027Triple Triple::get64BitArchVariant() const {
1028 Triple T(*this);
1029 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001030 case Triple::UnknownArch:
Tim Northoveraf6bfb22014-03-30 07:25:23 +00001031 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +00001032 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001033 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +00001034 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001035 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +00001036 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001037 case Triple::tce:
1038 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +00001039 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001040 case Triple::xcore:
Douglas Katzmane0ff2822015-04-29 19:15:08 +00001041 case Triple::sparcel:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001042 T.setArch(UnknownArch);
1043 break;
1044
Tim Northovere0e3aef2013-01-31 12:12:40 +00001045 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +00001046 case Triple::aarch64_be:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +00001047 case Triple::bpf:
JF Bastien32972ef2014-09-12 17:54:17 +00001048 case Triple::le64:
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001049 case Triple::amdil64:
Tom Stellard07f11602015-01-06 18:00:00 +00001050 case Triple::amdgcn:
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001051 case Triple::hsail64:
1052 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001053 case Triple::mips64:
1054 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001055 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001056 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001057 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001058 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +00001059 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001060 case Triple::x86_64:
1061 // Already 64-bit.
1062 break;
1063
JF Bastien32972ef2014-09-12 17:54:17 +00001064 case Triple::le32: T.setArch(Triple::le64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001065 case Triple::mips: T.setArch(Triple::mips64); break;
1066 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001067 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001068 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001069 case Triple::sparc: T.setArch(Triple::sparcv9); break;
1070 case Triple::x86: T.setArch(Triple::x86_64); break;
Matt Arsenault3f9b0212014-09-19 19:52:11 +00001071 case Triple::amdil: T.setArch(Triple::amdil64); break;
1072 case Triple::hsail: T.setArch(Triple::hsail64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +00001073 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +00001074 }
1075 return T;
1076}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001077
Renato Golinf5f373f2015-05-08 21:04:27 +00001078// FIXME: Use ARMTargetParser. This would require ARCHNames to hold
1079// specific CPU names, as well as default CPU arch.
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001080const char *Triple::getARMCPUForArch(StringRef MArch) const {
1081 if (MArch.empty())
1082 MArch = getArchName();
1083
Renato Goline8048f02015-05-20 15:05:07 +00001084 // Some defaults are forced.
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001085 switch (getOS()) {
Ed Masteea0257a2014-09-09 17:47:24 +00001086 case llvm::Triple::FreeBSD:
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001087 case llvm::Triple::NetBSD:
Renato Goline8048f02015-05-20 15:05:07 +00001088 // FIXME: This doesn't work on BE/thumb variants.
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001089 if (MArch == "armv6")
1090 return "arm1176jzf-s";
1091 break;
1092 case llvm::Triple::Win32:
1093 // FIXME: this is invalid for WindowsCE
1094 return "cortex-a9";
1095 default:
1096 break;
1097 }
1098
Renato Goline8048f02015-05-20 15:05:07 +00001099 MArch = ARMTargetParser::getCanonicalArchName(MArch);
1100 if (MArch.empty())
1101 return nullptr;
John Brawn50ed9472015-05-08 12:52:02 +00001102
Renato Goline8048f02015-05-20 15:05:07 +00001103 const char *CPU = ARMTargetParser::getDefaultCPU(MArch);
1104 if (CPU)
1105 return CPU;
1106
1107 // If no specific architecture version is requested, return the minimum CPU
1108 // required by the OS and environment.
1109 switch (getOS()) {
1110 case llvm::Triple::NetBSD:
1111 switch (getEnvironment()) {
1112 case llvm::Triple::GNUEABIHF:
1113 case llvm::Triple::GNUEABI:
1114 case llvm::Triple::EABIHF:
1115 case llvm::Triple::EABI:
1116 return "arm926ej-s";
John Brawn50ed9472015-05-08 12:52:02 +00001117 default:
Renato Goline8048f02015-05-20 15:05:07 +00001118 return "strongarm";
John Brawn50ed9472015-05-08 12:52:02 +00001119 }
Renato Goline8048f02015-05-20 15:05:07 +00001120 case llvm::Triple::NaCl:
1121 return "cortex-a8";
1122 default:
1123 switch (getEnvironment()) {
1124 case llvm::Triple::EABIHF:
1125 case llvm::Triple::GNUEABIHF:
1126 return "arm1176jzf-s";
1127 default:
1128 return "arm7tdmi";
1129 }
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001130 }
John Brawn50ed9472015-05-08 12:52:02 +00001131
Renato Goline8048f02015-05-20 15:05:07 +00001132 llvm_unreachable("invalid arch name");
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +00001133}