blob: ad7b18942e4f0f1ca39d9c650df706168a05ce09 [file] [log] [blame]
Daniel Dunbar23e97b02009-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 Carruthd04a8d42012-12-03 16:50:05 +000011#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskin0b228732009-10-06 21:45:26 +000012#include "llvm/ADT/SmallString.h"
Chandler Carruth06accda2012-02-12 09:27:38 +000013#include "llvm/ADT/StringSwitch.h"
David Blaikie4d6ccb52012-01-20 21:51:11 +000014#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov70748752009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar23e97b02009-04-01 21:53:23 +000016using namespace llvm;
17
Daniel Dunbar23e97b02009-04-01 21:53:23 +000018const char *Triple::getArchTypeName(ArchType Kind) {
19 switch (Kind) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +000020 case UnknownArch: return "unknown";
Jim Grosbache509aa92010-12-17 02:10:59 +000021
Daniel Dunbar6337f152009-07-26 04:23:03 +000022 case arm: return "arm";
Tony Linthicumb4b54152011-12-12 21:14:40 +000023 case hexagon: return "hexagon";
Daniel Dunbar6337f152009-07-26 04:23:03 +000024 case mips: return "mips";
25 case mipsel: return "mipsel";
Akira Hatanaka70303682011-09-20 18:09:37 +000026 case mips64: return "mips64";
27 case mips64el:return "mips64el";
Daniel Dunbar6337f152009-07-26 04:23:03 +000028 case msp430: return "msp430";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000029 case ppc64: return "powerpc64";
30 case ppc: return "powerpc";
Anton Korobeynikov74156592012-03-09 10:09:36 +000031 case r600: return "r600";
Daniel Dunbar6337f152009-07-26 04:23:03 +000032 case sparc: return "sparc";
Chris Lattner87c06d62010-02-04 06:34:01 +000033 case sparcv9: return "sparcv9";
Eli Friedman74db89e2009-08-19 20:46:03 +000034 case tce: return "tce";
Daniel Dunbar6337f152009-07-26 04:23:03 +000035 case thumb: return "thumb";
36 case x86: return "i386";
37 case x86_64: return "x86_64";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000038 case xcore: return "xcore";
Wesley Pecka70f28c2010-02-23 19:15:24 +000039 case mblaze: return "mblaze";
Justin Holewinski49683f32012-05-04 20:18:50 +000040 case nvptx: return "nvptx";
41 case nvptx64: return "nvptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000042 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000043 case amdil: return "amdil";
Micah Villmowe53d6052012-10-01 17:01:31 +000044 case spir: return "spir";
Guy Benyeiac39a032012-11-15 10:35:47 +000045 case spir64: return "spir64";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000046 }
47
David Blaikie4d6ccb52012-01-20 21:51:11 +000048 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000049}
50
Daniel Dunbar688b55b2009-08-24 09:53:06 +000051const char *Triple::getArchTypePrefix(ArchType Kind) {
52 switch (Kind) {
53 default:
54 return 0;
55
Daniel Dunbar688b55b2009-08-24 09:53:06 +000056 case arm:
57 case thumb: return "arm";
58
Daniel Dunbar688b55b2009-08-24 09:53:06 +000059 case ppc64:
60 case ppc: return "ppc";
61
Wesley Pecka70f28c2010-02-23 19:15:24 +000062 case mblaze: return "mblaze";
63
Benjamin Kramerd5dbc8c2012-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 Linthicumb4b54152011-12-12 21:14:40 +000070
Anton Korobeynikov74156592012-03-09 10:09:36 +000071 case r600: return "r600";
72
Chris Lattner87c06d62010-02-04 06:34:01 +000073 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000074 case sparc: return "sparc";
75
76 case x86:
77 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000078
Daniel Dunbar688b55b2009-08-24 09:53:06 +000079 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000080
Justin Holewinski49683f32012-05-04 20:18:50 +000081 case nvptx: return "nvptx";
82 case nvptx64: return "nvptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000083 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000084 case amdil: return "amdil";
Micah Villmowe53d6052012-10-01 17:01:31 +000085 case spir: return "spir";
Guy Benyeiac39a032012-11-15 10:35:47 +000086 case spir64: return "spir";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000087 }
88}
89
Daniel Dunbar23e97b02009-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 Lattner56ce0f42009-08-14 18:48:13 +000095 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000096 case SCEI: return "scei";
Hal Finkela47406c2012-04-02 18:31:33 +000097 case BGP: return "bgp";
98 case BGQ: return "bgq";
Hal Finkeld939cd62012-08-28 02:10:30 +000099 case Freescale: return "fsl";
Duncan Sands2e522d02012-10-12 11:08:57 +0000100 case IBM: return "ibm";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000101 }
102
David Blaikie4d6ccb52012-01-20 21:51:11 +0000103 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000104}
105
106const char *Triple::getOSTypeName(OSType Kind) {
107 switch (Kind) {
108 case UnknownOS: return "unknown";
109
Duncan Sands852cd112009-06-19 14:40:01 +0000110 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000111 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000112 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000113 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000114 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000115 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000116 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000117 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000118 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000119 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000120 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000121 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000122 case OpenBSD: return "openbsd";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000123 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000124 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000125 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000126 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000127 case RTEMS: return "rtems";
Eli Benderskyf659c0d2012-12-04 18:37:26 +0000128 case NaCl: return "nacl";
Hal Finkela47406c2012-04-02 18:31:33 +0000129 case CNK: return "cnk";
Eric Christopherb0f67592012-08-06 20:52:18 +0000130 case Bitrig: return "bitrig";
Duncan Sands2e522d02012-10-12 11:08:57 +0000131 case AIX: return "aix";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000132 }
133
David Blaikie4d6ccb52012-01-20 21:51:11 +0000134 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000135}
136
Duncan Sands5754a452010-09-16 08:25:48 +0000137const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
138 switch (Kind) {
139 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000140 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000141 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000142 case GNUEABI: return "gnueabi";
Eli Bendersky9dd2a3b2013-01-22 18:02:49 +0000143 case GNUX32: return "gnux32";
Renato Golin859f8182011-01-21 18:25:47 +0000144 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000145 case MachO: return "macho";
Logan Chien43bf7092012-09-02 09:29:46 +0000146 case Android: return "android";
Andrew Kaylor7bbd6e32012-10-02 18:38:34 +0000147 case ELF: return "elf";
Duncan Sands5754a452010-09-16 08:25:48 +0000148 }
149
David Blaikie4d6ccb52012-01-20 21:51:11 +0000150 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000151}
152
Daniel Dunbar2928c832009-11-06 10:58:06 +0000153Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000154 return StringSwitch<Triple::ArchType>(Name)
155 .Case("arm", arm)
Chandler Carruth06accda2012-02-12 09:27:38 +0000156 .Case("mips", mips)
157 .Case("mipsel", mipsel)
158 .Case("mips64", mips64)
159 .Case("mips64el", mips64el)
160 .Case("msp430", msp430)
161 .Case("ppc64", ppc64)
162 .Case("ppc32", ppc)
163 .Case("ppc", ppc)
164 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000165 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000166 .Case("hexagon", hexagon)
167 .Case("sparc", sparc)
168 .Case("sparcv9", sparcv9)
169 .Case("tce", tce)
170 .Case("thumb", thumb)
171 .Case("x86", x86)
172 .Case("x86-64", x86_64)
173 .Case("xcore", xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000174 .Case("nvptx", nvptx)
175 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000176 .Case("le32", le32)
177 .Case("amdil", amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000178 .Case("spir", spir)
Guy Benyeiac39a032012-11-15 10:35:47 +0000179 .Case("spir64", spir64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000180 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000181}
182
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000183// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000184const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000185 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000186 return NULL;
187
Chandler Carruth06accda2012-02-12 09:27:38 +0000188 return StringSwitch<const char*>(getArchName())
189 .Case("i386", "i386")
190 .Case("x86_64", "x86_64")
191 .Case("powerpc", "ppc")
192 .Case("powerpc64", "ppc64")
193 .Cases("mblaze", "microblaze", "mblaze")
194 .Case("arm", "arm")
195 .Cases("armv4t", "thumbv4t", "armv4t")
196 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
197 .Cases("armv6", "thumbv6", "armv6")
198 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000199 .Case("r600", "r600")
Justin Holewinski49683f32012-05-04 20:18:50 +0000200 .Case("nvptx", "nvptx")
201 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000202 .Case("le32", "le32")
203 .Case("amdil", "amdil")
Micah Villmowe53d6052012-10-01 17:01:31 +0000204 .Case("spir", "spir")
Guy Benyeiac39a032012-11-15 10:35:47 +0000205 .Case("spir64", "spir64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000206 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000207}
208
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000209static Triple::ArchType parseArch(StringRef ArchName) {
210 return StringSwitch<Triple::ArchType>(ArchName)
211 .Cases("i386", "i486", "i586", "i686", Triple::x86)
212 // FIXME: Do we need to support these?
213 .Cases("i786", "i886", "i986", Triple::x86)
214 .Cases("amd64", "x86_64", Triple::x86_64)
215 .Case("powerpc", Triple::ppc)
216 .Cases("powerpc64", "ppu", Triple::ppc64)
217 .Case("mblaze", Triple::mblaze)
218 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000219 // FIXME: It would be good to replace these with explicit names for all the
220 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000221 .StartsWith("armv", Triple::arm)
222 .Case("thumb", Triple::thumb)
223 .StartsWith("thumbv", Triple::thumb)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000224 .Case("msp430", Triple::msp430)
225 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000226 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000227 .Cases("mips64", "mips64eb", Triple::mips64)
228 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000229 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000230 .Case("hexagon", Triple::hexagon)
231 .Case("sparc", Triple::sparc)
232 .Case("sparcv9", Triple::sparcv9)
233 .Case("tce", Triple::tce)
234 .Case("xcore", Triple::xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000235 .Case("nvptx", Triple::nvptx)
236 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000237 .Case("le32", Triple::le32)
238 .Case("amdil", Triple::amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000239 .Case("spir", Triple::spir)
Guy Benyeiac39a032012-11-15 10:35:47 +0000240 .Case("spir64", Triple::spir64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000241 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000242}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000243
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000244static Triple::VendorType parseVendor(StringRef VendorName) {
245 return StringSwitch<Triple::VendorType>(VendorName)
246 .Case("apple", Triple::Apple)
247 .Case("pc", Triple::PC)
248 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000249 .Case("bgp", Triple::BGP)
250 .Case("bgq", Triple::BGQ)
Hal Finkeld939cd62012-08-28 02:10:30 +0000251 .Case("fsl", Triple::Freescale)
Duncan Sands2e522d02012-10-12 11:08:57 +0000252 .Case("ibm", Triple::IBM)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000253 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000254}
255
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000256static Triple::OSType parseOS(StringRef OSName) {
257 return StringSwitch<Triple::OSType>(OSName)
258 .StartsWith("auroraux", Triple::AuroraUX)
259 .StartsWith("cygwin", Triple::Cygwin)
260 .StartsWith("darwin", Triple::Darwin)
261 .StartsWith("dragonfly", Triple::DragonFly)
262 .StartsWith("freebsd", Triple::FreeBSD)
263 .StartsWith("ios", Triple::IOS)
264 .StartsWith("kfreebsd", Triple::KFreeBSD)
265 .StartsWith("linux", Triple::Linux)
266 .StartsWith("lv2", Triple::Lv2)
267 .StartsWith("macosx", Triple::MacOSX)
268 .StartsWith("mingw32", Triple::MinGW32)
269 .StartsWith("netbsd", Triple::NetBSD)
270 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000271 .StartsWith("solaris", Triple::Solaris)
272 .StartsWith("win32", Triple::Win32)
273 .StartsWith("haiku", Triple::Haiku)
274 .StartsWith("minix", Triple::Minix)
275 .StartsWith("rtems", Triple::RTEMS)
Eli Benderskyf659c0d2012-12-04 18:37:26 +0000276 .StartsWith("nacl", Triple::NaCl)
Hal Finkela47406c2012-04-02 18:31:33 +0000277 .StartsWith("cnk", Triple::CNK)
Eric Christopherb0f67592012-08-06 20:52:18 +0000278 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sands2e522d02012-10-12 11:08:57 +0000279 .StartsWith("aix", Triple::AIX)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000280 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000281}
282
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000283static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
284 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
285 .StartsWith("eabi", Triple::EABI)
286 .StartsWith("gnueabihf", Triple::GNUEABIHF)
287 .StartsWith("gnueabi", Triple::GNUEABI)
Eli Bendersky9dd2a3b2013-01-22 18:02:49 +0000288 .StartsWith("gnux32", Triple::GNUX32)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000289 .StartsWith("gnu", Triple::GNU)
290 .StartsWith("macho", Triple::MachO)
Logan Chien43bf7092012-09-02 09:29:46 +0000291 .StartsWith("android", Triple::Android)
Andrew Kaylor7bbd6e32012-10-02 18:38:34 +0000292 .StartsWith("elf", Triple::ELF)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000293 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000294}
295
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000296/// \brief Construct a triple from the string representation provided.
297///
Chandler Carruth0523f412012-02-21 08:31:18 +0000298/// This stores the string representation and parses the various pieces into
299/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000300Triple::Triple(const Twine &Str)
301 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000302 Arch(parseArch(getArchName())),
303 Vendor(parseVendor(getVendorName())),
304 OS(parseOS(getOSName())),
305 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000306}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000307
308/// \brief Construct a triple from string representations of the architecture,
309/// vendor, and OS.
310///
Chandler Carruth0523f412012-02-21 08:31:18 +0000311/// This joins each argument into a canonical string representation and parses
312/// them into enum members. It leaves the environment unknown and omits it from
313/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000314Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
315 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000316 Arch(parseArch(ArchStr.str())),
317 Vendor(parseVendor(VendorStr.str())),
318 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000319 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000320}
321
322/// \brief Construct a triple from string representations of the architecture,
323/// vendor, OS, and environment.
324///
Chandler Carruth0523f412012-02-21 08:31:18 +0000325/// This joins each argument into a canonical string representation and parses
326/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000327Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
328 const Twine &EnvironmentStr)
329 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
330 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000331 Arch(parseArch(ArchStr.str())),
332 Vendor(parseVendor(VendorStr.str())),
333 OS(parseOS(OSStr.str())),
334 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000335}
336
Duncan Sands335db222010-08-12 11:31:39 +0000337std::string Triple::normalize(StringRef Str) {
338 // Parse into components.
339 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000340 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000341
342 // If the first component corresponds to a known architecture, preferentially
343 // use it for the architecture. If the second component corresponds to a
344 // known vendor, preferentially use it for the vendor, etc. This avoids silly
345 // component movement when a component parses as (eg) both a valid arch and a
346 // valid os.
347 ArchType Arch = UnknownArch;
348 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000349 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000350 VendorType Vendor = UnknownVendor;
351 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000352 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000353 OSType OS = UnknownOS;
354 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000355 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000356 EnvironmentType Environment = UnknownEnvironment;
357 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000358 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000359
360 // Note which components are already in their final position. These will not
361 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000362 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000363 Found[0] = Arch != UnknownArch;
364 Found[1] = Vendor != UnknownVendor;
365 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000366 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000367
368 // If they are not there already, permute the components into their canonical
369 // positions by seeing if they parse as a valid architecture, and if so moving
370 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000371 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000372 if (Found[Pos])
373 continue; // Already in the canonical position.
374
375 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
376 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000377 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000378 continue;
379
380 // Does this component parse as valid for the target position?
381 bool Valid = false;
382 StringRef Comp = Components[Idx];
383 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000384 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000385 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000386 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000387 Valid = Arch != UnknownArch;
388 break;
389 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000390 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000391 Valid = Vendor != UnknownVendor;
392 break;
393 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000394 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000395 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000396 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000397 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000398 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000399 Valid = Environment != UnknownEnvironment;
400 break;
Duncan Sands335db222010-08-12 11:31:39 +0000401 }
402 if (!Valid)
403 continue; // Nope, try the next component.
404
405 // Move the component to the target position, pushing any non-fixed
406 // components that are in the way to the right. This tends to give
407 // good results in the common cases of a forgotten vendor component
408 // or a wrongly positioned environment.
409 if (Pos < Idx) {
410 // Insert left, pushing the existing components to the right. For
411 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
412 StringRef CurrentComponent(""); // The empty component.
413 // Replace the component we are moving with an empty component.
414 std::swap(CurrentComponent, Components[Idx]);
415 // Insert the component being moved at Pos, displacing any existing
416 // components to the right.
417 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
418 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000419 while (i < array_lengthof(Found) && Found[i])
420 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000421 // Place the component at the new position, getting the component
422 // that was at this position - it will be moved right.
423 std::swap(CurrentComponent, Components[i]);
424 }
425 } else if (Pos > Idx) {
426 // Push right by inserting empty components until the component at Idx
427 // reaches the target position Pos. For example, pc-a -> -pc-a when
428 // moving pc to the second position.
429 do {
430 // Insert one empty component at Idx.
431 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000432 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000433 // Place the component at the new position, getting the component
434 // that was at this position - it will be moved right.
435 std::swap(CurrentComponent, Components[i]);
436 // If it was placed on top of an empty component then we are done.
437 if (CurrentComponent.empty())
438 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000439 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000440 while (++i < array_lengthof(Found) && Found[i])
441 ;
Duncan Sands335db222010-08-12 11:31:39 +0000442 }
443 // The last component was pushed off the end - append it.
444 if (!CurrentComponent.empty())
445 Components.push_back(CurrentComponent);
446
447 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000448 while (++Idx < array_lengthof(Found) && Found[Idx])
449 ;
Duncan Sands335db222010-08-12 11:31:39 +0000450 } while (Idx < Pos); // Add more until the final position is reached.
451 }
452 assert(Pos < Components.size() && Components[Pos] == Comp &&
453 "Component moved wrong!");
454 Found[Pos] = true;
455 break;
456 }
457 }
458
459 // Special case logic goes here. At this point Arch, Vendor and OS have the
460 // correct values for the computed components.
461
462 // Stick the corrected components back together to form the normalized string.
463 std::string Normalized;
464 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
465 if (i) Normalized += '-';
466 Normalized += Components[i];
467 }
468 return Normalized;
469}
470
Daniel Dunbara14d2252009-07-26 03:31:47 +0000471StringRef Triple::getArchName() const {
472 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000473}
474
Daniel Dunbara14d2252009-07-26 03:31:47 +0000475StringRef Triple::getVendorName() const {
476 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
477 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000478}
479
Daniel Dunbara14d2252009-07-26 03:31:47 +0000480StringRef Triple::getOSName() const {
481 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
482 Tmp = Tmp.split('-').second; // Strip second component
483 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000484}
485
Daniel Dunbara14d2252009-07-26 03:31:47 +0000486StringRef Triple::getEnvironmentName() const {
487 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
488 Tmp = Tmp.split('-').second; // Strip second component
489 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000490}
491
Daniel Dunbara14d2252009-07-26 03:31:47 +0000492StringRef Triple::getOSAndEnvironmentName() const {
493 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
494 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000495}
496
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000497static unsigned EatNumber(StringRef &Str) {
498 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000499 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000500
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000501 do {
502 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000503 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000504
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000505 // Eat the digit.
506 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000507 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000508
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000509 return Result;
510}
511
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000512void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
513 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000514 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000515
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000516 // Assume that the OS portion of the triple starts with the canonical name.
517 StringRef OSTypeName = getOSTypeName(getOS());
518 if (OSName.startswith(OSTypeName))
519 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000520
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000521 // Any unset version defaults to 0.
522 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000523
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000524 // Parse up to three components.
525 unsigned *Components[3] = { &Major, &Minor, &Micro };
526 for (unsigned i = 0; i != 3; ++i) {
527 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
528 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000529
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000530 // Consume the leading number.
531 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000532
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000533 // Consume the separator, if present.
534 if (OSName.startswith("."))
535 OSName = OSName.substr(1);
536 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000537}
538
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000539bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
540 unsigned &Micro) const {
541 getOSVersion(Major, Minor, Micro);
542
543 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000544 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000545 case Darwin:
546 // Default to darwin8, i.e., MacOSX 10.4.
547 if (Major == 0)
548 Major = 8;
549 // Darwin version numbers are skewed from OS X versions.
550 if (Major < 4)
551 return false;
552 Micro = 0;
553 Minor = Major - 4;
554 Major = 10;
555 break;
556 case MacOSX:
557 // Default to 10.4.
558 if (Major == 0) {
559 Major = 10;
560 Minor = 4;
561 }
562 if (Major != 10)
563 return false;
564 break;
565 case IOS:
566 // Ignore the version from the triple. This is only handled because the
567 // the clang driver combines OS X and IOS support into a common Darwin
568 // toolchain that wants to know the OS X version number even when targeting
569 // IOS.
570 Major = 10;
571 Minor = 4;
572 Micro = 0;
573 break;
574 }
575 return true;
576}
577
Chad Rosierecee47e2012-05-09 17:23:48 +0000578void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
579 unsigned &Micro) const {
580 switch (getOS()) {
581 default: llvm_unreachable("unexpected OS for Darwin triple");
582 case Darwin:
583 case MacOSX:
584 // Ignore the version from the triple. This is only handled because the
585 // the clang driver combines OS X and IOS support into a common Darwin
586 // toolchain that wants to know the iOS version number even when targeting
587 // OS X.
Chad Rosier537c2f52012-05-09 18:23:00 +0000588 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000589 Minor = 0;
590 Micro = 0;
Chad Rosier26bbdee2012-05-09 17:38:47 +0000591 break;
Chad Rosierecee47e2012-05-09 17:23:48 +0000592 case IOS:
593 getOSVersion(Major, Minor, Micro);
Chad Rosier537c2f52012-05-09 18:23:00 +0000594 // Default to 3.0.
595 if (Major == 0)
596 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000597 break;
598 }
599}
600
Daniel Dunbara14d2252009-07-26 03:31:47 +0000601void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000602 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000603}
604
605void Triple::setArch(ArchType Kind) {
606 setArchName(getArchTypeName(Kind));
607}
608
609void Triple::setVendor(VendorType Kind) {
610 setVendorName(getVendorTypeName(Kind));
611}
612
613void Triple::setOS(OSType Kind) {
614 setOSName(getOSTypeName(Kind));
615}
616
Duncan Sands5754a452010-09-16 08:25:48 +0000617void Triple::setEnvironment(EnvironmentType Kind) {
618 setEnvironmentName(getEnvironmentTypeName(Kind));
619}
620
Daniel Dunbar2928c832009-11-06 10:58:06 +0000621void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000622 // Work around a miscompilation bug for Twines in gcc 4.0.3.
623 SmallString<64> Triple;
624 Triple += Str;
625 Triple += "-";
626 Triple += getVendorName();
627 Triple += "-";
628 Triple += getOSAndEnvironmentName();
629 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000630}
631
Daniel Dunbar2928c832009-11-06 10:58:06 +0000632void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000633 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
634}
635
Daniel Dunbar2928c832009-11-06 10:58:06 +0000636void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000637 if (hasEnvironment())
638 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
639 "-" + getEnvironmentName());
640 else
641 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
642}
643
Daniel Dunbar2928c832009-11-06 10:58:06 +0000644void Triple::setEnvironmentName(StringRef Str) {
645 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000646 "-" + Str);
647}
648
Daniel Dunbar2928c832009-11-06 10:58:06 +0000649void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000650 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
651}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000652
653static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
654 switch (Arch) {
655 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000656 return 0;
657
658 case llvm::Triple::msp430:
659 return 16;
660
661 case llvm::Triple::amdil:
662 case llvm::Triple::arm:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000663 case llvm::Triple::hexagon:
664 case llvm::Triple::le32:
665 case llvm::Triple::mblaze:
666 case llvm::Triple::mips:
667 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000668 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000669 case llvm::Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000670 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000671 case llvm::Triple::sparc:
672 case llvm::Triple::tce:
673 case llvm::Triple::thumb:
674 case llvm::Triple::x86:
675 case llvm::Triple::xcore:
Guy Benyeiac39a032012-11-15 10:35:47 +0000676 case llvm::Triple::spir:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000677 return 32;
678
679 case llvm::Triple::mips64:
680 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000681 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000682 case llvm::Triple::ppc64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000683 case llvm::Triple::sparcv9:
684 case llvm::Triple::x86_64:
Guy Benyeiac39a032012-11-15 10:35:47 +0000685 case llvm::Triple::spir64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000686 return 64;
687 }
688 llvm_unreachable("Invalid architecture value");
689}
690
691bool Triple::isArch64Bit() const {
692 return getArchPointerBitWidth(getArch()) == 64;
693}
694
695bool Triple::isArch32Bit() const {
696 return getArchPointerBitWidth(getArch()) == 32;
697}
698
699bool Triple::isArch16Bit() const {
700 return getArchPointerBitWidth(getArch()) == 16;
701}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000702
703Triple Triple::get32BitArchVariant() const {
704 Triple T(*this);
705 switch (getArch()) {
706 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000707 case Triple::msp430:
708 T.setArch(UnknownArch);
709 break;
710
711 case Triple::amdil:
Micah Villmowe53d6052012-10-01 17:01:31 +0000712 case Triple::spir:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000713 case Triple::arm:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000714 case Triple::hexagon:
715 case Triple::le32:
716 case Triple::mblaze:
717 case Triple::mips:
718 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000719 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000720 case Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000721 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000722 case Triple::sparc:
723 case Triple::tce:
724 case Triple::thumb:
725 case Triple::x86:
726 case Triple::xcore:
727 // Already 32-bit.
728 break;
729
730 case Triple::mips64: T.setArch(Triple::mips); break;
731 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000732 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000733 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000734 case Triple::sparcv9: T.setArch(Triple::sparc); break;
735 case Triple::x86_64: T.setArch(Triple::x86); break;
Guy Benyeiac39a032012-11-15 10:35:47 +0000736 case Triple::spir64: T.setArch(Triple::spir); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000737 }
738 return T;
739}
740
741Triple Triple::get64BitArchVariant() const {
742 Triple T(*this);
743 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000744 case Triple::UnknownArch:
745 case Triple::amdil:
746 case Triple::arm:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000747 case Triple::hexagon:
748 case Triple::le32:
749 case Triple::mblaze:
750 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000751 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000752 case Triple::tce:
753 case Triple::thumb:
754 case Triple::xcore:
755 T.setArch(UnknownArch);
756 break;
757
Guy Benyeiac39a032012-11-15 10:35:47 +0000758 case Triple::spir64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000759 case Triple::mips64:
760 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000761 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000762 case Triple::ppc64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000763 case Triple::sparcv9:
764 case Triple::x86_64:
765 // Already 64-bit.
766 break;
767
768 case Triple::mips: T.setArch(Triple::mips64); break;
769 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000770 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000771 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000772 case Triple::sparc: T.setArch(Triple::sparcv9); break;
773 case Triple::x86: T.setArch(Triple::x86_64); break;
Guy Benyeiac39a032012-11-15 10:35:47 +0000774 case Triple::spir: T.setArch(Triple::spir64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000775 }
776 return T;
777}