blob: eefb96bfee0ec0110a26433856e032305b14f472 [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
Daniel Dunbar172649b2009-07-26 04:23:03 +000022 case arm: return "arm";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023 case hexagon: return "hexagon";
Daniel Dunbar172649b2009-07-26 04:23:03 +000024 case mips: return "mips";
25 case mipsel: return "mipsel";
Akira Hatanaka6c3ad652011-09-20 18:09:37 +000026 case mips64: return "mips64";
27 case mips64el:return "mips64el";
Daniel Dunbar172649b2009-07-26 04:23:03 +000028 case msp430: return "msp430";
Daniel Dunbarbd481a12009-07-26 04:52:45 +000029 case ppc64: return "powerpc64";
30 case ppc: return "powerpc";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000031 case r600: return "r600";
Daniel Dunbar172649b2009-07-26 04:23:03 +000032 case sparc: return "sparc";
Chris Lattner8228b112010-02-04 06:34:01 +000033 case sparcv9: return "sparcv9";
Eli Friedman28c7bbf2009-08-19 20:46:03 +000034 case tce: return "tce";
Daniel Dunbar172649b2009-07-26 04:23:03 +000035 case thumb: return "thumb";
36 case x86: return "i386";
37 case x86_64: return "x86_64";
Daniel Dunbarbd481a12009-07-26 04:52:45 +000038 case xcore: return "xcore";
Wesley Pecke4801e42010-02-23 19:15:24 +000039 case mblaze: return "mblaze";
Justin Holewinskiae556d32012-05-04 20:18:50 +000040 case nvptx: return "nvptx";
41 case nvptx64: return "nvptx64";
Ivan Krasin771ef8c2011-08-23 16:59:00 +000042 case le32: return "le32";
Tobias Grosser516dbb22011-08-29 15:44:55 +000043 case amdil: return "amdil";
Micah Villmow48c8ddc2012-10-01 17:01:31 +000044 case spir: return "spir";
Guy Benyeia4d31a32012-11-15 10:35:47 +000045 case spir64: return "spir64";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000046 }
47
David Blaikie46a9f012012-01-20 21:51:11 +000048 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000049}
50
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000051const char *Triple::getArchTypePrefix(ArchType Kind) {
52 switch (Kind) {
53 default:
54 return 0;
55
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000056 case arm:
57 case thumb: return "arm";
58
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000059 case ppc64:
60 case ppc: return "ppc";
61
Wesley Pecke4801e42010-02-23 19:15:24 +000062 case mblaze: return "mblaze";
63
Benjamin Kramerae3c3002012-06-28 19:09:53 +000064 case mips:
65 case mipsel:
66 case mips64:
67 case mips64el:return "mips";
68
69 case hexagon: return "hexagon";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000070
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000071 case r600: return "r600";
72
Chris Lattner8228b112010-02-04 06:34:01 +000073 case sparcv9:
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000074 case sparc: return "sparc";
75
76 case x86:
77 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000078
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000079 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000080
Justin Holewinskiae556d32012-05-04 20:18:50 +000081 case nvptx: return "nvptx";
82 case nvptx64: return "nvptx";
Ivan Krasin771ef8c2011-08-23 16:59:00 +000083 case le32: return "le32";
Tobias Grosser516dbb22011-08-29 15:44:55 +000084 case amdil: return "amdil";
Micah Villmow48c8ddc2012-10-01 17:01:31 +000085 case spir: return "spir";
Guy Benyeia4d31a32012-11-15 10:35:47 +000086 case spir64: return "spir";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000087 }
88}
89
Daniel Dunbar4abd5662009-04-01 21:53:23 +000090const char *Triple::getVendorTypeName(VendorType Kind) {
91 switch (Kind) {
92 case UnknownVendor: return "unknown";
93
94 case Apple: return "apple";
Chris Lattner07921952009-08-14 18:48:13 +000095 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +000096 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +000097 case BGP: return "bgp";
98 case BGQ: return "bgq";
Hal Finkelb5d177e2012-08-28 02:10:30 +000099 case Freescale: return "fsl";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000100 case IBM: return "ibm";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000101 }
102
David Blaikie46a9f012012-01-20 21:51:11 +0000103 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000104}
105
106const char *Triple::getOSTypeName(OSType Kind) {
107 switch (Kind) {
108 case UnknownOS: return "unknown";
109
Duncan Sands0de39b42009-06-19 14:40:01 +0000110 case AuroraUX: return "auroraux";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000111 case Cygwin: return "cygwin";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000112 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000113 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000114 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000115 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000116 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000117 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000118 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000119 case MacOSX: return "macosx";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000120 case MinGW32: return "mingw32";
Chris Lattner01218d52009-07-13 20:22:23 +0000121 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000122 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000123 case Solaris: return "solaris";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000124 case Win32: return "win32";
Chris Lattner27f20492009-10-16 02:06:30 +0000125 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000126 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000127 case RTEMS: return "rtems";
Eli Benderskyabe54632012-12-04 18:37:26 +0000128 case NaCl: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000129 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000130 case Bitrig: return "bitrig";
Duncan Sandsd5772de2012-10-12 11:08:57 +0000131 case AIX: return "aix";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000132 }
133
David Blaikie46a9f012012-01-20 21:51:11 +0000134 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000135}
136
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000137const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
138 switch (Kind) {
139 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000140 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000141 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000142 case GNUEABI: return "gnueabi";
143 case EABI: return "eabi";
Evan Chengd22a4a12011-02-01 01:14:13 +0000144 case MachO: return "macho";
Logan Chien9ab55b82012-09-02 09:29:46 +0000145 case Android: return "android";
Andrew Kaylorfeb805f2012-10-02 18:38:34 +0000146 case ELF: return "elf";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000147 }
148
David Blaikie46a9f012012-01-20 21:51:11 +0000149 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000150}
151
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000152Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000153 return StringSwitch<Triple::ArchType>(Name)
154 .Case("arm", arm)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000155 .Case("mips", mips)
156 .Case("mipsel", mipsel)
157 .Case("mips64", mips64)
158 .Case("mips64el", mips64el)
159 .Case("msp430", msp430)
160 .Case("ppc64", ppc64)
161 .Case("ppc32", ppc)
162 .Case("ppc", ppc)
163 .Case("mblaze", mblaze)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000164 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000165 .Case("hexagon", hexagon)
166 .Case("sparc", sparc)
167 .Case("sparcv9", sparcv9)
168 .Case("tce", tce)
169 .Case("thumb", thumb)
170 .Case("x86", x86)
171 .Case("x86-64", x86_64)
172 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000173 .Case("nvptx", nvptx)
174 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000175 .Case("le32", le32)
176 .Case("amdil", amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000177 .Case("spir", spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000178 .Case("spir64", spir64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000179 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000180}
181
Duncan Sands8712da32010-03-24 09:05:14 +0000182// Returns architecture name that is understood by the target assembler.
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000183const char *Triple::getArchNameForAssembler() {
Daniel Dunbar163a0962011-04-19 21:12:05 +0000184 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000185 return NULL;
186
Chandler Carruthff6f3562012-02-12 09:27:38 +0000187 return StringSwitch<const char*>(getArchName())
188 .Case("i386", "i386")
189 .Case("x86_64", "x86_64")
190 .Case("powerpc", "ppc")
191 .Case("powerpc64", "ppc64")
192 .Cases("mblaze", "microblaze", "mblaze")
193 .Case("arm", "arm")
194 .Cases("armv4t", "thumbv4t", "armv4t")
195 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
196 .Cases("armv6", "thumbv6", "armv6")
197 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000198 .Case("r600", "r600")
Justin Holewinskiae556d32012-05-04 20:18:50 +0000199 .Case("nvptx", "nvptx")
200 .Case("nvptx64", "nvptx64")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000201 .Case("le32", "le32")
202 .Case("amdil", "amdil")
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000203 .Case("spir", "spir")
Guy Benyeia4d31a32012-11-15 10:35:47 +0000204 .Case("spir64", "spir64")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000205 .Default(NULL);
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000206}
207
Chandler Carruthaec97082012-02-21 08:53:32 +0000208static Triple::ArchType parseArch(StringRef ArchName) {
209 return StringSwitch<Triple::ArchType>(ArchName)
210 .Cases("i386", "i486", "i586", "i686", Triple::x86)
211 // FIXME: Do we need to support these?
212 .Cases("i786", "i886", "i986", Triple::x86)
213 .Cases("amd64", "x86_64", Triple::x86_64)
214 .Case("powerpc", Triple::ppc)
215 .Cases("powerpc64", "ppu", Triple::ppc64)
216 .Case("mblaze", Triple::mblaze)
217 .Cases("arm", "xscale", Triple::arm)
Chandler Carruthb54950b2012-02-18 04:34:17 +0000218 // FIXME: It would be good to replace these with explicit names for all the
219 // various suffixes supported.
Chandler Carruthaec97082012-02-21 08:53:32 +0000220 .StartsWith("armv", Triple::arm)
221 .Case("thumb", Triple::thumb)
222 .StartsWith("thumbv", Triple::thumb)
Chandler Carruthaec97082012-02-21 08:53:32 +0000223 .Case("msp430", Triple::msp430)
224 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000225 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000226 .Cases("mips64", "mips64eb", Triple::mips64)
227 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000228 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000229 .Case("hexagon", Triple::hexagon)
230 .Case("sparc", Triple::sparc)
231 .Case("sparcv9", Triple::sparcv9)
232 .Case("tce", Triple::tce)
233 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000234 .Case("nvptx", Triple::nvptx)
235 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000236 .Case("le32", Triple::le32)
237 .Case("amdil", Triple::amdil)
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000238 .Case("spir", Triple::spir)
Guy Benyeia4d31a32012-11-15 10:35:47 +0000239 .Case("spir64", Triple::spir64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000240 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000241}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000242
Chandler Carruthaec97082012-02-21 08:53:32 +0000243static Triple::VendorType parseVendor(StringRef VendorName) {
244 return StringSwitch<Triple::VendorType>(VendorName)
245 .Case("apple", Triple::Apple)
246 .Case("pc", Triple::PC)
247 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000248 .Case("bgp", Triple::BGP)
249 .Case("bgq", Triple::BGQ)
Hal Finkelb5d177e2012-08-28 02:10:30 +0000250 .Case("fsl", Triple::Freescale)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000251 .Case("ibm", Triple::IBM)
Chandler Carruthaec97082012-02-21 08:53:32 +0000252 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000253}
254
Chandler Carruthaec97082012-02-21 08:53:32 +0000255static Triple::OSType parseOS(StringRef OSName) {
256 return StringSwitch<Triple::OSType>(OSName)
257 .StartsWith("auroraux", Triple::AuroraUX)
258 .StartsWith("cygwin", Triple::Cygwin)
259 .StartsWith("darwin", Triple::Darwin)
260 .StartsWith("dragonfly", Triple::DragonFly)
261 .StartsWith("freebsd", Triple::FreeBSD)
262 .StartsWith("ios", Triple::IOS)
263 .StartsWith("kfreebsd", Triple::KFreeBSD)
264 .StartsWith("linux", Triple::Linux)
265 .StartsWith("lv2", Triple::Lv2)
266 .StartsWith("macosx", Triple::MacOSX)
267 .StartsWith("mingw32", Triple::MinGW32)
268 .StartsWith("netbsd", Triple::NetBSD)
269 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000270 .StartsWith("solaris", Triple::Solaris)
271 .StartsWith("win32", Triple::Win32)
272 .StartsWith("haiku", Triple::Haiku)
273 .StartsWith("minix", Triple::Minix)
274 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyabe54632012-12-04 18:37:26 +0000275 .StartsWith("nacl", Triple::NaCl)
Hal Finkelf208af02012-04-02 18:31:33 +0000276 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000277 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sandsd5772de2012-10-12 11:08:57 +0000278 .StartsWith("aix", Triple::AIX)
Chandler Carruthaec97082012-02-21 08:53:32 +0000279 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000280}
281
Chandler Carruthaec97082012-02-21 08:53:32 +0000282static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
283 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
284 .StartsWith("eabi", Triple::EABI)
285 .StartsWith("gnueabihf", Triple::GNUEABIHF)
286 .StartsWith("gnueabi", Triple::GNUEABI)
287 .StartsWith("gnu", Triple::GNU)
288 .StartsWith("macho", Triple::MachO)
Logan Chien9ab55b82012-09-02 09:29:46 +0000289 .StartsWith("android", Triple::Android)
Andrew Kaylorfeb805f2012-10-02 18:38:34 +0000290 .StartsWith("elf", Triple::ELF)
Chandler Carruthaec97082012-02-21 08:53:32 +0000291 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000292}
293
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000294/// \brief Construct a triple from the string representation provided.
295///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000296/// This stores the string representation and parses the various pieces into
297/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000298Triple::Triple(const Twine &Str)
299 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000300 Arch(parseArch(getArchName())),
301 Vendor(parseVendor(getVendorName())),
302 OS(parseOS(getOSName())),
303 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000304}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000305
306/// \brief Construct a triple from string representations of the architecture,
307/// vendor, and OS.
308///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000309/// This joins each argument into a canonical string representation and parses
310/// them into enum members. It leaves the environment unknown and omits it from
311/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000312Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
313 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000314 Arch(parseArch(ArchStr.str())),
315 Vendor(parseVendor(VendorStr.str())),
316 OS(parseOS(OSStr.str())),
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000317 Environment() {
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000318}
319
320/// \brief Construct a triple from string representations of the architecture,
321/// vendor, OS, and environment.
322///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000323/// This joins each argument into a canonical string representation and parses
324/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000325Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
326 const Twine &EnvironmentStr)
327 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
328 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000329 Arch(parseArch(ArchStr.str())),
330 Vendor(parseVendor(VendorStr.str())),
331 OS(parseOS(OSStr.str())),
332 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000333}
334
Duncan Sands501dff72010-08-12 11:31:39 +0000335std::string Triple::normalize(StringRef Str) {
336 // Parse into components.
337 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000338 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000339
340 // If the first component corresponds to a known architecture, preferentially
341 // use it for the architecture. If the second component corresponds to a
342 // known vendor, preferentially use it for the vendor, etc. This avoids silly
343 // component movement when a component parses as (eg) both a valid arch and a
344 // valid os.
345 ArchType Arch = UnknownArch;
346 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000347 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000348 VendorType Vendor = UnknownVendor;
349 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000350 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000351 OSType OS = UnknownOS;
352 if (Components.size() > 2)
Chandler Carruthaec97082012-02-21 08:53:32 +0000353 OS = parseOS(Components[2]);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000354 EnvironmentType Environment = UnknownEnvironment;
355 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000356 Environment = parseEnvironment(Components[3]);
Duncan Sands501dff72010-08-12 11:31:39 +0000357
358 // Note which components are already in their final position. These will not
359 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000360 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000361 Found[0] = Arch != UnknownArch;
362 Found[1] = Vendor != UnknownVendor;
363 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000364 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000365
366 // If they are not there already, permute the components into their canonical
367 // positions by seeing if they parse as a valid architecture, and if so moving
368 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000369 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000370 if (Found[Pos])
371 continue; // Already in the canonical position.
372
373 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
374 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000375 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000376 continue;
377
378 // Does this component parse as valid for the target position?
379 bool Valid = false;
380 StringRef Comp = Components[Idx];
381 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000382 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000383 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000384 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000385 Valid = Arch != UnknownArch;
386 break;
387 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000388 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000389 Valid = Vendor != UnknownVendor;
390 break;
391 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000392 OS = parseOS(Comp);
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000393 Valid = OS != UnknownOS;
Duncan Sands501dff72010-08-12 11:31:39 +0000394 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000395 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000396 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000397 Valid = Environment != UnknownEnvironment;
398 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000399 }
400 if (!Valid)
401 continue; // Nope, try the next component.
402
403 // Move the component to the target position, pushing any non-fixed
404 // components that are in the way to the right. This tends to give
405 // good results in the common cases of a forgotten vendor component
406 // or a wrongly positioned environment.
407 if (Pos < Idx) {
408 // Insert left, pushing the existing components to the right. For
409 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
410 StringRef CurrentComponent(""); // The empty component.
411 // Replace the component we are moving with an empty component.
412 std::swap(CurrentComponent, Components[Idx]);
413 // Insert the component being moved at Pos, displacing any existing
414 // components to the right.
415 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
416 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000417 while (i < array_lengthof(Found) && Found[i])
418 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000419 // Place the component at the new position, getting the component
420 // that was at this position - it will be moved right.
421 std::swap(CurrentComponent, Components[i]);
422 }
423 } else if (Pos > Idx) {
424 // Push right by inserting empty components until the component at Idx
425 // reaches the target position Pos. For example, pc-a -> -pc-a when
426 // moving pc to the second position.
427 do {
428 // Insert one empty component at Idx.
429 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000430 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000431 // Place the component at the new position, getting the component
432 // that was at this position - it will be moved right.
433 std::swap(CurrentComponent, Components[i]);
434 // If it was placed on top of an empty component then we are done.
435 if (CurrentComponent.empty())
436 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000437 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000438 while (++i < array_lengthof(Found) && Found[i])
439 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000440 }
441 // The last component was pushed off the end - append it.
442 if (!CurrentComponent.empty())
443 Components.push_back(CurrentComponent);
444
445 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000446 while (++Idx < array_lengthof(Found) && Found[Idx])
447 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000448 } while (Idx < Pos); // Add more until the final position is reached.
449 }
450 assert(Pos < Components.size() && Components[Pos] == Comp &&
451 "Component moved wrong!");
452 Found[Pos] = true;
453 break;
454 }
455 }
456
457 // Special case logic goes here. At this point Arch, Vendor and OS have the
458 // correct values for the computed components.
459
460 // Stick the corrected components back together to form the normalized string.
461 std::string Normalized;
462 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
463 if (i) Normalized += '-';
464 Normalized += Components[i];
465 }
466 return Normalized;
467}
468
Daniel Dunbar19e70762009-07-26 03:31:47 +0000469StringRef Triple::getArchName() const {
470 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000471}
472
Daniel Dunbar19e70762009-07-26 03:31:47 +0000473StringRef Triple::getVendorName() const {
474 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
475 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000476}
477
Daniel Dunbar19e70762009-07-26 03:31:47 +0000478StringRef Triple::getOSName() const {
479 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
480 Tmp = Tmp.split('-').second; // Strip second component
481 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000482}
483
Daniel Dunbar19e70762009-07-26 03:31:47 +0000484StringRef Triple::getEnvironmentName() const {
485 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
486 Tmp = Tmp.split('-').second; // Strip second component
487 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000488}
489
Daniel Dunbar19e70762009-07-26 03:31:47 +0000490StringRef Triple::getOSAndEnvironmentName() const {
491 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
492 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000493}
494
Chris Lattner553c9f32009-08-12 06:19:40 +0000495static unsigned EatNumber(StringRef &Str) {
496 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000497 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000498
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000499 do {
500 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000501 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000502
Chris Lattner553c9f32009-08-12 06:19:40 +0000503 // Eat the digit.
504 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000505 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000506
Chris Lattner553c9f32009-08-12 06:19:40 +0000507 return Result;
508}
509
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000510void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
511 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000512 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000513
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000514 // Assume that the OS portion of the triple starts with the canonical name.
515 StringRef OSTypeName = getOSTypeName(getOS());
516 if (OSName.startswith(OSTypeName))
517 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000518
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000519 // Any unset version defaults to 0.
520 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000521
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000522 // Parse up to three components.
523 unsigned *Components[3] = { &Major, &Minor, &Micro };
524 for (unsigned i = 0; i != 3; ++i) {
525 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
526 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000527
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000528 // Consume the leading number.
529 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000530
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000531 // Consume the separator, if present.
532 if (OSName.startswith("."))
533 OSName = OSName.substr(1);
534 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000535}
536
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000537bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
538 unsigned &Micro) const {
539 getOSVersion(Major, Minor, Micro);
540
541 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000542 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000543 case Darwin:
544 // Default to darwin8, i.e., MacOSX 10.4.
545 if (Major == 0)
546 Major = 8;
547 // Darwin version numbers are skewed from OS X versions.
548 if (Major < 4)
549 return false;
550 Micro = 0;
551 Minor = Major - 4;
552 Major = 10;
553 break;
554 case MacOSX:
555 // Default to 10.4.
556 if (Major == 0) {
557 Major = 10;
558 Minor = 4;
559 }
560 if (Major != 10)
561 return false;
562 break;
563 case IOS:
564 // Ignore the version from the triple. This is only handled because the
565 // the clang driver combines OS X and IOS support into a common Darwin
566 // toolchain that wants to know the OS X version number even when targeting
567 // IOS.
568 Major = 10;
569 Minor = 4;
570 Micro = 0;
571 break;
572 }
573 return true;
574}
575
Chad Rosierd84eaac2012-05-09 17:23:48 +0000576void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
577 unsigned &Micro) const {
578 switch (getOS()) {
579 default: llvm_unreachable("unexpected OS for Darwin triple");
580 case Darwin:
581 case MacOSX:
582 // Ignore the version from the triple. This is only handled because the
583 // the clang driver combines OS X and IOS support into a common Darwin
584 // toolchain that wants to know the iOS version number even when targeting
585 // OS X.
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000586 Major = 3;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000587 Minor = 0;
588 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000589 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000590 case IOS:
591 getOSVersion(Major, Minor, Micro);
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000592 // Default to 3.0.
593 if (Major == 0)
594 Major = 3;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000595 break;
596 }
597}
598
Daniel Dunbar19e70762009-07-26 03:31:47 +0000599void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000600 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000601}
602
603void Triple::setArch(ArchType Kind) {
604 setArchName(getArchTypeName(Kind));
605}
606
607void Triple::setVendor(VendorType Kind) {
608 setVendorName(getVendorTypeName(Kind));
609}
610
611void Triple::setOS(OSType Kind) {
612 setOSName(getOSTypeName(Kind));
613}
614
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000615void Triple::setEnvironment(EnvironmentType Kind) {
616 setEnvironmentName(getEnvironmentTypeName(Kind));
617}
618
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000619void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000620 // Work around a miscompilation bug for Twines in gcc 4.0.3.
621 SmallString<64> Triple;
622 Triple += Str;
623 Triple += "-";
624 Triple += getVendorName();
625 Triple += "-";
626 Triple += getOSAndEnvironmentName();
627 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000628}
629
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000630void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000631 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
632}
633
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000634void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000635 if (hasEnvironment())
636 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
637 "-" + getEnvironmentName());
638 else
639 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
640}
641
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000642void Triple::setEnvironmentName(StringRef Str) {
643 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000644 "-" + Str);
645}
646
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000647void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000648 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
649}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000650
651static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
652 switch (Arch) {
653 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000654 return 0;
655
656 case llvm::Triple::msp430:
657 return 16;
658
659 case llvm::Triple::amdil:
660 case llvm::Triple::arm:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000661 case llvm::Triple::hexagon:
662 case llvm::Triple::le32:
663 case llvm::Triple::mblaze:
664 case llvm::Triple::mips:
665 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000666 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000667 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000668 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000669 case llvm::Triple::sparc:
670 case llvm::Triple::tce:
671 case llvm::Triple::thumb:
672 case llvm::Triple::x86:
673 case llvm::Triple::xcore:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000674 case llvm::Triple::spir:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000675 return 32;
676
677 case llvm::Triple::mips64:
678 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000679 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000680 case llvm::Triple::ppc64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000681 case llvm::Triple::sparcv9:
682 case llvm::Triple::x86_64:
Guy Benyeia4d31a32012-11-15 10:35:47 +0000683 case llvm::Triple::spir64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000684 return 64;
685 }
686 llvm_unreachable("Invalid architecture value");
687}
688
689bool Triple::isArch64Bit() const {
690 return getArchPointerBitWidth(getArch()) == 64;
691}
692
693bool Triple::isArch32Bit() const {
694 return getArchPointerBitWidth(getArch()) == 32;
695}
696
697bool Triple::isArch16Bit() const {
698 return getArchPointerBitWidth(getArch()) == 16;
699}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000700
701Triple Triple::get32BitArchVariant() const {
702 Triple T(*this);
703 switch (getArch()) {
704 case Triple::UnknownArch:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000705 case Triple::msp430:
706 T.setArch(UnknownArch);
707 break;
708
709 case Triple::amdil:
Micah Villmow48c8ddc2012-10-01 17:01:31 +0000710 case Triple::spir:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000711 case Triple::arm:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000712 case Triple::hexagon:
713 case Triple::le32:
714 case Triple::mblaze:
715 case Triple::mips:
716 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000717 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000718 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000719 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000720 case Triple::sparc:
721 case Triple::tce:
722 case Triple::thumb:
723 case Triple::x86:
724 case Triple::xcore:
725 // Already 32-bit.
726 break;
727
728 case Triple::mips64: T.setArch(Triple::mips); break;
729 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000730 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000731 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000732 case Triple::sparcv9: T.setArch(Triple::sparc); break;
733 case Triple::x86_64: T.setArch(Triple::x86); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000734 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000735 }
736 return T;
737}
738
739Triple Triple::get64BitArchVariant() const {
740 Triple T(*this);
741 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000742 case Triple::UnknownArch:
743 case Triple::amdil:
744 case Triple::arm:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000745 case Triple::hexagon:
746 case Triple::le32:
747 case Triple::mblaze:
748 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000749 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000750 case Triple::tce:
751 case Triple::thumb:
752 case Triple::xcore:
753 T.setArch(UnknownArch);
754 break;
755
Guy Benyeia4d31a32012-11-15 10:35:47 +0000756 case Triple::spir64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000757 case Triple::mips64:
758 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000759 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000760 case Triple::ppc64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000761 case Triple::sparcv9:
762 case Triple::x86_64:
763 // Already 64-bit.
764 break;
765
766 case Triple::mips: T.setArch(Triple::mips64); break;
767 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000768 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000769 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000770 case Triple::sparc: T.setArch(Triple::sparcv9); break;
771 case Triple::x86: T.setArch(Triple::x86_64); break;
Guy Benyeia4d31a32012-11-15 10:35:47 +0000772 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000773 }
774 return T;
775}