blob: cca549dad56787c0496c7d29b27a0ee9bfc180fd [file] [log] [blame]
Daniel Dunbar4abd5662009-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 Yasskine2595b52009-10-06 21:45:26 +000011#include "llvm/ADT/SmallString.h"
Chandler Carruthff6f3562012-02-12 09:27:38 +000012#include "llvm/ADT/StringSwitch.h"
Duncan Sands84a3bdd2010-09-16 08:25:48 +000013#include "llvm/ADT/STLExtras.h"
David Blaikie46a9f012012-01-20 21:51:11 +000014#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov12062ba2009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar4abd5662009-04-01 21:53:23 +000016using namespace llvm;
17
Daniel Dunbar4abd5662009-04-01 21:53:23 +000018const char *Triple::getArchTypeName(ArchType Kind) {
19 switch (Kind) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +000020 case UnknownArch: return "unknown";
Jim Grosbachf638b262010-12-17 02:10:59 +000021
Daniel Dunbar172649b2009-07-26 04:23:03 +000022 case arm: return "arm";
23 case cellspu: return "cellspu";
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024 case hexagon: return "hexagon";
Daniel Dunbar172649b2009-07-26 04:23:03 +000025 case mips: return "mips";
26 case mipsel: return "mipsel";
Akira Hatanaka6c3ad652011-09-20 18:09:37 +000027 case mips64: return "mips64";
28 case mips64el:return "mips64el";
Daniel Dunbar172649b2009-07-26 04:23:03 +000029 case msp430: return "msp430";
Daniel Dunbarbd481a12009-07-26 04:52:45 +000030 case ppc64: return "powerpc64";
31 case ppc: return "powerpc";
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000032 case r600: return "r600";
Daniel Dunbar172649b2009-07-26 04:23:03 +000033 case sparc: return "sparc";
Chris Lattner8228b112010-02-04 06:34:01 +000034 case sparcv9: return "sparcv9";
Eli Friedman28c7bbf2009-08-19 20:46:03 +000035 case tce: return "tce";
Daniel Dunbar172649b2009-07-26 04:23:03 +000036 case thumb: return "thumb";
37 case x86: return "i386";
38 case x86_64: return "x86_64";
Daniel Dunbarbd481a12009-07-26 04:52:45 +000039 case xcore: return "xcore";
Wesley Pecke4801e42010-02-23 19:15:24 +000040 case mblaze: return "mblaze";
Justin Holewinskiae556d32012-05-04 20:18:50 +000041 case nvptx: return "nvptx";
42 case nvptx64: return "nvptx64";
Ivan Krasin771ef8c2011-08-23 16:59:00 +000043 case le32: return "le32";
Tobias Grosser516dbb22011-08-29 15:44:55 +000044 case amdil: return "amdil";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000045 }
46
David Blaikie46a9f012012-01-20 21:51:11 +000047 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +000048}
49
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000050const char *Triple::getArchTypePrefix(ArchType Kind) {
51 switch (Kind) {
52 default:
53 return 0;
54
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000055 case arm:
56 case thumb: return "arm";
57
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000058 case cellspu: return "spu";
59
60 case ppc64:
61 case ppc: return "ppc";
62
Wesley Pecke4801e42010-02-23 19:15:24 +000063 case mblaze: return "mblaze";
64
Benjamin Kramerae3c3002012-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 Linthicum1213a7a2011-12-12 21:14:40 +000071
Anton Korobeynikovf32638d2012-03-09 10:09:36 +000072 case r600: return "r600";
73
Chris Lattner8228b112010-02-04 06:34:01 +000074 case sparcv9:
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000075 case sparc: return "sparc";
76
77 case x86:
78 case x86_64: return "x86";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000079
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000080 case xcore: return "xcore";
Nick Lewycky4c82c6c2010-09-07 18:14:24 +000081
Justin Holewinskiae556d32012-05-04 20:18:50 +000082 case nvptx: return "nvptx";
83 case nvptx64: return "nvptx";
Ivan Krasin771ef8c2011-08-23 16:59:00 +000084 case le32: return "le32";
Tobias Grosser516dbb22011-08-29 15:44:55 +000085 case amdil: return "amdil";
Daniel Dunbarb8dc4ea2009-08-24 09:53:06 +000086 }
87}
88
Daniel Dunbar4abd5662009-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 Lattner07921952009-08-14 18:48:13 +000094 case PC: return "pc";
John Thompsond0332e42011-03-15 21:51:56 +000095 case SCEI: return "scei";
Hal Finkelf208af02012-04-02 18:31:33 +000096 case BGP: return "bgp";
97 case BGQ: return "bgq";
Daniel Dunbar4abd5662009-04-01 21:53:23 +000098 }
99
David Blaikie46a9f012012-01-20 21:51:11 +0000100 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000101}
102
103const char *Triple::getOSTypeName(OSType Kind) {
104 switch (Kind) {
105 case UnknownOS: return "unknown";
106
Duncan Sands0de39b42009-06-19 14:40:01 +0000107 case AuroraUX: return "auroraux";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000108 case Cygwin: return "cygwin";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000109 case Darwin: return "darwin";
Daniel Dunbare3384c42009-05-22 02:24:11 +0000110 case DragonFly: return "dragonfly";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000111 case FreeBSD: return "freebsd";
Daniel Dunbard74bac72011-04-19 20:19:27 +0000112 case IOS: return "ios";
Duncan Sandsfe44f672011-07-26 15:30:04 +0000113 case KFreeBSD: return "kfreebsd";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000114 case Linux: return "linux";
Edward O'Callaghanba993b82009-11-19 11:59:00 +0000115 case Lv2: return "lv2";
Daniel Dunbar0854f342011-04-19 23:34:12 +0000116 case MacOSX: return "macosx";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000117 case MinGW32: return "mingw32";
Chris Lattner01218d52009-07-13 20:22:23 +0000118 case NetBSD: return "netbsd";
Duncan Sands14814d42009-06-29 13:36:13 +0000119 case OpenBSD: return "openbsd";
Daniel Dunbar781f94d2009-08-18 04:43:27 +0000120 case Solaris: return "solaris";
Daniel Dunbar172649b2009-07-26 04:23:03 +0000121 case Win32: return "win32";
Chris Lattner27f20492009-10-16 02:06:30 +0000122 case Haiku: return "haiku";
Chris Lattnerca97c922010-07-07 15:52:27 +0000123 case Minix: return "minix";
Douglas Gregorde3c9262011-07-01 22:41:06 +0000124 case RTEMS: return "rtems";
Ivan Krasin44306e22011-08-18 22:54:21 +0000125 case NativeClient: return "nacl";
Hal Finkelf208af02012-04-02 18:31:33 +0000126 case CNK: return "cnk";
Eric Christopher22738d02012-08-06 20:52:18 +0000127 case Bitrig: return "bitrig";
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000128 }
129
David Blaikie46a9f012012-01-20 21:51:11 +0000130 llvm_unreachable("Invalid OSType");
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000131}
132
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000133const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
134 switch (Kind) {
135 case UnknownEnvironment: return "unknown";
Renato Golin83758d5c2011-01-21 18:25:47 +0000136 case GNU: return "gnu";
Rafael Espindolaf5e78fa2012-01-18 23:35:29 +0000137 case GNUEABIHF: return "gnueabihf";
Renato Golin83758d5c2011-01-21 18:25:47 +0000138 case GNUEABI: return "gnueabi";
139 case EABI: return "eabi";
Evan Chengd22a4a12011-02-01 01:14:13 +0000140 case MachO: return "macho";
Chandler Carruth9a7510a2012-01-10 19:46:00 +0000141 case ANDROIDEABI: return "androideabi";
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000142 }
143
David Blaikie46a9f012012-01-20 21:51:11 +0000144 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000145}
146
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000147Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruthff6f3562012-02-12 09:27:38 +0000148 return StringSwitch<Triple::ArchType>(Name)
149 .Case("arm", arm)
150 .Case("cellspu", cellspu)
151 .Case("mips", mips)
152 .Case("mipsel", mipsel)
153 .Case("mips64", mips64)
154 .Case("mips64el", mips64el)
155 .Case("msp430", msp430)
156 .Case("ppc64", ppc64)
157 .Case("ppc32", ppc)
158 .Case("ppc", ppc)
159 .Case("mblaze", mblaze)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000160 .Case("r600", r600)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000161 .Case("hexagon", hexagon)
162 .Case("sparc", sparc)
163 .Case("sparcv9", sparcv9)
164 .Case("tce", tce)
165 .Case("thumb", thumb)
166 .Case("x86", x86)
167 .Case("x86-64", x86_64)
168 .Case("xcore", xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000169 .Case("nvptx", nvptx)
170 .Case("nvptx64", nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000171 .Case("le32", le32)
172 .Case("amdil", amdil)
173 .Default(UnknownArch);
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000174}
175
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000176Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbara8597a32009-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 Dunbaraec990e2009-09-09 23:01:25 +0000186 // This code must be kept in sync with Clang's Darwin specific argument
187 // translation.
188
Chandler Carruthff6f3562012-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 Korobeynikovf32638d2012-03-09 10:09:36 +0000200 .Case("r600", Triple::r600)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000201 .Case("nvptx", Triple::nvptx)
202 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthff6f3562012-02-12 09:27:38 +0000203 .Case("amdil", Triple::amdil)
204 .Default(Triple::UnknownArch);
Daniel Dunbara8597a32009-09-08 23:32:51 +0000205}
206
Duncan Sands8712da32010-03-24 09:05:14 +0000207// Returns architecture name that is understood by the target assembler.
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000208const char *Triple::getArchNameForAssembler() {
Daniel Dunbar163a0962011-04-19 21:12:05 +0000209 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000210 return NULL;
211
Chandler Carruthff6f3562012-02-12 09:27:38 +0000212 return StringSwitch<const char*>(getArchName())
213 .Case("i386", "i386")
214 .Case("x86_64", "x86_64")
215 .Case("powerpc", "ppc")
216 .Case("powerpc64", "ppc64")
217 .Cases("mblaze", "microblaze", "mblaze")
218 .Case("arm", "arm")
219 .Cases("armv4t", "thumbv4t", "armv4t")
220 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
221 .Cases("armv6", "thumbv6", "armv6")
222 .Cases("armv7", "thumbv7", "armv7")
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000223 .Case("r600", "r600")
Justin Holewinskiae556d32012-05-04 20:18:50 +0000224 .Case("nvptx", "nvptx")
225 .Case("nvptx64", "nvptx64")
Chandler Carruthff6f3562012-02-12 09:27:38 +0000226 .Case("le32", "le32")
227 .Case("amdil", "amdil")
228 .Default(NULL);
Viktor Kutuzovdafdd882009-11-17 18:48:27 +0000229}
230
Chandler Carruthaec97082012-02-21 08:53:32 +0000231static Triple::ArchType parseArch(StringRef ArchName) {
232 return StringSwitch<Triple::ArchType>(ArchName)
233 .Cases("i386", "i486", "i586", "i686", Triple::x86)
234 // FIXME: Do we need to support these?
235 .Cases("i786", "i886", "i986", Triple::x86)
236 .Cases("amd64", "x86_64", Triple::x86_64)
237 .Case("powerpc", Triple::ppc)
238 .Cases("powerpc64", "ppu", Triple::ppc64)
239 .Case("mblaze", Triple::mblaze)
240 .Cases("arm", "xscale", Triple::arm)
Chandler Carruthb54950b2012-02-18 04:34:17 +0000241 // FIXME: It would be good to replace these with explicit names for all the
242 // various suffixes supported.
Chandler Carruthaec97082012-02-21 08:53:32 +0000243 .StartsWith("armv", Triple::arm)
244 .Case("thumb", Triple::thumb)
245 .StartsWith("thumbv", Triple::thumb)
246 .Cases("spu", "cellspu", Triple::cellspu)
247 .Case("msp430", Triple::msp430)
248 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruth0c7a7cc2012-02-22 11:32:54 +0000249 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruthaec97082012-02-21 08:53:32 +0000250 .Cases("mips64", "mips64eb", Triple::mips64)
251 .Case("mips64el", Triple::mips64el)
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000252 .Case("r600", Triple::r600)
Chandler Carruthaec97082012-02-21 08:53:32 +0000253 .Case("hexagon", Triple::hexagon)
254 .Case("sparc", Triple::sparc)
255 .Case("sparcv9", Triple::sparcv9)
256 .Case("tce", Triple::tce)
257 .Case("xcore", Triple::xcore)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000258 .Case("nvptx", Triple::nvptx)
259 .Case("nvptx64", Triple::nvptx64)
Chandler Carruthaec97082012-02-21 08:53:32 +0000260 .Case("le32", Triple::le32)
261 .Case("amdil", Triple::amdil)
262 .Default(Triple::UnknownArch);
Duncan Sands501dff72010-08-12 11:31:39 +0000263}
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000264
Chandler Carruthaec97082012-02-21 08:53:32 +0000265static Triple::VendorType parseVendor(StringRef VendorName) {
266 return StringSwitch<Triple::VendorType>(VendorName)
267 .Case("apple", Triple::Apple)
268 .Case("pc", Triple::PC)
269 .Case("scei", Triple::SCEI)
Hal Finkelf208af02012-04-02 18:31:33 +0000270 .Case("bgp", Triple::BGP)
271 .Case("bgq", Triple::BGQ)
Chandler Carruthaec97082012-02-21 08:53:32 +0000272 .Default(Triple::UnknownVendor);
Duncan Sands501dff72010-08-12 11:31:39 +0000273}
274
Chandler Carruthaec97082012-02-21 08:53:32 +0000275static Triple::OSType parseOS(StringRef OSName) {
276 return StringSwitch<Triple::OSType>(OSName)
277 .StartsWith("auroraux", Triple::AuroraUX)
278 .StartsWith("cygwin", Triple::Cygwin)
279 .StartsWith("darwin", Triple::Darwin)
280 .StartsWith("dragonfly", Triple::DragonFly)
281 .StartsWith("freebsd", Triple::FreeBSD)
282 .StartsWith("ios", Triple::IOS)
283 .StartsWith("kfreebsd", Triple::KFreeBSD)
284 .StartsWith("linux", Triple::Linux)
285 .StartsWith("lv2", Triple::Lv2)
286 .StartsWith("macosx", Triple::MacOSX)
287 .StartsWith("mingw32", Triple::MinGW32)
288 .StartsWith("netbsd", Triple::NetBSD)
289 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruthaec97082012-02-21 08:53:32 +0000290 .StartsWith("solaris", Triple::Solaris)
291 .StartsWith("win32", Triple::Win32)
292 .StartsWith("haiku", Triple::Haiku)
293 .StartsWith("minix", Triple::Minix)
294 .StartsWith("rtems", Triple::RTEMS)
295 .StartsWith("nacl", Triple::NativeClient)
Hal Finkelf208af02012-04-02 18:31:33 +0000296 .StartsWith("cnk", Triple::CNK)
Eric Christopher22738d02012-08-06 20:52:18 +0000297 .StartsWith("bitrig", Triple::Bitrig)
Chandler Carruthaec97082012-02-21 08:53:32 +0000298 .Default(Triple::UnknownOS);
Duncan Sands501dff72010-08-12 11:31:39 +0000299}
300
Chandler Carruthaec97082012-02-21 08:53:32 +0000301static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
302 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
303 .StartsWith("eabi", Triple::EABI)
304 .StartsWith("gnueabihf", Triple::GNUEABIHF)
305 .StartsWith("gnueabi", Triple::GNUEABI)
306 .StartsWith("gnu", Triple::GNU)
307 .StartsWith("macho", Triple::MachO)
308 .StartsWith("androideabi", Triple::ANDROIDEABI)
309 .Default(Triple::UnknownEnvironment);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000310}
311
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000312/// \brief Construct a triple from the string representation provided.
313///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000314/// This stores the string representation and parses the various pieces into
315/// enum members.
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000316Triple::Triple(const Twine &Str)
317 : Data(Str.str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000318 Arch(parseArch(getArchName())),
319 Vendor(parseVendor(getVendorName())),
320 OS(parseOS(getOSName())),
321 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000322}
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000323
324/// \brief Construct a triple from string representations of the architecture,
325/// vendor, and OS.
326///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000327/// This joins each argument into a canonical string representation and parses
328/// them into enum members. It leaves the environment unknown and omits it from
329/// the string representation.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000330Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
331 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000332 Arch(parseArch(ArchStr.str())),
333 Vendor(parseVendor(VendorStr.str())),
334 OS(parseOS(OSStr.str())),
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000335 Environment() {
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000336}
337
338/// \brief Construct a triple from string representations of the architecture,
339/// vendor, OS, and environment.
340///
Chandler Carruth1f3325a2012-02-21 08:31:18 +0000341/// This joins each argument into a canonical string representation and parses
342/// them into enum members.
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000343Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
344 const Twine &EnvironmentStr)
345 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
346 EnvironmentStr).str()),
Chandler Carruthaec97082012-02-21 08:53:32 +0000347 Arch(parseArch(ArchStr.str())),
348 Vendor(parseVendor(VendorStr.str())),
349 OS(parseOS(OSStr.str())),
350 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruth0d887dd2012-02-20 00:02:47 +0000351}
352
Duncan Sands501dff72010-08-12 11:31:39 +0000353std::string Triple::normalize(StringRef Str) {
354 // Parse into components.
355 SmallVector<StringRef, 4> Components;
Chandler Carruth6ea6de72012-02-21 09:12:48 +0000356 Str.split(Components, "-");
Duncan Sands501dff72010-08-12 11:31:39 +0000357
358 // If the first component corresponds to a known architecture, preferentially
359 // use it for the architecture. If the second component corresponds to a
360 // known vendor, preferentially use it for the vendor, etc. This avoids silly
361 // component movement when a component parses as (eg) both a valid arch and a
362 // valid os.
363 ArchType Arch = UnknownArch;
364 if (Components.size() > 0)
Chandler Carruthaec97082012-02-21 08:53:32 +0000365 Arch = parseArch(Components[0]);
Duncan Sands501dff72010-08-12 11:31:39 +0000366 VendorType Vendor = UnknownVendor;
367 if (Components.size() > 1)
Chandler Carruthaec97082012-02-21 08:53:32 +0000368 Vendor = parseVendor(Components[1]);
Duncan Sands501dff72010-08-12 11:31:39 +0000369 OSType OS = UnknownOS;
370 if (Components.size() > 2)
Chandler Carruthaec97082012-02-21 08:53:32 +0000371 OS = parseOS(Components[2]);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000372 EnvironmentType Environment = UnknownEnvironment;
373 if (Components.size() > 3)
Chandler Carruthaec97082012-02-21 08:53:32 +0000374 Environment = parseEnvironment(Components[3]);
Duncan Sands501dff72010-08-12 11:31:39 +0000375
376 // Note which components are already in their final position. These will not
377 // be moved.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000378 bool Found[4];
Duncan Sands501dff72010-08-12 11:31:39 +0000379 Found[0] = Arch != UnknownArch;
380 Found[1] = Vendor != UnknownVendor;
381 Found[2] = OS != UnknownOS;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000382 Found[3] = Environment != UnknownEnvironment;
Duncan Sands501dff72010-08-12 11:31:39 +0000383
384 // If they are not there already, permute the components into their canonical
385 // positions by seeing if they parse as a valid architecture, and if so moving
386 // the component to the architecture position etc.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000387 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands501dff72010-08-12 11:31:39 +0000388 if (Found[Pos])
389 continue; // Already in the canonical position.
390
391 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
392 // Do not reparse any components that already matched.
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000393 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands501dff72010-08-12 11:31:39 +0000394 continue;
395
396 // Does this component parse as valid for the target position?
397 bool Valid = false;
398 StringRef Comp = Components[Idx];
399 switch (Pos) {
Craig Toppera2886c22012-02-07 05:05:23 +0000400 default: llvm_unreachable("unexpected component type!");
Duncan Sands501dff72010-08-12 11:31:39 +0000401 case 0:
Chandler Carruthaec97082012-02-21 08:53:32 +0000402 Arch = parseArch(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000403 Valid = Arch != UnknownArch;
404 break;
405 case 1:
Chandler Carruthaec97082012-02-21 08:53:32 +0000406 Vendor = parseVendor(Comp);
Duncan Sands501dff72010-08-12 11:31:39 +0000407 Valid = Vendor != UnknownVendor;
408 break;
409 case 2:
Chandler Carruthaec97082012-02-21 08:53:32 +0000410 OS = parseOS(Comp);
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000411 Valid = OS != UnknownOS;
Duncan Sands501dff72010-08-12 11:31:39 +0000412 break;
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000413 case 3:
Chandler Carruthaec97082012-02-21 08:53:32 +0000414 Environment = parseEnvironment(Comp);
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000415 Valid = Environment != UnknownEnvironment;
416 break;
Duncan Sands501dff72010-08-12 11:31:39 +0000417 }
418 if (!Valid)
419 continue; // Nope, try the next component.
420
421 // Move the component to the target position, pushing any non-fixed
422 // components that are in the way to the right. This tends to give
423 // good results in the common cases of a forgotten vendor component
424 // or a wrongly positioned environment.
425 if (Pos < Idx) {
426 // Insert left, pushing the existing components to the right. For
427 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
428 StringRef CurrentComponent(""); // The empty component.
429 // Replace the component we are moving with an empty component.
430 std::swap(CurrentComponent, Components[Idx]);
431 // Insert the component being moved at Pos, displacing any existing
432 // components to the right.
433 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
434 // Skip over any fixed components.
Chandler Carruth362087b2012-02-21 09:29:14 +0000435 while (i < array_lengthof(Found) && Found[i])
436 ++i;
Duncan Sands501dff72010-08-12 11:31:39 +0000437 // Place the component at the new position, getting the component
438 // that was at this position - it will be moved right.
439 std::swap(CurrentComponent, Components[i]);
440 }
441 } else if (Pos > Idx) {
442 // Push right by inserting empty components until the component at Idx
443 // reaches the target position Pos. For example, pc-a -> -pc-a when
444 // moving pc to the second position.
445 do {
446 // Insert one empty component at Idx.
447 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000448 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands501dff72010-08-12 11:31:39 +0000449 // Place the component at the new position, getting the component
450 // that was at this position - it will be moved right.
451 std::swap(CurrentComponent, Components[i]);
452 // If it was placed on top of an empty component then we are done.
453 if (CurrentComponent.empty())
454 break;
Duncan Sandsfdfdbd02011-02-02 10:08:38 +0000455 // Advance to the next component, skipping any fixed components.
Anders Carlsson630125a2011-02-05 18:19:35 +0000456 while (++i < array_lengthof(Found) && Found[i])
457 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000458 }
459 // The last component was pushed off the end - append it.
460 if (!CurrentComponent.empty())
461 Components.push_back(CurrentComponent);
462
463 // Advance Idx to the component's new position.
Chandler Carruth362087b2012-02-21 09:29:14 +0000464 while (++Idx < array_lengthof(Found) && Found[Idx])
465 ;
Duncan Sands501dff72010-08-12 11:31:39 +0000466 } while (Idx < Pos); // Add more until the final position is reached.
467 }
468 assert(Pos < Components.size() && Components[Pos] == Comp &&
469 "Component moved wrong!");
470 Found[Pos] = true;
471 break;
472 }
473 }
474
475 // Special case logic goes here. At this point Arch, Vendor and OS have the
476 // correct values for the computed components.
477
478 // Stick the corrected components back together to form the normalized string.
479 std::string Normalized;
480 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
481 if (i) Normalized += '-';
482 Normalized += Components[i];
483 }
484 return Normalized;
485}
486
Daniel Dunbar19e70762009-07-26 03:31:47 +0000487StringRef Triple::getArchName() const {
488 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000489}
490
Daniel Dunbar19e70762009-07-26 03:31:47 +0000491StringRef Triple::getVendorName() const {
492 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
493 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000494}
495
Daniel Dunbar19e70762009-07-26 03:31:47 +0000496StringRef Triple::getOSName() const {
497 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
498 Tmp = Tmp.split('-').second; // Strip second component
499 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000500}
501
Daniel Dunbar19e70762009-07-26 03:31:47 +0000502StringRef Triple::getEnvironmentName() const {
503 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
504 Tmp = Tmp.split('-').second; // Strip second component
505 return Tmp.split('-').second; // Strip third component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000506}
507
Daniel Dunbar19e70762009-07-26 03:31:47 +0000508StringRef Triple::getOSAndEnvironmentName() const {
509 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
510 return Tmp.split('-').second; // Strip second component
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000511}
512
Chris Lattner553c9f32009-08-12 06:19:40 +0000513static unsigned EatNumber(StringRef &Str) {
514 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000515 unsigned Result = 0;
Jim Grosbachf638b262010-12-17 02:10:59 +0000516
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000517 do {
518 // Consume the leading digit.
Chris Lattner553c9f32009-08-12 06:19:40 +0000519 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000520
Chris Lattner553c9f32009-08-12 06:19:40 +0000521 // Eat the digit.
522 Str = Str.substr(1);
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000523 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbachf638b262010-12-17 02:10:59 +0000524
Chris Lattner553c9f32009-08-12 06:19:40 +0000525 return Result;
526}
527
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000528void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
529 unsigned &Micro) const {
Chris Lattner553c9f32009-08-12 06:19:40 +0000530 StringRef OSName = getOSName();
Jim Grosbachf638b262010-12-17 02:10:59 +0000531
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000532 // Assume that the OS portion of the triple starts with the canonical name.
533 StringRef OSTypeName = getOSTypeName(getOS());
534 if (OSName.startswith(OSTypeName))
535 OSName = OSName.substr(OSTypeName.size());
Jim Grosbachf638b262010-12-17 02:10:59 +0000536
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000537 // Any unset version defaults to 0.
538 Major = Minor = Micro = 0;
Chris Lattner553c9f32009-08-12 06:19:40 +0000539
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000540 // Parse up to three components.
541 unsigned *Components[3] = { &Major, &Minor, &Micro };
542 for (unsigned i = 0; i != 3; ++i) {
543 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
544 break;
Chris Lattner553c9f32009-08-12 06:19:40 +0000545
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000546 // Consume the leading number.
547 *Components[i] = EatNumber(OSName);
Jim Grosbachf638b262010-12-17 02:10:59 +0000548
Daniel Dunbar99f904c2011-04-19 20:24:34 +0000549 // Consume the separator, if present.
550 if (OSName.startswith("."))
551 OSName = OSName.substr(1);
552 }
Chris Lattner553c9f32009-08-12 06:19:40 +0000553}
554
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000555bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
556 unsigned &Micro) const {
557 getOSVersion(Major, Minor, Micro);
558
559 switch (getOS()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000560 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonaa30aff2012-01-31 22:32:29 +0000561 case Darwin:
562 // Default to darwin8, i.e., MacOSX 10.4.
563 if (Major == 0)
564 Major = 8;
565 // Darwin version numbers are skewed from OS X versions.
566 if (Major < 4)
567 return false;
568 Micro = 0;
569 Minor = Major - 4;
570 Major = 10;
571 break;
572 case MacOSX:
573 // Default to 10.4.
574 if (Major == 0) {
575 Major = 10;
576 Minor = 4;
577 }
578 if (Major != 10)
579 return false;
580 break;
581 case IOS:
582 // Ignore the version from the triple. This is only handled because the
583 // the clang driver combines OS X and IOS support into a common Darwin
584 // toolchain that wants to know the OS X version number even when targeting
585 // IOS.
586 Major = 10;
587 Minor = 4;
588 Micro = 0;
589 break;
590 }
591 return true;
592}
593
Chad Rosierd84eaac2012-05-09 17:23:48 +0000594void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
595 unsigned &Micro) const {
596 switch (getOS()) {
597 default: llvm_unreachable("unexpected OS for Darwin triple");
598 case Darwin:
599 case MacOSX:
600 // Ignore the version from the triple. This is only handled because the
601 // the clang driver combines OS X and IOS support into a common Darwin
602 // toolchain that wants to know the iOS version number even when targeting
603 // OS X.
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000604 Major = 3;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000605 Minor = 0;
606 Micro = 0;
Chad Rosier2778cbc2012-05-09 17:38:47 +0000607 break;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000608 case IOS:
609 getOSVersion(Major, Minor, Micro);
Chad Rosier9d7b1ce2012-05-09 18:23:00 +0000610 // Default to 3.0.
611 if (Major == 0)
612 Major = 3;
Chad Rosierd84eaac2012-05-09 17:23:48 +0000613 break;
614 }
615}
616
Daniel Dunbar19e70762009-07-26 03:31:47 +0000617void Triple::setTriple(const Twine &Str) {
Chandler Carruth2d27b0f2012-02-21 03:39:36 +0000618 *this = Triple(Str);
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000619}
620
621void Triple::setArch(ArchType Kind) {
622 setArchName(getArchTypeName(Kind));
623}
624
625void Triple::setVendor(VendorType Kind) {
626 setVendorName(getVendorTypeName(Kind));
627}
628
629void Triple::setOS(OSType Kind) {
630 setOSName(getOSTypeName(Kind));
631}
632
Duncan Sands84a3bdd2010-09-16 08:25:48 +0000633void Triple::setEnvironment(EnvironmentType Kind) {
634 setEnvironmentName(getEnvironmentTypeName(Kind));
635}
636
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000637void Triple::setArchName(StringRef Str) {
Jeffrey Yasskine2595b52009-10-06 21:45:26 +0000638 // Work around a miscompilation bug for Twines in gcc 4.0.3.
639 SmallString<64> Triple;
640 Triple += Str;
641 Triple += "-";
642 Triple += getVendorName();
643 Triple += "-";
644 Triple += getOSAndEnvironmentName();
645 setTriple(Triple.str());
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000646}
647
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000648void Triple::setVendorName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000649 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
650}
651
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000652void Triple::setOSName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000653 if (hasEnvironment())
654 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
655 "-" + getEnvironmentName());
656 else
657 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
658}
659
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000660void Triple::setEnvironmentName(StringRef Str) {
661 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000662 "-" + Str);
663}
664
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000665void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar4abd5662009-04-01 21:53:23 +0000666 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
667}
Chandler Carruthb90c1022012-01-31 04:52:32 +0000668
669static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
670 switch (Arch) {
671 case llvm::Triple::UnknownArch:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000672 return 0;
673
674 case llvm::Triple::msp430:
675 return 16;
676
677 case llvm::Triple::amdil:
678 case llvm::Triple::arm:
679 case llvm::Triple::cellspu:
680 case llvm::Triple::hexagon:
681 case llvm::Triple::le32:
682 case llvm::Triple::mblaze:
683 case llvm::Triple::mips:
684 case llvm::Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000685 case llvm::Triple::nvptx:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000686 case llvm::Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000687 case llvm::Triple::r600:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000688 case llvm::Triple::sparc:
689 case llvm::Triple::tce:
690 case llvm::Triple::thumb:
691 case llvm::Triple::x86:
692 case llvm::Triple::xcore:
693 return 32;
694
695 case llvm::Triple::mips64:
696 case llvm::Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000697 case llvm::Triple::nvptx64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000698 case llvm::Triple::ppc64:
Chandler Carruthb90c1022012-01-31 04:52:32 +0000699 case llvm::Triple::sparcv9:
700 case llvm::Triple::x86_64:
701 return 64;
702 }
703 llvm_unreachable("Invalid architecture value");
704}
705
706bool Triple::isArch64Bit() const {
707 return getArchPointerBitWidth(getArch()) == 64;
708}
709
710bool Triple::isArch32Bit() const {
711 return getArchPointerBitWidth(getArch()) == 32;
712}
713
714bool Triple::isArch16Bit() const {
715 return getArchPointerBitWidth(getArch()) == 16;
716}
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000717
718Triple Triple::get32BitArchVariant() const {
719 Triple T(*this);
720 switch (getArch()) {
721 case Triple::UnknownArch:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000722 case Triple::msp430:
723 T.setArch(UnknownArch);
724 break;
725
726 case Triple::amdil:
727 case Triple::arm:
728 case Triple::cellspu:
729 case Triple::hexagon:
730 case Triple::le32:
731 case Triple::mblaze:
732 case Triple::mips:
733 case Triple::mipsel:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000734 case Triple::nvptx:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000735 case Triple::ppc:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000736 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000737 case Triple::sparc:
738 case Triple::tce:
739 case Triple::thumb:
740 case Triple::x86:
741 case Triple::xcore:
742 // Already 32-bit.
743 break;
744
745 case Triple::mips64: T.setArch(Triple::mips); break;
746 case Triple::mips64el: T.setArch(Triple::mipsel); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000747 case Triple::nvptx64: T.setArch(Triple::nvptx); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000748 case Triple::ppc64: T.setArch(Triple::ppc); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000749 case Triple::sparcv9: T.setArch(Triple::sparc); break;
750 case Triple::x86_64: T.setArch(Triple::x86); break;
751 }
752 return T;
753}
754
755Triple Triple::get64BitArchVariant() const {
756 Triple T(*this);
757 switch (getArch()) {
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000758 case Triple::UnknownArch:
759 case Triple::amdil:
760 case Triple::arm:
761 case Triple::cellspu:
762 case Triple::hexagon:
763 case Triple::le32:
764 case Triple::mblaze:
765 case Triple::msp430:
Anton Korobeynikovf32638d2012-03-09 10:09:36 +0000766 case Triple::r600:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000767 case Triple::tce:
768 case Triple::thumb:
769 case Triple::xcore:
770 T.setArch(UnknownArch);
771 break;
772
773 case Triple::mips64:
774 case Triple::mips64el:
Justin Holewinskiae556d32012-05-04 20:18:50 +0000775 case Triple::nvptx64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000776 case Triple::ppc64:
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000777 case Triple::sparcv9:
778 case Triple::x86_64:
779 // Already 64-bit.
780 break;
781
782 case Triple::mips: T.setArch(Triple::mips64); break;
783 case Triple::mipsel: T.setArch(Triple::mips64el); break;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000784 case Triple::nvptx: T.setArch(Triple::nvptx64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000785 case Triple::ppc: T.setArch(Triple::ppc64); break;
Chandler Carruth07cfb4b2012-02-06 20:46:33 +0000786 case Triple::sparc: T.setArch(Triple::sparcv9); break;
787 case Triple::x86: T.setArch(Triple::x86_64); break;
788 }
789 return T;
790}