blob: d1dc7c81af06ca6178fa73f1e0cc10e77798ceec [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";
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 cellspu: return "spu";
59
60 case ppc64:
61 case ppc: return "ppc";
62
Wesley Pecka70f28c2010-02-23 19:15:24 +000063 case mblaze: return "mblaze";
64
Benjamin Kramerd5dbc8c2012-06-28 19:09:53 +000065 case mips:
66 case mipsel:
67 case mips64:
68 case mips64el:return "mips";
69
70 case hexagon: return "hexagon";
Tony Linthicumb4b54152011-12-12 21:14:40 +000071
Anton Korobeynikov74156592012-03-09 10:09:36 +000072 case r600: return "r600";
73
Chris Lattner87c06d62010-02-04 06:34:01 +000074 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000075 case sparc: return "sparc";
76
77 case x86:
78 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000079
Daniel Dunbar688b55b2009-08-24 09:53:06 +000080 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000081
Justin Holewinski49683f32012-05-04 20:18:50 +000082 case nvptx: return "nvptx";
83 case nvptx64: return "nvptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000084 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000085 case amdil: return "amdil";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000086 }
87}
88
Daniel Dunbar23e97b02009-04-01 21:53:23 +000089const char *Triple::getVendorTypeName(VendorType Kind) {
90 switch (Kind) {
91 case UnknownVendor: return "unknown";
92
93 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000094 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000095 case SCEI: return "scei";
Hal Finkela47406c2012-04-02 18:31:33 +000096 case BGP: return "bgp";
97 case BGQ: return "bgq";
Hal Finkeld939cd62012-08-28 02:10:30 +000098 case Freescale: return "fsl";
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";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000129 }
130
David Blaikie4d6ccb52012-01-20 21:51:11 +0000131 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000132}
133
Duncan Sands5754a452010-09-16 08:25:48 +0000134const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
135 switch (Kind) {
136 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000137 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000138 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000139 case GNUEABI: return "gnueabi";
140 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 case MachO: return "macho";
Logan Chien43bf7092012-09-02 09:29:46 +0000142 case Android: return "android";
Duncan Sands5754a452010-09-16 08:25:48 +0000143 }
144
David Blaikie4d6ccb52012-01-20 21:51:11 +0000145 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000146}
147
Daniel Dunbar2928c832009-11-06 10:58:06 +0000148Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000149 return StringSwitch<Triple::ArchType>(Name)
150 .Case("arm", arm)
151 .Case("cellspu", cellspu)
152 .Case("mips", mips)
153 .Case("mipsel", mipsel)
154 .Case("mips64", mips64)
155 .Case("mips64el", mips64el)
156 .Case("msp430", msp430)
157 .Case("ppc64", ppc64)
158 .Case("ppc32", ppc)
159 .Case("ppc", ppc)
160 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000161 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000162 .Case("hexagon", hexagon)
163 .Case("sparc", sparc)
164 .Case("sparcv9", sparcv9)
165 .Case("tce", tce)
166 .Case("thumb", thumb)
167 .Case("x86", x86)
168 .Case("x86-64", x86_64)
169 .Case("xcore", xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000170 .Case("nvptx", nvptx)
171 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000172 .Case("le32", le32)
173 .Case("amdil", amdil)
174 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000175}
176
Daniel Dunbar2928c832009-11-06 10:58:06 +0000177Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000178 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
179 // archs which Darwin doesn't use.
180
181 // The matching this routine does is fairly pointless, since it is neither the
182 // complete architecture list, nor a reasonable subset. The problem is that
183 // historically the driver driver accepts this and also ties its -march=
184 // handling to the architecture name, so we need to be careful before removing
185 // support for it.
186
Daniel Dunbared687882009-09-09 23:01:25 +0000187 // This code must be kept in sync with Clang's Darwin specific argument
188 // translation.
189
Chandler Carruth06accda2012-02-12 09:27:38 +0000190 return StringSwitch<ArchType>(Str)
191 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
192 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
193 .Case("ppc64", Triple::ppc64)
194 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
195 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
196 Triple::x86)
197 .Case("x86_64", Triple::x86_64)
198 // This is derived from the driver driver.
199 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
200 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000201 .Case("r600", Triple::r600)
Justin Holewinski49683f32012-05-04 20:18:50 +0000202 .Case("nvptx", Triple::nvptx)
203 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000204 .Case("amdil", Triple::amdil)
205 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000206}
207
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000208// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000209const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000210 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000211 return NULL;
212
Chandler Carruth06accda2012-02-12 09:27:38 +0000213 return StringSwitch<const char*>(getArchName())
214 .Case("i386", "i386")
215 .Case("x86_64", "x86_64")
216 .Case("powerpc", "ppc")
217 .Case("powerpc64", "ppc64")
218 .Cases("mblaze", "microblaze", "mblaze")
219 .Case("arm", "arm")
220 .Cases("armv4t", "thumbv4t", "armv4t")
221 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
222 .Cases("armv6", "thumbv6", "armv6")
223 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000224 .Case("r600", "r600")
Justin Holewinski49683f32012-05-04 20:18:50 +0000225 .Case("nvptx", "nvptx")
226 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000227 .Case("le32", "le32")
228 .Case("amdil", "amdil")
229 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000230}
231
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000232static Triple::ArchType parseArch(StringRef ArchName) {
233 return StringSwitch<Triple::ArchType>(ArchName)
234 .Cases("i386", "i486", "i586", "i686", Triple::x86)
235 // FIXME: Do we need to support these?
236 .Cases("i786", "i886", "i986", Triple::x86)
237 .Cases("amd64", "x86_64", Triple::x86_64)
238 .Case("powerpc", Triple::ppc)
239 .Cases("powerpc64", "ppu", Triple::ppc64)
240 .Case("mblaze", Triple::mblaze)
241 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000242 // FIXME: It would be good to replace these with explicit names for all the
243 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000244 .StartsWith("armv", Triple::arm)
245 .Case("thumb", Triple::thumb)
246 .StartsWith("thumbv", Triple::thumb)
247 .Cases("spu", "cellspu", Triple::cellspu)
248 .Case("msp430", Triple::msp430)
249 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000250 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000251 .Cases("mips64", "mips64eb", Triple::mips64)
252 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000253 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000254 .Case("hexagon", Triple::hexagon)
255 .Case("sparc", Triple::sparc)
256 .Case("sparcv9", Triple::sparcv9)
257 .Case("tce", Triple::tce)
258 .Case("xcore", Triple::xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000259 .Case("nvptx", Triple::nvptx)
260 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000261 .Case("le32", Triple::le32)
262 .Case("amdil", Triple::amdil)
263 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000264}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000265
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000266static Triple::VendorType parseVendor(StringRef VendorName) {
267 return StringSwitch<Triple::VendorType>(VendorName)
268 .Case("apple", Triple::Apple)
269 .Case("pc", Triple::PC)
270 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000271 .Case("bgp", Triple::BGP)
272 .Case("bgq", Triple::BGQ)
Hal Finkeld939cd62012-08-28 02:10:30 +0000273 .Case("fsl", Triple::Freescale)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000274 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000275}
276
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000277static Triple::OSType parseOS(StringRef OSName) {
278 return StringSwitch<Triple::OSType>(OSName)
279 .StartsWith("auroraux", Triple::AuroraUX)
280 .StartsWith("cygwin", Triple::Cygwin)
281 .StartsWith("darwin", Triple::Darwin)
282 .StartsWith("dragonfly", Triple::DragonFly)
283 .StartsWith("freebsd", Triple::FreeBSD)
284 .StartsWith("ios", Triple::IOS)
285 .StartsWith("kfreebsd", Triple::KFreeBSD)
286 .StartsWith("linux", Triple::Linux)
287 .StartsWith("lv2", Triple::Lv2)
288 .StartsWith("macosx", Triple::MacOSX)
289 .StartsWith("mingw32", Triple::MinGW32)
290 .StartsWith("netbsd", Triple::NetBSD)
291 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000292 .StartsWith("solaris", Triple::Solaris)
293 .StartsWith("win32", Triple::Win32)
294 .StartsWith("haiku", Triple::Haiku)
295 .StartsWith("minix", Triple::Minix)
296 .StartsWith("rtems", Triple::RTEMS)
297 .StartsWith("nacl", Triple::NativeClient)
Hal Finkela47406c2012-04-02 18:31:33 +0000298 .StartsWith("cnk", Triple::CNK)
Eric Christopherb0f67592012-08-06 20:52:18 +0000299 .StartsWith("bitrig", Triple::Bitrig)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000300 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000301}
302
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000303static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
304 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
305 .StartsWith("eabi", Triple::EABI)
306 .StartsWith("gnueabihf", Triple::GNUEABIHF)
307 .StartsWith("gnueabi", Triple::GNUEABI)
308 .StartsWith("gnu", Triple::GNU)
309 .StartsWith("macho", Triple::MachO)
Logan Chien43bf7092012-09-02 09:29:46 +0000310 .StartsWith("android", Triple::Android)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000311 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000312}
313
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000314/// \brief Construct a triple from the string representation provided.
315///
Chandler Carruth0523f412012-02-21 08:31:18 +0000316/// This stores the string representation and parses the various pieces into
317/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000318Triple::Triple(const Twine &Str)
319 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000320 Arch(parseArch(getArchName())),
321 Vendor(parseVendor(getVendorName())),
322 OS(parseOS(getOSName())),
323 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000324}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000325
326/// \brief Construct a triple from string representations of the architecture,
327/// vendor, and OS.
328///
Chandler Carruth0523f412012-02-21 08:31:18 +0000329/// This joins each argument into a canonical string representation and parses
330/// them into enum members. It leaves the environment unknown and omits it from
331/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000332Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
333 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000334 Arch(parseArch(ArchStr.str())),
335 Vendor(parseVendor(VendorStr.str())),
336 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000337 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000338}
339
340/// \brief Construct a triple from string representations of the architecture,
341/// vendor, OS, and environment.
342///
Chandler Carruth0523f412012-02-21 08:31:18 +0000343/// This joins each argument into a canonical string representation and parses
344/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000345Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
346 const Twine &EnvironmentStr)
347 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
348 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000349 Arch(parseArch(ArchStr.str())),
350 Vendor(parseVendor(VendorStr.str())),
351 OS(parseOS(OSStr.str())),
352 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000353}
354
Duncan Sands335db222010-08-12 11:31:39 +0000355std::string Triple::normalize(StringRef Str) {
356 // Parse into components.
357 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000358 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000359
360 // If the first component corresponds to a known architecture, preferentially
361 // use it for the architecture. If the second component corresponds to a
362 // known vendor, preferentially use it for the vendor, etc. This avoids silly
363 // component movement when a component parses as (eg) both a valid arch and a
364 // valid os.
365 ArchType Arch = UnknownArch;
366 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000367 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000368 VendorType Vendor = UnknownVendor;
369 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000370 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000371 OSType OS = UnknownOS;
372 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000373 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000374 EnvironmentType Environment = UnknownEnvironment;
375 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000376 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000377
378 // Note which components are already in their final position. These will not
379 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000380 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000381 Found[0] = Arch != UnknownArch;
382 Found[1] = Vendor != UnknownVendor;
383 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000384 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000385
386 // If they are not there already, permute the components into their canonical
387 // positions by seeing if they parse as a valid architecture, and if so moving
388 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000389 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000390 if (Found[Pos])
391 continue; // Already in the canonical position.
392
393 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
394 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000395 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000396 continue;
397
398 // Does this component parse as valid for the target position?
399 bool Valid = false;
400 StringRef Comp = Components[Idx];
401 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000402 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000403 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000404 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000405 Valid = Arch != UnknownArch;
406 break;
407 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000408 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000409 Valid = Vendor != UnknownVendor;
410 break;
411 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000412 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000413 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000414 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000415 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000416 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000417 Valid = Environment != UnknownEnvironment;
418 break;
Duncan Sands335db222010-08-12 11:31:39 +0000419 }
420 if (!Valid)
421 continue; // Nope, try the next component.
422
423 // Move the component to the target position, pushing any non-fixed
424 // components that are in the way to the right. This tends to give
425 // good results in the common cases of a forgotten vendor component
426 // or a wrongly positioned environment.
427 if (Pos < Idx) {
428 // Insert left, pushing the existing components to the right. For
429 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
430 StringRef CurrentComponent(""); // The empty component.
431 // Replace the component we are moving with an empty component.
432 std::swap(CurrentComponent, Components[Idx]);
433 // Insert the component being moved at Pos, displacing any existing
434 // components to the right.
435 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
436 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000437 while (i < array_lengthof(Found) && Found[i])
438 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000439 // Place the component at the new position, getting the component
440 // that was at this position - it will be moved right.
441 std::swap(CurrentComponent, Components[i]);
442 }
443 } else if (Pos > Idx) {
444 // Push right by inserting empty components until the component at Idx
445 // reaches the target position Pos. For example, pc-a -> -pc-a when
446 // moving pc to the second position.
447 do {
448 // Insert one empty component at Idx.
449 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000450 for (unsigned i = Idx; i < Components.size();) {
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 // If it was placed on top of an empty component then we are done.
455 if (CurrentComponent.empty())
456 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000457 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000458 while (++i < array_lengthof(Found) && Found[i])
459 ;
Duncan Sands335db222010-08-12 11:31:39 +0000460 }
461 // The last component was pushed off the end - append it.
462 if (!CurrentComponent.empty())
463 Components.push_back(CurrentComponent);
464
465 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000466 while (++Idx < array_lengthof(Found) && Found[Idx])
467 ;
Duncan Sands335db222010-08-12 11:31:39 +0000468 } while (Idx < Pos); // Add more until the final position is reached.
469 }
470 assert(Pos < Components.size() && Components[Pos] == Comp &&
471 "Component moved wrong!");
472 Found[Pos] = true;
473 break;
474 }
475 }
476
477 // Special case logic goes here. At this point Arch, Vendor and OS have the
478 // correct values for the computed components.
479
480 // Stick the corrected components back together to form the normalized string.
481 std::string Normalized;
482 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
483 if (i) Normalized += '-';
484 Normalized += Components[i];
485 }
486 return Normalized;
487}
488
Daniel Dunbara14d2252009-07-26 03:31:47 +0000489StringRef Triple::getArchName() const {
490 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000491}
492
Daniel Dunbara14d2252009-07-26 03:31:47 +0000493StringRef Triple::getVendorName() const {
494 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
495 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000496}
497
Daniel Dunbara14d2252009-07-26 03:31:47 +0000498StringRef Triple::getOSName() const {
499 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
500 Tmp = Tmp.split('-').second; // Strip second component
501 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000502}
503
Daniel Dunbara14d2252009-07-26 03:31:47 +0000504StringRef Triple::getEnvironmentName() const {
505 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
506 Tmp = Tmp.split('-').second; // Strip second component
507 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000508}
509
Daniel Dunbara14d2252009-07-26 03:31:47 +0000510StringRef Triple::getOSAndEnvironmentName() const {
511 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
512 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000513}
514
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000515static unsigned EatNumber(StringRef &Str) {
516 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000517 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000518
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000519 do {
520 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000521 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000522
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000523 // Eat the digit.
524 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000525 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000526
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000527 return Result;
528}
529
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000530void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
531 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000532 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000533
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000534 // Assume that the OS portion of the triple starts with the canonical name.
535 StringRef OSTypeName = getOSTypeName(getOS());
536 if (OSName.startswith(OSTypeName))
537 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000538
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000539 // Any unset version defaults to 0.
540 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000541
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000542 // Parse up to three components.
543 unsigned *Components[3] = { &Major, &Minor, &Micro };
544 for (unsigned i = 0; i != 3; ++i) {
545 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
546 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000547
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000548 // Consume the leading number.
549 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000550
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000551 // Consume the separator, if present.
552 if (OSName.startswith("."))
553 OSName = OSName.substr(1);
554 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000555}
556
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000557bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
558 unsigned &Micro) const {
559 getOSVersion(Major, Minor, Micro);
560
561 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000562 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000563 case Darwin:
564 // Default to darwin8, i.e., MacOSX 10.4.
565 if (Major == 0)
566 Major = 8;
567 // Darwin version numbers are skewed from OS X versions.
568 if (Major < 4)
569 return false;
570 Micro = 0;
571 Minor = Major - 4;
572 Major = 10;
573 break;
574 case MacOSX:
575 // Default to 10.4.
576 if (Major == 0) {
577 Major = 10;
578 Minor = 4;
579 }
580 if (Major != 10)
581 return false;
582 break;
583 case IOS:
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 OS X version number even when targeting
587 // IOS.
588 Major = 10;
589 Minor = 4;
590 Micro = 0;
591 break;
592 }
593 return true;
594}
595
Chad Rosierecee47e2012-05-09 17:23:48 +0000596void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
597 unsigned &Micro) const {
598 switch (getOS()) {
599 default: llvm_unreachable("unexpected OS for Darwin triple");
600 case Darwin:
601 case MacOSX:
602 // Ignore the version from the triple. This is only handled because the
603 // the clang driver combines OS X and IOS support into a common Darwin
604 // toolchain that wants to know the iOS version number even when targeting
605 // OS X.
Chad Rosier537c2f52012-05-09 18:23:00 +0000606 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000607 Minor = 0;
608 Micro = 0;
Chad Rosier26bbdee2012-05-09 17:38:47 +0000609 break;
Chad Rosierecee47e2012-05-09 17:23:48 +0000610 case IOS:
611 getOSVersion(Major, Minor, Micro);
Chad Rosier537c2f52012-05-09 18:23:00 +0000612 // Default to 3.0.
613 if (Major == 0)
614 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000615 break;
616 }
617}
618
Daniel Dunbara14d2252009-07-26 03:31:47 +0000619void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000620 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000621}
622
623void Triple::setArch(ArchType Kind) {
624 setArchName(getArchTypeName(Kind));
625}
626
627void Triple::setVendor(VendorType Kind) {
628 setVendorName(getVendorTypeName(Kind));
629}
630
631void Triple::setOS(OSType Kind) {
632 setOSName(getOSTypeName(Kind));
633}
634
Duncan Sands5754a452010-09-16 08:25:48 +0000635void Triple::setEnvironment(EnvironmentType Kind) {
636 setEnvironmentName(getEnvironmentTypeName(Kind));
637}
638
Daniel Dunbar2928c832009-11-06 10:58:06 +0000639void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000640 // Work around a miscompilation bug for Twines in gcc 4.0.3.
641 SmallString<64> Triple;
642 Triple += Str;
643 Triple += "-";
644 Triple += getVendorName();
645 Triple += "-";
646 Triple += getOSAndEnvironmentName();
647 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000648}
649
Daniel Dunbar2928c832009-11-06 10:58:06 +0000650void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000651 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
652}
653
Daniel Dunbar2928c832009-11-06 10:58:06 +0000654void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000655 if (hasEnvironment())
656 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
657 "-" + getEnvironmentName());
658 else
659 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
660}
661
Daniel Dunbar2928c832009-11-06 10:58:06 +0000662void Triple::setEnvironmentName(StringRef Str) {
663 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000664 "-" + Str);
665}
666
Daniel Dunbar2928c832009-11-06 10:58:06 +0000667void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000668 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
669}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000670
671static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
672 switch (Arch) {
673 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000674 return 0;
675
676 case llvm::Triple::msp430:
677 return 16;
678
679 case llvm::Triple::amdil:
680 case llvm::Triple::arm:
681 case llvm::Triple::cellspu:
682 case llvm::Triple::hexagon:
683 case llvm::Triple::le32:
684 case llvm::Triple::mblaze:
685 case llvm::Triple::mips:
686 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000687 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000688 case llvm::Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000689 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000690 case llvm::Triple::sparc:
691 case llvm::Triple::tce:
692 case llvm::Triple::thumb:
693 case llvm::Triple::x86:
694 case llvm::Triple::xcore:
695 return 32;
696
697 case llvm::Triple::mips64:
698 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000699 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000700 case llvm::Triple::ppc64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000701 case llvm::Triple::sparcv9:
702 case llvm::Triple::x86_64:
703 return 64;
704 }
705 llvm_unreachable("Invalid architecture value");
706}
707
708bool Triple::isArch64Bit() const {
709 return getArchPointerBitWidth(getArch()) == 64;
710}
711
712bool Triple::isArch32Bit() const {
713 return getArchPointerBitWidth(getArch()) == 32;
714}
715
716bool Triple::isArch16Bit() const {
717 return getArchPointerBitWidth(getArch()) == 16;
718}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000719
720Triple Triple::get32BitArchVariant() const {
721 Triple T(*this);
722 switch (getArch()) {
723 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000724 case Triple::msp430:
725 T.setArch(UnknownArch);
726 break;
727
728 case Triple::amdil:
729 case Triple::arm:
730 case Triple::cellspu:
731 case Triple::hexagon:
732 case Triple::le32:
733 case Triple::mblaze:
734 case Triple::mips:
735 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000736 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000737 case Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000738 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000739 case Triple::sparc:
740 case Triple::tce:
741 case Triple::thumb:
742 case Triple::x86:
743 case Triple::xcore:
744 // Already 32-bit.
745 break;
746
747 case Triple::mips64: T.setArch(Triple::mips); break;
748 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000749 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000750 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000751 case Triple::sparcv9: T.setArch(Triple::sparc); break;
752 case Triple::x86_64: T.setArch(Triple::x86); break;
753 }
754 return T;
755}
756
757Triple Triple::get64BitArchVariant() const {
758 Triple T(*this);
759 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000760 case Triple::UnknownArch:
761 case Triple::amdil:
762 case Triple::arm:
763 case Triple::cellspu:
764 case Triple::hexagon:
765 case Triple::le32:
766 case Triple::mblaze:
767 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000768 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000769 case Triple::tce:
770 case Triple::thumb:
771 case Triple::xcore:
772 T.setArch(UnknownArch);
773 break;
774
775 case Triple::mips64:
776 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000777 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000778 case Triple::ppc64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000779 case Triple::sparcv9:
780 case Triple::x86_64:
781 // Already 64-bit.
782 break;
783
784 case Triple::mips: T.setArch(Triple::mips64); break;
785 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000786 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000787 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000788 case Triple::sparc: T.setArch(Triple::sparcv9); break;
789 case Triple::x86: T.setArch(Triple::x86_64); break;
790 }
791 return T;
792}