blob: 7bf65f72a77c5396d89f7357337b04ef80638651 [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"
Jeffrey Yasskin0b228732009-10-06 21:45:26 +000011#include "llvm/ADT/SmallString.h"
Chandler Carruth06accda2012-02-12 09:27:38 +000012#include "llvm/ADT/StringSwitch.h"
Duncan Sands5754a452010-09-16 08:25:48 +000013#include "llvm/ADT/STLExtras.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";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000045 }
46
David Blaikie4d6ccb52012-01-20 21:51:11 +000047 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000048}
49
Daniel Dunbar688b55b2009-08-24 09:53:06 +000050const char *Triple::getArchTypePrefix(ArchType Kind) {
51 switch (Kind) {
52 default:
53 return 0;
54
Daniel Dunbar688b55b2009-08-24 09:53:06 +000055 case arm:
56 case thumb: return "arm";
57
Daniel Dunbar688b55b2009-08-24 09:53:06 +000058 case ppc64:
59 case ppc: return "ppc";
60
Wesley Pecka70f28c2010-02-23 19:15:24 +000061 case mblaze: return "mblaze";
62
Benjamin Kramerd5dbc8c2012-06-28 19:09:53 +000063 case mips:
64 case mipsel:
65 case mips64:
66 case mips64el:return "mips";
67
68 case hexagon: return "hexagon";
Tony Linthicumb4b54152011-12-12 21:14:40 +000069
Anton Korobeynikov74156592012-03-09 10:09:36 +000070 case r600: return "r600";
71
Chris Lattner87c06d62010-02-04 06:34:01 +000072 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000073 case sparc: return "sparc";
74
75 case x86:
76 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000077
Daniel Dunbar688b55b2009-08-24 09:53:06 +000078 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000079
Justin Holewinski49683f32012-05-04 20:18:50 +000080 case nvptx: return "nvptx";
81 case nvptx64: return "nvptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000082 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000083 case amdil: return "amdil";
Micah Villmowe53d6052012-10-01 17:01:31 +000084 case spir: return "spir";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000085 }
86}
87
Daniel Dunbar23e97b02009-04-01 21:53:23 +000088const char *Triple::getVendorTypeName(VendorType Kind) {
89 switch (Kind) {
90 case UnknownVendor: return "unknown";
91
92 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000093 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000094 case SCEI: return "scei";
Hal Finkela47406c2012-04-02 18:31:33 +000095 case BGP: return "bgp";
96 case BGQ: return "bgq";
Hal Finkeld939cd62012-08-28 02:10:30 +000097 case Freescale: return "fsl";
Duncan Sands2e522d02012-10-12 11:08:57 +000098 case IBM: return "ibm";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000099 }
100
David Blaikie4d6ccb52012-01-20 21:51:11 +0000101 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000102}
103
104const char *Triple::getOSTypeName(OSType Kind) {
105 switch (Kind) {
106 case UnknownOS: return "unknown";
107
Duncan Sands852cd112009-06-19 14:40:01 +0000108 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000109 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000110 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000111 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000112 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000113 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000114 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000115 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000116 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000117 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000118 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000119 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000120 case OpenBSD: return "openbsd";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000121 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000122 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000123 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000124 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000125 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000126 case NativeClient: return "nacl";
Hal Finkela47406c2012-04-02 18:31:33 +0000127 case CNK: return "cnk";
Eric Christopherb0f67592012-08-06 20:52:18 +0000128 case Bitrig: return "bitrig";
Duncan Sands2e522d02012-10-12 11:08:57 +0000129 case AIX: return "aix";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000130 }
131
David Blaikie4d6ccb52012-01-20 21:51:11 +0000132 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000133}
134
Duncan Sands5754a452010-09-16 08:25:48 +0000135const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
136 switch (Kind) {
137 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000138 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000139 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000140 case GNUEABI: return "gnueabi";
141 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000142 case MachO: return "macho";
Logan Chien43bf7092012-09-02 09:29:46 +0000143 case Android: return "android";
Andrew Kaylor7bbd6e32012-10-02 18:38:34 +0000144 case ELF: return "elf";
Duncan Sands5754a452010-09-16 08:25:48 +0000145 }
146
David Blaikie4d6ccb52012-01-20 21:51:11 +0000147 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000148}
149
Daniel Dunbar2928c832009-11-06 10:58:06 +0000150Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000151 return StringSwitch<Triple::ArchType>(Name)
152 .Case("arm", arm)
Chandler Carruth06accda2012-02-12 09:27:38 +0000153 .Case("mips", mips)
154 .Case("mipsel", mipsel)
155 .Case("mips64", mips64)
156 .Case("mips64el", mips64el)
157 .Case("msp430", msp430)
158 .Case("ppc64", ppc64)
159 .Case("ppc32", ppc)
160 .Case("ppc", ppc)
161 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000162 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000163 .Case("hexagon", hexagon)
164 .Case("sparc", sparc)
165 .Case("sparcv9", sparcv9)
166 .Case("tce", tce)
167 .Case("thumb", thumb)
168 .Case("x86", x86)
169 .Case("x86-64", x86_64)
170 .Case("xcore", xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000171 .Case("nvptx", nvptx)
172 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000173 .Case("le32", le32)
174 .Case("amdil", amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000175 .Case("spir", spir)
Chandler Carruth06accda2012-02-12 09:27:38 +0000176 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000177}
178
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000179// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000180const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000181 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000182 return NULL;
183
Chandler Carruth06accda2012-02-12 09:27:38 +0000184 return StringSwitch<const char*>(getArchName())
185 .Case("i386", "i386")
186 .Case("x86_64", "x86_64")
187 .Case("powerpc", "ppc")
188 .Case("powerpc64", "ppc64")
189 .Cases("mblaze", "microblaze", "mblaze")
190 .Case("arm", "arm")
191 .Cases("armv4t", "thumbv4t", "armv4t")
192 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
193 .Cases("armv6", "thumbv6", "armv6")
194 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000195 .Case("r600", "r600")
Justin Holewinski49683f32012-05-04 20:18:50 +0000196 .Case("nvptx", "nvptx")
197 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000198 .Case("le32", "le32")
199 .Case("amdil", "amdil")
Micah Villmowe53d6052012-10-01 17:01:31 +0000200 .Case("spir", "spir")
Chandler Carruth06accda2012-02-12 09:27:38 +0000201 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000202}
203
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000204static Triple::ArchType parseArch(StringRef ArchName) {
205 return StringSwitch<Triple::ArchType>(ArchName)
206 .Cases("i386", "i486", "i586", "i686", Triple::x86)
207 // FIXME: Do we need to support these?
208 .Cases("i786", "i886", "i986", Triple::x86)
209 .Cases("amd64", "x86_64", Triple::x86_64)
210 .Case("powerpc", Triple::ppc)
211 .Cases("powerpc64", "ppu", Triple::ppc64)
212 .Case("mblaze", Triple::mblaze)
213 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000214 // FIXME: It would be good to replace these with explicit names for all the
215 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000216 .StartsWith("armv", Triple::arm)
217 .Case("thumb", Triple::thumb)
218 .StartsWith("thumbv", Triple::thumb)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000219 .Case("msp430", Triple::msp430)
220 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000221 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000222 .Cases("mips64", "mips64eb", Triple::mips64)
223 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000224 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000225 .Case("hexagon", Triple::hexagon)
226 .Case("sparc", Triple::sparc)
227 .Case("sparcv9", Triple::sparcv9)
228 .Case("tce", Triple::tce)
229 .Case("xcore", Triple::xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000230 .Case("nvptx", Triple::nvptx)
231 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000232 .Case("le32", Triple::le32)
233 .Case("amdil", Triple::amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000234 .Case("spir", Triple::spir)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000235 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000236}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000237
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000238static Triple::VendorType parseVendor(StringRef VendorName) {
239 return StringSwitch<Triple::VendorType>(VendorName)
240 .Case("apple", Triple::Apple)
241 .Case("pc", Triple::PC)
242 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000243 .Case("bgp", Triple::BGP)
244 .Case("bgq", Triple::BGQ)
Hal Finkeld939cd62012-08-28 02:10:30 +0000245 .Case("fsl", Triple::Freescale)
Duncan Sands2e522d02012-10-12 11:08:57 +0000246 .Case("ibm", Triple::IBM)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000247 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000248}
249
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000250static Triple::OSType parseOS(StringRef OSName) {
251 return StringSwitch<Triple::OSType>(OSName)
252 .StartsWith("auroraux", Triple::AuroraUX)
253 .StartsWith("cygwin", Triple::Cygwin)
254 .StartsWith("darwin", Triple::Darwin)
255 .StartsWith("dragonfly", Triple::DragonFly)
256 .StartsWith("freebsd", Triple::FreeBSD)
257 .StartsWith("ios", Triple::IOS)
258 .StartsWith("kfreebsd", Triple::KFreeBSD)
259 .StartsWith("linux", Triple::Linux)
260 .StartsWith("lv2", Triple::Lv2)
261 .StartsWith("macosx", Triple::MacOSX)
262 .StartsWith("mingw32", Triple::MinGW32)
263 .StartsWith("netbsd", Triple::NetBSD)
264 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000265 .StartsWith("solaris", Triple::Solaris)
266 .StartsWith("win32", Triple::Win32)
267 .StartsWith("haiku", Triple::Haiku)
268 .StartsWith("minix", Triple::Minix)
269 .StartsWith("rtems", Triple::RTEMS)
270 .StartsWith("nacl", Triple::NativeClient)
Hal Finkela47406c2012-04-02 18:31:33 +0000271 .StartsWith("cnk", Triple::CNK)
Eric Christopherb0f67592012-08-06 20:52:18 +0000272 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sands2e522d02012-10-12 11:08:57 +0000273 .StartsWith("aix", Triple::AIX)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000274 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000275}
276
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000277static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
278 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
279 .StartsWith("eabi", Triple::EABI)
280 .StartsWith("gnueabihf", Triple::GNUEABIHF)
281 .StartsWith("gnueabi", Triple::GNUEABI)
282 .StartsWith("gnu", Triple::GNU)
283 .StartsWith("macho", Triple::MachO)
Logan Chien43bf7092012-09-02 09:29:46 +0000284 .StartsWith("android", Triple::Android)
Andrew Kaylor7bbd6e32012-10-02 18:38:34 +0000285 .StartsWith("elf", Triple::ELF)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000286 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000287}
288
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000289/// \brief Construct a triple from the string representation provided.
290///
Chandler Carruth0523f412012-02-21 08:31:18 +0000291/// This stores the string representation and parses the various pieces into
292/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000293Triple::Triple(const Twine &Str)
294 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000295 Arch(parseArch(getArchName())),
296 Vendor(parseVendor(getVendorName())),
297 OS(parseOS(getOSName())),
298 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000299}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000300
301/// \brief Construct a triple from string representations of the architecture,
302/// vendor, and OS.
303///
Chandler Carruth0523f412012-02-21 08:31:18 +0000304/// This joins each argument into a canonical string representation and parses
305/// them into enum members. It leaves the environment unknown and omits it from
306/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000307Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
308 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000309 Arch(parseArch(ArchStr.str())),
310 Vendor(parseVendor(VendorStr.str())),
311 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000312 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000313}
314
315/// \brief Construct a triple from string representations of the architecture,
316/// vendor, OS, and environment.
317///
Chandler Carruth0523f412012-02-21 08:31:18 +0000318/// This joins each argument into a canonical string representation and parses
319/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000320Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
321 const Twine &EnvironmentStr)
322 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
323 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000324 Arch(parseArch(ArchStr.str())),
325 Vendor(parseVendor(VendorStr.str())),
326 OS(parseOS(OSStr.str())),
327 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000328}
329
Duncan Sands335db222010-08-12 11:31:39 +0000330std::string Triple::normalize(StringRef Str) {
331 // Parse into components.
332 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000333 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000334
335 // If the first component corresponds to a known architecture, preferentially
336 // use it for the architecture. If the second component corresponds to a
337 // known vendor, preferentially use it for the vendor, etc. This avoids silly
338 // component movement when a component parses as (eg) both a valid arch and a
339 // valid os.
340 ArchType Arch = UnknownArch;
341 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000342 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000343 VendorType Vendor = UnknownVendor;
344 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000345 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000346 OSType OS = UnknownOS;
347 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000348 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000349 EnvironmentType Environment = UnknownEnvironment;
350 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000351 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000352
353 // Note which components are already in their final position. These will not
354 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000355 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000356 Found[0] = Arch != UnknownArch;
357 Found[1] = Vendor != UnknownVendor;
358 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000359 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000360
361 // If they are not there already, permute the components into their canonical
362 // positions by seeing if they parse as a valid architecture, and if so moving
363 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000364 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000365 if (Found[Pos])
366 continue; // Already in the canonical position.
367
368 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
369 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000370 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000371 continue;
372
373 // Does this component parse as valid for the target position?
374 bool Valid = false;
375 StringRef Comp = Components[Idx];
376 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000377 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000378 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000379 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000380 Valid = Arch != UnknownArch;
381 break;
382 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000383 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000384 Valid = Vendor != UnknownVendor;
385 break;
386 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000387 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000388 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000389 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000390 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000391 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000392 Valid = Environment != UnknownEnvironment;
393 break;
Duncan Sands335db222010-08-12 11:31:39 +0000394 }
395 if (!Valid)
396 continue; // Nope, try the next component.
397
398 // Move the component to the target position, pushing any non-fixed
399 // components that are in the way to the right. This tends to give
400 // good results in the common cases of a forgotten vendor component
401 // or a wrongly positioned environment.
402 if (Pos < Idx) {
403 // Insert left, pushing the existing components to the right. For
404 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
405 StringRef CurrentComponent(""); // The empty component.
406 // Replace the component we are moving with an empty component.
407 std::swap(CurrentComponent, Components[Idx]);
408 // Insert the component being moved at Pos, displacing any existing
409 // components to the right.
410 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
411 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000412 while (i < array_lengthof(Found) && Found[i])
413 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000414 // Place the component at the new position, getting the component
415 // that was at this position - it will be moved right.
416 std::swap(CurrentComponent, Components[i]);
417 }
418 } else if (Pos > Idx) {
419 // Push right by inserting empty components until the component at Idx
420 // reaches the target position Pos. For example, pc-a -> -pc-a when
421 // moving pc to the second position.
422 do {
423 // Insert one empty component at Idx.
424 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000425 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000426 // Place the component at the new position, getting the component
427 // that was at this position - it will be moved right.
428 std::swap(CurrentComponent, Components[i]);
429 // If it was placed on top of an empty component then we are done.
430 if (CurrentComponent.empty())
431 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000432 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000433 while (++i < array_lengthof(Found) && Found[i])
434 ;
Duncan Sands335db222010-08-12 11:31:39 +0000435 }
436 // The last component was pushed off the end - append it.
437 if (!CurrentComponent.empty())
438 Components.push_back(CurrentComponent);
439
440 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000441 while (++Idx < array_lengthof(Found) && Found[Idx])
442 ;
Duncan Sands335db222010-08-12 11:31:39 +0000443 } while (Idx < Pos); // Add more until the final position is reached.
444 }
445 assert(Pos < Components.size() && Components[Pos] == Comp &&
446 "Component moved wrong!");
447 Found[Pos] = true;
448 break;
449 }
450 }
451
452 // Special case logic goes here. At this point Arch, Vendor and OS have the
453 // correct values for the computed components.
454
455 // Stick the corrected components back together to form the normalized string.
456 std::string Normalized;
457 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
458 if (i) Normalized += '-';
459 Normalized += Components[i];
460 }
461 return Normalized;
462}
463
Daniel Dunbara14d2252009-07-26 03:31:47 +0000464StringRef Triple::getArchName() const {
465 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000466}
467
Daniel Dunbara14d2252009-07-26 03:31:47 +0000468StringRef Triple::getVendorName() const {
469 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
470 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000471}
472
Daniel Dunbara14d2252009-07-26 03:31:47 +0000473StringRef Triple::getOSName() const {
474 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
475 Tmp = Tmp.split('-').second; // Strip second component
476 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000477}
478
Daniel Dunbara14d2252009-07-26 03:31:47 +0000479StringRef Triple::getEnvironmentName() const {
480 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
481 Tmp = Tmp.split('-').second; // Strip second component
482 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000483}
484
Daniel Dunbara14d2252009-07-26 03:31:47 +0000485StringRef Triple::getOSAndEnvironmentName() const {
486 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
487 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000488}
489
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000490static unsigned EatNumber(StringRef &Str) {
491 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000492 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000493
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000494 do {
495 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000496 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000497
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000498 // Eat the digit.
499 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000500 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000501
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000502 return Result;
503}
504
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000505void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
506 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000507 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000508
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000509 // Assume that the OS portion of the triple starts with the canonical name.
510 StringRef OSTypeName = getOSTypeName(getOS());
511 if (OSName.startswith(OSTypeName))
512 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000513
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000514 // Any unset version defaults to 0.
515 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000516
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000517 // Parse up to three components.
518 unsigned *Components[3] = { &Major, &Minor, &Micro };
519 for (unsigned i = 0; i != 3; ++i) {
520 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
521 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000522
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000523 // Consume the leading number.
524 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000525
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000526 // Consume the separator, if present.
527 if (OSName.startswith("."))
528 OSName = OSName.substr(1);
529 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000530}
531
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000532bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
533 unsigned &Micro) const {
534 getOSVersion(Major, Minor, Micro);
535
536 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000537 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000538 case Darwin:
539 // Default to darwin8, i.e., MacOSX 10.4.
540 if (Major == 0)
541 Major = 8;
542 // Darwin version numbers are skewed from OS X versions.
543 if (Major < 4)
544 return false;
545 Micro = 0;
546 Minor = Major - 4;
547 Major = 10;
548 break;
549 case MacOSX:
550 // Default to 10.4.
551 if (Major == 0) {
552 Major = 10;
553 Minor = 4;
554 }
555 if (Major != 10)
556 return false;
557 break;
558 case IOS:
559 // Ignore the version from the triple. This is only handled because the
560 // the clang driver combines OS X and IOS support into a common Darwin
561 // toolchain that wants to know the OS X version number even when targeting
562 // IOS.
563 Major = 10;
564 Minor = 4;
565 Micro = 0;
566 break;
567 }
568 return true;
569}
570
Chad Rosierecee47e2012-05-09 17:23:48 +0000571void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
572 unsigned &Micro) const {
573 switch (getOS()) {
574 default: llvm_unreachable("unexpected OS for Darwin triple");
575 case Darwin:
576 case MacOSX:
577 // Ignore the version from the triple. This is only handled because the
578 // the clang driver combines OS X and IOS support into a common Darwin
579 // toolchain that wants to know the iOS version number even when targeting
580 // OS X.
Chad Rosier537c2f52012-05-09 18:23:00 +0000581 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000582 Minor = 0;
583 Micro = 0;
Chad Rosier26bbdee2012-05-09 17:38:47 +0000584 break;
Chad Rosierecee47e2012-05-09 17:23:48 +0000585 case IOS:
586 getOSVersion(Major, Minor, Micro);
Chad Rosier537c2f52012-05-09 18:23:00 +0000587 // Default to 3.0.
588 if (Major == 0)
589 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000590 break;
591 }
592}
593
Daniel Dunbara14d2252009-07-26 03:31:47 +0000594void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000595 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000596}
597
598void Triple::setArch(ArchType Kind) {
599 setArchName(getArchTypeName(Kind));
600}
601
602void Triple::setVendor(VendorType Kind) {
603 setVendorName(getVendorTypeName(Kind));
604}
605
606void Triple::setOS(OSType Kind) {
607 setOSName(getOSTypeName(Kind));
608}
609
Duncan Sands5754a452010-09-16 08:25:48 +0000610void Triple::setEnvironment(EnvironmentType Kind) {
611 setEnvironmentName(getEnvironmentTypeName(Kind));
612}
613
Daniel Dunbar2928c832009-11-06 10:58:06 +0000614void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000615 // Work around a miscompilation bug for Twines in gcc 4.0.3.
616 SmallString<64> Triple;
617 Triple += Str;
618 Triple += "-";
619 Triple += getVendorName();
620 Triple += "-";
621 Triple += getOSAndEnvironmentName();
622 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000623}
624
Daniel Dunbar2928c832009-11-06 10:58:06 +0000625void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000626 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
627}
628
Daniel Dunbar2928c832009-11-06 10:58:06 +0000629void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000630 if (hasEnvironment())
631 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
632 "-" + getEnvironmentName());
633 else
634 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
635}
636
Daniel Dunbar2928c832009-11-06 10:58:06 +0000637void Triple::setEnvironmentName(StringRef Str) {
638 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000639 "-" + Str);
640}
641
Daniel Dunbar2928c832009-11-06 10:58:06 +0000642void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000643 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
644}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000645
646static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
647 switch (Arch) {
Micah Villmowe53d6052012-10-01 17:01:31 +0000648 case llvm::Triple::spir:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000649 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000650 return 0;
651
652 case llvm::Triple::msp430:
653 return 16;
654
655 case llvm::Triple::amdil:
656 case llvm::Triple::arm:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000657 case llvm::Triple::hexagon:
658 case llvm::Triple::le32:
659 case llvm::Triple::mblaze:
660 case llvm::Triple::mips:
661 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000662 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000663 case llvm::Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000664 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000665 case llvm::Triple::sparc:
666 case llvm::Triple::tce:
667 case llvm::Triple::thumb:
668 case llvm::Triple::x86:
669 case llvm::Triple::xcore:
670 return 32;
671
672 case llvm::Triple::mips64:
673 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000674 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000675 case llvm::Triple::ppc64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000676 case llvm::Triple::sparcv9:
677 case llvm::Triple::x86_64:
678 return 64;
679 }
680 llvm_unreachable("Invalid architecture value");
681}
682
683bool Triple::isArch64Bit() const {
684 return getArchPointerBitWidth(getArch()) == 64;
685}
686
687bool Triple::isArch32Bit() const {
688 return getArchPointerBitWidth(getArch()) == 32;
689}
690
691bool Triple::isArch16Bit() const {
692 return getArchPointerBitWidth(getArch()) == 16;
693}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000694
695Triple Triple::get32BitArchVariant() const {
696 Triple T(*this);
697 switch (getArch()) {
698 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000699 case Triple::msp430:
700 T.setArch(UnknownArch);
701 break;
702
703 case Triple::amdil:
Micah Villmowe53d6052012-10-01 17:01:31 +0000704 case Triple::spir:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000705 case Triple::arm:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000706 case Triple::hexagon:
707 case Triple::le32:
708 case Triple::mblaze:
709 case Triple::mips:
710 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000711 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000712 case Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000713 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000714 case Triple::sparc:
715 case Triple::tce:
716 case Triple::thumb:
717 case Triple::x86:
718 case Triple::xcore:
719 // Already 32-bit.
720 break;
721
722 case Triple::mips64: T.setArch(Triple::mips); break;
723 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000724 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000725 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000726 case Triple::sparcv9: T.setArch(Triple::sparc); break;
727 case Triple::x86_64: T.setArch(Triple::x86); break;
728 }
729 return T;
730}
731
732Triple Triple::get64BitArchVariant() const {
733 Triple T(*this);
734 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000735 case Triple::UnknownArch:
736 case Triple::amdil:
737 case Triple::arm:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000738 case Triple::hexagon:
739 case Triple::le32:
740 case Triple::mblaze:
741 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000742 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000743 case Triple::tce:
744 case Triple::thumb:
745 case Triple::xcore:
746 T.setArch(UnknownArch);
747 break;
748
Micah Villmowe53d6052012-10-01 17:01:31 +0000749 case Triple::spir:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000750 case Triple::mips64:
751 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000752 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000753 case Triple::ppc64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000754 case Triple::sparcv9:
755 case Triple::x86_64:
756 // Already 64-bit.
757 break;
758
759 case Triple::mips: T.setArch(Triple::mips64); break;
760 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000761 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000762 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000763 case Triple::sparc: T.setArch(Triple::sparcv9); break;
764 case Triple::x86: T.setArch(Triple::x86_64); break;
765 }
766 return T;
767}