blob: 2cc7a5846240147cfa48afefc86e8f8e049a3297 [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";
23 case cellspu: return "cellspu";
Tony Linthicumb4b54152011-12-12 21:14:40 +000024 case hexagon: return "hexagon";
Daniel Dunbar6337f152009-07-26 04:23:03 +000025 case mips: return "mips";
26 case mipsel: return "mipsel";
Akira Hatanaka70303682011-09-20 18:09:37 +000027 case mips64: return "mips64";
28 case mips64el:return "mips64el";
Daniel Dunbar6337f152009-07-26 04:23:03 +000029 case msp430: return "msp430";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000030 case ppc64: return "powerpc64";
31 case ppc: return "powerpc";
Anton Korobeynikov74156592012-03-09 10:09:36 +000032 case r600: return "r600";
Daniel Dunbar6337f152009-07-26 04:23:03 +000033 case sparc: return "sparc";
Chris Lattner87c06d62010-02-04 06:34:01 +000034 case sparcv9: return "sparcv9";
Eli Friedman74db89e2009-08-19 20:46:03 +000035 case tce: return "tce";
Daniel Dunbar6337f152009-07-26 04:23:03 +000036 case thumb: return "thumb";
37 case x86: return "i386";
38 case x86_64: return "x86_64";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000039 case xcore: return "xcore";
Wesley Pecka70f28c2010-02-23 19:15:24 +000040 case mblaze: return "mblaze";
Justin Holewinski49683f32012-05-04 20:18:50 +000041 case nvptx: return "nvptx";
42 case nvptx64: return "nvptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000043 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000044 case amdil: return "amdil";
Micah Villmowe53d6052012-10-01 17:01:31 +000045 case spir: return "spir";
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 cellspu: return "spu";
60
61 case ppc64:
62 case ppc: return "ppc";
63
Wesley Pecka70f28c2010-02-23 19:15:24 +000064 case mblaze: return "mblaze";
65
Benjamin Kramerd5dbc8c2012-06-28 19:09:53 +000066 case mips:
67 case mipsel:
68 case mips64:
69 case mips64el:return "mips";
70
71 case hexagon: return "hexagon";
Tony Linthicumb4b54152011-12-12 21:14:40 +000072
Anton Korobeynikov74156592012-03-09 10:09:36 +000073 case r600: return "r600";
74
Chris Lattner87c06d62010-02-04 06:34:01 +000075 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000076 case sparc: return "sparc";
77
78 case x86:
79 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000080
Daniel Dunbar688b55b2009-08-24 09:53:06 +000081 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000082
Justin Holewinski49683f32012-05-04 20:18:50 +000083 case nvptx: return "nvptx";
84 case nvptx64: return "nvptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000085 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000086 case amdil: return "amdil";
Micah Villmowe53d6052012-10-01 17:01:31 +000087 case spir: return "spir";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000088 }
89}
90
Daniel Dunbar23e97b02009-04-01 21:53:23 +000091const char *Triple::getVendorTypeName(VendorType Kind) {
92 switch (Kind) {
93 case UnknownVendor: return "unknown";
94
95 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000096 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000097 case SCEI: return "scei";
Hal Finkela47406c2012-04-02 18:31:33 +000098 case BGP: return "bgp";
99 case BGQ: return "bgq";
Hal Finkeld939cd62012-08-28 02:10:30 +0000100 case Freescale: return "fsl";
Duncan Sands2e522d02012-10-12 11:08:57 +0000101 case IBM: return "ibm";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000102 }
103
David Blaikie4d6ccb52012-01-20 21:51:11 +0000104 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000105}
106
107const char *Triple::getOSTypeName(OSType Kind) {
108 switch (Kind) {
109 case UnknownOS: return "unknown";
110
Duncan Sands852cd112009-06-19 14:40:01 +0000111 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000112 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000113 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000114 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000115 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000116 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000117 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000118 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000119 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000120 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000121 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000122 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000123 case OpenBSD: return "openbsd";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000124 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000125 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000126 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000127 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000128 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000129 case NativeClient: return "nacl";
Hal Finkela47406c2012-04-02 18:31:33 +0000130 case CNK: return "cnk";
Eric Christopherb0f67592012-08-06 20:52:18 +0000131 case Bitrig: return "bitrig";
Duncan Sands2e522d02012-10-12 11:08:57 +0000132 case AIX: return "aix";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000133 }
134
David Blaikie4d6ccb52012-01-20 21:51:11 +0000135 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000136}
137
Duncan Sands5754a452010-09-16 08:25:48 +0000138const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
139 switch (Kind) {
140 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000141 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000142 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000143 case GNUEABI: return "gnueabi";
144 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)
156 .Case("cellspu", cellspu)
157 .Case("mips", mips)
158 .Case("mipsel", mipsel)
159 .Case("mips64", mips64)
160 .Case("mips64el", mips64el)
161 .Case("msp430", msp430)
162 .Case("ppc64", ppc64)
163 .Case("ppc32", ppc)
164 .Case("ppc", ppc)
165 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000166 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000167 .Case("hexagon", hexagon)
168 .Case("sparc", sparc)
169 .Case("sparcv9", sparcv9)
170 .Case("tce", tce)
171 .Case("thumb", thumb)
172 .Case("x86", x86)
173 .Case("x86-64", x86_64)
174 .Case("xcore", xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000175 .Case("nvptx", nvptx)
176 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000177 .Case("le32", le32)
178 .Case("amdil", amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000179 .Case("spir", spir)
Chandler Carruth06accda2012-02-12 09:27:38 +0000180 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000181}
182
Daniel Dunbar2928c832009-11-06 10:58:06 +0000183Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000184 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
185 // archs which Darwin doesn't use.
186
187 // The matching this routine does is fairly pointless, since it is neither the
188 // complete architecture list, nor a reasonable subset. The problem is that
189 // historically the driver driver accepts this and also ties its -march=
190 // handling to the architecture name, so we need to be careful before removing
191 // support for it.
192
Daniel Dunbared687882009-09-09 23:01:25 +0000193 // This code must be kept in sync with Clang's Darwin specific argument
194 // translation.
195
Chandler Carruth06accda2012-02-12 09:27:38 +0000196 return StringSwitch<ArchType>(Str)
197 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
198 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
199 .Case("ppc64", Triple::ppc64)
200 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
201 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
202 Triple::x86)
203 .Case("x86_64", Triple::x86_64)
204 // This is derived from the driver driver.
205 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
206 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000207 .Case("r600", Triple::r600)
Justin Holewinski49683f32012-05-04 20:18:50 +0000208 .Case("nvptx", Triple::nvptx)
209 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000210 .Case("amdil", Triple::amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000211 .Case("spir", Triple::spir)
Chandler Carruth06accda2012-02-12 09:27:38 +0000212 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000213}
214
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000215// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000216const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000217 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000218 return NULL;
219
Chandler Carruth06accda2012-02-12 09:27:38 +0000220 return StringSwitch<const char*>(getArchName())
221 .Case("i386", "i386")
222 .Case("x86_64", "x86_64")
223 .Case("powerpc", "ppc")
224 .Case("powerpc64", "ppc64")
225 .Cases("mblaze", "microblaze", "mblaze")
226 .Case("arm", "arm")
227 .Cases("armv4t", "thumbv4t", "armv4t")
228 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
229 .Cases("armv6", "thumbv6", "armv6")
230 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000231 .Case("r600", "r600")
Justin Holewinski49683f32012-05-04 20:18:50 +0000232 .Case("nvptx", "nvptx")
233 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000234 .Case("le32", "le32")
235 .Case("amdil", "amdil")
Micah Villmowe53d6052012-10-01 17:01:31 +0000236 .Case("spir", "spir")
Chandler Carruth06accda2012-02-12 09:27:38 +0000237 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000238}
239
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000240static Triple::ArchType parseArch(StringRef ArchName) {
241 return StringSwitch<Triple::ArchType>(ArchName)
242 .Cases("i386", "i486", "i586", "i686", Triple::x86)
243 // FIXME: Do we need to support these?
244 .Cases("i786", "i886", "i986", Triple::x86)
245 .Cases("amd64", "x86_64", Triple::x86_64)
246 .Case("powerpc", Triple::ppc)
247 .Cases("powerpc64", "ppu", Triple::ppc64)
248 .Case("mblaze", Triple::mblaze)
249 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000250 // FIXME: It would be good to replace these with explicit names for all the
251 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000252 .StartsWith("armv", Triple::arm)
253 .Case("thumb", Triple::thumb)
254 .StartsWith("thumbv", Triple::thumb)
255 .Cases("spu", "cellspu", Triple::cellspu)
256 .Case("msp430", Triple::msp430)
257 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000258 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000259 .Cases("mips64", "mips64eb", Triple::mips64)
260 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000261 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000262 .Case("hexagon", Triple::hexagon)
263 .Case("sparc", Triple::sparc)
264 .Case("sparcv9", Triple::sparcv9)
265 .Case("tce", Triple::tce)
266 .Case("xcore", Triple::xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000267 .Case("nvptx", Triple::nvptx)
268 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000269 .Case("le32", Triple::le32)
270 .Case("amdil", Triple::amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000271 .Case("spir", Triple::spir)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000272 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000273}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000274
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000275static Triple::VendorType parseVendor(StringRef VendorName) {
276 return StringSwitch<Triple::VendorType>(VendorName)
277 .Case("apple", Triple::Apple)
278 .Case("pc", Triple::PC)
279 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000280 .Case("bgp", Triple::BGP)
281 .Case("bgq", Triple::BGQ)
Hal Finkeld939cd62012-08-28 02:10:30 +0000282 .Case("fsl", Triple::Freescale)
Duncan Sands2e522d02012-10-12 11:08:57 +0000283 .Case("ibm", Triple::IBM)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000284 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000285}
286
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000287static Triple::OSType parseOS(StringRef OSName) {
288 return StringSwitch<Triple::OSType>(OSName)
289 .StartsWith("auroraux", Triple::AuroraUX)
290 .StartsWith("cygwin", Triple::Cygwin)
291 .StartsWith("darwin", Triple::Darwin)
292 .StartsWith("dragonfly", Triple::DragonFly)
293 .StartsWith("freebsd", Triple::FreeBSD)
294 .StartsWith("ios", Triple::IOS)
295 .StartsWith("kfreebsd", Triple::KFreeBSD)
296 .StartsWith("linux", Triple::Linux)
297 .StartsWith("lv2", Triple::Lv2)
298 .StartsWith("macosx", Triple::MacOSX)
299 .StartsWith("mingw32", Triple::MinGW32)
300 .StartsWith("netbsd", Triple::NetBSD)
301 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000302 .StartsWith("solaris", Triple::Solaris)
303 .StartsWith("win32", Triple::Win32)
304 .StartsWith("haiku", Triple::Haiku)
305 .StartsWith("minix", Triple::Minix)
306 .StartsWith("rtems", Triple::RTEMS)
307 .StartsWith("nacl", Triple::NativeClient)
Hal Finkela47406c2012-04-02 18:31:33 +0000308 .StartsWith("cnk", Triple::CNK)
Eric Christopherb0f67592012-08-06 20:52:18 +0000309 .StartsWith("bitrig", Triple::Bitrig)
Duncan Sands2e522d02012-10-12 11:08:57 +0000310 .StartsWith("aix", Triple::AIX)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000311 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000312}
313
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000314static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
315 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
316 .StartsWith("eabi", Triple::EABI)
317 .StartsWith("gnueabihf", Triple::GNUEABIHF)
318 .StartsWith("gnueabi", Triple::GNUEABI)
319 .StartsWith("gnu", Triple::GNU)
320 .StartsWith("macho", Triple::MachO)
Logan Chien43bf7092012-09-02 09:29:46 +0000321 .StartsWith("android", Triple::Android)
Andrew Kaylor7bbd6e32012-10-02 18:38:34 +0000322 .StartsWith("elf", Triple::ELF)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000323 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000324}
325
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000326/// \brief Construct a triple from the string representation provided.
327///
Chandler Carruth0523f412012-02-21 08:31:18 +0000328/// This stores the string representation and parses the various pieces into
329/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000330Triple::Triple(const Twine &Str)
331 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000332 Arch(parseArch(getArchName())),
333 Vendor(parseVendor(getVendorName())),
334 OS(parseOS(getOSName())),
335 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000336}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000337
338/// \brief Construct a triple from string representations of the architecture,
339/// vendor, and OS.
340///
Chandler Carruth0523f412012-02-21 08:31:18 +0000341/// This joins each argument into a canonical string representation and parses
342/// them into enum members. It leaves the environment unknown and omits it from
343/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000344Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
345 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000346 Arch(parseArch(ArchStr.str())),
347 Vendor(parseVendor(VendorStr.str())),
348 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000349 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000350}
351
352/// \brief Construct a triple from string representations of the architecture,
353/// vendor, OS, and environment.
354///
Chandler Carruth0523f412012-02-21 08:31:18 +0000355/// This joins each argument into a canonical string representation and parses
356/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000357Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
358 const Twine &EnvironmentStr)
359 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
360 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000361 Arch(parseArch(ArchStr.str())),
362 Vendor(parseVendor(VendorStr.str())),
363 OS(parseOS(OSStr.str())),
364 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000365}
366
Duncan Sands335db222010-08-12 11:31:39 +0000367std::string Triple::normalize(StringRef Str) {
368 // Parse into components.
369 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000370 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000371
372 // If the first component corresponds to a known architecture, preferentially
373 // use it for the architecture. If the second component corresponds to a
374 // known vendor, preferentially use it for the vendor, etc. This avoids silly
375 // component movement when a component parses as (eg) both a valid arch and a
376 // valid os.
377 ArchType Arch = UnknownArch;
378 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000379 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000380 VendorType Vendor = UnknownVendor;
381 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000382 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000383 OSType OS = UnknownOS;
384 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000385 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000386 EnvironmentType Environment = UnknownEnvironment;
387 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000388 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000389
390 // Note which components are already in their final position. These will not
391 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000392 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000393 Found[0] = Arch != UnknownArch;
394 Found[1] = Vendor != UnknownVendor;
395 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000396 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000397
398 // If they are not there already, permute the components into their canonical
399 // positions by seeing if they parse as a valid architecture, and if so moving
400 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000401 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000402 if (Found[Pos])
403 continue; // Already in the canonical position.
404
405 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
406 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000407 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000408 continue;
409
410 // Does this component parse as valid for the target position?
411 bool Valid = false;
412 StringRef Comp = Components[Idx];
413 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000414 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000415 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000416 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000417 Valid = Arch != UnknownArch;
418 break;
419 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000420 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000421 Valid = Vendor != UnknownVendor;
422 break;
423 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000424 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000425 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000426 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000427 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000428 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000429 Valid = Environment != UnknownEnvironment;
430 break;
Duncan Sands335db222010-08-12 11:31:39 +0000431 }
432 if (!Valid)
433 continue; // Nope, try the next component.
434
435 // Move the component to the target position, pushing any non-fixed
436 // components that are in the way to the right. This tends to give
437 // good results in the common cases of a forgotten vendor component
438 // or a wrongly positioned environment.
439 if (Pos < Idx) {
440 // Insert left, pushing the existing components to the right. For
441 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
442 StringRef CurrentComponent(""); // The empty component.
443 // Replace the component we are moving with an empty component.
444 std::swap(CurrentComponent, Components[Idx]);
445 // Insert the component being moved at Pos, displacing any existing
446 // components to the right.
447 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
448 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000449 while (i < array_lengthof(Found) && Found[i])
450 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000451 // Place the component at the new position, getting the component
452 // that was at this position - it will be moved right.
453 std::swap(CurrentComponent, Components[i]);
454 }
455 } else if (Pos > Idx) {
456 // Push right by inserting empty components until the component at Idx
457 // reaches the target position Pos. For example, pc-a -> -pc-a when
458 // moving pc to the second position.
459 do {
460 // Insert one empty component at Idx.
461 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000462 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000463 // Place the component at the new position, getting the component
464 // that was at this position - it will be moved right.
465 std::swap(CurrentComponent, Components[i]);
466 // If it was placed on top of an empty component then we are done.
467 if (CurrentComponent.empty())
468 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000469 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000470 while (++i < array_lengthof(Found) && Found[i])
471 ;
Duncan Sands335db222010-08-12 11:31:39 +0000472 }
473 // The last component was pushed off the end - append it.
474 if (!CurrentComponent.empty())
475 Components.push_back(CurrentComponent);
476
477 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000478 while (++Idx < array_lengthof(Found) && Found[Idx])
479 ;
Duncan Sands335db222010-08-12 11:31:39 +0000480 } while (Idx < Pos); // Add more until the final position is reached.
481 }
482 assert(Pos < Components.size() && Components[Pos] == Comp &&
483 "Component moved wrong!");
484 Found[Pos] = true;
485 break;
486 }
487 }
488
489 // Special case logic goes here. At this point Arch, Vendor and OS have the
490 // correct values for the computed components.
491
492 // Stick the corrected components back together to form the normalized string.
493 std::string Normalized;
494 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
495 if (i) Normalized += '-';
496 Normalized += Components[i];
497 }
498 return Normalized;
499}
500
Daniel Dunbara14d2252009-07-26 03:31:47 +0000501StringRef Triple::getArchName() const {
502 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000503}
504
Daniel Dunbara14d2252009-07-26 03:31:47 +0000505StringRef Triple::getVendorName() const {
506 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
507 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000508}
509
Daniel Dunbara14d2252009-07-26 03:31:47 +0000510StringRef Triple::getOSName() const {
511 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
512 Tmp = Tmp.split('-').second; // Strip second component
513 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000514}
515
Daniel Dunbara14d2252009-07-26 03:31:47 +0000516StringRef Triple::getEnvironmentName() const {
517 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
518 Tmp = Tmp.split('-').second; // Strip second component
519 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000520}
521
Daniel Dunbara14d2252009-07-26 03:31:47 +0000522StringRef Triple::getOSAndEnvironmentName() const {
523 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
524 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000525}
526
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000527static unsigned EatNumber(StringRef &Str) {
528 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000529 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000530
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000531 do {
532 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000533 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000534
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000535 // Eat the digit.
536 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000537 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000538
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000539 return Result;
540}
541
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000542void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
543 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000544 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000545
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000546 // Assume that the OS portion of the triple starts with the canonical name.
547 StringRef OSTypeName = getOSTypeName(getOS());
548 if (OSName.startswith(OSTypeName))
549 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000550
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000551 // Any unset version defaults to 0.
552 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000553
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000554 // Parse up to three components.
555 unsigned *Components[3] = { &Major, &Minor, &Micro };
556 for (unsigned i = 0; i != 3; ++i) {
557 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
558 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000559
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000560 // Consume the leading number.
561 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000562
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000563 // Consume the separator, if present.
564 if (OSName.startswith("."))
565 OSName = OSName.substr(1);
566 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000567}
568
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000569bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
570 unsigned &Micro) const {
571 getOSVersion(Major, Minor, Micro);
572
573 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000574 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000575 case Darwin:
576 // Default to darwin8, i.e., MacOSX 10.4.
577 if (Major == 0)
578 Major = 8;
579 // Darwin version numbers are skewed from OS X versions.
580 if (Major < 4)
581 return false;
582 Micro = 0;
583 Minor = Major - 4;
584 Major = 10;
585 break;
586 case MacOSX:
587 // Default to 10.4.
588 if (Major == 0) {
589 Major = 10;
590 Minor = 4;
591 }
592 if (Major != 10)
593 return false;
594 break;
595 case IOS:
596 // Ignore the version from the triple. This is only handled because the
597 // the clang driver combines OS X and IOS support into a common Darwin
598 // toolchain that wants to know the OS X version number even when targeting
599 // IOS.
600 Major = 10;
601 Minor = 4;
602 Micro = 0;
603 break;
604 }
605 return true;
606}
607
Chad Rosierecee47e2012-05-09 17:23:48 +0000608void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
609 unsigned &Micro) const {
610 switch (getOS()) {
611 default: llvm_unreachable("unexpected OS for Darwin triple");
612 case Darwin:
613 case MacOSX:
614 // Ignore the version from the triple. This is only handled because the
615 // the clang driver combines OS X and IOS support into a common Darwin
616 // toolchain that wants to know the iOS version number even when targeting
617 // OS X.
Chad Rosier537c2f52012-05-09 18:23:00 +0000618 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000619 Minor = 0;
620 Micro = 0;
Chad Rosier26bbdee2012-05-09 17:38:47 +0000621 break;
Chad Rosierecee47e2012-05-09 17:23:48 +0000622 case IOS:
623 getOSVersion(Major, Minor, Micro);
Chad Rosier537c2f52012-05-09 18:23:00 +0000624 // Default to 3.0.
625 if (Major == 0)
626 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000627 break;
628 }
629}
630
Daniel Dunbara14d2252009-07-26 03:31:47 +0000631void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000632 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000633}
634
635void Triple::setArch(ArchType Kind) {
636 setArchName(getArchTypeName(Kind));
637}
638
639void Triple::setVendor(VendorType Kind) {
640 setVendorName(getVendorTypeName(Kind));
641}
642
643void Triple::setOS(OSType Kind) {
644 setOSName(getOSTypeName(Kind));
645}
646
Duncan Sands5754a452010-09-16 08:25:48 +0000647void Triple::setEnvironment(EnvironmentType Kind) {
648 setEnvironmentName(getEnvironmentTypeName(Kind));
649}
650
Daniel Dunbar2928c832009-11-06 10:58:06 +0000651void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000652 // Work around a miscompilation bug for Twines in gcc 4.0.3.
653 SmallString<64> Triple;
654 Triple += Str;
655 Triple += "-";
656 Triple += getVendorName();
657 Triple += "-";
658 Triple += getOSAndEnvironmentName();
659 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000660}
661
Daniel Dunbar2928c832009-11-06 10:58:06 +0000662void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000663 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
664}
665
Daniel Dunbar2928c832009-11-06 10:58:06 +0000666void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000667 if (hasEnvironment())
668 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
669 "-" + getEnvironmentName());
670 else
671 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
672}
673
Daniel Dunbar2928c832009-11-06 10:58:06 +0000674void Triple::setEnvironmentName(StringRef Str) {
675 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000676 "-" + Str);
677}
678
Daniel Dunbar2928c832009-11-06 10:58:06 +0000679void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000680 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
681}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000682
683static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
684 switch (Arch) {
Micah Villmowe53d6052012-10-01 17:01:31 +0000685 case llvm::Triple::spir:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000686 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000687 return 0;
688
689 case llvm::Triple::msp430:
690 return 16;
691
692 case llvm::Triple::amdil:
693 case llvm::Triple::arm:
694 case llvm::Triple::cellspu:
695 case llvm::Triple::hexagon:
696 case llvm::Triple::le32:
697 case llvm::Triple::mblaze:
698 case llvm::Triple::mips:
699 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000700 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000701 case llvm::Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000702 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000703 case llvm::Triple::sparc:
704 case llvm::Triple::tce:
705 case llvm::Triple::thumb:
706 case llvm::Triple::x86:
707 case llvm::Triple::xcore:
708 return 32;
709
710 case llvm::Triple::mips64:
711 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000712 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000713 case llvm::Triple::ppc64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000714 case llvm::Triple::sparcv9:
715 case llvm::Triple::x86_64:
716 return 64;
717 }
718 llvm_unreachable("Invalid architecture value");
719}
720
721bool Triple::isArch64Bit() const {
722 return getArchPointerBitWidth(getArch()) == 64;
723}
724
725bool Triple::isArch32Bit() const {
726 return getArchPointerBitWidth(getArch()) == 32;
727}
728
729bool Triple::isArch16Bit() const {
730 return getArchPointerBitWidth(getArch()) == 16;
731}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000732
733Triple Triple::get32BitArchVariant() const {
734 Triple T(*this);
735 switch (getArch()) {
736 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000737 case Triple::msp430:
738 T.setArch(UnknownArch);
739 break;
740
741 case Triple::amdil:
Micah Villmowe53d6052012-10-01 17:01:31 +0000742 case Triple::spir:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000743 case Triple::arm:
744 case Triple::cellspu:
745 case Triple::hexagon:
746 case Triple::le32:
747 case Triple::mblaze:
748 case Triple::mips:
749 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000750 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000751 case Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000752 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000753 case Triple::sparc:
754 case Triple::tce:
755 case Triple::thumb:
756 case Triple::x86:
757 case Triple::xcore:
758 // Already 32-bit.
759 break;
760
761 case Triple::mips64: T.setArch(Triple::mips); break;
762 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000763 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000764 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000765 case Triple::sparcv9: T.setArch(Triple::sparc); break;
766 case Triple::x86_64: T.setArch(Triple::x86); break;
767 }
768 return T;
769}
770
771Triple Triple::get64BitArchVariant() const {
772 Triple T(*this);
773 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000774 case Triple::UnknownArch:
775 case Triple::amdil:
776 case Triple::arm:
777 case Triple::cellspu:
778 case Triple::hexagon:
779 case Triple::le32:
780 case Triple::mblaze:
781 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000782 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000783 case Triple::tce:
784 case Triple::thumb:
785 case Triple::xcore:
786 T.setArch(UnknownArch);
787 break;
788
Micah Villmowe53d6052012-10-01 17:01:31 +0000789 case Triple::spir:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000790 case Triple::mips64:
791 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000792 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000793 case Triple::ppc64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000794 case Triple::sparcv9:
795 case Triple::x86_64:
796 // Already 64-bit.
797 break;
798
799 case Triple::mips: T.setArch(Triple::mips64); break;
800 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000801 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000802 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000803 case Triple::sparc: T.setArch(Triple::sparcv9); break;
804 case Triple::x86: T.setArch(Triple::x86_64); break;
805 }
806 return T;
807}