blob: 2fff70c05edcc9184a875657c0cfb1c34efe7816 [file] [log] [blame]
Daniel Dunbar4abd5662009-04-01 21:53:23 +00001//===--- Triple.cpp - Target triple helper class --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskine2595b52009-10-06 21:45:26 +000012#include "llvm/ADT/SmallString.h"
Chandler Carruthff6f3562012-02-12 09:27:38 +000013#include "llvm/ADT/StringSwitch.h"
David Blaikie46a9f012012-01-20 21:51:11 +000014#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov12062ba2009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar4abd5662009-04-01 21:53:23 +000016using namespace llvm;
17
Daniel Dunbar4abd5662009-04-01 21:53:23 +000018const char *Triple::getArchTypeName(ArchType Kind) {
19 switch (Kind) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +000020 case UnknownArch: return "unknown";
Jim Grosbachf638b262010-12-17 02:10:59 +000021
Christian Pirker6c2f4d42014-02-24 11:34:50 +000022 case aarch64: return "aarch64";
23 case aarch64_be: return "aarch64_be";
24 case arm: return "arm";
Christian Pirker2a111602014-03-28 14:35:30 +000025 case armeb: return "armeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000026 case hexagon: return "hexagon";
27 case mips: return "mips";
28 case mipsel: return "mipsel";
29 case mips64: return "mips64";
30 case mips64el: return "mips64el";
31 case msp430: return "msp430";
32 case ppc64: return "powerpc64";
33 case ppc64le: return "powerpc64le";
34 case ppc: return "powerpc";
35 case r600: return "r600";
36 case sparc: return "sparc";
37 case sparcv9: return "sparcv9";
38 case systemz: return "s390x";
39 case tce: return "tce";
40 case thumb: return "thumb";
Christian Pirker2a111602014-03-28 14:35:30 +000041 case thumbeb: return "thumbeb";
Christian Pirker6c2f4d42014-02-24 11:34:50 +000042 case x86: return "i386";
43 case x86_64: return "x86_64";
44 case xcore: return "xcore";
45 case nvptx: return "nvptx";
46 case nvptx64: return "nvptx64";
47 case le32: return "le32";
48 case amdil: return "amdil";
49 case spir: return "spir";
50 case spir64: return "spir64";
Eric Christopher54fe1b22014-07-10 17:26:54 +000051 case kalimba: return "kalimba";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000052 }
53
David Blaikie46a9f012012-01-20 21:51:11 +000054 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000055}
56
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000057const char *Triple::getArchTypePrefix(ArchType Kind) {
58 switch (Kind) {
59 default:
Craig Topperc10719f2014-04-07 04:17:22 +000060 return nullptr;
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000061
Christian Pirker6c2f4d42014-02-24 11:34:50 +000062 case aarch64:
63 case aarch64_be: return "aarch64";
Tim Northovere0e3aef2013-01-31 12:12:40 +000064
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000065 case arm:
Christian Pirker2a111602014-03-28 14:35:30 +000066 case armeb:
67 case thumb:
68 case thumbeb: return "arm";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000069
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000070 case ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +000071 case ppc64le:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000072 case ppc: return "ppc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000073
Benjamin Kramerae3c3002012-06-28 19:09:53 +000074 case mips:
75 case mipsel:
76 case mips64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000077 case mips64el: return "mips";
Benjamin Kramerae3c3002012-06-28 19:09:53 +000078
Christian Pirker6c2f4d42014-02-24 11:34:50 +000079 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080
Christian Pirker6c2f4d42014-02-24 11:34:50 +000081 case r600: return "r600";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000082
Chris Lattner8228b112010-02-04 06:34:01 +000083 case sparcv9:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000084 case sparc: return "sparc";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000085
Christian Pirker6c2f4d42014-02-24 11:34:50 +000086 case systemz: return "systemz";
Richard Sandiforda238c5e2013-05-03 11:05:17 +000087
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000088 case x86:
Christian Pirker6c2f4d42014-02-24 11:34:50 +000089 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000090
Christian Pirker6c2f4d42014-02-24 11:34:50 +000091 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000092
Christian Pirker6c2f4d42014-02-24 11:34:50 +000093 case nvptx: return "nvptx";
94 case nvptx64: return "nvptx";
Tim Northover00ed9962014-03-29 10:18:08 +000095
Christian Pirker6c2f4d42014-02-24 11:34:50 +000096 case le32: return "le32";
97 case amdil: return "amdil";
98 case spir: return "spir";
99 case spir64: return "spir";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000100 case kalimba: return "kalimba";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +0000101 }
102}
103
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000104const char *Triple::getVendorTypeName(VendorType Kind) {
105 switch (Kind) {
106 case UnknownVendor: return "unknown";
107
108 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +0000109 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +0000110 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +0000111 case BGP: return "bgp";
112 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +0000113 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000114 case IBM: return "ibm";
Daniel Sandersc5626f42014-07-09 16:03:10 +0000115 case ImaginationTechnologies: return "img";
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000116 case MipsTechnologies: return "mti";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000117 case NVIDIA: return "nvidia";
Eric Christopher54fe1b22014-07-10 17:26:54 +0000118 case CSR: return "csr";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000119 }
120
David Blaikie46a9f012012-01-20 21:51:11 +0000121 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000122}
123
124const char *Triple::getOSTypeName(OSType Kind) {
125 switch (Kind) {
126 case UnknownOS: return "unknown";
127
128 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000129 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000130 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000131 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000132 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000133 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000134 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000135 case MacOSX: return "macosx";
Chris Lattner01218d52009-07-13 20:22:23 +0000136 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000137 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000138 case Solaris: return "solaris";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000139 case Win32: return "windows";
Chris Lattner27f20492009-10-16 02:06:30 +0000140 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000141 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000142 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000143 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000144 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000145 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000146 case AIX: return "aix";
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000147 case CUDA: return "cuda";
148 case NVCL: return "nvcl";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000149 }
150
David Blaikie46a9f012012-01-20 21:51:11 +0000151 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000152}
153
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000154const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
155 switch (Kind) {
156 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000157 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000158 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000159 case GNUEABI: return "gnueabi";
Eli Bendersky0893e102013-01-22 18:02:49 +0000160 case GNUX32: return "gnux32";
David Woodhouse71d15ed2014-01-20 12:02:25 +0000161 case CODE16: return "code16";
Renato Golin83758d5c2011-01-21 18:25:47 +0000162 case EABI: return "eabi";
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000163 case EABIHF: return "eabihf";
Logan Chien9ab55b82012-09-02 09:29:46 +0000164 case Android: return "android";
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000165 case MSVC: return "msvc";
166 case Itanium: return "itanium";
167 case Cygnus: return "cygnus";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000168 }
169
David Blaikie46a9f012012-01-20 21:51:11 +0000170 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000171}
172
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000173Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000174 return StringSwitch<Triple::ArchType>(Name)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000175 .Case("aarch64", aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000176 .Case("aarch64_be", aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000177 .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
Chandler Carruthff6f3562012-02-12 09:27:38 +0000178 .Case("arm", arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000179 .Case("armeb", armeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000180 .Case("mips", mips)
181 .Case("mipsel", mipsel)
182 .Case("mips64", mips64)
183 .Case("mips64el", mips64el)
184 .Case("msp430", msp430)
185 .Case("ppc64", ppc64)
186 .Case("ppc32", ppc)
187 .Case("ppc", ppc)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000188 .Case("ppc64le", ppc64le)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000189 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000190 .Case("hexagon", hexagon)
191 .Case("sparc", sparc)
192 .Case("sparcv9", sparcv9)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000193 .Case("systemz", systemz)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000194 .Case("tce", tce)
195 .Case("thumb", thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000196 .Case("thumbeb", thumbeb)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000197 .Case("x86", x86)
198 .Case("x86-64", x86_64)
199 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000200 .Case("nvptx", nvptx)
201 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000202 .Case("le32", le32)
203 .Case("amdil", amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000204 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000205 .Case("spir64", spir64)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000206 .Case("kalimba", kalimba)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000207 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000208}
209
Chandler Carruthaec97082012-02-21 08:53:32 +0000210static Triple::ArchType parseArch(StringRef ArchName) {
211 return StringSwitch<Triple::ArchType>(ArchName)
212 .Cases("i386", "i486", "i586", "i686", Triple::x86)
213 // FIXME: Do we need to support these?
214 .Cases("i786", "i886", "i986", Triple::x86)
Jim Grosbach664d1482013-11-16 00:52:57 +0000215 .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000216 .Case("powerpc", Triple::ppc)
217 .Cases("powerpc64", "ppu", Triple::ppc64)
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000218 .Case("powerpc64le", Triple::ppc64le)
Tim Northovere0e3aef2013-01-31 12:12:40 +0000219 .Case("aarch64", Triple::aarch64)
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000220 .Case("aarch64_be", Triple::aarch64_be)
Tim Northovere19bed72014-07-23 12:32:47 +0000221 .Case("arm64", Triple::aarch64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000222 .Cases("arm", "xscale", Triple::arm)
Chandler Carruthb54950b2012-02-18 04:34:17 +0000223 // FIXME: It would be good to replace these with explicit names for all the
224 // various suffixes supported.
Chandler Carruthaec97082012-02-21 08:53:32 +0000225 .StartsWith("armv", Triple::arm)
Christian Pirker2a111602014-03-28 14:35:30 +0000226 .Case("armeb", Triple::armeb)
227 .StartsWith("armebv", Triple::armeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000228 .Case("thumb", Triple::thumb)
229 .StartsWith("thumbv", Triple::thumb)
Christian Pirker2a111602014-03-28 14:35:30 +0000230 .Case("thumbeb", Triple::thumbeb)
231 .StartsWith("thumbebv", Triple::thumbeb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000232 .Case("msp430", Triple::msp430)
233 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000234 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000235 .Cases("mips64", "mips64eb", Triple::mips64)
236 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000237 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000238 .Case("hexagon", Triple::hexagon)
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000239 .Case("s390x", Triple::systemz)
Chandler Carruthaec97082012-02-21 08:53:32 +0000240 .Case("sparc", Triple::sparc)
Jakob Stoklund Olesenabc3d232013-05-14 17:47:27 +0000241 .Cases("sparcv9", "sparc64", Triple::sparcv9)
Chandler Carruthaec97082012-02-21 08:53:32 +0000242 .Case("tce", Triple::tce)
243 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000244 .Case("nvptx", Triple::nvptx)
245 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000246 .Case("le32", Triple::le32)
247 .Case("amdil", Triple::amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000248 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000249 .Case("spir64", Triple::spir64)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000250 .StartsWith("kalimba", Triple::kalimba)
Chandler Carruthaec97082012-02-21 08:53:32 +0000251 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000252}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000253
Chandler Carruthaec97082012-02-21 08:53:32 +0000254static Triple::VendorType parseVendor(StringRef VendorName) {
255 return StringSwitch<Triple::VendorType>(VendorName)
256 .Case("apple", Triple::Apple)
257 .Case("pc", Triple::PC)
258 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000259 .Case("bgp", Triple::BGP)
260 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000261 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000262 .Case("ibm", Triple::IBM)
Daniel Sandersc5626f42014-07-09 16:03:10 +0000263 .Case("img", Triple::ImaginationTechnologies)
Daniel Sandersdc6a9412014-07-18 14:28:19 +0000264 .Case("mti", Triple::MipsTechnologies)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000265 .Case("nvidia", Triple::NVIDIA)
Eric Christopher54fe1b22014-07-10 17:26:54 +0000266 .Case("csr", Triple::CSR)
Chandler Carruthaec97082012-02-21 08:53:32 +0000267 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000268}
269
Chandler Carruthaec97082012-02-21 08:53:32 +0000270static Triple::OSType parseOS(StringRef OSName) {
271 return StringSwitch<Triple::OSType>(OSName)
Chandler Carruthaec97082012-02-21 08:53:32 +0000272 .StartsWith("darwin", Triple::Darwin)
273 .StartsWith("dragonfly", Triple::DragonFly)
274 .StartsWith("freebsd", Triple::FreeBSD)
275 .StartsWith("ios", Triple::IOS)
276 .StartsWith("kfreebsd", Triple::KFreeBSD)
277 .StartsWith("linux", Triple::Linux)
278 .StartsWith("lv2", Triple::Lv2)
279 .StartsWith("macosx", Triple::MacOSX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000280 .StartsWith("netbsd", Triple::NetBSD)
281 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000282 .StartsWith("solaris", Triple::Solaris)
283 .StartsWith("win32", Triple::Win32)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000284 .StartsWith("windows", Triple::Win32)
Chandler Carruthaec97082012-02-21 08:53:32 +0000285 .StartsWith("haiku", Triple::Haiku)
286 .StartsWith("minix", Triple::Minix)
287 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000288 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000289 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000290 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000291 .StartsWith("aix", Triple::AIX)
Justin Holewinskib6e6cd32013-06-21 18:51:49 +0000292 .StartsWith("cuda", Triple::CUDA)
293 .StartsWith("nvcl", Triple::NVCL)
Chandler Carruthaec97082012-02-21 08:53:32 +0000294 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000295}
296
Chandler Carruthaec97082012-02-21 08:53:32 +0000297static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
298 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
Joerg Sonnenberger8fe41b72013-12-16 18:51:28 +0000299 .StartsWith("eabihf", Triple::EABIHF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000300 .StartsWith("eabi", Triple::EABI)
301 .StartsWith("gnueabihf", Triple::GNUEABIHF)
302 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky0893e102013-01-22 18:02:49 +0000303 .StartsWith("gnux32", Triple::GNUX32)
David Woodhouse71d15ed2014-01-20 12:02:25 +0000304 .StartsWith("code16", Triple::CODE16)
Chandler Carruthaec97082012-02-21 08:53:32 +0000305 .StartsWith("gnu", Triple::GNU)
Logan Chien9ab55b82012-09-02 09:29:46 +0000306 .StartsWith("android", Triple::Android)
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000307 .StartsWith("msvc", Triple::MSVC)
308 .StartsWith("itanium", Triple::Itanium)
309 .StartsWith("cygnus", Triple::Cygnus)
Chandler Carruthaec97082012-02-21 08:53:32 +0000310 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000311}
312
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000313static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
314 return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
315 .EndsWith("coff", Triple::COFF)
316 .EndsWith("elf", Triple::ELF)
317 .EndsWith("macho", Triple::MachO)
318 .Default(Triple::UnknownObjectFormat);
319}
320
Renato Golinc17a07b2014-07-18 12:00:48 +0000321static Triple::SubArchType parseSubArch(StringRef SubArchName) {
322 return StringSwitch<Triple::SubArchType>(SubArchName)
323 .EndsWith("v8", Triple::ARMSubArch_v8)
324 .EndsWith("v8a", Triple::ARMSubArch_v8)
325 .EndsWith("v7", Triple::ARMSubArch_v7)
326 .EndsWith("v7a", Triple::ARMSubArch_v7)
327 .EndsWith("v7em", Triple::ARMSubArch_v7em)
328 .EndsWith("v7l", Triple::ARMSubArch_v7)
329 .EndsWith("v7m", Triple::ARMSubArch_v7m)
330 .EndsWith("v7r", Triple::ARMSubArch_v7)
331 .EndsWith("v7s", Triple::ARMSubArch_v7s)
332 .EndsWith("v6", Triple::ARMSubArch_v6)
333 .EndsWith("v6m", Triple::ARMSubArch_v6m)
334 .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
335 .EndsWith("v5", Triple::ARMSubArch_v5)
336 .EndsWith("v5e", Triple::ARMSubArch_v5)
337 .EndsWith("v5t", Triple::ARMSubArch_v5)
338 .EndsWith("v5te", Triple::ARMSubArch_v5te)
339 .EndsWith("v4t", Triple::ARMSubArch_v4t)
Matthew Gardiner3b15a892014-09-05 06:46:43 +0000340
341 .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
342 .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
343 .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
344
Renato Golinc17a07b2014-07-18 12:00:48 +0000345 .Default(Triple::NoSubArch);
346}
347
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000348static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
349 switch (Kind) {
350 case Triple::UnknownObjectFormat: return "";
351 case Triple::COFF: return "coff";
352 case Triple::ELF: return "elf";
353 case Triple::MachO: return "macho";
354 }
355 llvm_unreachable("unknown object format type");
356}
357
358static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
359 if (T.isOSDarwin())
360 return Triple::MachO;
361 else if (T.isOSWindows())
362 return Triple::COFF;
363 return Triple::ELF;
364}
365
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000366/// \brief Construct a triple from the string representation provided.
367///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000368/// This stores the string representation and parses the various pieces into
369/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000370Triple::Triple(const Twine &Str)
371 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000372 Arch(parseArch(getArchName())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000373 SubArch(parseSubArch(getArchName())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000374 Vendor(parseVendor(getVendorName())),
375 OS(parseOS(getOSName())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000376 Environment(parseEnvironment(getEnvironmentName())),
377 ObjectFormat(parseFormat(getEnvironmentName())) {
378 if (ObjectFormat == Triple::UnknownObjectFormat)
379 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000380}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000381
382/// \brief Construct a triple from string representations of the architecture,
383/// vendor, and OS.
384///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000385/// This joins each argument into a canonical string representation and parses
386/// them into enum members. It leaves the environment unknown and omits it from
387/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000388Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
389 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000390 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000391 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000392 Vendor(parseVendor(VendorStr.str())),
393 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000394 Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
395 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000396}
397
398/// \brief Construct a triple from string representations of the architecture,
399/// vendor, OS, and environment.
400///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000401/// This joins each argument into a canonical string representation and parses
402/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000403Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
404 const Twine &EnvironmentStr)
405 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
406 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000407 Arch(parseArch(ArchStr.str())),
Renato Golinc17a07b2014-07-18 12:00:48 +0000408 SubArch(parseSubArch(ArchStr.str())),
Chandler Carruthaec97082012-02-21 08:53:32 +0000409 Vendor(parseVendor(VendorStr.str())),
410 OS(parseOS(OSStr.str())),
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000411 Environment(parseEnvironment(EnvironmentStr.str())),
412 ObjectFormat(parseFormat(EnvironmentStr.str())) {
413 if (ObjectFormat == Triple::UnknownObjectFormat)
414 ObjectFormat = getDefaultFormat(*this);
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000415}
416
Duncan Sands501dff72010-08-12 11:31:39 +0000417std::string Triple::normalize(StringRef Str) {
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000418 bool IsMinGW32 = false;
419 bool IsCygwin = false;
420
Duncan Sands501dff72010-08-12 11:31:39 +0000421 // Parse into components.
422 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000423 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000424
425 // If the first component corresponds to a known architecture, preferentially
426 // use it for the architecture. If the second component corresponds to a
427 // known vendor, preferentially use it for the vendor, etc. This avoids silly
428 // component movement when a component parses as (eg) both a valid arch and a
429 // valid os.
430 ArchType Arch = UnknownArch;
431 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000432 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000433 VendorType Vendor = UnknownVendor;
434 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000435 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000436 OSType OS = UnknownOS;
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000437 if (Components.size() > 2) {
Chandler Carruthaec97082012-02-21 08:53:32 +0000438 OS = parseOS(Components[2]);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000439 IsCygwin = Components[2].startswith("cygwin");
440 IsMinGW32 = Components[2].startswith("mingw");
441 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000442 EnvironmentType Environment = UnknownEnvironment;
443 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000444 Environment = parseEnvironment(Components[3]);
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000445 ObjectFormatType ObjectFormat = UnknownObjectFormat;
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000446 if (Components.size() > 4)
447 ObjectFormat = parseFormat(Components[4]);
Duncan Sands501dff72010-08-12 11:31:39 +0000448
449 // Note which components are already in their final position. These will not
450 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000451 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000452 Found[0] = Arch != UnknownArch;
453 Found[1] = Vendor != UnknownVendor;
454 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000455 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000456
457 // If they are not there already, permute the components into their canonical
458 // positions by seeing if they parse as a valid architecture, and if so moving
459 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000460 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000461 if (Found[Pos])
462 continue; // Already in the canonical position.
463
464 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
465 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000466 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000467 continue;
468
469 // Does this component parse as valid for the target position?
470 bool Valid = false;
471 StringRef Comp = Components[Idx];
472 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000473 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000474 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000475 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000476 Valid = Arch != UnknownArch;
477 break;
478 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000479 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000480 Valid = Vendor != UnknownVendor;
481 break;
482 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000483 OS = parseOS(Comp);
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000484 IsCygwin = Comp.startswith("cygwin");
485 IsMinGW32 = Comp.startswith("mingw");
486 Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
Duncan Sands501dff72010-08-12 11:31:39 +0000487 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000488 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000489 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000490 Valid = Environment != UnknownEnvironment;
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000491 if (!Valid) {
492 ObjectFormat = parseFormat(Comp);
493 Valid = ObjectFormat != UnknownObjectFormat;
494 }
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000495 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000496 }
497 if (!Valid)
498 continue; // Nope, try the next component.
499
500 // Move the component to the target position, pushing any non-fixed
501 // components that are in the way to the right. This tends to give
502 // good results in the common cases of a forgotten vendor component
503 // or a wrongly positioned environment.
504 if (Pos < Idx) {
505 // Insert left, pushing the existing components to the right. For
506 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
507 StringRef CurrentComponent(""); // The empty component.
508 // Replace the component we are moving with an empty component.
509 std::swap(CurrentComponent, Components[Idx]);
510 // Insert the component being moved at Pos, displacing any existing
511 // components to the right.
512 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
513 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000514 while (i < array_lengthof(Found) && Found[i])
515 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000516 // Place the component at the new position, getting the component
517 // that was at this position - it will be moved right.
518 std::swap(CurrentComponent, Components[i]);
519 }
520 } else if (Pos > Idx) {
521 // Push right by inserting empty components until the component at Idx
522 // reaches the target position Pos. For example, pc-a -> -pc-a when
523 // moving pc to the second position.
524 do {
525 // Insert one empty component at Idx.
526 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000527 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000528 // Place the component at the new position, getting the component
529 // that was at this position - it will be moved right.
530 std::swap(CurrentComponent, Components[i]);
531 // If it was placed on top of an empty component then we are done.
532 if (CurrentComponent.empty())
533 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000534 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000535 while (++i < array_lengthof(Found) && Found[i])
536 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000537 }
538 // The last component was pushed off the end - append it.
539 if (!CurrentComponent.empty())
540 Components.push_back(CurrentComponent);
541
542 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000543 while (++Idx < array_lengthof(Found) && Found[Idx])
544 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000545 } while (Idx < Pos); // Add more until the final position is reached.
546 }
547 assert(Pos < Components.size() && Components[Pos] == Comp &&
548 "Component moved wrong!");
549 Found[Pos] = true;
550 break;
551 }
552 }
553
554 // Special case logic goes here. At this point Arch, Vendor and OS have the
555 // correct values for the computed components.
556
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000557 if (OS == Triple::Win32) {
558 Components.resize(4);
559 Components[2] = "windows";
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000560 if (Environment == UnknownEnvironment) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000561 if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000562 Components[3] = "msvc";
563 else
564 Components[3] = getObjectFormatTypeName(ObjectFormat);
Saleem Abdulrasoolf80b49b2014-03-30 07:19:31 +0000565 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000566 } else if (IsMinGW32) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000567 Components.resize(4);
568 Components[2] = "windows";
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000569 Components[3] = "gnu";
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000570 } else if (IsCygwin) {
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000571 Components.resize(4);
572 Components[2] = "windows";
573 Components[3] = "cygnus";
574 }
Saleem Abdulrasool0e1b31c2014-08-09 23:12:20 +0000575 if (IsMinGW32 || IsCygwin ||
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000576 (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
577 if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
578 Components.resize(5);
579 Components[4] = getObjectFormatTypeName(ObjectFormat);
580 }
581 }
Saleem Abdulrasooledbdd2e2014-03-27 22:50:05 +0000582
Duncan Sands501dff72010-08-12 11:31:39 +0000583 // Stick the corrected components back together to form the normalized string.
584 std::string Normalized;
585 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
586 if (i) Normalized += '-';
587 Normalized += Components[i];
588 }
589 return Normalized;
590}
591
Daniel Dunbar19e70762009-07-26 03:31:47 +0000592StringRef Triple::getArchName() const {
593 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000594}
595
Daniel Dunbar19e70762009-07-26 03:31:47 +0000596StringRef Triple::getVendorName() const {
597 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
598 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000599}
600
Daniel Dunbar19e70762009-07-26 03:31:47 +0000601StringRef Triple::getOSName() const {
602 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
603 Tmp = Tmp.split('-').second; // Strip second component
604 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000605}
606
Daniel Dunbar19e70762009-07-26 03:31:47 +0000607StringRef Triple::getEnvironmentName() const {
608 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
609 Tmp = Tmp.split('-').second; // Strip second component
610 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000611}
612
Daniel Dunbar19e70762009-07-26 03:31:47 +0000613StringRef Triple::getOSAndEnvironmentName() const {
614 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
615 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000616}
617
Chris Lattner553c9f32009-08-12 06:19:40 +0000618static unsigned EatNumber(StringRef &Str) {
619 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000620 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000621
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000622 do {
623 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000624 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000625
Chris Lattner553c9f32009-08-12 06:19:40 +0000626 // Eat the digit.
627 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000628 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000629
Chris Lattner553c9f32009-08-12 06:19:40 +0000630 return Result;
631}
632
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000633void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
634 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000635 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000636
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000637 // Assume that the OS portion of the triple starts with the canonical name.
638 StringRef OSTypeName = getOSTypeName(getOS());
639 if (OSName.startswith(OSTypeName))
640 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000641
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000642 // Any unset version defaults to 0.
643 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000644
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000645 // Parse up to three components.
646 unsigned *Components[3] = { &Major, &Minor, &Micro };
647 for (unsigned i = 0; i != 3; ++i) {
648 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
649 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000650
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000651 // Consume the leading number.
652 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000653
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000654 // Consume the separator, if present.
655 if (OSName.startswith("."))
656 OSName = OSName.substr(1);
657 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000658}
659
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000660bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
661 unsigned &Micro) const {
662 getOSVersion(Major, Minor, Micro);
663
664 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000665 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000666 case Darwin:
667 // Default to darwin8, i.e., MacOSX 10.4.
668 if (Major == 0)
669 Major = 8;
670 // Darwin version numbers are skewed from OS X versions.
671 if (Major < 4)
672 return false;
673 Micro = 0;
674 Minor = Major - 4;
675 Major = 10;
676 break;
677 case MacOSX:
678 // Default to 10.4.
679 if (Major == 0) {
680 Major = 10;
681 Minor = 4;
682 }
683 if (Major != 10)
684 return false;
685 break;
686 case IOS:
687 // Ignore the version from the triple. This is only handled because the
688 // the clang driver combines OS X and IOS support into a common Darwin
689 // toolchain that wants to know the OS X version number even when targeting
690 // IOS.
691 Major = 10;
692 Minor = 4;
693 Micro = 0;
694 break;
695 }
696 return true;
697}
698
Chad Rosierd84eaac2012-05-09 17:23:48 +0000699void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
700 unsigned &Micro) const {
701 switch (getOS()) {
702 default: llvm_unreachable("unexpected OS for Darwin triple");
703 case Darwin:
704 case MacOSX:
705 // Ignore the version from the triple. This is only handled because the
706 // the clang driver combines OS X and IOS support into a common Darwin
707 // toolchain that wants to know the iOS version number even when targeting
708 // OS X.
Tim Northover3e8df692013-12-10 11:53:16 +0000709 Major = 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000710 Minor = 0;
711 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000712 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000713 case IOS:
714 getOSVersion(Major, Minor, Micro);
Tim Northover00ed9962014-03-29 10:18:08 +0000715 // Default to 5.0 (or 7.0 for arm64).
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000716 if (Major == 0)
Tim Northovere19bed72014-07-23 12:32:47 +0000717 Major = (getArch() == aarch64) ? 7 : 5;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000718 break;
719 }
720}
721
Daniel Dunbar19e70762009-07-26 03:31:47 +0000722void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000723 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000724}
725
726void Triple::setArch(ArchType Kind) {
727 setArchName(getArchTypeName(Kind));
728}
729
730void Triple::setVendor(VendorType Kind) {
731 setVendorName(getVendorTypeName(Kind));
732}
733
734void Triple::setOS(OSType Kind) {
735 setOSName(getOSTypeName(Kind));
736}
737
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000738void Triple::setEnvironment(EnvironmentType Kind) {
739 setEnvironmentName(getEnvironmentTypeName(Kind));
740}
741
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000742void Triple::setObjectFormat(ObjectFormatType Kind) {
Saleem Abdulrasool28b82bc2014-03-31 16:34:41 +0000743 if (Environment == UnknownEnvironment)
744 return setEnvironmentName(getObjectFormatTypeName(Kind));
745
Benjamin Kramercccdadc2014-07-08 14:55:06 +0000746 setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
747 getObjectFormatTypeName(Kind)).str());
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000748}
749
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000750void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000751 // Work around a miscompilation bug for Twines in gcc 4.0.3.
752 SmallString<64> Triple;
753 Triple += Str;
754 Triple += "-";
755 Triple += getVendorName();
756 Triple += "-";
757 Triple += getOSAndEnvironmentName();
758 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000759}
760
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000761void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000762 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
763}
764
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000765void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000766 if (hasEnvironment())
767 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
768 "-" + getEnvironmentName());
769 else
770 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
771}
772
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000773void Triple::setEnvironmentName(StringRef Str) {
774 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000775 "-" + Str);
776}
777
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000778void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000779 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
780}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000781
782static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
783 switch (Arch) {
784 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000785 return 0;
786
787 case llvm::Triple::msp430:
788 return 16;
789
790 case llvm::Triple::amdil:
791 case llvm::Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000792 case llvm::Triple::armeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000793 case llvm::Triple::hexagon:
794 case llvm::Triple::le32:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000795 case llvm::Triple::mips:
796 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000797 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000798 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000799 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000800 case llvm::Triple::sparc:
801 case llvm::Triple::tce:
802 case llvm::Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000803 case llvm::Triple::thumbeb:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000804 case llvm::Triple::x86:
805 case llvm::Triple::xcore:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000806 case llvm::Triple::spir:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000807 case llvm::Triple::kalimba:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000808 return 32;
809
Tim Northovere0e3aef2013-01-31 12:12:40 +0000810 case llvm::Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000811 case llvm::Triple::aarch64_be:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000812 case llvm::Triple::mips64:
813 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000814 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000815 case llvm::Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000816 case llvm::Triple::ppc64le:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000817 case llvm::Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000818 case llvm::Triple::systemz:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000819 case llvm::Triple::x86_64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000820 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000821 return 64;
822 }
823 llvm_unreachable("Invalid architecture value");
824}
825
826bool Triple::isArch64Bit() const {
827 return getArchPointerBitWidth(getArch()) == 64;
828}
829
830bool Triple::isArch32Bit() const {
831 return getArchPointerBitWidth(getArch()) == 32;
832}
833
834bool Triple::isArch16Bit() const {
835 return getArchPointerBitWidth(getArch()) == 16;
836}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000837
838Triple Triple::get32BitArchVariant() const {
839 Triple T(*this);
840 switch (getArch()) {
841 case Triple::UnknownArch:
Tim Northovere0e3aef2013-01-31 12:12:40 +0000842 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000843 case Triple::aarch64_be:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000844 case Triple::msp430:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000845 case Triple::systemz:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000846 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000847 T.setArch(UnknownArch);
848 break;
849
850 case Triple::amdil:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000851 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000852 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000853 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000854 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000855 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000856 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000857 case Triple::mips:
858 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000859 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000860 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000861 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000862 case Triple::sparc:
863 case Triple::tce:
864 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000865 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000866 case Triple::x86:
867 case Triple::xcore:
868 // Already 32-bit.
869 break;
870
871 case Triple::mips64: T.setArch(Triple::mips); break;
872 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000873 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Bob Wilsona2790b12014-03-09 23:17:28 +0000874 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000875 case Triple::sparcv9: T.setArch(Triple::sparc); break;
876 case Triple::x86_64: T.setArch(Triple::x86); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000877 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000878 }
879 return T;
880}
881
882Triple Triple::get64BitArchVariant() const {
883 Triple T(*this);
884 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000885 case Triple::UnknownArch:
886 case Triple::amdil:
Tim Northoveraf6bfb22014-03-30 07:25:23 +0000887 case Triple::arm:
Christian Pirker2a111602014-03-28 14:35:30 +0000888 case Triple::armeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000889 case Triple::hexagon:
Eric Christopher54fe1b22014-07-10 17:26:54 +0000890 case Triple::kalimba:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000891 case Triple::le32:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000892 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000893 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000894 case Triple::tce:
895 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000896 case Triple::thumbeb:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000897 case Triple::xcore:
898 T.setArch(UnknownArch);
899 break;
900
Tim Northovere0e3aef2013-01-31 12:12:40 +0000901 case Triple::aarch64:
Christian Pirker6c2f4d42014-02-24 11:34:50 +0000902 case Triple::aarch64_be:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000903 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000904 case Triple::mips64:
905 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000906 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000907 case Triple::ppc64:
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000908 case Triple::ppc64le:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000909 case Triple::sparcv9:
Richard Sandiforda238c5e2013-05-03 11:05:17 +0000910 case Triple::systemz:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000911 case Triple::x86_64:
912 // Already 64-bit.
913 break;
914
915 case Triple::mips: T.setArch(Triple::mips64); break;
916 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000917 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000918 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000919 case Triple::sparc: T.setArch(Triple::sparcv9); break;
920 case Triple::x86: T.setArch(Triple::x86_64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000921 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000922 }
923 return T;
924}
Argyrios Kyrtzidis730abd22014-07-11 21:44:54 +0000925
926// FIXME: tblgen this.
927const char *Triple::getARMCPUForArch(StringRef MArch) const {
928 if (MArch.empty())
929 MArch = getArchName();
930
931 switch (getOS()) {
932 case llvm::Triple::NetBSD:
933 if (MArch == "armv6")
934 return "arm1176jzf-s";
935 break;
936 case llvm::Triple::Win32:
937 // FIXME: this is invalid for WindowsCE
938 return "cortex-a9";
939 default:
940 break;
941 }
942
943 const char *result = nullptr;
944 size_t offset = StringRef::npos;
945 if (MArch.startswith("arm"))
946 offset = 3;
947 if (MArch.startswith("thumb"))
948 offset = 5;
949 if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
950 offset += 2;
951 if (offset != StringRef::npos)
952 result = llvm::StringSwitch<const char *>(MArch.substr(offset))
953 .Cases("v2", "v2a", "arm2")
954 .Case("v3", "arm6")
955 .Case("v3m", "arm7m")
956 .Case("v4", "strongarm")
957 .Case("v4t", "arm7tdmi")
958 .Cases("v5", "v5t", "arm10tdmi")
959 .Cases("v5e", "v5te", "arm1022e")
960 .Case("v5tej", "arm926ej-s")
961 .Cases("v6", "v6k", "arm1136jf-s")
962 .Case("v6j", "arm1136j-s")
963 .Cases("v6z", "v6zk", "arm1176jzf-s")
964 .Case("v6t2", "arm1156t2-s")
965 .Cases("v6m", "v6-m", "cortex-m0")
966 .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
967 .Cases("v7s", "v7-s", "swift")
968 .Cases("v7r", "v7-r", "cortex-r4")
969 .Cases("v7m", "v7-m", "cortex-m3")
970 .Cases("v7em", "v7e-m", "cortex-m4")
971 .Cases("v8", "v8a", "v8-a", "cortex-a53")
972 .Default(nullptr);
973 else
974 result = llvm::StringSwitch<const char *>(MArch)
975 .Case("ep9312", "ep9312")
976 .Case("iwmmxt", "iwmmxt")
977 .Case("xscale", "xscale")
978 .Default(nullptr);
979
980 if (result)
981 return result;
982
983 // If all else failed, return the most base CPU with thumb interworking
984 // supported by LLVM.
985 // FIXME: Should warn once that we're falling back.
986 switch (getOS()) {
987 case llvm::Triple::NetBSD:
988 switch (getEnvironment()) {
989 case llvm::Triple::GNUEABIHF:
990 case llvm::Triple::GNUEABI:
991 case llvm::Triple::EABIHF:
992 case llvm::Triple::EABI:
993 return "arm926ej-s";
994 default:
995 return "strongarm";
996 }
997 default:
998 switch (getEnvironment()) {
999 case llvm::Triple::EABIHF:
1000 case llvm::Triple::GNUEABIHF:
1001 return "arm1176jzf-s";
1002 default:
1003 return "arm7tdmi";
1004 }
1005 }
1006}