blob: 7b26ea9b422540036be93b506fcb20b51094b9b3 [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";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000098 }
99
David Blaikie4d6ccb52012-01-20 21:51:11 +0000100 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000101}
102
103const char *Triple::getOSTypeName(OSType Kind) {
104 switch (Kind) {
105 case UnknownOS: return "unknown";
106
Duncan Sands852cd112009-06-19 14:40:01 +0000107 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000108 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000109 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000110 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000111 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000112 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000113 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000114 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000115 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000116 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000117 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000118 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000119 case OpenBSD: return "openbsd";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000120 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000121 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000122 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000123 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000124 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000125 case NativeClient: return "nacl";
Hal Finkela47406c2012-04-02 18:31:33 +0000126 case CNK: return "cnk";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000127 }
128
David Blaikie4d6ccb52012-01-20 21:51:11 +0000129 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000130}
131
Duncan Sands5754a452010-09-16 08:25:48 +0000132const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
133 switch (Kind) {
134 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000135 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000136 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000137 case GNUEABI: return "gnueabi";
138 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000139 case MachO: return "macho";
Chandler Carruthfd553c22012-01-10 19:46:00 +0000140 case ANDROIDEABI: return "androideabi";
Duncan Sands5754a452010-09-16 08:25:48 +0000141 }
142
David Blaikie4d6ccb52012-01-20 21:51:11 +0000143 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000144}
145
Daniel Dunbar2928c832009-11-06 10:58:06 +0000146Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000147 return StringSwitch<Triple::ArchType>(Name)
148 .Case("arm", arm)
149 .Case("cellspu", cellspu)
150 .Case("mips", mips)
151 .Case("mipsel", mipsel)
152 .Case("mips64", mips64)
153 .Case("mips64el", mips64el)
154 .Case("msp430", msp430)
155 .Case("ppc64", ppc64)
156 .Case("ppc32", ppc)
157 .Case("ppc", ppc)
158 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000159 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000160 .Case("hexagon", hexagon)
161 .Case("sparc", sparc)
162 .Case("sparcv9", sparcv9)
163 .Case("tce", tce)
164 .Case("thumb", thumb)
165 .Case("x86", x86)
166 .Case("x86-64", x86_64)
167 .Case("xcore", xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000168 .Case("nvptx", nvptx)
169 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000170 .Case("le32", le32)
171 .Case("amdil", amdil)
172 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000173}
174
Daniel Dunbar2928c832009-11-06 10:58:06 +0000175Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000176 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
177 // archs which Darwin doesn't use.
178
179 // The matching this routine does is fairly pointless, since it is neither the
180 // complete architecture list, nor a reasonable subset. The problem is that
181 // historically the driver driver accepts this and also ties its -march=
182 // handling to the architecture name, so we need to be careful before removing
183 // support for it.
184
Daniel Dunbared687882009-09-09 23:01:25 +0000185 // This code must be kept in sync with Clang's Darwin specific argument
186 // translation.
187
Chandler Carruth06accda2012-02-12 09:27:38 +0000188 return StringSwitch<ArchType>(Str)
189 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
190 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
191 .Case("ppc64", Triple::ppc64)
192 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
193 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
194 Triple::x86)
195 .Case("x86_64", Triple::x86_64)
196 // This is derived from the driver driver.
197 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
198 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000199 .Case("r600", Triple::r600)
Justin Holewinski49683f32012-05-04 20:18:50 +0000200 .Case("nvptx", Triple::nvptx)
201 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000202 .Case("amdil", Triple::amdil)
203 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000204}
205
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000206// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000207const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000208 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000209 return NULL;
210
Chandler Carruth06accda2012-02-12 09:27:38 +0000211 return StringSwitch<const char*>(getArchName())
212 .Case("i386", "i386")
213 .Case("x86_64", "x86_64")
214 .Case("powerpc", "ppc")
215 .Case("powerpc64", "ppc64")
216 .Cases("mblaze", "microblaze", "mblaze")
217 .Case("arm", "arm")
218 .Cases("armv4t", "thumbv4t", "armv4t")
219 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
220 .Cases("armv6", "thumbv6", "armv6")
221 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000222 .Case("r600", "r600")
Justin Holewinski49683f32012-05-04 20:18:50 +0000223 .Case("nvptx", "nvptx")
224 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000225 .Case("le32", "le32")
226 .Case("amdil", "amdil")
227 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000228}
229
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000230static Triple::ArchType parseArch(StringRef ArchName) {
231 return StringSwitch<Triple::ArchType>(ArchName)
232 .Cases("i386", "i486", "i586", "i686", Triple::x86)
233 // FIXME: Do we need to support these?
234 .Cases("i786", "i886", "i986", Triple::x86)
235 .Cases("amd64", "x86_64", Triple::x86_64)
236 .Case("powerpc", Triple::ppc)
237 .Cases("powerpc64", "ppu", Triple::ppc64)
238 .Case("mblaze", Triple::mblaze)
239 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000240 // FIXME: It would be good to replace these with explicit names for all the
241 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000242 .StartsWith("armv", Triple::arm)
243 .Case("thumb", Triple::thumb)
244 .StartsWith("thumbv", Triple::thumb)
245 .Cases("spu", "cellspu", Triple::cellspu)
246 .Case("msp430", Triple::msp430)
247 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000248 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000249 .Cases("mips64", "mips64eb", Triple::mips64)
250 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000251 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000252 .Case("hexagon", Triple::hexagon)
253 .Case("sparc", Triple::sparc)
254 .Case("sparcv9", Triple::sparcv9)
255 .Case("tce", Triple::tce)
256 .Case("xcore", Triple::xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000257 .Case("nvptx", Triple::nvptx)
258 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000259 .Case("le32", Triple::le32)
260 .Case("amdil", Triple::amdil)
261 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000262}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000263
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000264static Triple::VendorType parseVendor(StringRef VendorName) {
265 return StringSwitch<Triple::VendorType>(VendorName)
266 .Case("apple", Triple::Apple)
267 .Case("pc", Triple::PC)
268 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000269 .Case("bgp", Triple::BGP)
270 .Case("bgq", Triple::BGQ)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000271 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000272}
273
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000274static Triple::OSType parseOS(StringRef OSName) {
275 return StringSwitch<Triple::OSType>(OSName)
276 .StartsWith("auroraux", Triple::AuroraUX)
277 .StartsWith("cygwin", Triple::Cygwin)
278 .StartsWith("darwin", Triple::Darwin)
279 .StartsWith("dragonfly", Triple::DragonFly)
280 .StartsWith("freebsd", Triple::FreeBSD)
281 .StartsWith("ios", Triple::IOS)
282 .StartsWith("kfreebsd", Triple::KFreeBSD)
283 .StartsWith("linux", Triple::Linux)
284 .StartsWith("lv2", Triple::Lv2)
285 .StartsWith("macosx", Triple::MacOSX)
286 .StartsWith("mingw32", Triple::MinGW32)
287 .StartsWith("netbsd", Triple::NetBSD)
288 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000289 .StartsWith("solaris", Triple::Solaris)
290 .StartsWith("win32", Triple::Win32)
291 .StartsWith("haiku", Triple::Haiku)
292 .StartsWith("minix", Triple::Minix)
293 .StartsWith("rtems", Triple::RTEMS)
294 .StartsWith("nacl", Triple::NativeClient)
Hal Finkela47406c2012-04-02 18:31:33 +0000295 .StartsWith("cnk", Triple::CNK)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000296 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000297}
298
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000299static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
300 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
301 .StartsWith("eabi", Triple::EABI)
302 .StartsWith("gnueabihf", Triple::GNUEABIHF)
303 .StartsWith("gnueabi", Triple::GNUEABI)
304 .StartsWith("gnu", Triple::GNU)
305 .StartsWith("macho", Triple::MachO)
306 .StartsWith("androideabi", Triple::ANDROIDEABI)
307 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000308}
309
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000310/// \brief Construct a triple from the string representation provided.
311///
Chandler Carruth0523f412012-02-21 08:31:18 +0000312/// This stores the string representation and parses the various pieces into
313/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000314Triple::Triple(const Twine &Str)
315 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000316 Arch(parseArch(getArchName())),
317 Vendor(parseVendor(getVendorName())),
318 OS(parseOS(getOSName())),
319 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000320}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000321
322/// \brief Construct a triple from string representations of the architecture,
323/// vendor, and OS.
324///
Chandler Carruth0523f412012-02-21 08:31:18 +0000325/// This joins each argument into a canonical string representation and parses
326/// them into enum members. It leaves the environment unknown and omits it from
327/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000328Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
329 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000330 Arch(parseArch(ArchStr.str())),
331 Vendor(parseVendor(VendorStr.str())),
332 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000333 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000334}
335
336/// \brief Construct a triple from string representations of the architecture,
337/// vendor, OS, and environment.
338///
Chandler Carruth0523f412012-02-21 08:31:18 +0000339/// This joins each argument into a canonical string representation and parses
340/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000341Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
342 const Twine &EnvironmentStr)
343 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
344 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000345 Arch(parseArch(ArchStr.str())),
346 Vendor(parseVendor(VendorStr.str())),
347 OS(parseOS(OSStr.str())),
348 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000349}
350
Duncan Sands335db222010-08-12 11:31:39 +0000351std::string Triple::normalize(StringRef Str) {
352 // Parse into components.
353 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000354 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000355
356 // If the first component corresponds to a known architecture, preferentially
357 // use it for the architecture. If the second component corresponds to a
358 // known vendor, preferentially use it for the vendor, etc. This avoids silly
359 // component movement when a component parses as (eg) both a valid arch and a
360 // valid os.
361 ArchType Arch = UnknownArch;
362 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000363 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000364 VendorType Vendor = UnknownVendor;
365 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000366 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000367 OSType OS = UnknownOS;
368 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000369 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000370 EnvironmentType Environment = UnknownEnvironment;
371 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000372 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000373
374 // Note which components are already in their final position. These will not
375 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000376 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000377 Found[0] = Arch != UnknownArch;
378 Found[1] = Vendor != UnknownVendor;
379 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000380 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000381
382 // If they are not there already, permute the components into their canonical
383 // positions by seeing if they parse as a valid architecture, and if so moving
384 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000385 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000386 if (Found[Pos])
387 continue; // Already in the canonical position.
388
389 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
390 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000391 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000392 continue;
393
394 // Does this component parse as valid for the target position?
395 bool Valid = false;
396 StringRef Comp = Components[Idx];
397 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000398 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000399 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000400 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000401 Valid = Arch != UnknownArch;
402 break;
403 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000404 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000405 Valid = Vendor != UnknownVendor;
406 break;
407 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000408 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000409 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000410 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000411 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000412 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000413 Valid = Environment != UnknownEnvironment;
414 break;
Duncan Sands335db222010-08-12 11:31:39 +0000415 }
416 if (!Valid)
417 continue; // Nope, try the next component.
418
419 // Move the component to the target position, pushing any non-fixed
420 // components that are in the way to the right. This tends to give
421 // good results in the common cases of a forgotten vendor component
422 // or a wrongly positioned environment.
423 if (Pos < Idx) {
424 // Insert left, pushing the existing components to the right. For
425 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
426 StringRef CurrentComponent(""); // The empty component.
427 // Replace the component we are moving with an empty component.
428 std::swap(CurrentComponent, Components[Idx]);
429 // Insert the component being moved at Pos, displacing any existing
430 // components to the right.
431 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
432 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000433 while (i < array_lengthof(Found) && Found[i])
434 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000435 // Place the component at the new position, getting the component
436 // that was at this position - it will be moved right.
437 std::swap(CurrentComponent, Components[i]);
438 }
439 } else if (Pos > Idx) {
440 // Push right by inserting empty components until the component at Idx
441 // reaches the target position Pos. For example, pc-a -> -pc-a when
442 // moving pc to the second position.
443 do {
444 // Insert one empty component at Idx.
445 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000446 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000447 // Place the component at the new position, getting the component
448 // that was at this position - it will be moved right.
449 std::swap(CurrentComponent, Components[i]);
450 // If it was placed on top of an empty component then we are done.
451 if (CurrentComponent.empty())
452 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000453 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000454 while (++i < array_lengthof(Found) && Found[i])
455 ;
Duncan Sands335db222010-08-12 11:31:39 +0000456 }
457 // The last component was pushed off the end - append it.
458 if (!CurrentComponent.empty())
459 Components.push_back(CurrentComponent);
460
461 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000462 while (++Idx < array_lengthof(Found) && Found[Idx])
463 ;
Duncan Sands335db222010-08-12 11:31:39 +0000464 } while (Idx < Pos); // Add more until the final position is reached.
465 }
466 assert(Pos < Components.size() && Components[Pos] == Comp &&
467 "Component moved wrong!");
468 Found[Pos] = true;
469 break;
470 }
471 }
472
473 // Special case logic goes here. At this point Arch, Vendor and OS have the
474 // correct values for the computed components.
475
476 // Stick the corrected components back together to form the normalized string.
477 std::string Normalized;
478 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
479 if (i) Normalized += '-';
480 Normalized += Components[i];
481 }
482 return Normalized;
483}
484
Daniel Dunbara14d2252009-07-26 03:31:47 +0000485StringRef Triple::getArchName() const {
486 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000487}
488
Daniel Dunbara14d2252009-07-26 03:31:47 +0000489StringRef Triple::getVendorName() const {
490 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
491 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000492}
493
Daniel Dunbara14d2252009-07-26 03:31:47 +0000494StringRef Triple::getOSName() const {
495 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
496 Tmp = Tmp.split('-').second; // Strip second component
497 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000498}
499
Daniel Dunbara14d2252009-07-26 03:31:47 +0000500StringRef Triple::getEnvironmentName() const {
501 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
502 Tmp = Tmp.split('-').second; // Strip second component
503 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000504}
505
Daniel Dunbara14d2252009-07-26 03:31:47 +0000506StringRef Triple::getOSAndEnvironmentName() const {
507 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
508 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000509}
510
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000511static unsigned EatNumber(StringRef &Str) {
512 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000513 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000514
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000515 do {
516 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000517 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000518
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000519 // Eat the digit.
520 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000521 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000522
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000523 return Result;
524}
525
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000526void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
527 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000528 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000529
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000530 // Assume that the OS portion of the triple starts with the canonical name.
531 StringRef OSTypeName = getOSTypeName(getOS());
532 if (OSName.startswith(OSTypeName))
533 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000534
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000535 // Any unset version defaults to 0.
536 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000537
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000538 // Parse up to three components.
539 unsigned *Components[3] = { &Major, &Minor, &Micro };
540 for (unsigned i = 0; i != 3; ++i) {
541 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
542 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000543
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000544 // Consume the leading number.
545 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000546
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000547 // Consume the separator, if present.
548 if (OSName.startswith("."))
549 OSName = OSName.substr(1);
550 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000551}
552
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000553bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
554 unsigned &Micro) const {
555 getOSVersion(Major, Minor, Micro);
556
557 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000558 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000559 case Darwin:
560 // Default to darwin8, i.e., MacOSX 10.4.
561 if (Major == 0)
562 Major = 8;
563 // Darwin version numbers are skewed from OS X versions.
564 if (Major < 4)
565 return false;
566 Micro = 0;
567 Minor = Major - 4;
568 Major = 10;
569 break;
570 case MacOSX:
571 // Default to 10.4.
572 if (Major == 0) {
573 Major = 10;
574 Minor = 4;
575 }
576 if (Major != 10)
577 return false;
578 break;
579 case IOS:
580 // Ignore the version from the triple. This is only handled because the
581 // the clang driver combines OS X and IOS support into a common Darwin
582 // toolchain that wants to know the OS X version number even when targeting
583 // IOS.
584 Major = 10;
585 Minor = 4;
586 Micro = 0;
587 break;
588 }
589 return true;
590}
591
Chad Rosierecee47e2012-05-09 17:23:48 +0000592void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
593 unsigned &Micro) const {
594 switch (getOS()) {
595 default: llvm_unreachable("unexpected OS for Darwin triple");
596 case Darwin:
597 case MacOSX:
598 // Ignore the version from the triple. This is only handled because the
599 // the clang driver combines OS X and IOS support into a common Darwin
600 // toolchain that wants to know the iOS version number even when targeting
601 // OS X.
Chad Rosier537c2f52012-05-09 18:23:00 +0000602 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000603 Minor = 0;
604 Micro = 0;
Chad Rosier26bbdee2012-05-09 17:38:47 +0000605 break;
Chad Rosierecee47e2012-05-09 17:23:48 +0000606 case IOS:
607 getOSVersion(Major, Minor, Micro);
Chad Rosier537c2f52012-05-09 18:23:00 +0000608 // Default to 3.0.
609 if (Major == 0)
610 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000611 break;
612 }
613}
614
Daniel Dunbara14d2252009-07-26 03:31:47 +0000615void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000616 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000617}
618
619void Triple::setArch(ArchType Kind) {
620 setArchName(getArchTypeName(Kind));
621}
622
623void Triple::setVendor(VendorType Kind) {
624 setVendorName(getVendorTypeName(Kind));
625}
626
627void Triple::setOS(OSType Kind) {
628 setOSName(getOSTypeName(Kind));
629}
630
Duncan Sands5754a452010-09-16 08:25:48 +0000631void Triple::setEnvironment(EnvironmentType Kind) {
632 setEnvironmentName(getEnvironmentTypeName(Kind));
633}
634
Daniel Dunbar2928c832009-11-06 10:58:06 +0000635void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000636 // Work around a miscompilation bug for Twines in gcc 4.0.3.
637 SmallString<64> Triple;
638 Triple += Str;
639 Triple += "-";
640 Triple += getVendorName();
641 Triple += "-";
642 Triple += getOSAndEnvironmentName();
643 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000644}
645
Daniel Dunbar2928c832009-11-06 10:58:06 +0000646void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000647 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
648}
649
Daniel Dunbar2928c832009-11-06 10:58:06 +0000650void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000651 if (hasEnvironment())
652 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
653 "-" + getEnvironmentName());
654 else
655 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
656}
657
Daniel Dunbar2928c832009-11-06 10:58:06 +0000658void Triple::setEnvironmentName(StringRef Str) {
659 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000660 "-" + Str);
661}
662
Daniel Dunbar2928c832009-11-06 10:58:06 +0000663void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000664 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
665}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000666
667static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
668 switch (Arch) {
669 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000670 return 0;
671
672 case llvm::Triple::msp430:
673 return 16;
674
675 case llvm::Triple::amdil:
676 case llvm::Triple::arm:
677 case llvm::Triple::cellspu:
678 case llvm::Triple::hexagon:
679 case llvm::Triple::le32:
680 case llvm::Triple::mblaze:
681 case llvm::Triple::mips:
682 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000683 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000684 case llvm::Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000685 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000686 case llvm::Triple::sparc:
687 case llvm::Triple::tce:
688 case llvm::Triple::thumb:
689 case llvm::Triple::x86:
690 case llvm::Triple::xcore:
691 return 32;
692
693 case llvm::Triple::mips64:
694 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000695 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000696 case llvm::Triple::ppc64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000697 case llvm::Triple::sparcv9:
698 case llvm::Triple::x86_64:
699 return 64;
700 }
701 llvm_unreachable("Invalid architecture value");
702}
703
704bool Triple::isArch64Bit() const {
705 return getArchPointerBitWidth(getArch()) == 64;
706}
707
708bool Triple::isArch32Bit() const {
709 return getArchPointerBitWidth(getArch()) == 32;
710}
711
712bool Triple::isArch16Bit() const {
713 return getArchPointerBitWidth(getArch()) == 16;
714}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000715
716Triple Triple::get32BitArchVariant() const {
717 Triple T(*this);
718 switch (getArch()) {
719 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000720 case Triple::msp430:
721 T.setArch(UnknownArch);
722 break;
723
724 case Triple::amdil:
725 case Triple::arm:
726 case Triple::cellspu:
727 case Triple::hexagon:
728 case Triple::le32:
729 case Triple::mblaze:
730 case Triple::mips:
731 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000732 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000733 case Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000734 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000735 case Triple::sparc:
736 case Triple::tce:
737 case Triple::thumb:
738 case Triple::x86:
739 case Triple::xcore:
740 // Already 32-bit.
741 break;
742
743 case Triple::mips64: T.setArch(Triple::mips); break;
744 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000745 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000746 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000747 case Triple::sparcv9: T.setArch(Triple::sparc); break;
748 case Triple::x86_64: T.setArch(Triple::x86); break;
749 }
750 return T;
751}
752
753Triple Triple::get64BitArchVariant() const {
754 Triple T(*this);
755 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000756 case Triple::UnknownArch:
757 case Triple::amdil:
758 case Triple::arm:
759 case Triple::cellspu:
760 case Triple::hexagon:
761 case Triple::le32:
762 case Triple::mblaze:
763 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000764 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000765 case Triple::tce:
766 case Triple::thumb:
767 case Triple::xcore:
768 T.setArch(UnknownArch);
769 break;
770
771 case Triple::mips64:
772 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000773 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000774 case Triple::ppc64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000775 case Triple::sparcv9:
776 case Triple::x86_64:
777 // Already 64-bit.
778 break;
779
780 case Triple::mips: T.setArch(Triple::mips64); break;
781 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000782 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000783 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000784 case Triple::sparc: T.setArch(Triple::sparcv9); break;
785 case Triple::x86: T.setArch(Triple::x86_64); break;
786 }
787 return T;
788}