blob: 4ee4716b37fb42cde0a4396aa8663e34735d4eb4 [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 Holewinskie1fee482011-04-20 15:37:17 +000041 case ptx32: return "ptx32";
42 case ptx64: return "ptx64";
Justin Holewinski49683f32012-05-04 20:18:50 +000043 case nvptx: return "nvptx";
44 case nvptx64: return "nvptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000045 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000046 case amdil: return "amdil";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000047 }
48
David Blaikie4d6ccb52012-01-20 21:51:11 +000049 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000050}
51
Daniel Dunbar688b55b2009-08-24 09:53:06 +000052const char *Triple::getArchTypePrefix(ArchType Kind) {
53 switch (Kind) {
54 default:
55 return 0;
56
Daniel Dunbar688b55b2009-08-24 09:53:06 +000057 case arm:
58 case thumb: return "arm";
59
Daniel Dunbar688b55b2009-08-24 09:53:06 +000060 case cellspu: return "spu";
61
62 case ppc64:
63 case ppc: return "ppc";
64
Wesley Pecka70f28c2010-02-23 19:15:24 +000065 case mblaze: return "mblaze";
66
Tony Linthicumb4b54152011-12-12 21:14:40 +000067 case hexagon: return "hexagon";
68
Anton Korobeynikov74156592012-03-09 10:09:36 +000069 case r600: return "r600";
70
Chris Lattner87c06d62010-02-04 06:34:01 +000071 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000072 case sparc: return "sparc";
73
74 case x86:
75 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000076
Daniel Dunbar688b55b2009-08-24 09:53:06 +000077 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000078
Justin Holewinskie1fee482011-04-20 15:37:17 +000079 case ptx32: return "ptx";
80 case ptx64: return "ptx";
Justin Holewinski49683f32012-05-04 20:18:50 +000081 case nvptx: return "nvptx";
82 case nvptx64: return "nvptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000083 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000084 case amdil: return "amdil";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000085 }
86}
87
Daniel Dunbar23e97b02009-04-01 21:53:23 +000088const char *Triple::getVendorTypeName(VendorType Kind) {
89 switch (Kind) {
90 case UnknownVendor: return "unknown";
91
92 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000093 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000094 case SCEI: return "scei";
Hal Finkela47406c2012-04-02 18:31:33 +000095 case BGP: return "bgp";
96 case BGQ: return "bgq";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000097 }
98
David Blaikie4d6ccb52012-01-20 21:51:11 +000099 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000100}
101
102const char *Triple::getOSTypeName(OSType Kind) {
103 switch (Kind) {
104 case UnknownOS: return "unknown";
105
Duncan Sands852cd112009-06-19 14:40:01 +0000106 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000107 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000108 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000109 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000110 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000111 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000112 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000113 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000114 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000115 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000116 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000117 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000118 case OpenBSD: return "openbsd";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000119 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000120 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000121 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000122 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000123 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000124 case NativeClient: return "nacl";
Hal Finkela47406c2012-04-02 18:31:33 +0000125 case CNK: return "cnk";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000126 }
127
David Blaikie4d6ccb52012-01-20 21:51:11 +0000128 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000129}
130
Duncan Sands5754a452010-09-16 08:25:48 +0000131const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
132 switch (Kind) {
133 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000134 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000135 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000136 case GNUEABI: return "gnueabi";
137 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 case MachO: return "macho";
Chandler Carruthfd553c22012-01-10 19:46:00 +0000139 case ANDROIDEABI: return "androideabi";
Duncan Sands5754a452010-09-16 08:25:48 +0000140 }
141
David Blaikie4d6ccb52012-01-20 21:51:11 +0000142 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000143}
144
Daniel Dunbar2928c832009-11-06 10:58:06 +0000145Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000146 return StringSwitch<Triple::ArchType>(Name)
147 .Case("arm", arm)
148 .Case("cellspu", cellspu)
149 .Case("mips", mips)
150 .Case("mipsel", mipsel)
151 .Case("mips64", mips64)
152 .Case("mips64el", mips64el)
153 .Case("msp430", msp430)
154 .Case("ppc64", ppc64)
155 .Case("ppc32", ppc)
156 .Case("ppc", ppc)
157 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000158 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000159 .Case("hexagon", hexagon)
160 .Case("sparc", sparc)
161 .Case("sparcv9", sparcv9)
162 .Case("tce", tce)
163 .Case("thumb", thumb)
164 .Case("x86", x86)
165 .Case("x86-64", x86_64)
166 .Case("xcore", xcore)
167 .Case("ptx32", ptx32)
168 .Case("ptx64", ptx64)
Justin Holewinski49683f32012-05-04 20:18:50 +0000169 .Case("nvptx", nvptx)
170 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000171 .Case("le32", le32)
172 .Case("amdil", amdil)
173 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000174}
175
Daniel Dunbar2928c832009-11-06 10:58:06 +0000176Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000177 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
178 // archs which Darwin doesn't use.
179
180 // The matching this routine does is fairly pointless, since it is neither the
181 // complete architecture list, nor a reasonable subset. The problem is that
182 // historically the driver driver accepts this and also ties its -march=
183 // handling to the architecture name, so we need to be careful before removing
184 // support for it.
185
Daniel Dunbared687882009-09-09 23:01:25 +0000186 // This code must be kept in sync with Clang's Darwin specific argument
187 // translation.
188
Chandler Carruth06accda2012-02-12 09:27:38 +0000189 return StringSwitch<ArchType>(Str)
190 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
191 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
192 .Case("ppc64", Triple::ppc64)
193 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
194 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
195 Triple::x86)
196 .Case("x86_64", Triple::x86_64)
197 // This is derived from the driver driver.
198 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
199 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000200 .Case("r600", Triple::r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000201 .Case("ptx32", Triple::ptx32)
202 .Case("ptx64", Triple::ptx64)
Justin Holewinski49683f32012-05-04 20:18:50 +0000203 .Case("nvptx", Triple::nvptx)
204 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000205 .Case("amdil", Triple::amdil)
206 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000207}
208
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000209// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000210const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000211 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000212 return NULL;
213
Chandler Carruth06accda2012-02-12 09:27:38 +0000214 return StringSwitch<const char*>(getArchName())
215 .Case("i386", "i386")
216 .Case("x86_64", "x86_64")
217 .Case("powerpc", "ppc")
218 .Case("powerpc64", "ppc64")
219 .Cases("mblaze", "microblaze", "mblaze")
220 .Case("arm", "arm")
221 .Cases("armv4t", "thumbv4t", "armv4t")
222 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
223 .Cases("armv6", "thumbv6", "armv6")
224 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000225 .Case("r600", "r600")
Chandler Carruth06accda2012-02-12 09:27:38 +0000226 .Case("ptx32", "ptx32")
227 .Case("ptx64", "ptx64")
Justin Holewinski49683f32012-05-04 20:18:50 +0000228 .Case("nvptx", "nvptx")
229 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000230 .Case("le32", "le32")
231 .Case("amdil", "amdil")
232 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000233}
234
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000235static Triple::ArchType parseArch(StringRef ArchName) {
236 return StringSwitch<Triple::ArchType>(ArchName)
237 .Cases("i386", "i486", "i586", "i686", Triple::x86)
238 // FIXME: Do we need to support these?
239 .Cases("i786", "i886", "i986", Triple::x86)
240 .Cases("amd64", "x86_64", Triple::x86_64)
241 .Case("powerpc", Triple::ppc)
242 .Cases("powerpc64", "ppu", Triple::ppc64)
243 .Case("mblaze", Triple::mblaze)
244 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000245 // FIXME: It would be good to replace these with explicit names for all the
246 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000247 .StartsWith("armv", Triple::arm)
248 .Case("thumb", Triple::thumb)
249 .StartsWith("thumbv", Triple::thumb)
250 .Cases("spu", "cellspu", Triple::cellspu)
251 .Case("msp430", Triple::msp430)
252 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000253 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000254 .Cases("mips64", "mips64eb", Triple::mips64)
255 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000256 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000257 .Case("hexagon", Triple::hexagon)
258 .Case("sparc", Triple::sparc)
259 .Case("sparcv9", Triple::sparcv9)
260 .Case("tce", Triple::tce)
261 .Case("xcore", Triple::xcore)
262 .Case("ptx32", Triple::ptx32)
263 .Case("ptx64", Triple::ptx64)
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)
268 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000269}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000270
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000271static Triple::VendorType parseVendor(StringRef VendorName) {
272 return StringSwitch<Triple::VendorType>(VendorName)
273 .Case("apple", Triple::Apple)
274 .Case("pc", Triple::PC)
275 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000276 .Case("bgp", Triple::BGP)
277 .Case("bgq", Triple::BGQ)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000278 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000279}
280
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000281static Triple::OSType parseOS(StringRef OSName) {
282 return StringSwitch<Triple::OSType>(OSName)
283 .StartsWith("auroraux", Triple::AuroraUX)
284 .StartsWith("cygwin", Triple::Cygwin)
285 .StartsWith("darwin", Triple::Darwin)
286 .StartsWith("dragonfly", Triple::DragonFly)
287 .StartsWith("freebsd", Triple::FreeBSD)
288 .StartsWith("ios", Triple::IOS)
289 .StartsWith("kfreebsd", Triple::KFreeBSD)
290 .StartsWith("linux", Triple::Linux)
291 .StartsWith("lv2", Triple::Lv2)
292 .StartsWith("macosx", Triple::MacOSX)
293 .StartsWith("mingw32", Triple::MinGW32)
294 .StartsWith("netbsd", Triple::NetBSD)
295 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000296 .StartsWith("solaris", Triple::Solaris)
297 .StartsWith("win32", Triple::Win32)
298 .StartsWith("haiku", Triple::Haiku)
299 .StartsWith("minix", Triple::Minix)
300 .StartsWith("rtems", Triple::RTEMS)
301 .StartsWith("nacl", Triple::NativeClient)
Hal Finkela47406c2012-04-02 18:31:33 +0000302 .StartsWith("cnk", Triple::CNK)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000303 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000304}
305
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000306static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
307 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
308 .StartsWith("eabi", Triple::EABI)
309 .StartsWith("gnueabihf", Triple::GNUEABIHF)
310 .StartsWith("gnueabi", Triple::GNUEABI)
311 .StartsWith("gnu", Triple::GNU)
312 .StartsWith("macho", Triple::MachO)
313 .StartsWith("androideabi", Triple::ANDROIDEABI)
314 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000315}
316
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000317/// \brief Construct a triple from the string representation provided.
318///
Chandler Carruth0523f412012-02-21 08:31:18 +0000319/// This stores the string representation and parses the various pieces into
320/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000321Triple::Triple(const Twine &Str)
322 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000323 Arch(parseArch(getArchName())),
324 Vendor(parseVendor(getVendorName())),
325 OS(parseOS(getOSName())),
326 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000327}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000328
329/// \brief Construct a triple from string representations of the architecture,
330/// vendor, and OS.
331///
Chandler Carruth0523f412012-02-21 08:31:18 +0000332/// This joins each argument into a canonical string representation and parses
333/// them into enum members. It leaves the environment unknown and omits it from
334/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000335Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
336 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000337 Arch(parseArch(ArchStr.str())),
338 Vendor(parseVendor(VendorStr.str())),
339 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000340 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000341}
342
343/// \brief Construct a triple from string representations of the architecture,
344/// vendor, OS, and environment.
345///
Chandler Carruth0523f412012-02-21 08:31:18 +0000346/// This joins each argument into a canonical string representation and parses
347/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000348Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
349 const Twine &EnvironmentStr)
350 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
351 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000352 Arch(parseArch(ArchStr.str())),
353 Vendor(parseVendor(VendorStr.str())),
354 OS(parseOS(OSStr.str())),
355 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000356}
357
Duncan Sands335db222010-08-12 11:31:39 +0000358std::string Triple::normalize(StringRef Str) {
359 // Parse into components.
360 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000361 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000362
363 // If the first component corresponds to a known architecture, preferentially
364 // use it for the architecture. If the second component corresponds to a
365 // known vendor, preferentially use it for the vendor, etc. This avoids silly
366 // component movement when a component parses as (eg) both a valid arch and a
367 // valid os.
368 ArchType Arch = UnknownArch;
369 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000370 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000371 VendorType Vendor = UnknownVendor;
372 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000373 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000374 OSType OS = UnknownOS;
375 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000376 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000377 EnvironmentType Environment = UnknownEnvironment;
378 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000379 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000380
381 // Note which components are already in their final position. These will not
382 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000383 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000384 Found[0] = Arch != UnknownArch;
385 Found[1] = Vendor != UnknownVendor;
386 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000387 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000388
389 // If they are not there already, permute the components into their canonical
390 // positions by seeing if they parse as a valid architecture, and if so moving
391 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000392 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000393 if (Found[Pos])
394 continue; // Already in the canonical position.
395
396 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
397 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000398 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000399 continue;
400
401 // Does this component parse as valid for the target position?
402 bool Valid = false;
403 StringRef Comp = Components[Idx];
404 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000405 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000406 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000407 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000408 Valid = Arch != UnknownArch;
409 break;
410 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000411 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000412 Valid = Vendor != UnknownVendor;
413 break;
414 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000415 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000416 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000417 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000418 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000419 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000420 Valid = Environment != UnknownEnvironment;
421 break;
Duncan Sands335db222010-08-12 11:31:39 +0000422 }
423 if (!Valid)
424 continue; // Nope, try the next component.
425
426 // Move the component to the target position, pushing any non-fixed
427 // components that are in the way to the right. This tends to give
428 // good results in the common cases of a forgotten vendor component
429 // or a wrongly positioned environment.
430 if (Pos < Idx) {
431 // Insert left, pushing the existing components to the right. For
432 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
433 StringRef CurrentComponent(""); // The empty component.
434 // Replace the component we are moving with an empty component.
435 std::swap(CurrentComponent, Components[Idx]);
436 // Insert the component being moved at Pos, displacing any existing
437 // components to the right.
438 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
439 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000440 while (i < array_lengthof(Found) && Found[i])
441 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000442 // Place the component at the new position, getting the component
443 // that was at this position - it will be moved right.
444 std::swap(CurrentComponent, Components[i]);
445 }
446 } else if (Pos > Idx) {
447 // Push right by inserting empty components until the component at Idx
448 // reaches the target position Pos. For example, pc-a -> -pc-a when
449 // moving pc to the second position.
450 do {
451 // Insert one empty component at Idx.
452 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000453 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000454 // Place the component at the new position, getting the component
455 // that was at this position - it will be moved right.
456 std::swap(CurrentComponent, Components[i]);
457 // If it was placed on top of an empty component then we are done.
458 if (CurrentComponent.empty())
459 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000460 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000461 while (++i < array_lengthof(Found) && Found[i])
462 ;
Duncan Sands335db222010-08-12 11:31:39 +0000463 }
464 // The last component was pushed off the end - append it.
465 if (!CurrentComponent.empty())
466 Components.push_back(CurrentComponent);
467
468 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000469 while (++Idx < array_lengthof(Found) && Found[Idx])
470 ;
Duncan Sands335db222010-08-12 11:31:39 +0000471 } while (Idx < Pos); // Add more until the final position is reached.
472 }
473 assert(Pos < Components.size() && Components[Pos] == Comp &&
474 "Component moved wrong!");
475 Found[Pos] = true;
476 break;
477 }
478 }
479
480 // Special case logic goes here. At this point Arch, Vendor and OS have the
481 // correct values for the computed components.
482
483 // Stick the corrected components back together to form the normalized string.
484 std::string Normalized;
485 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
486 if (i) Normalized += '-';
487 Normalized += Components[i];
488 }
489 return Normalized;
490}
491
Daniel Dunbara14d2252009-07-26 03:31:47 +0000492StringRef Triple::getArchName() const {
493 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000494}
495
Daniel Dunbara14d2252009-07-26 03:31:47 +0000496StringRef Triple::getVendorName() const {
497 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
498 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000499}
500
Daniel Dunbara14d2252009-07-26 03:31:47 +0000501StringRef Triple::getOSName() const {
502 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
503 Tmp = Tmp.split('-').second; // Strip second component
504 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000505}
506
Daniel Dunbara14d2252009-07-26 03:31:47 +0000507StringRef Triple::getEnvironmentName() const {
508 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
509 Tmp = Tmp.split('-').second; // Strip second component
510 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000511}
512
Daniel Dunbara14d2252009-07-26 03:31:47 +0000513StringRef Triple::getOSAndEnvironmentName() const {
514 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
515 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000516}
517
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000518static unsigned EatNumber(StringRef &Str) {
519 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000520 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000521
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000522 do {
523 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000524 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000525
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000526 // Eat the digit.
527 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000528 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000529
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000530 return Result;
531}
532
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000533void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
534 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000535 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000536
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000537 // Assume that the OS portion of the triple starts with the canonical name.
538 StringRef OSTypeName = getOSTypeName(getOS());
539 if (OSName.startswith(OSTypeName))
540 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000541
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000542 // Any unset version defaults to 0.
543 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000544
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000545 // Parse up to three components.
546 unsigned *Components[3] = { &Major, &Minor, &Micro };
547 for (unsigned i = 0; i != 3; ++i) {
548 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
549 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000550
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000551 // Consume the leading number.
552 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000553
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000554 // Consume the separator, if present.
555 if (OSName.startswith("."))
556 OSName = OSName.substr(1);
557 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000558}
559
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000560bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
561 unsigned &Micro) const {
562 getOSVersion(Major, Minor, Micro);
563
564 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000565 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000566 case Darwin:
567 // Default to darwin8, i.e., MacOSX 10.4.
568 if (Major == 0)
569 Major = 8;
570 // Darwin version numbers are skewed from OS X versions.
571 if (Major < 4)
572 return false;
573 Micro = 0;
574 Minor = Major - 4;
575 Major = 10;
576 break;
577 case MacOSX:
578 // Default to 10.4.
579 if (Major == 0) {
580 Major = 10;
581 Minor = 4;
582 }
583 if (Major != 10)
584 return false;
585 break;
586 case IOS:
587 // Ignore the version from the triple. This is only handled because the
588 // the clang driver combines OS X and IOS support into a common Darwin
589 // toolchain that wants to know the OS X version number even when targeting
590 // IOS.
591 Major = 10;
592 Minor = 4;
593 Micro = 0;
594 break;
595 }
596 return true;
597}
598
Daniel Dunbara14d2252009-07-26 03:31:47 +0000599void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000600 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000601}
602
603void Triple::setArch(ArchType Kind) {
604 setArchName(getArchTypeName(Kind));
605}
606
607void Triple::setVendor(VendorType Kind) {
608 setVendorName(getVendorTypeName(Kind));
609}
610
611void Triple::setOS(OSType Kind) {
612 setOSName(getOSTypeName(Kind));
613}
614
Duncan Sands5754a452010-09-16 08:25:48 +0000615void Triple::setEnvironment(EnvironmentType Kind) {
616 setEnvironmentName(getEnvironmentTypeName(Kind));
617}
618
Daniel Dunbar2928c832009-11-06 10:58:06 +0000619void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000620 // Work around a miscompilation bug for Twines in gcc 4.0.3.
621 SmallString<64> Triple;
622 Triple += Str;
623 Triple += "-";
624 Triple += getVendorName();
625 Triple += "-";
626 Triple += getOSAndEnvironmentName();
627 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000628}
629
Daniel Dunbar2928c832009-11-06 10:58:06 +0000630void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000631 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
632}
633
Daniel Dunbar2928c832009-11-06 10:58:06 +0000634void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000635 if (hasEnvironment())
636 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
637 "-" + getEnvironmentName());
638 else
639 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
640}
641
Daniel Dunbar2928c832009-11-06 10:58:06 +0000642void Triple::setEnvironmentName(StringRef Str) {
643 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000644 "-" + Str);
645}
646
Daniel Dunbar2928c832009-11-06 10:58:06 +0000647void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000648 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
649}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000650
651static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
652 switch (Arch) {
653 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000654 return 0;
655
656 case llvm::Triple::msp430:
657 return 16;
658
659 case llvm::Triple::amdil:
660 case llvm::Triple::arm:
661 case llvm::Triple::cellspu:
662 case llvm::Triple::hexagon:
663 case llvm::Triple::le32:
664 case llvm::Triple::mblaze:
665 case llvm::Triple::mips:
666 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000667 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000668 case llvm::Triple::ppc:
669 case llvm::Triple::ptx32:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000670 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000671 case llvm::Triple::sparc:
672 case llvm::Triple::tce:
673 case llvm::Triple::thumb:
674 case llvm::Triple::x86:
675 case llvm::Triple::xcore:
676 return 32;
677
678 case llvm::Triple::mips64:
679 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000680 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000681 case llvm::Triple::ppc64:
682 case llvm::Triple::ptx64:
683 case llvm::Triple::sparcv9:
684 case llvm::Triple::x86_64:
685 return 64;
686 }
687 llvm_unreachable("Invalid architecture value");
688}
689
690bool Triple::isArch64Bit() const {
691 return getArchPointerBitWidth(getArch()) == 64;
692}
693
694bool Triple::isArch32Bit() const {
695 return getArchPointerBitWidth(getArch()) == 32;
696}
697
698bool Triple::isArch16Bit() const {
699 return getArchPointerBitWidth(getArch()) == 16;
700}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000701
702Triple Triple::get32BitArchVariant() const {
703 Triple T(*this);
704 switch (getArch()) {
705 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000706 case Triple::msp430:
707 T.setArch(UnknownArch);
708 break;
709
710 case Triple::amdil:
711 case Triple::arm:
712 case Triple::cellspu:
713 case Triple::hexagon:
714 case Triple::le32:
715 case Triple::mblaze:
716 case Triple::mips:
717 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000718 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000719 case Triple::ppc:
720 case Triple::ptx32:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000721 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000722 case Triple::sparc:
723 case Triple::tce:
724 case Triple::thumb:
725 case Triple::x86:
726 case Triple::xcore:
727 // Already 32-bit.
728 break;
729
730 case Triple::mips64: T.setArch(Triple::mips); break;
731 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000732 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000733 case Triple::ppc64: T.setArch(Triple::ppc); break;
734 case Triple::ptx64: T.setArch(Triple::ptx32); break;
735 case Triple::sparcv9: T.setArch(Triple::sparc); break;
736 case Triple::x86_64: T.setArch(Triple::x86); break;
737 }
738 return T;
739}
740
741Triple Triple::get64BitArchVariant() const {
742 Triple T(*this);
743 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000744 case Triple::UnknownArch:
745 case Triple::amdil:
746 case Triple::arm:
747 case Triple::cellspu:
748 case Triple::hexagon:
749 case Triple::le32:
750 case Triple::mblaze:
751 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000752 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000753 case Triple::tce:
754 case Triple::thumb:
755 case Triple::xcore:
756 T.setArch(UnknownArch);
757 break;
758
759 case Triple::mips64:
760 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000761 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000762 case Triple::ppc64:
763 case Triple::ptx64:
764 case Triple::sparcv9:
765 case Triple::x86_64:
766 // Already 64-bit.
767 break;
768
769 case Triple::mips: T.setArch(Triple::mips64); break;
770 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000771 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000772 case Triple::ppc: T.setArch(Triple::ppc64); break;
773 case Triple::ptx32: T.setArch(Triple::ptx64); break;
774 case Triple::sparc: T.setArch(Triple::sparcv9); break;
775 case Triple::x86: T.setArch(Triple::x86_64); break;
776 }
777 return T;
778}