blob: 822ada7d9beb2be1f69f50d4db0ea610bb96a6d3 [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
Tony Linthicumb4b54152011-12-12 21:14:40 +000065 case hexagon: return "hexagon";
66
Anton Korobeynikov74156592012-03-09 10:09:36 +000067 case r600: return "r600";
68
Chris Lattner87c06d62010-02-04 06:34:01 +000069 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000070 case sparc: return "sparc";
71
72 case x86:
73 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000074
Daniel Dunbar688b55b2009-08-24 09:53:06 +000075 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000076
Justin Holewinski49683f32012-05-04 20:18:50 +000077 case nvptx: return "nvptx";
78 case nvptx64: return "nvptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000079 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000080 case amdil: return "amdil";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000081 }
82}
83
Daniel Dunbar23e97b02009-04-01 21:53:23 +000084const char *Triple::getVendorTypeName(VendorType Kind) {
85 switch (Kind) {
86 case UnknownVendor: return "unknown";
87
88 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000089 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000090 case SCEI: return "scei";
Hal Finkela47406c2012-04-02 18:31:33 +000091 case BGP: return "bgp";
92 case BGQ: return "bgq";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000093 }
94
David Blaikie4d6ccb52012-01-20 21:51:11 +000095 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000096}
97
98const char *Triple::getOSTypeName(OSType Kind) {
99 switch (Kind) {
100 case UnknownOS: return "unknown";
101
Duncan Sands852cd112009-06-19 14:40:01 +0000102 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000103 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000104 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000105 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000106 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000107 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000108 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000109 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000110 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000111 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000112 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000113 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000114 case OpenBSD: return "openbsd";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000115 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000116 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000117 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000118 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000119 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000120 case NativeClient: return "nacl";
Hal Finkela47406c2012-04-02 18:31:33 +0000121 case CNK: return "cnk";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000122 }
123
David Blaikie4d6ccb52012-01-20 21:51:11 +0000124 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000125}
126
Duncan Sands5754a452010-09-16 08:25:48 +0000127const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
128 switch (Kind) {
129 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000130 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000131 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000132 case GNUEABI: return "gnueabi";
133 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000134 case MachO: return "macho";
Chandler Carruthfd553c22012-01-10 19:46:00 +0000135 case ANDROIDEABI: return "androideabi";
Duncan Sands5754a452010-09-16 08:25:48 +0000136 }
137
David Blaikie4d6ccb52012-01-20 21:51:11 +0000138 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000139}
140
Daniel Dunbar2928c832009-11-06 10:58:06 +0000141Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000142 return StringSwitch<Triple::ArchType>(Name)
143 .Case("arm", arm)
144 .Case("cellspu", cellspu)
145 .Case("mips", mips)
146 .Case("mipsel", mipsel)
147 .Case("mips64", mips64)
148 .Case("mips64el", mips64el)
149 .Case("msp430", msp430)
150 .Case("ppc64", ppc64)
151 .Case("ppc32", ppc)
152 .Case("ppc", ppc)
153 .Case("mblaze", mblaze)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000154 .Case("r600", r600)
Chandler Carruth06accda2012-02-12 09:27:38 +0000155 .Case("hexagon", hexagon)
156 .Case("sparc", sparc)
157 .Case("sparcv9", sparcv9)
158 .Case("tce", tce)
159 .Case("thumb", thumb)
160 .Case("x86", x86)
161 .Case("x86-64", x86_64)
162 .Case("xcore", xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000163 .Case("nvptx", nvptx)
164 .Case("nvptx64", nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000165 .Case("le32", le32)
166 .Case("amdil", amdil)
167 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000168}
169
Daniel Dunbar2928c832009-11-06 10:58:06 +0000170Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000171 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
172 // archs which Darwin doesn't use.
173
174 // The matching this routine does is fairly pointless, since it is neither the
175 // complete architecture list, nor a reasonable subset. The problem is that
176 // historically the driver driver accepts this and also ties its -march=
177 // handling to the architecture name, so we need to be careful before removing
178 // support for it.
179
Daniel Dunbared687882009-09-09 23:01:25 +0000180 // This code must be kept in sync with Clang's Darwin specific argument
181 // translation.
182
Chandler Carruth06accda2012-02-12 09:27:38 +0000183 return StringSwitch<ArchType>(Str)
184 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
185 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
186 .Case("ppc64", Triple::ppc64)
187 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
188 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
189 Triple::x86)
190 .Case("x86_64", Triple::x86_64)
191 // This is derived from the driver driver.
192 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
193 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000194 .Case("r600", Triple::r600)
Justin Holewinski49683f32012-05-04 20:18:50 +0000195 .Case("nvptx", Triple::nvptx)
196 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth06accda2012-02-12 09:27:38 +0000197 .Case("amdil", Triple::amdil)
198 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000199}
200
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000201// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000202const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000203 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000204 return NULL;
205
Chandler Carruth06accda2012-02-12 09:27:38 +0000206 return StringSwitch<const char*>(getArchName())
207 .Case("i386", "i386")
208 .Case("x86_64", "x86_64")
209 .Case("powerpc", "ppc")
210 .Case("powerpc64", "ppc64")
211 .Cases("mblaze", "microblaze", "mblaze")
212 .Case("arm", "arm")
213 .Cases("armv4t", "thumbv4t", "armv4t")
214 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
215 .Cases("armv6", "thumbv6", "armv6")
216 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikov74156592012-03-09 10:09:36 +0000217 .Case("r600", "r600")
Justin Holewinski49683f32012-05-04 20:18:50 +0000218 .Case("nvptx", "nvptx")
219 .Case("nvptx64", "nvptx64")
Chandler Carruth06accda2012-02-12 09:27:38 +0000220 .Case("le32", "le32")
221 .Case("amdil", "amdil")
222 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000223}
224
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000225static Triple::ArchType parseArch(StringRef ArchName) {
226 return StringSwitch<Triple::ArchType>(ArchName)
227 .Cases("i386", "i486", "i586", "i686", Triple::x86)
228 // FIXME: Do we need to support these?
229 .Cases("i786", "i886", "i986", Triple::x86)
230 .Cases("amd64", "x86_64", Triple::x86_64)
231 .Case("powerpc", Triple::ppc)
232 .Cases("powerpc64", "ppu", Triple::ppc64)
233 .Case("mblaze", Triple::mblaze)
234 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000235 // FIXME: It would be good to replace these with explicit names for all the
236 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000237 .StartsWith("armv", Triple::arm)
238 .Case("thumb", Triple::thumb)
239 .StartsWith("thumbv", Triple::thumb)
240 .Cases("spu", "cellspu", Triple::cellspu)
241 .Case("msp430", Triple::msp430)
242 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000243 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000244 .Cases("mips64", "mips64eb", Triple::mips64)
245 .Case("mips64el", Triple::mips64el)
Anton Korobeynikov74156592012-03-09 10:09:36 +0000246 .Case("r600", Triple::r600)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000247 .Case("hexagon", Triple::hexagon)
248 .Case("sparc", Triple::sparc)
249 .Case("sparcv9", Triple::sparcv9)
250 .Case("tce", Triple::tce)
251 .Case("xcore", Triple::xcore)
Justin Holewinski49683f32012-05-04 20:18:50 +0000252 .Case("nvptx", Triple::nvptx)
253 .Case("nvptx64", Triple::nvptx64)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000254 .Case("le32", Triple::le32)
255 .Case("amdil", Triple::amdil)
256 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000257}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000258
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000259static Triple::VendorType parseVendor(StringRef VendorName) {
260 return StringSwitch<Triple::VendorType>(VendorName)
261 .Case("apple", Triple::Apple)
262 .Case("pc", Triple::PC)
263 .Case("scei", Triple::SCEI)
Hal Finkela47406c2012-04-02 18:31:33 +0000264 .Case("bgp", Triple::BGP)
265 .Case("bgq", Triple::BGQ)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000266 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000267}
268
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000269static Triple::OSType parseOS(StringRef OSName) {
270 return StringSwitch<Triple::OSType>(OSName)
271 .StartsWith("auroraux", Triple::AuroraUX)
272 .StartsWith("cygwin", Triple::Cygwin)
273 .StartsWith("darwin", Triple::Darwin)
274 .StartsWith("dragonfly", Triple::DragonFly)
275 .StartsWith("freebsd", Triple::FreeBSD)
276 .StartsWith("ios", Triple::IOS)
277 .StartsWith("kfreebsd", Triple::KFreeBSD)
278 .StartsWith("linux", Triple::Linux)
279 .StartsWith("lv2", Triple::Lv2)
280 .StartsWith("macosx", Triple::MacOSX)
281 .StartsWith("mingw32", Triple::MinGW32)
282 .StartsWith("netbsd", Triple::NetBSD)
283 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000284 .StartsWith("solaris", Triple::Solaris)
285 .StartsWith("win32", Triple::Win32)
286 .StartsWith("haiku", Triple::Haiku)
287 .StartsWith("minix", Triple::Minix)
288 .StartsWith("rtems", Triple::RTEMS)
289 .StartsWith("nacl", Triple::NativeClient)
Hal Finkela47406c2012-04-02 18:31:33 +0000290 .StartsWith("cnk", Triple::CNK)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000291 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000292}
293
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000294static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
295 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
296 .StartsWith("eabi", Triple::EABI)
297 .StartsWith("gnueabihf", Triple::GNUEABIHF)
298 .StartsWith("gnueabi", Triple::GNUEABI)
299 .StartsWith("gnu", Triple::GNU)
300 .StartsWith("macho", Triple::MachO)
301 .StartsWith("androideabi", Triple::ANDROIDEABI)
302 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000303}
304
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000305/// \brief Construct a triple from the string representation provided.
306///
Chandler Carruth0523f412012-02-21 08:31:18 +0000307/// This stores the string representation and parses the various pieces into
308/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000309Triple::Triple(const Twine &Str)
310 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000311 Arch(parseArch(getArchName())),
312 Vendor(parseVendor(getVendorName())),
313 OS(parseOS(getOSName())),
314 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000315}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000316
317/// \brief Construct a triple from string representations of the architecture,
318/// vendor, and OS.
319///
Chandler Carruth0523f412012-02-21 08:31:18 +0000320/// This joins each argument into a canonical string representation and parses
321/// them into enum members. It leaves the environment unknown and omits it from
322/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000323Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
324 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000325 Arch(parseArch(ArchStr.str())),
326 Vendor(parseVendor(VendorStr.str())),
327 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000328 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000329}
330
331/// \brief Construct a triple from string representations of the architecture,
332/// vendor, OS, and environment.
333///
Chandler Carruth0523f412012-02-21 08:31:18 +0000334/// This joins each argument into a canonical string representation and parses
335/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000336Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
337 const Twine &EnvironmentStr)
338 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
339 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000340 Arch(parseArch(ArchStr.str())),
341 Vendor(parseVendor(VendorStr.str())),
342 OS(parseOS(OSStr.str())),
343 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000344}
345
Duncan Sands335db222010-08-12 11:31:39 +0000346std::string Triple::normalize(StringRef Str) {
347 // Parse into components.
348 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000349 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000350
351 // If the first component corresponds to a known architecture, preferentially
352 // use it for the architecture. If the second component corresponds to a
353 // known vendor, preferentially use it for the vendor, etc. This avoids silly
354 // component movement when a component parses as (eg) both a valid arch and a
355 // valid os.
356 ArchType Arch = UnknownArch;
357 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000358 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000359 VendorType Vendor = UnknownVendor;
360 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000361 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000362 OSType OS = UnknownOS;
363 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000364 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000365 EnvironmentType Environment = UnknownEnvironment;
366 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000367 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000368
369 // Note which components are already in their final position. These will not
370 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000371 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000372 Found[0] = Arch != UnknownArch;
373 Found[1] = Vendor != UnknownVendor;
374 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000375 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000376
377 // If they are not there already, permute the components into their canonical
378 // positions by seeing if they parse as a valid architecture, and if so moving
379 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000380 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000381 if (Found[Pos])
382 continue; // Already in the canonical position.
383
384 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
385 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000386 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000387 continue;
388
389 // Does this component parse as valid for the target position?
390 bool Valid = false;
391 StringRef Comp = Components[Idx];
392 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000393 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000394 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000395 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000396 Valid = Arch != UnknownArch;
397 break;
398 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000399 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000400 Valid = Vendor != UnknownVendor;
401 break;
402 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000403 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000404 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000405 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000406 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000407 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000408 Valid = Environment != UnknownEnvironment;
409 break;
Duncan Sands335db222010-08-12 11:31:39 +0000410 }
411 if (!Valid)
412 continue; // Nope, try the next component.
413
414 // Move the component to the target position, pushing any non-fixed
415 // components that are in the way to the right. This tends to give
416 // good results in the common cases of a forgotten vendor component
417 // or a wrongly positioned environment.
418 if (Pos < Idx) {
419 // Insert left, pushing the existing components to the right. For
420 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
421 StringRef CurrentComponent(""); // The empty component.
422 // Replace the component we are moving with an empty component.
423 std::swap(CurrentComponent, Components[Idx]);
424 // Insert the component being moved at Pos, displacing any existing
425 // components to the right.
426 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
427 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000428 while (i < array_lengthof(Found) && Found[i])
429 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000430 // Place the component at the new position, getting the component
431 // that was at this position - it will be moved right.
432 std::swap(CurrentComponent, Components[i]);
433 }
434 } else if (Pos > Idx) {
435 // Push right by inserting empty components until the component at Idx
436 // reaches the target position Pos. For example, pc-a -> -pc-a when
437 // moving pc to the second position.
438 do {
439 // Insert one empty component at Idx.
440 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000441 for (unsigned i = Idx; i < Components.size();) {
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 // If it was placed on top of an empty component then we are done.
446 if (CurrentComponent.empty())
447 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000448 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000449 while (++i < array_lengthof(Found) && Found[i])
450 ;
Duncan Sands335db222010-08-12 11:31:39 +0000451 }
452 // The last component was pushed off the end - append it.
453 if (!CurrentComponent.empty())
454 Components.push_back(CurrentComponent);
455
456 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000457 while (++Idx < array_lengthof(Found) && Found[Idx])
458 ;
Duncan Sands335db222010-08-12 11:31:39 +0000459 } while (Idx < Pos); // Add more until the final position is reached.
460 }
461 assert(Pos < Components.size() && Components[Pos] == Comp &&
462 "Component moved wrong!");
463 Found[Pos] = true;
464 break;
465 }
466 }
467
468 // Special case logic goes here. At this point Arch, Vendor and OS have the
469 // correct values for the computed components.
470
471 // Stick the corrected components back together to form the normalized string.
472 std::string Normalized;
473 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
474 if (i) Normalized += '-';
475 Normalized += Components[i];
476 }
477 return Normalized;
478}
479
Daniel Dunbara14d2252009-07-26 03:31:47 +0000480StringRef Triple::getArchName() const {
481 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000482}
483
Daniel Dunbara14d2252009-07-26 03:31:47 +0000484StringRef Triple::getVendorName() const {
485 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
486 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000487}
488
Daniel Dunbara14d2252009-07-26 03:31:47 +0000489StringRef Triple::getOSName() const {
490 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
491 Tmp = Tmp.split('-').second; // Strip second component
492 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000493}
494
Daniel Dunbara14d2252009-07-26 03:31:47 +0000495StringRef Triple::getEnvironmentName() const {
496 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
497 Tmp = Tmp.split('-').second; // Strip second component
498 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000499}
500
Daniel Dunbara14d2252009-07-26 03:31:47 +0000501StringRef Triple::getOSAndEnvironmentName() const {
502 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
503 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000504}
505
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000506static unsigned EatNumber(StringRef &Str) {
507 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000508 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000509
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000510 do {
511 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000512 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000513
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000514 // Eat the digit.
515 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000516 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000517
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000518 return Result;
519}
520
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000521void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
522 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000523 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000524
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000525 // Assume that the OS portion of the triple starts with the canonical name.
526 StringRef OSTypeName = getOSTypeName(getOS());
527 if (OSName.startswith(OSTypeName))
528 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000529
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000530 // Any unset version defaults to 0.
531 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000532
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000533 // Parse up to three components.
534 unsigned *Components[3] = { &Major, &Minor, &Micro };
535 for (unsigned i = 0; i != 3; ++i) {
536 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
537 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000538
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000539 // Consume the leading number.
540 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000541
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000542 // Consume the separator, if present.
543 if (OSName.startswith("."))
544 OSName = OSName.substr(1);
545 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000546}
547
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000548bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
549 unsigned &Micro) const {
550 getOSVersion(Major, Minor, Micro);
551
552 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000553 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000554 case Darwin:
555 // Default to darwin8, i.e., MacOSX 10.4.
556 if (Major == 0)
557 Major = 8;
558 // Darwin version numbers are skewed from OS X versions.
559 if (Major < 4)
560 return false;
561 Micro = 0;
562 Minor = Major - 4;
563 Major = 10;
564 break;
565 case MacOSX:
566 // Default to 10.4.
567 if (Major == 0) {
568 Major = 10;
569 Minor = 4;
570 }
571 if (Major != 10)
572 return false;
573 break;
574 case IOS:
575 // Ignore the version from the triple. This is only handled because the
576 // the clang driver combines OS X and IOS support into a common Darwin
577 // toolchain that wants to know the OS X version number even when targeting
578 // IOS.
579 Major = 10;
580 Minor = 4;
581 Micro = 0;
582 break;
583 }
584 return true;
585}
586
Chad Rosierecee47e2012-05-09 17:23:48 +0000587void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
588 unsigned &Micro) const {
589 switch (getOS()) {
590 default: llvm_unreachable("unexpected OS for Darwin triple");
591 case Darwin:
592 case MacOSX:
593 // Ignore the version from the triple. This is only handled because the
594 // the clang driver combines OS X and IOS support into a common Darwin
595 // toolchain that wants to know the iOS version number even when targeting
596 // OS X.
Chad Rosier537c2f52012-05-09 18:23:00 +0000597 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000598 Minor = 0;
599 Micro = 0;
Chad Rosier26bbdee2012-05-09 17:38:47 +0000600 break;
Chad Rosierecee47e2012-05-09 17:23:48 +0000601 case IOS:
602 getOSVersion(Major, Minor, Micro);
Chad Rosier537c2f52012-05-09 18:23:00 +0000603 // Default to 3.0.
604 if (Major == 0)
605 Major = 3;
Chad Rosierecee47e2012-05-09 17:23:48 +0000606 break;
607 }
608}
609
Daniel Dunbara14d2252009-07-26 03:31:47 +0000610void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000611 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000612}
613
614void Triple::setArch(ArchType Kind) {
615 setArchName(getArchTypeName(Kind));
616}
617
618void Triple::setVendor(VendorType Kind) {
619 setVendorName(getVendorTypeName(Kind));
620}
621
622void Triple::setOS(OSType Kind) {
623 setOSName(getOSTypeName(Kind));
624}
625
Duncan Sands5754a452010-09-16 08:25:48 +0000626void Triple::setEnvironment(EnvironmentType Kind) {
627 setEnvironmentName(getEnvironmentTypeName(Kind));
628}
629
Daniel Dunbar2928c832009-11-06 10:58:06 +0000630void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000631 // Work around a miscompilation bug for Twines in gcc 4.0.3.
632 SmallString<64> Triple;
633 Triple += Str;
634 Triple += "-";
635 Triple += getVendorName();
636 Triple += "-";
637 Triple += getOSAndEnvironmentName();
638 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000639}
640
Daniel Dunbar2928c832009-11-06 10:58:06 +0000641void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000642 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
643}
644
Daniel Dunbar2928c832009-11-06 10:58:06 +0000645void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000646 if (hasEnvironment())
647 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
648 "-" + getEnvironmentName());
649 else
650 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
651}
652
Daniel Dunbar2928c832009-11-06 10:58:06 +0000653void Triple::setEnvironmentName(StringRef Str) {
654 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000655 "-" + Str);
656}
657
Daniel Dunbar2928c832009-11-06 10:58:06 +0000658void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000659 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
660}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000661
662static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
663 switch (Arch) {
664 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000665 return 0;
666
667 case llvm::Triple::msp430:
668 return 16;
669
670 case llvm::Triple::amdil:
671 case llvm::Triple::arm:
672 case llvm::Triple::cellspu:
673 case llvm::Triple::hexagon:
674 case llvm::Triple::le32:
675 case llvm::Triple::mblaze:
676 case llvm::Triple::mips:
677 case llvm::Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000678 case llvm::Triple::nvptx:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000679 case llvm::Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000680 case llvm::Triple::r600:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000681 case llvm::Triple::sparc:
682 case llvm::Triple::tce:
683 case llvm::Triple::thumb:
684 case llvm::Triple::x86:
685 case llvm::Triple::xcore:
686 return 32;
687
688 case llvm::Triple::mips64:
689 case llvm::Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000690 case llvm::Triple::nvptx64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000691 case llvm::Triple::ppc64:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000692 case llvm::Triple::sparcv9:
693 case llvm::Triple::x86_64:
694 return 64;
695 }
696 llvm_unreachable("Invalid architecture value");
697}
698
699bool Triple::isArch64Bit() const {
700 return getArchPointerBitWidth(getArch()) == 64;
701}
702
703bool Triple::isArch32Bit() const {
704 return getArchPointerBitWidth(getArch()) == 32;
705}
706
707bool Triple::isArch16Bit() const {
708 return getArchPointerBitWidth(getArch()) == 16;
709}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000710
711Triple Triple::get32BitArchVariant() const {
712 Triple T(*this);
713 switch (getArch()) {
714 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000715 case Triple::msp430:
716 T.setArch(UnknownArch);
717 break;
718
719 case Triple::amdil:
720 case Triple::arm:
721 case Triple::cellspu:
722 case Triple::hexagon:
723 case Triple::le32:
724 case Triple::mblaze:
725 case Triple::mips:
726 case Triple::mipsel:
Justin Holewinski49683f32012-05-04 20:18:50 +0000727 case Triple::nvptx:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000728 case Triple::ppc:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000729 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000730 case Triple::sparc:
731 case Triple::tce:
732 case Triple::thumb:
733 case Triple::x86:
734 case Triple::xcore:
735 // Already 32-bit.
736 break;
737
738 case Triple::mips64: T.setArch(Triple::mips); break;
739 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000740 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000741 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000742 case Triple::sparcv9: T.setArch(Triple::sparc); break;
743 case Triple::x86_64: T.setArch(Triple::x86); break;
744 }
745 return T;
746}
747
748Triple Triple::get64BitArchVariant() const {
749 Triple T(*this);
750 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000751 case Triple::UnknownArch:
752 case Triple::amdil:
753 case Triple::arm:
754 case Triple::cellspu:
755 case Triple::hexagon:
756 case Triple::le32:
757 case Triple::mblaze:
758 case Triple::msp430:
Anton Korobeynikov74156592012-03-09 10:09:36 +0000759 case Triple::r600:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000760 case Triple::tce:
761 case Triple::thumb:
762 case Triple::xcore:
763 T.setArch(UnknownArch);
764 break;
765
766 case Triple::mips64:
767 case Triple::mips64el:
Justin Holewinski49683f32012-05-04 20:18:50 +0000768 case Triple::nvptx64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000769 case Triple::ppc64:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000770 case Triple::sparcv9:
771 case Triple::x86_64:
772 // Already 64-bit.
773 break;
774
775 case Triple::mips: T.setArch(Triple::mips64); break;
776 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinski49683f32012-05-04 20:18:50 +0000777 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000778 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000779 case Triple::sparc: T.setArch(Triple::sparcv9); break;
780 case Triple::x86: T.setArch(Triple::x86_64); break;
781 }
782 return T;
783}