blob: 32f4074b15fa4d80104c21dbf11ef99d0283a33e [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";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000101 }
102
David Blaikie4d6ccb52012-01-20 21:51:11 +0000103 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000104}
105
106const char *Triple::getOSTypeName(OSType Kind) {
107 switch (Kind) {
108 case UnknownOS: return "unknown";
109
Duncan Sands852cd112009-06-19 14:40:01 +0000110 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000111 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000112 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000113 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000114 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000115 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000116 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000117 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000118 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000119 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000120 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000121 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000122 case OpenBSD: return "openbsd";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000123 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000124 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000125 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000126 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000127 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000128 case NativeClient: return "nacl";
Hal Finkela47406c2012-04-02 18:31:33 +0000129 case CNK: return "cnk";
Eric Christopherb0f67592012-08-06 20:52:18 +0000130 case Bitrig: return "bitrig";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000131 }
132
David Blaikie4d6ccb52012-01-20 21:51:11 +0000133 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000134}
135
Duncan Sands5754a452010-09-16 08:25:48 +0000136const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
137 switch (Kind) {
138 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000139 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000140 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000141 case GNUEABI: return "gnueabi";
142 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000143 case MachO: return "macho";
Logan Chien43bf7092012-09-02 09:29:46 +0000144 case Android: return "android";
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)
153 .Case("cellspu", cellspu)
154 .Case("mips", mips)
155 .Case("mipsel", mipsel)
156 .Case("mips64", mips64)
157 .Case("mips64el", mips64el)
158 .Case("msp430", msp430)
159 .Case("ppc64", ppc64)
160 .Case("ppc32", ppc)
161 .Case("ppc", ppc)
162 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000163 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000164 .Case("hexagon", hexagon)
165 .Case("sparc", sparc)
166 .Case("sparcv9", sparcv9)
167 .Case("tce", tce)
168 .Case("thumb", thumb)
169 .Case("x86", x86)
170 .Case("x86-64", x86_64)
171 .Case("xcore", xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000172 .Case("nvptx", nvptx)
173 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000174 .Case("le32", le32)
175 .Case("amdil", amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000176 .Case("spir", spir)
Chandler Carruth06accda2012-02-12 09:27:38 +0000177 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000178}
179
Daniel Dunbar2928c832009-11-06 10:58:06 +0000180Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000181 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
182 // archs which Darwin doesn't use.
183
184 // The matching this routine does is fairly pointless, since it is neither the
185 // complete architecture list, nor a reasonable subset. The problem is that
186 // historically the driver driver accepts this and also ties its -march=
187 // handling to the architecture name, so we need to be careful before removing
188 // support for it.
189
Daniel Dunbared687882009-09-09 23:01:25 +0000190 // This code must be kept in sync with Clang's Darwin specific argument
191 // translation.
192
Chandler Carruth06accda2012-02-12 09:27:38 +0000193 return StringSwitch<ArchType>(Str)
194 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
195 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
196 .Case("ppc64", Triple::ppc64)
197 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
198 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
199 Triple::x86)
200 .Case("x86_64", Triple::x86_64)
201 // This is derived from the driver driver.
202 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
203 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000204 .Case("r600", Triple::r600)
Justin Holewinski49683f32012-05-04 20:18:50 +0000205 .Case("nvptx", Triple::nvptx)
206 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000207 .Case("amdil", Triple::amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000208 .Case("spir", Triple::spir)
Chandler Carruth06accda2012-02-12 09:27:38 +0000209 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000210}
211
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000212// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000213const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000214 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000215 return NULL;
216
Chandler Carruth06accda2012-02-12 09:27:38 +0000217 return StringSwitch<const char*>(getArchName())
218 .Case("i386", "i386")
219 .Case("x86_64", "x86_64")
220 .Case("powerpc", "ppc")
221 .Case("powerpc64", "ppc64")
222 .Cases("mblaze", "microblaze", "mblaze")
223 .Case("arm", "arm")
224 .Cases("armv4t", "thumbv4t", "armv4t")
225 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
226 .Cases("armv6", "thumbv6", "armv6")
227 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000228 .Case("r600", "r600")
Justin Holewinski49683f32012-05-04 20:18:50 +0000229 .Case("nvptx", "nvptx")
230 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000231 .Case("le32", "le32")
232 .Case("amdil", "amdil")
Micah Villmowe53d6052012-10-01 17:01:31 +0000233 .Case("spir", "spir")
Chandler Carruth06accda2012-02-12 09:27:38 +0000234 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000235}
236
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000237static Triple::ArchType parseArch(StringRef ArchName) {
238 return StringSwitch<Triple::ArchType>(ArchName)
239 .Cases("i386", "i486", "i586", "i686", Triple::x86)
240 // FIXME: Do we need to support these?
241 .Cases("i786", "i886", "i986", Triple::x86)
242 .Cases("amd64", "x86_64", Triple::x86_64)
243 .Case("powerpc", Triple::ppc)
244 .Cases("powerpc64", "ppu", Triple::ppc64)
245 .Case("mblaze", Triple::mblaze)
246 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000247 // FIXME: It would be good to replace these with explicit names for all the
248 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000249 .StartsWith("armv", Triple::arm)
250 .Case("thumb", Triple::thumb)
251 .StartsWith("thumbv", Triple::thumb)
252 .Cases("spu", "cellspu", Triple::cellspu)
253 .Case("msp430", Triple::msp430)
254 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000255 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000256 .Cases("mips64", "mips64eb", Triple::mips64)
257 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000258 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000259 .Case("hexagon", Triple::hexagon)
260 .Case("sparc", Triple::sparc)
261 .Case("sparcv9", Triple::sparcv9)
262 .Case("tce", Triple::tce)
263 .Case("xcore", Triple::xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000264 .Case("nvptx", Triple::nvptx)
265 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000266 .Case("le32", Triple::le32)
267 .Case("amdil", Triple::amdil)
Micah Villmowe53d6052012-10-01 17:01:31 +0000268 .Case("spir", Triple::spir)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000269 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000270}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000271
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000272static Triple::VendorType parseVendor(StringRef VendorName) {
273 return StringSwitch<Triple::VendorType>(VendorName)
274 .Case("apple", Triple::Apple)
275 .Case("pc", Triple::PC)
276 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000277 .Case("bgp", Triple::BGP)
278 .Case("bgq", Triple::BGQ)
Hal Finkeld939cd62012-08-28 02:10:30 +0000279 .Case("fsl", Triple::Freescale)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000280 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000281}
282
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000283static Triple::OSType parseOS(StringRef OSName) {
284 return StringSwitch<Triple::OSType>(OSName)
285 .StartsWith("auroraux", Triple::AuroraUX)
286 .StartsWith("cygwin", Triple::Cygwin)
287 .StartsWith("darwin", Triple::Darwin)
288 .StartsWith("dragonfly", Triple::DragonFly)
289 .StartsWith("freebsd", Triple::FreeBSD)
290 .StartsWith("ios", Triple::IOS)
291 .StartsWith("kfreebsd", Triple::KFreeBSD)
292 .StartsWith("linux", Triple::Linux)
293 .StartsWith("lv2", Triple::Lv2)
294 .StartsWith("macosx", Triple::MacOSX)
295 .StartsWith("mingw32", Triple::MinGW32)
296 .StartsWith("netbsd", Triple::NetBSD)
297 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000298 .StartsWith("solaris", Triple::Solaris)
299 .StartsWith("win32", Triple::Win32)
300 .StartsWith("haiku", Triple::Haiku)
301 .StartsWith("minix", Triple::Minix)
302 .StartsWith("rtems", Triple::RTEMS)
303 .StartsWith("nacl", Triple::NativeClient)
Hal Finkela47406c2012-04-02 18:31:33 +0000304 .StartsWith("cnk", Triple::CNK)
Eric Christopherb0f67592012-08-06 20:52:18 +0000305 .StartsWith("bitrig", Triple::Bitrig)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000306 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000307}
308
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000309static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
310 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
311 .StartsWith("eabi", Triple::EABI)
312 .StartsWith("gnueabihf", Triple::GNUEABIHF)
313 .StartsWith("gnueabi", Triple::GNUEABI)
314 .StartsWith("gnu", Triple::GNU)
315 .StartsWith("macho", Triple::MachO)
Logan Chien43bf7092012-09-02 09:29:46 +0000316 .StartsWith("android", Triple::Android)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000317 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000318}
319
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000320/// \brief Construct a triple from the string representation provided.
321///
Chandler Carruth0523f412012-02-21 08:31:18 +0000322/// This stores the string representation and parses the various pieces into
323/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000324Triple::Triple(const Twine &Str)
325 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000326 Arch(parseArch(getArchName())),
327 Vendor(parseVendor(getVendorName())),
328 OS(parseOS(getOSName())),
329 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000330}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000331
332/// \brief Construct a triple from string representations of the architecture,
333/// vendor, and OS.
334///
Chandler Carruth0523f412012-02-21 08:31:18 +0000335/// This joins each argument into a canonical string representation and parses
336/// them into enum members. It leaves the environment unknown and omits it from
337/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000338Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
339 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000340 Arch(parseArch(ArchStr.str())),
341 Vendor(parseVendor(VendorStr.str())),
342 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000343 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000344}
345
346/// \brief Construct a triple from string representations of the architecture,
347/// vendor, OS, and environment.
348///
Chandler Carruth0523f412012-02-21 08:31:18 +0000349/// This joins each argument into a canonical string representation and parses
350/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000351Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
352 const Twine &EnvironmentStr)
353 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
354 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000355 Arch(parseArch(ArchStr.str())),
356 Vendor(parseVendor(VendorStr.str())),
357 OS(parseOS(OSStr.str())),
358 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000359}
360
Duncan Sands335db222010-08-12 11:31:39 +0000361std::string Triple::normalize(StringRef Str) {
362 // Parse into components.
363 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000364 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000365
366 // If the first component corresponds to a known architecture, preferentially
367 // use it for the architecture. If the second component corresponds to a
368 // known vendor, preferentially use it for the vendor, etc. This avoids silly
369 // component movement when a component parses as (eg) both a valid arch and a
370 // valid os.
371 ArchType Arch = UnknownArch;
372 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000373 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000374 VendorType Vendor = UnknownVendor;
375 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000376 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000377 OSType OS = UnknownOS;
378 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000379 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000380 EnvironmentType Environment = UnknownEnvironment;
381 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000382 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000383
384 // Note which components are already in their final position. These will not
385 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000386 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000387 Found[0] = Arch != UnknownArch;
388 Found[1] = Vendor != UnknownVendor;
389 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000390 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000391
392 // If they are not there already, permute the components into their canonical
393 // positions by seeing if they parse as a valid architecture, and if so moving
394 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000395 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000396 if (Found[Pos])
397 continue; // Already in the canonical position.
398
399 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
400 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000401 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000402 continue;
403
404 // Does this component parse as valid for the target position?
405 bool Valid = false;
406 StringRef Comp = Components[Idx];
407 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000408 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000409 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000410 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000411 Valid = Arch != UnknownArch;
412 break;
413 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000414 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000415 Valid = Vendor != UnknownVendor;
416 break;
417 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000418 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000419 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000420 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000421 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000422 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000423 Valid = Environment != UnknownEnvironment;
424 break;
Duncan Sands335db222010-08-12 11:31:39 +0000425 }
426 if (!Valid)
427 continue; // Nope, try the next component.
428
429 // Move the component to the target position, pushing any non-fixed
430 // components that are in the way to the right. This tends to give
431 // good results in the common cases of a forgotten vendor component
432 // or a wrongly positioned environment.
433 if (Pos < Idx) {
434 // Insert left, pushing the existing components to the right. For
435 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
436 StringRef CurrentComponent(""); // The empty component.
437 // Replace the component we are moving with an empty component.
438 std::swap(CurrentComponent, Components[Idx]);
439 // Insert the component being moved at Pos, displacing any existing
440 // components to the right.
441 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
442 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000443 while (i < array_lengthof(Found) && Found[i])
444 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000445 // Place the component at the new position, getting the component
446 // that was at this position - it will be moved right.
447 std::swap(CurrentComponent, Components[i]);
448 }
449 } else if (Pos > Idx) {
450 // Push right by inserting empty components until the component at Idx
451 // reaches the target position Pos. For example, pc-a -> -pc-a when
452 // moving pc to the second position.
453 do {
454 // Insert one empty component at Idx.
455 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000456 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000457 // Place the component at the new position, getting the component
458 // that was at this position - it will be moved right.
459 std::swap(CurrentComponent, Components[i]);
460 // If it was placed on top of an empty component then we are done.
461 if (CurrentComponent.empty())
462 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000463 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000464 while (++i < array_lengthof(Found) && Found[i])
465 ;
Duncan Sands335db222010-08-12 11:31:39 +0000466 }
467 // The last component was pushed off the end - append it.
468 if (!CurrentComponent.empty())
469 Components.push_back(CurrentComponent);
470
471 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000472 while (++Idx < array_lengthof(Found) && Found[Idx])
473 ;
Duncan Sands335db222010-08-12 11:31:39 +0000474 } while (Idx < Pos); // Add more until the final position is reached.
475 }
476 assert(Pos < Components.size() && Components[Pos] == Comp &&
477 "Component moved wrong!");
478 Found[Pos] = true;
479 break;
480 }
481 }
482
483 // Special case logic goes here. At this point Arch, Vendor and OS have the
484 // correct values for the computed components.
485
486 // Stick the corrected components back together to form the normalized string.
487 std::string Normalized;
488 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
489 if (i) Normalized += '-';
490 Normalized += Components[i];
491 }
492 return Normalized;
493}
494
Daniel Dunbara14d2252009-07-26 03:31:47 +0000495StringRef Triple::getArchName() const {
496 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000497}
498
Daniel Dunbara14d2252009-07-26 03:31:47 +0000499StringRef Triple::getVendorName() const {
500 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
501 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000502}
503
Daniel Dunbara14d2252009-07-26 03:31:47 +0000504StringRef Triple::getOSName() const {
505 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
506 Tmp = Tmp.split('-').second; // Strip second component
507 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000508}
509
Daniel Dunbara14d2252009-07-26 03:31:47 +0000510StringRef Triple::getEnvironmentName() const {
511 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
512 Tmp = Tmp.split('-').second; // Strip second component
513 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000514}
515
Daniel Dunbara14d2252009-07-26 03:31:47 +0000516StringRef Triple::getOSAndEnvironmentName() const {
517 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
518 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000519}
520
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000521static unsigned EatNumber(StringRef &Str) {
522 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000523 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000524
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000525 do {
526 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000527 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000528
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000529 // Eat the digit.
530 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000531 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000532
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000533 return Result;
534}
535
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000536void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
537 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000538 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000539
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000540 // Assume that the OS portion of the triple starts with the canonical name.
541 StringRef OSTypeName = getOSTypeName(getOS());
542 if (OSName.startswith(OSTypeName))
543 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000544
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000545 // Any unset version defaults to 0.
546 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000547
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000548 // Parse up to three components.
549 unsigned *Components[3] = { &Major, &Minor, &Micro };
550 for (unsigned i = 0; i != 3; ++i) {
551 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
552 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000553
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000554 // Consume the leading number.
555 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000556
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000557 // Consume the separator, if present.
558 if (OSName.startswith("."))
559 OSName = OSName.substr(1);
560 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000561}
562
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000563bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
564 unsigned &Micro) const {
565 getOSVersion(Major, Minor, Micro);
566
567 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000568 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000569 case Darwin:
570 // Default to darwin8, i.e., MacOSX 10.4.
571 if (Major == 0)
572 Major = 8;
573 // Darwin version numbers are skewed from OS X versions.
574 if (Major < 4)
575 return false;
576 Micro = 0;
577 Minor = Major - 4;
578 Major = 10;
579 break;
580 case MacOSX:
581 // Default to 10.4.
582 if (Major == 0) {
583 Major = 10;
584 Minor = 4;
585 }
586 if (Major != 10)
587 return false;
588 break;
589 case IOS:
590 // Ignore the version from the triple. This is only handled because the
591 // the clang driver combines OS X and IOS support into a common Darwin
592 // toolchain that wants to know the OS X version number even when targeting
593 // IOS.
594 Major = 10;
595 Minor = 4;
596 Micro = 0;
597 break;
598 }
599 return true;
600}
601
Chad Rosierecee47e2012-05-09 17:23:48 +0000602void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
603 unsigned &Micro) const {
604 switch (getOS()) {
605 default: llvm_unreachable("unexpected OS for Darwin triple");
606 case Darwin:
607 case MacOSX:
608 // Ignore the version from the triple. This is only handled because the
609 // the clang driver combines OS X and IOS support into a common Darwin
610 // toolchain that wants to know the iOS version number even when targeting
611 // OS X.
Chad Rosier537c2f52012-05-09 18:23:00 +0000612 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000613 Minor = 0;
614 Micro = 0;
Chad Rosier26bbdee2012-05-09 17:38:47 +0000615 break;
Chad Rosierecee47e2012-05-09 17:23:48 +0000616 case IOS:
617 getOSVersion(Major, Minor, Micro);
Chad Rosier537c2f52012-05-09 18:23:00 +0000618 // Default to 3.0.
619 if (Major == 0)
620 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000621 break;
622 }
623}
624
Daniel Dunbara14d2252009-07-26 03:31:47 +0000625void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000626 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000627}
628
629void Triple::setArch(ArchType Kind) {
630 setArchName(getArchTypeName(Kind));
631}
632
633void Triple::setVendor(VendorType Kind) {
634 setVendorName(getVendorTypeName(Kind));
635}
636
637void Triple::setOS(OSType Kind) {
638 setOSName(getOSTypeName(Kind));
639}
640
Duncan Sands5754a452010-09-16 08:25:48 +0000641void Triple::setEnvironment(EnvironmentType Kind) {
642 setEnvironmentName(getEnvironmentTypeName(Kind));
643}
644
Daniel Dunbar2928c832009-11-06 10:58:06 +0000645void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000646 // Work around a miscompilation bug for Twines in gcc 4.0.3.
647 SmallString<64> Triple;
648 Triple += Str;
649 Triple += "-";
650 Triple += getVendorName();
651 Triple += "-";
652 Triple += getOSAndEnvironmentName();
653 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000654}
655
Daniel Dunbar2928c832009-11-06 10:58:06 +0000656void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000657 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
658}
659
Daniel Dunbar2928c832009-11-06 10:58:06 +0000660void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000661 if (hasEnvironment())
662 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
663 "-" + getEnvironmentName());
664 else
665 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
666}
667
Daniel Dunbar2928c832009-11-06 10:58:06 +0000668void Triple::setEnvironmentName(StringRef Str) {
669 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000670 "-" + Str);
671}
672
Daniel Dunbar2928c832009-11-06 10:58:06 +0000673void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000674 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
675}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000676
677static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
678 switch (Arch) {
Micah Villmowe53d6052012-10-01 17:01:31 +0000679 case llvm::Triple::spir:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000680 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000681 return 0;
682
683 case llvm::Triple::msp430:
684 return 16;
685
686 case llvm::Triple::amdil:
687 case llvm::Triple::arm:
688 case llvm::Triple::cellspu:
689 case llvm::Triple::hexagon:
690 case llvm::Triple::le32:
691 case llvm::Triple::mblaze:
692 case llvm::Triple::mips:
693 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000694 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000695 case llvm::Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000696 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000697 case llvm::Triple::sparc:
698 case llvm::Triple::tce:
699 case llvm::Triple::thumb:
700 case llvm::Triple::x86:
701 case llvm::Triple::xcore:
702 return 32;
703
704 case llvm::Triple::mips64:
705 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000706 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000707 case llvm::Triple::ppc64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000708 case llvm::Triple::sparcv9:
709 case llvm::Triple::x86_64:
710 return 64;
711 }
712 llvm_unreachable("Invalid architecture value");
713}
714
715bool Triple::isArch64Bit() const {
716 return getArchPointerBitWidth(getArch()) == 64;
717}
718
719bool Triple::isArch32Bit() const {
720 return getArchPointerBitWidth(getArch()) == 32;
721}
722
723bool Triple::isArch16Bit() const {
724 return getArchPointerBitWidth(getArch()) == 16;
725}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000726
727Triple Triple::get32BitArchVariant() const {
728 Triple T(*this);
729 switch (getArch()) {
730 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000731 case Triple::msp430:
732 T.setArch(UnknownArch);
733 break;
734
735 case Triple::amdil:
Micah Villmowe53d6052012-10-01 17:01:31 +0000736 case Triple::spir:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000737 case Triple::arm:
738 case Triple::cellspu:
739 case Triple::hexagon:
740 case Triple::le32:
741 case Triple::mblaze:
742 case Triple::mips:
743 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000744 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000745 case Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000746 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000747 case Triple::sparc:
748 case Triple::tce:
749 case Triple::thumb:
750 case Triple::x86:
751 case Triple::xcore:
752 // Already 32-bit.
753 break;
754
755 case Triple::mips64: T.setArch(Triple::mips); break;
756 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000757 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000758 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000759 case Triple::sparcv9: T.setArch(Triple::sparc); break;
760 case Triple::x86_64: T.setArch(Triple::x86); break;
761 }
762 return T;
763}
764
765Triple Triple::get64BitArchVariant() const {
766 Triple T(*this);
767 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000768 case Triple::UnknownArch:
769 case Triple::amdil:
770 case Triple::arm:
771 case Triple::cellspu:
772 case Triple::hexagon:
773 case Triple::le32:
774 case Triple::mblaze:
775 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000776 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000777 case Triple::tce:
778 case Triple::thumb:
779 case Triple::xcore:
780 T.setArch(UnknownArch);
781 break;
782
Micah Villmowe53d6052012-10-01 17:01:31 +0000783 case Triple::spir:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000784 case Triple::mips64:
785 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000786 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000787 case Triple::ppc64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000788 case Triple::sparcv9:
789 case Triple::x86_64:
790 // Already 64-bit.
791 break;
792
793 case Triple::mips: T.setArch(Triple::mips64); break;
794 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000795 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000796 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000797 case Triple::sparc: T.setArch(Triple::sparcv9); break;
798 case Triple::x86: T.setArch(Triple::x86_64); break;
799 }
800 return T;
801}